日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

四则运算——初级

發布時間:2024/3/12 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 四则运算——初级 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

SE Individual Project

文章目錄

  • SE Individual Project
    • 四則運算——初級
      • 題目
      • 需求分析
      • 設計
      • 編碼
      • 重構代碼
      • 測試
      • 思路說明
      • 時間花費比較
      • 已知問題

四則運算——初級

題目

編寫一個四則運算程序,滿足如下要求

  • 題目要求:100以內加減法,滿足小學二年級數學口算需求
  • 使用參數控制生成題目的個數
  • 每道題目中出現的運算符個數不超過兩個
  • 程序一次運行生成的題目不能重復,請思考關于重復的定義。生成的題目存入執行程序的當前目錄下的Exercises.txt文件,格式如下:
  • 四則運算題目1
  • 四則運算題目2
  • 再生成題目的同時,計算出所有題目的答案,并存入執行程序的當前目錄下的Answers.txt文件,格式如下:
  • 答案1
  • 答案2
  • 估計需求分析、設計、編碼、測試各階段時間,記錄實際工作中各項工作時間花費,并列表進行對比
  • 需求分析

    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

  • 用num存儲生成題目的個數。
  • 用隨機數ra的0和1表示運算符的個數,0代表1個運算符,1代表2個運算符。
  • 再用隨機數opt的0和1代表±運算符,0代表+,1代表-。
  • 隨機生成參與運算的0-100之間數字,首先計算結果,如果大于100就不記入已產生的四則運算式子。
  • 對于重復的,我們可以采用數據結構來存儲,將加減號轉換為正負號,存入[-100,+100]的兩個或者三個數組內。
  • 使用文件流操作,將數據讀入txt其中。
  • 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:19

    end 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是否出現過即可。

    最后將運算式及其答案分別存入文件即可。

    時間花費比較

    需求分析12minutes
    設計13minutes
    編碼26minutes
    測試6minutes

    已知問題

  • 界面不夠直觀, 操作便利性不足
  • ? 解決方案: 進一步優化界面設計,可以用前端語言做一個便于操作的界面

  • 隨機數生成難以生成足夠多的題目

    解決方案:暫無良好的解決方案

  • 總結

    以上是生活随笔為你收集整理的四则运算——初级的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。