#include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { int x,y,z,i,l,b,a,c=0; char h; cout<<"是否要进行测试?(y/n):"; cin>>h; //srand(time(null)); while(h=='y') { do {cout<<"请选择要测试的类型(1:加法;2:减法;3:乘法):"; cin>>l; }while(l!=1&&l!=2&&l!=3); cout<<"请选择要测试的题数:"; cin>>i; if(l==1) { c=0; for(b=0;b<i;b++) { x=rand() / (RAND_MAX / 100 + 1); y=rand() / (RAND_MAX / 100 + 1); z=x+y; cout<<x<<"+"<<y<<"="; cin>>a; if(a==z) c++; else cout<<"答案错误,正确答案是:"<<z<<endl; } cout<<"你答对的题数是:"<<c<<endl; } else if(l==2) { c=0; for(b=0;b<i;b++) { x=rand()%100; y=rand()%100; if(x>y) //小学生部会做负数的题! { z=x-y; cout<<x<<"-"<<y<<"="; cin>>a; if(a==z) c++; else cout<<"答案错误,正确答案是:"<<z<<endl; } else { z=y-x; cout<<y<<"-"<<x<<"="; cin>>a; if(a==z) c++; else cout<<"答案错误,正确答案是:"<<z<<endl; } } cout<<"你答对的题数是:"<<c<<endl; } else { c=0; for(b=0;b<i;b++) { x=rand()%100; y=rand()%100; z=x*y; cout<<x<<"*"<<y<<"="; cin>>a; if(a==z) c++; else cout<<"答案错误,正确答案是:"<<z<<endl; } cout<<"你答对的题数是:"<<c<<endl; } cout<<"是否要再次测试?(y/n):"; cin>>h; } return 0; } 利用如何产生随机数!