实现四则运算在线答题

头文件

#ifndef count_h
#define count_h
class arithmetic
{
private:
long int a[30], //第一个操作数
b[30], //第二个操作数
m[30],
x[30],
a1[30], //第一个操作数
b1[30], //第二个操作数
m1[30],
x1[30];
public:
///////////////////////////////////////产生两个操作数
int s(); //产生运算符
};

源文件

#include<iostream>
#include"count.h"
using namespace std;

int arithmetic::s()
{
int n,jg,ys; //jg为输入的答案
int count=0;
int static count1=0;
while(count<30)
{
cout<<"请输入结果:\n";
a[count]=rand()%100;
b[count]=rand()%100;
m[count]=1+rand()%4; //产生随机符号
/*for(int i=0;i<count&&a[i]==a[count]&&b[i]==b[count]&&m[i]==m[count];i++)
{
break;
}*/
if (m[count]==1)
{
cout<<a[count]<<"+"<<b[count]<<"=\t";
x[count]=a[count]+b[count];

cin>>jg;
if(jg==x[count])
{
cout<<"正确\n";
}
else
{
cout<<"错误\n";
a1[count1]=a[count];
b1[count1]=b[count];
x1[count1]=x[count];
m1[count1]=m[count];
count1++;
}
count++;

}
if (m[count]==2)
{
cout<<a[count]<<"-"<<b[count]<<"=\t";
x[count]=a[count]-b[count];

cin>>jg;
if(jg==x[count])
{
cout<<"正确";
}
else
{
cout<<"错误";
a1[count1]=a[count];
b1[count1]=b[count];
x1[count1]=x[count];
m1[count1]=m[count];
count1++;
}
count++;
}
if (m[count]==3)
{
cout<<a[count]<<"*"<<b[count]<<"=\t";
x[count]=a[count]*b[count];

cin>>jg;
if(jg==x[count])
{
cout<<"正确\n";
}
else
{
cout<<"错误\n";
a1[count1]=a[count];
b1[count1]=b[count];
x1[count1]=x[count];
m1[count1]=m[count];
count1++;
}
count++;
}
if (m[count]==4&&b!=0)
{
cout<<a[count]<<"/"<<b[count]<<"=\t";
x[count]=a[count]/b[count];
n=a[count]%b[count];

cin>>jg;
cout<<"请输入余数";
cin>>ys;
if(jg==x[count])
{
cout<<"正确\n";
}
else
{
cout<<"错误\n";
a1[count1]=a[count];
b1[count1]=b[count];
x1[count1]=x[count];
m1[count1]=m[count];
count1++;
}
count++;

}
else
continue;
}

cout<<"是否显示结果:(是输入y)";
char l;
cin>>l;
while(1)
{

if(l=='y')
{
for(int i=0;i<30;i++)
{
cout<<x[i]<<"\t";
}
break;
}
else
{
cout<<"输入错误!\n";
}
}
return 0;

}

#include<iostream>
#include"count.h"
using namespace std;
int r()
{
return rand()%100;
}
int main ()
{
arithmetic a;
a.s();
system ("pause");
return 0;
}

 

posted @ 2018-10-31 21:32  一颗茶瓜子  阅读(114)  评论(0编辑  收藏  举报