# 简单四则运算

Myapp.exe -n 10

1、支持整数、分数的四则运算；
2、能够使用参数控制生成题目的个数。
3、输入结果，计算正确率


基本功能



class Point{ //所有数字都采用分数形式
public:
int x,y; //分子分母
public:
void set (int a, int b){x=a,y=b;}
};
Point Function(Point s);
void Print(Point s);
string to_String(int n);

Point jia(Point a,Point b){
Point s;
int k,l;
k=(a.xb.y)+(a.yb.x),l=a.yb.y;
s.set(k,l);
return s;
}
Point jian( Point a,const Point b){
Point s;
int k,l;
k=(a.x
b.y)-(a.yb.x),l=a.yb.y;
s.set(k,l);
return s;
}

Point cheng(Point a,Point b){
Point s;
s.set(a.xb.x,a.yb.y);
return s;
}
Point chu(Point a, Point b){
Point s;
s.set(a.xb.y,a.yb.x);
return s;
}

string tobeString(int n) //int to string
{
int m=n;
int i=0,j=0;
char s[100];
char ss[100];
while(m>0)
{
s[i++]= m%10 + '0';
m/=10;
}
s[i]='\0';

i=i-1;
while(i>=0)
{
ss[j++]=s[i--];
}
ss[j]='\0';

return ss;


}

int main()
{
Point s1[100],s2[100],s3[100];
int n,t=0,w[100];
string ch[]={"＋","-","×","÷"},ch1[100],s4[100],s5[100],k[100];
cout<<"题目个数：";
cin>>n;
cout<<"\n生成题目：\n";
srand(time(0));
for(int i=0;i<n;i++)
{
w[i]=rand()%4;
ch1[i]=ch[w[i]];
}
for(int i=0;i<n;i++)
{
s1[i].set(rand()%10+1,rand()%10+1);
s2[i].set(rand()%10+1,rand()%10+1);
}
for(int i=0;i<n;i++)
{
if(w[i]0)
s3[i]=jia(s1[i],s2[i]);
if(w[i]
1)
s3[i]=jian(s1[i],s2[i]);
if(w[i]2)
s3[i]=cheng(s1[i],s2[i]);
if(w[i]
3)
s3[i]=chu(s1[i],s2[i]);
}

 for(int i=0;i<n;i++)
{
s1[i]=Function(s1[i]);
s2[i]=Function(s2[i]);
s3[i]=Function(s3[i]);
}
for(int i=0;i<n;i++)
{
if(s3[i].x%s3[i].y==0){
s4[i]=tobeString(s3[i].x*s3[i].y);
}
else{
if(s3[i].x>0)
s4[i]=tobeString(s3[i].x)+"/"+tobeString(s3[i].y);
else{
s4[i]=tobeString(s3[i].x)+tobeString(s3[i].y);
//s4[i].append("-").append(tobeString(s3[i].x)).append("/").append(tobeString(s3[i].y));
}
}

}

for(int i=0;i<n;i++)
{
Print(s1[i]);
cout<<ch1[i];
Print(s2[i]);
cout<<"=";
cout<<"?"<<endl;
}
cout<<"输入答案：";
for(int i=0;i<n+1;i++)
{
getline(cin,s5[i]);
}
cout<<"正确答案："<<endl;
for(int i=0;i<n;i++)
{
Print(s1[i]);
cout<<ch1[i];
Print(s2[i]);
cout<<"=";
Print(s3[i]);
cout<<endl;
}
for(int i=0;i<n;i++)
{
if(s4[i]==s5[i+1])
t++;
}
cout<<"正确率为："<<(double(t)/n)*100<<"%"<<endl;


}

Point Function(Point s) //约分
{
Point i;
int a,b,k,r,j,t;
a=s.x;
j=s.x;
b=s.y;
k=s.y;
r=a%b;
while(r)
{
a=b;
b=r;
r=a%b;
}
t=abs(b);
j=j/t;
k=k/t;
i.set(j,k);
return i;

}

void Print(Point s){ //打印函数
if(s.x%s.y==0)
cout<<s.x/s.y;
else
printf("%d%s%d", s.x,"/",s.y);

}

PSP展示

PSP2.1 Personal Software Process Stages Time (%) Senior Student(/hour) Time (%)(/hour)
· Planning 计划 2 1.5
· Estimate 估计这个任务需要多少时间 37 40
· Analysis 需求分析 (包括学习新技术) 1 1
· Coding Standard 代码规范 0.5 0.5
· Design 具体设计 2 1
· Coding 具体编码 35 35
· Test 测试（自我测试，修改代码，提交修改） 1 1
Reporting 报告 1 1
posted on 2017-03-05 22:31  cjz113  阅读(224)  评论(1编辑  收藏  举报