设计思想:
1.里用随机数生成函数生成4个随机数
2.中间随机数加工,除4取余,0,1,2,3分别代表加减乘除.最后一个随机数除2取余,0代表整数,1代表假分数
3.当生成除法判断除数不为0
4.随机题目生成,循环30道题目
5.输出
源程序代码:
#include<iostream> using namespace std; #include<time.h> struct yunsuan { int fuhao; int one; int two; int select; }; int main() { yunsuan num[30]; srand((int)time(NULL)); int i; for(i=0;i<30;i++) { num[i].one = rand()%100; num[i].two = rand()%100; num[i].fuhao = rand() % 4; num[i].select = rand() % 2; if(num[i].fuhao==3) { if(num[i].two == 0&&num[i].one < num[i].two) { i=i-1; } } } for(i=0;i<30;i++) { if(num[i].select == 0) { if(num[i].fuhao==0) { cout << num[i].one << " + " << num[i].two << " = " << endl; } if(num[i].fuhao==1) { cout << num[i].one << " - " << num[i].two << " = " << endl; } if(num[i].fuhao==2) { cout << num[i].one << " * " << num[i].two<< " = " << endl; } if(num[i].fuhao==3) { cout << num[i].one << " / " << num[i].two<< " = " << endl; } } else { int num1 = rand() % 100+1; int num2 = num[i].one+1; int num3 = rand() % 100+1; int num4 = num[i].two+1; if(num[i].fuhao==0) { cout << num1 << "/" << num2 << " + " << num3 << "/" << num4 << " = " << endl; } if(num[i].fuhao==1) { cout << num1 << "/" << num2 << " + " << num3 << "/" << num4 << " = " << endl; } if(num[i].fuhao==2) { cout << "(" << num1 << "/" << num2 << ") * (" <<num3 << "/" << num4 << ") = " << endl; } if(num[i].fuhao==3) { cout << "(" << num1 << "/" << num2 << ") /(" <<num3 << "/" << num4 << ") = " << endl; } } } }
运行结果截图:
上课未按时完成的原因: