代码:
1 #include <iostream> 2 #include <time.h> 3 using namespace std; 4 5 void main() 6 { 7 srand((int)time(NULL)); //每次执行种子不同,生成不同的随机数 8 int a, b, c, d, e, i, mark,opt; 9 for (i = 0; i < 30; i++) 10 { 11 opt = 1 + rand() % 2;//随机生成1或者2来表示整数和真分数 12 a = 1 + rand() % 100;//将1到100内的数随机赋予a 13 b = 1 + rand() % 100;//将1到100内的数随机赋予b 14 c = 1 + rand() % 100;//将1到100内的数随机赋予c 15 d = 1 + rand() % 100;//将1到100内的数随机赋予d 16 if (opt == 1) 17 { 18 mark = 1 + rand() % 4;//随机生成1-4来分别表示四个运算符 19 cout << a; 20 switch (mark) 21 { 22 case 1:cout << "+"; break; 23 case 2:cout << "-"; break; 24 case 3:cout << "×"; break; 25 case 4:cout << "÷"; break; 26 } 27 cout << b << "=" << endl; 28 } 29 if (opt == 2) 30 { 31 if (a > b) 32 { 33 e = a; 34 a = b; 35 b = e; 36 } 37 cout << "(" << a << "/" << b <<")"; 38 mark = 1 + rand() % 4; 39 switch (mark) 40 { 41 case 1:cout << "+"; break; 42 case 2:cout << "-"; break; 43 case 3:cout << "×"; break; 44 case 4:cout << "÷"; break; 45 } 46 if (c > d) 47 { 48 e = c; 49 c = d; 50 d = e; 51 } 52 cout << "(" << c << "/" << d << ")" <<"="<< endl; 53 } 54 55 } 56 }