嘗試本身寫一個能自動生成小學四則運算題目的命令行「軟件」,分別知足下面的各類需求:ios
a)自動生成小學四則運算題目,除整數之外,還支持真分數的四則運算;c++
b)能接受用戶輸入答案,採用if語句來判斷對錯。最後給出總共對錯的數量;dom
c)運用for循環來實現批量給出100道的題目,而且使用計算機C語言中的rand函數,rand是真正的隨機數生成器,而srand()會設置供rand()使用的隨機數種子,來保證題目不能重複;函數
d)能夠經過增長case數量來增長運算符,由於步驟太多太難,這個程序中的題目只有一個運算符;學習
e)爲了更好的表達結果,全部的結果非整數的除法運算和真分數運算的結果都只支持分數形式,由於我c++學很差,好多不會,查閱了好多資料後,結果也只能如下圖中的形式輸入,雖然結果能很好的表示出來,可是不美觀,但願之後經過更多的學習機會去學到更好的c++知識。spa
程序運行結果截圖:命令行
程序代碼:3d
#include<iostream>
#include<stdlib.h>
#include<iomanip>
#include<cmath>
#include<time.h>
using namespace std;blog
#define random() (rand()%100)ip
void main(){ int s1,s2,s3,s4; double s5,s6; int a,b,c,d,e,f,g; int right=0; int wrong=0; srand((int)time(NULL)); for(int i=0;i<100;i++) { s1=random(); s2=random(); int k=(int)rand()%5; switch(k) { case 0: { cout<<s1<<"+"<<s2<<"="; cin>>s5; if(s1+s2==s5) { cout<<"right!"<<endl;right++; } else { cout<<"wrong!"<<endl;wrong++; } break; } case 1: { cout<<s1<<"-"<<s2<<"="; cin>>s5; if(s1-s2==s5) { cout<<"right!"<<endl;right++; } else { cout<<"wrong!"<<endl;wrong++; } break; } case 2: { cout<<s1<<"*"<<s2<<"="; cin>>s5; if(s1*s2==s5) { cout<<"right!"<<endl;right++; } else { cout<<"wrong!"<<endl;wrong++; } break; } case 3: { while(s2==0) s2=random(); cout<<s1<<"/"<<s2<<"="; if(s1%s2==0) {cin>>s5; if(s5==s1/s2) { cout<<"right!"<<endl;right++; } else { cout<<"wrong!"<<endl;wrong++; } } else { cin>>s5;cout<<"/"<<endl;cin>>s6; a=s1; b=s2; for(c=a;c>=1&&c<=a;c--) { d=a%c; e=b%c; if(d==0&&e==0) { f=a/c; g=b/c; } } if((f==s5)&&(g==s6)) { cout<<"right!"<<endl;right++; } else { cout<<"wrong!"<<endl;wrong++; } break; } } case 4: { while(s2==0||s1>=s2) { s1=random(); s2=random(); } s3=random(); s4=random(); while(s4==0||s3>=s4) { s3=random(); s4=random(); } int l=(int)rand()%4; switch(l) { case 0: { cout<<"("<<s1<<"/"<<s2<<")"<<"+"<<"("<<s3<<"/"<<s4<<")"<<"="; cin>>s5;cout<<"/"<<endl;cin>>s6; a=s1*s4+s2*s3; b=s2*s4; if(a>b) { for(c=b;c>=1&&c<=b;c--) { d=a%c; e=b%c; if(d==0&&e==0) { f=a/c; g=b/c; } } } else { for(c=a;c>=1&&c<=a;c--) { d=a%c; e=b%c; if(d==0&&e==0) { f=a/c; g=b/c; } } } if((f==s5)&&(g==s6)) { cout<<"right!"<<endl;right++; } else { cout<<"wrong!"<<endl;wrong++; } break; } case 1: { cout<<"("<<s1<<"/"<<s2<<")"<<"-"<<"("<<s3<<"/"<<s4<<")"<<"="; cin>>s5;cout<<"/"<<endl;cin>>s6; a=s1*s4-s2*s3; b=s2*s4; if(a>b) { for(c=b;c>=1&&c<=b;c--) { d=a%c; e=b%c; if(d==0&&e==0) { f=a/c; g=b/c; } } } else { for(c=a;c>=1&&c<=a;c--) { d=a%c; e=b%c; if(d==0&&e==0) { f=a/c; g=b/c; } } } if((f==s5)&&(g==s6)) { cout<<"right!"<<endl;right++; } else { cout<<"wrong!"<<endl;wrong++; } break; } case 2: { cout<<"("<<s1<<"/"<<s2<<")"<<"*"<<"("<<s3<<"/"<<s4<<")"<<"="; cin>>s5;cout<<"/"<<endl;cin>>s6; a=s1*s3; b=s2*s4; if(a>b) { for(c=b;c>=1&&c<=b;c--) { d=a%c; e=b%c; if(d==0&&e==0) { f=a/c; g=b/c; } } } else { for(c=a;c>=1&&c<=a;c--) { d=a%c; e=b%c; if(d==0&&e==0) { f=a/c; g=b/c; } } } if((f==s5)&&(g==s6)) { cout<<"right!"<<endl;right++; } else { cout<<"wrong!"<<endl;wrong++; } break; } case 3: { cout<<"("<<s1<<"/"<<s2<<")"<<"/"<<"("<<s3<<"/"<<s4<<")"<<"="; cin>>s5;cout<<"/"<<endl;cin>>s6; a=s1*s4; b=s2*s3; if(a>b) { for(c=b;c>=1&&c<=b;c--) { d=a%c; e=b%c; if(d==0&&e==0) { f=a/c; g=b/c; } } } else { for(c=a;c>=1&&c<=a;c--) { d=a%c; e=b%c; if(d==0&&e==0) { f=a/c; g=b/c; } } } if((f==s5)&&(g==s6)) { cout<<"right!"<<endl;right++; } else { cout<<"wrong!"<<endl;wrong++; } break; } } }}}cout<<"the number of right answer:"<<right<<","<<"the number of wrong answer:"<<wrong<<endl;system("pause");}