小学二年级四则运算

一家软件公司程序员二柱的小孩上了小学二年级,老师让家长每天出30道四则运算题目给小学生做。

此次编程运用c++语言,完成了30道随机四则运算题目的编写。其中,随机数生成函数rand(),srand()的使用参考了部分资料。

此次编程实现功能:30道四则运算题的编写(不重复编写)

                                支持整型四则运算

                                可制定出题的数量

 1 //开始编程时间:13:20
 2   //结束编程时间:14:56
 3   #include <iostream>
 4   #include <time.h>
 5   #include <stdlib.h>
 6   using namespace std;
 7   #define max 100
 8   #define n 30
 9   int main()
10  {
11           srand(time(NULL));       //seed
12           int num1,num2;
13           int i,m;
14           char ysf[4]={'+','-','*','/'};
15           for(m=0;m<n;m++)
16           {
17                  i=rand()%4;   // 生成0-3的随机数
18                  num1=rand() % max;  // 生成0-max-1的随机数
19                  num2=rand() % max;
20                  switch(i){
21                      case 0: //整数相加
22                          cout<<num1<<ysf[i]<<num2<<"="<<endl;
23                      break;
24                      case 1: //整数相减
25                          cout<<num1<<ysf[i]<<num2<<"="<<endl;
26                      break;
27                      case 2://整数相乘
28                          cout<<num1<<ysf[i]<<num2<<"="<<endl;
29                      break;
30                      case 3: //整数相除
31                          while(num2==0)
32                             {//除数不为0
33                                num2=rand() % max;;
34                             }
35                          cout<<num1<<ysf[i]<<num2<<"="<<endl;
36                      break;
37                      default:
38                      break;}
39           }
40  }

编程运行截图如下:

原文地址:https://www.cnblogs.com/pcyyyyy/p/9755488.html