首页 > 其他 > 详细

四则运算

时间:2018-11-11 21:58:27      阅读:156      评论:0      收藏:0      [点我收藏+]

代码

#include<stdio.h>
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
void boom()
{
FILE *fpt;
fpt = fopen("四则运算.txt","w");
float x,y;
int z,t;
int first_num,second_num,third_num,fourth_num;
srand(time(NULL));
int n;
float d;
float daan[100];
cout<<"列出数学题数量?"<<endl;
cin>>n;
cout<<"--------------------------------------------"<<endl;
for(int i=0;i<n;i++)

{
z=rand()%4;
first_num=rand()%10;
second_num=rand()%10;
third_num=rand()%10;
fourth_num=rand()%10;
if (second_num==1)
{
x=(float)first_num;
}
else if(first_num>second_num)
{
t=first_num;
first_num=second_num;
second_num=t;
}
x=(float)first_num/second_num;
if (fourth_num==1)
{
y=(float)third_num;
}
else if(third_num>fourth_num)
{
t=third_num;
third_num=fourth_num;
fourth_num=t;
}
y=(float)third_num/fourth_num;
z=rand()%4;
switch(z)
{
case 0:
cout<<i+1<<"、 "<<first_num<<"/"<<second_num<<"+"<<third_num<<"/"<<fourth_num<<"="<<" ";

fprintf(fpt,"%d 、 %d/%d + %d/%d = \n",i+1,first_num,second_num,third_num,fourth_num);
daan[i+1]=x+y;
break;

case 1:
cout<<i+1<<"、 "<<first_num<<"/"<<second_num<<"-"<<third_num<<"/"<<fourth_num<<"="<<" ";
fprintf(fpt,"%d 、 %d/%d - %d/%d = \n",i+1,first_num,second_num,third_num,fourth_num);
daan[i+1]=x-y;
break;
case 2:
cout<<i+1<<"、 "<<first_num<<"/"<<second_num<<"*"<<third_num<<"/"<<fourth_num<<"="<<" ";
fprintf(fpt,"%d 、 %d/%d * %d/%d = \n",i+1,first_num,second_num,third_num,fourth_num);
daan[i+1]=x*y;
break;
case 3:
if(y!=0)
{
cout<<i+1<<"、 "<<first_num<<"/"<<second_num<<"÷"<<third_num<<"/"<<fourth_num<<"="<<" ";
fprintf(fpt,"%d 、 %d/%d ÷ %d/%d = \n",i+1,first_num,second_num,third_num,fourth_num);
}
else
{
i=i-1;
}
daan[i+1]=(float)x/y;
break;
}
if((i+1)%3==0)
{
for(int c=0;c<=1;c++)
{
cout<<endl;
}
}
if((i+1)==n)
{
cout<<endl;
}
}
fclose(fpt);
for(i=1;i<=n;i++)
{
cout<<"请作答:"<<endl;
cout<<i<<"、";
cin>>d;
if(d==daan[i])
{
cout<<"回答正确"<<endl;
cout<<endl;

}
else
{
cout<<"回答错误,正确答案为:"<<" "<<daan[i]<<endl;
cout<<endl;

}
}

}
int main()
{
boom();
int c;
cout<<"还继续要答题吗?(0:继续 1:退出)"<<endl;
cin>>c;
if(c==0)
{
cout<<endl;
main();
}
else
{
cout<<endl;
return 0;
}
return 0;
}

截图

 技术分享图片

 技术分享图片

 

四则运算

原文:https://www.cnblogs.com/guci/p/9943552.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!