四则运算——初级
SE Individual Project
文章目錄
- SE Individual Project
- 四則運算——初級
- 題目
- 需求分析
- 設計
- 編碼
- 重構代碼
- 測試
- 思路說明
- 時間花費比較
- 已知問題
四則運算——初級
題目
編寫一個四則運算程序,滿足如下要求
需求分析
start at 20:30
首先,100以內加減法,100以內加減法是指參與運算的數不超過100,還是運算結果不超過100?
通過對市面上的100以內加減法的習題的分析研究,以及對于小學二年級家長與老師的采訪調研,得出100以內加減法,100以內加減法是指參與運算的數和運算結果都不超過100的含義。
使用參數控制生成題目的個數就是需要用戶輸入數量
每道題目中出現的運算符個數不超過兩個即為,運算符只能是一個或者兩個
一次運行生成的題目不能重復,那么對于一個運算符的情況,不能出現1+2和2+1或1+2和1+2的情況,對于兩個運算符情況,不能出現1+1+2與1+2+1的情況
生成的題目及其答案需要對應的存入相應的文件里面
end at 20:42
cost time 12min
設計
start at 22:35
end at 22:48
cost time 13min
編碼
begin at 0:07
#include <bits/stdc++.h> //#define LOCAL using namespace std; typedef long long ll; #define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); const double pi=acos(-1.0); const int INF=1000000000; const int maxn=1000005; int num,ans=0; int plustwo[110][110],minutwo[110][110],operatthree[500][500][500]; int main() {IOS;#ifdef LOCALfreopen("input.txt","r",stdin);freopen("output.txt","w",stdout);#endifsrand(time(NULL));memset(plustwo,0,sizeof(plustwo));memset(minutwo,0,sizeof(minutwo));memset(operatthree,0,sizeof(operatthree));cout<<"please input the number of the questions that you need:"<<endl;cin>>num;fstream f;f.open("Exercises.txt",ios::out);f.close();f.open("Answers.txt",ios::out);f.close();while(ans<num){int ra=rand()%2+1;if(ra==1){int a1=rand()%100+1,a2=rand()%100+1,opt=rand()%2+1,res;if(opt==1){res=a1+a2;if(res<0||res>100) continue;if(plustwo[a1][a2]==1) continue;plustwo[a1][a2]=1;//cout<<ans+1<<":"<<a1<<"+"<<a2<<"="<<res<<endl;f.open("Exercises.txt",ios::out|ios::app); f<<ans+1<<":"<<a1<<"+"<<a2<<"="<<endl; f.close(); f.open("Answers.txt",ios::out|ios::app); f<<ans+1<<":"<<res<<endl; f.close(); } else{res=a1-a2;if(res<0||res>100) continue;if(minutwo[a1][a2]==1) continue;minutwo[a1][a2]=1;//cout<<ans+1<<":"<<a1<<"-"<<a2<<"="<<res<<endl;f.open("Exercises.txt",ios::out|ios::app); f<<ans+1<<":"<<a1<<"-"<<a2<<"="<<endl; f.close(); f.open("Answers.txt",ios::out|ios::app); f<<ans+1<<":"<<res<<endl; f.close(); } ans++;}else {int a1=rand()%100+1,a2=rand()%100+1,a3=rand()%100+1,res,opt1=rand()%2+1,opt2=rand()%2+1;int b1=a1,b2=a2,b3=a3;if(opt1==2) b2=-b2;if(opt2==2) b3=-b3;res=b1+b2+b3;if(res<0||res>100) continue;if(operatthree[b1][200+b2][200+b3]==1) continue;operatthree[b1][200+b2][200+b3]=1;/*cout<<ans+1<<":"<<a1;if(opt1==1) cout<<"+";else cout<<"-";cout<<a2;if(opt2==1) cout<<"+";else cout<<"-";cout<<a3<<"="<<res<<endl;*/f.open("Exercises.txt",ios::out|ios::app); f<<ans+1<<":"<<a1;if(opt1==1) f<<"+";else f<<"-";f<<a2;if(opt2==1) f<<"+";else f<<"-";f<<a3<<"="<<endl; f.close(); f.open("Answers.txt",ios::out|ios::app); f<<ans+1<<":"<<res<<endl; f.close(); ans++; }}return 0; }end at 0:33
cost time 26min
重構代碼
#include <bits/stdc++.h> using namespace std;int num,ans=0; int plustwo[110][110],minutwo[110][110],operatthree[500][500][500]; fstream f;void gentxt(); //創建Exercises和Answers文件 void outtwoex(int a1,int a2,int opt); //單符號加減法輸出到Exercises void outans(int res); //輸出到Answers void outthreex(int a1,int a2,int a3,int opt1,int opt2); //雙符號加減法輸出到Exercisesint main() {srand(time(NULL));cout<<"please input the number of the questions that you need:"<<endl;cin>>num;gentxt();while(ans<num){int ra=rand()%2+1; //判斷單雙符號if(ra==1){int a1=rand()%100+1,a2=rand()%100+1,opt=rand()%2+1,res;if(opt==1){ //單符號為+res=a1+a2;if(res<0||res>100) continue;if(plustwo[a1][a2]==1) continue; //判斷重復plustwo[a1][a2]=1;outtwoex(a1,a2,opt);outans(res); } else{ //單符號為-res=a1-a2;if(res<0||res>100) continue;if(minutwo[a1][a2]==1) continue; //判斷重復minutwo[a1][a2]=1;outtwoex(a1,a2,opt);outans(res); } ans++;}else { //雙符號int a1=rand()%100+1,a2=rand()%100+1,a3=rand()%100+1,res,opt1=rand()%2+1,opt2=rand()%2+1;int b1=a1,b2=a2,b3=a3;if(opt1==2) b2=-b2;if(opt2==2) b3=-b3;res=b1+b2+b3;if(res<0||res>100) continue;if(operatthree[b1][200+b2][200+b3]==1) continue;operatthree[b1][200+b2][200+b3]=1;outthreex(a1,a2,a3,opt1,opt2);outans(res); ans++; }}return 0; } void gentxt(){f.open("Exercises.txt",ios::out);f.close();f.open("Answers.txt",ios::out);f.close(); } void outtwoex(int a1,int a2,int opt){f.open("Exercises.txt",ios::out|ios::app);if(opt==1) f<<ans+1<<":"<<a1<<"+"<<a2<<"="<<endl; else f<<ans+1<<":"<<a1<<"-"<<a2<<"="<<endl; f.close(); } void outans(int res){f.open("Answers.txt",ios::out|ios::app); f<<ans+1<<":"<<res<<endl; f.close(); } void outthreex(int a1,int a2,int a3,int opt1,int opt2){f.open("Exercises.txt",ios::out|ios::app); f<<ans+1<<":"<<a1;if(opt1==1) f<<"+";else f<<"-";f<<a2;if(opt2==1) f<<"+";else f<<"-";f<<a3<<"="<<endl; f.close(); }測試
start at 0:37
無bug
輸入:20
//Exercises.txt 1:46+88-51= 2:90-78= 3:69-48= 4:47+100-91= 5:18+52= 6:69-80+47= 7:85+77-64= 8:41+42+14= 9:98-66= 10:95-67= 11:8+66+17= 12:8+64-59= 13:16-9= 14:38+39= 15:81-56+26= 16:70-32+53= 17:69-23+40= 18:87-74= 19:64+8= 20:72-97+44= //Answers.txt 1:83 2:12 3:21 4:56 5:70 6:36 7:98 8:97 9:32 10:28 11:91 12:13 13:7 14:77 15:51 16:91 17:86 18:13 19:72 20:19end at 0:43
cost time 6min
思路說明
首先輸入要生成多少條四則運算題目。
然后對于每一條題目我們來隨機生成是一個運算符還是兩個運算符。
如果是一個運算符,隨機生成a+b或者a-b的形式。
如果是兩個運算符,就隨機生成a+b+c或a+b-c或a-b+c或a-b-b的形式。
然后我們來考慮如何避免重復的情況發生。
對于一個運算符+的情況,我們用二維數組來存儲a和b是否出現過。
對于一個運算符-的情況,我們再用另一個二維數組來存儲a和b是否出現過。
對于兩個運算符,我們將減100變成加-100,從而全部轉換到+域上來。
簡而言之就是當運算符為-的時候,將后面的一個數字變為相反數。
然后我們只需要用一個三維數組來存儲a、b、c是否出現過即可。
最后將運算式及其答案分別存入文件即可。
時間花費比較
| 設計 | 13minutes |
| 編碼 | 26minutes |
| 測試 | 6minutes |
已知問題
? 解決方案: 進一步優化界面設計,可以用前端語言做一個便于操作的界面
隨機數生成難以生成足夠多的題目
解決方案:暫無良好的解決方案
總結