对于今日课堂问题,我的答案是:
1.方案:在大循环中,分别生成两个0到99之间的随机数a和b,继而再生成一个0到3之间的数c;用c来控制运算符,即可得到想要的结果。
2.代码:
//四则运算,王昭博。
#include<iostream>
using namespace std;
#include<cmath>
void main ()
{
double a , b , c ;
for(int i = 0 ; i < 30 ; i++)
{
a = rand()%100 ;
b = rand()%100 ;
c = rand()%4 ;
if(c == 0)
cout<< a << " + " << b << " = " <<endl;
if(c == 1)
cout<< a << " - " << b << " = " <<endl;
if(c == 2) c
out<< a << " / " << b << " = " <<endl;
if(c == 3)
cout<< a << " * " << b << " = " <<endl;
}
}
3.结果:
41 / 67 =
0 + 69 =
78 / 58 =
64 - 5 =
81 - 27 =
91 / 95 =
27 * 36 =
4 - 2 =
92 - 82 =
16 * 18 =
47 * 26 =
38 + 69 =
67 * 99 =
94 * 3 =
22 - 33 =
64 * 41 =
53 * 68 =
44 - 62 =
37 * 59 =
41 / 29 =
16 / 35 =
42 / 88 =
40 + 42 =
48 - 46 =
90 / 29 =
50 - 6 =
93 - 48 =
23 / 84 =
56 / 40 =
76 + 31 =
4.课上未作出的原因:
随机数的生成代码没记住。