码迷,mamicode.com
首页 > 其他好文 > 详细

小学生二年级四则运算2.0

时间:2018-10-08 23:12:01      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:ace   功能   截图   iostream   space   def   cout   inf   png   

在原有基础之上,希望加上分数的四则运算。

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

                                支持整型四则运算,还有分数四则运算

                                可制定出题的数量

 

 1 //开始编程时间:15:18
 2 //结束编程时间:16:42
 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,num3,num4;
13          int i,j,m;
14          char ysf[4]={‘+‘,‘-‘,‘*‘,‘/‘};
15          for(m=0;m<n;m++)
16          {
17              j=rand()%2;  // 生成0或1
18              if(j==0)  // 整型四则运算
19              {
20                 i=rand()%4;   // 生成0-3的随机数
21                 num1=rand() % max;  // 生成0-max-1的随机数
22                 num2=rand() % max;
23                 switch(i){
24                     case 0: //整数相加
25                         cout<<num1<<ysf[i]<<num2<<"="<<endl;
26                     break;
27                     case 1: //整数相减
28                         cout<<num1<<ysf[i]<<num2<<"="<<endl;
29                     break;
30                     case 2://整数相乘
31                         cout<<num1<<ysf[i]<<num2<<"="<<endl;
32                     break;
33                     case 3: //整数相除
34                         while(num2==0)
35                            {//除数不为0
36                               num2=rand() % max;;
37                            }
38                         cout<<num1<<ysf[i]<<num2<<"="<<endl;
39                     break;
40                     default:
41                     break;}
42              }
43               else if(j==1)  // 分数四则运算
44                 {
45                     i=rand()%4;
46                     num1=rand() % max;
47                     num2=rand() % max;
48                     while(num2==0||num1>=num2){//分数分母不为0并且分子不能大于分母
49                         num1=rand() % max;
50                         num2=rand() % max;
51                      }
52                     num3=rand() % max;
53                     num4=rand() % max;
54                     while(num4==0||num3>=num4){//分数分母不为0并且分子不能大于分母
55                         num3=rand() % max;
56                         num4=rand() % max;
57                      }
58                     switch(i){
59                         case 0:        //分数相加
60                             cout<<num1<<"/"<<num2<<ysf[i]<<num3<<"/"<<num4<<"="<<endl;
61                         break;
62                         case 1:        //分数相减
63                             cout<<num1<<"/"<<num2<<ysf[i]<<num3<<"/"<<num4<<"="<<endl;
64                         break;
65                         case 2:        //分数相乘
66                             cout<<"("<<num1<<"/"<<num2<<")"<<ysf[i]<<"("<<num3<<"/"<<num4<<")"<<"="<<endl;
67                         break;
68                         case 3:       //分数相除
69                             while(num3==0){//作为分母的分数不为0
70                                 num3=rand() % max;
71                             while(num3>=num4){
72                                 num3=rand() % max;
73                                 num4=rand() % max;
74                                 }
75                              }
76                            cout<<"("<<num1<<"/"<<num2<<")"<<ysf[i]<<"("<<num3<<"/"<<num4<<")"<<"="<<endl;
77                        break;
78                        default:
79                        break;
80                        }
81                    }
82          }
83 }

编程运行截图如下:

技术分享图片

 

 

技术分享图片

 

小学生二年级四则运算2.0

标签:ace   功能   截图   iostream   space   def   cout   inf   png   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!