1. 程式人生 > >二柱子牌出題器——四則運算生成

二柱子牌出題器——四則運算生成

題目:

       1.除了整數以外,還要支援真分數的四則運算,真分數的運算

         2.運算子為 +, −, ×, ÷

         3.並且要求能處理使用者的輸入,並判斷對錯

         4.使用 -n 引數控制生成題目的個數。

解決思路:

       1.根據使用者輸入的引數,生成n道分數運算題、

         2.通過使用者輸入的答案,判斷對錯

         3.列印題目的檔案生成——使用fprinrf語句實現

程式程式碼:

 

#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);
fclose(fpt);
}
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;
}
}
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;
}

 

 

執行截圖: