自己编的运算程序,嘿嘿,分享一下
#include<iostream>#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
int a[2];
int i,j;
srand(time(NULL));
while(1)
{
bool quit=false;
cout<<"请输入您的选择:1.+"<<'\t'<<"2.-"<<'\t'<<"3.*"<<'\t'<<"4./"<<'\t'<<"5.quit:"<<endl;
int choice;
cin>>choice;
switch(choice)
{
case 1:
for(i=0;i<2;i++)
a[i]=rand()%100;
cout<<a[0]<<"+"<<a[1]<<"=";
cin>>j;
if(j==a[0]+a[1])
cout<<"your answer is true"<<endl;
else
if(j!=a[0]+a[1])
{
cout<<"your answer is false"<<endl;
cout<<"正确答案为:"<<a[0]<<"+"<<a[1]<<"="<<a[0]+a[1]<<endl;
}
break;
case 2:
for(i=0;i<2;i++)
a[i]=rand()%100;
cout<<a[0]<<"-"<<a[1]<<"=";
cin>>j;
if(j==a[0]-a[1])
cout<<"your answer is true"<<endl;
else
if(j!=a[0]-a[1])
{
cout<<"your answer is false"<<endl;
cout<<"正确答案为:"<<a[0]<<"-"<<a[1]<<"="<<a[0]-a[1]<<endl;
}
break;
case 3:
for(i=0;i<2;i++)
a[i]=rand()%100;
cout<<a[0]<<"*"<<a[1]<<"=";
cin>>j;
if(j==a[0]*a[1])
cout<<"your answer is true"<<endl;
else
if(j!=a[0]*a[1])
{
cout<<"your answer is false"<<endl;
cout<<"正确答案为:"<<a[0]<<"*"<<a[1]<<"="<<a[0]*a[1]<<endl;
}
break;
case 4:
for(i=0;i<2;i++)
a[i]=rand()%100;
if(a[1]!=0)
cout<<a[0]<<"/"<<a[1]<<"=";
else
if(a[0]=0)
cout<<"the data is illegle"<<endl;
cin>>j;
if(j==a[0]/a[1])
cout<<"your answer is true"<<endl;
else
if(j!=a[0]/a[1])
{
cout<<"your answer is false"<<endl;
cout<<"正确答案为:"<<a[0]<<"/"<<a[1]<<"="<<a[0]/a[1]<<endl;
}
break;
case 5:
quit=true;
cout<<"请输入您的选择:1.+"<<'\t'<<"2.-"<<'\t'<<"3.*"<<'\t'<<"4./"<<'\t'<<"5.quit:"<<endl;
break;
default:
break;
}
if(quit==true)
{break;}
}
system("pause");
return 0;
}