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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

个人作业1——四则运算题目生成程序(基于控制台)

發(fā)布時(shí)間:2025/7/14 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 个人作业1——四则运算题目生成程序(基于控制台) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目描述:

從《構(gòu)建之法》第一章的 “程序” 例子出發(fā),像阿超那樣,花二十分鐘寫(xiě)一個(gè)能自動(dòng)生成小學(xué)四則運(yùn)算題目的命令行 “軟件”,滿足以下需求:

  • 除了整數(shù)以外,還要支持真分?jǐn)?shù)的四則運(yùn)算,真分?jǐn)?shù)的運(yùn)算,例如:1/6 + 1/8 = 7/24
  • 運(yùn)算符為 +,-,×,÷
  • 并且要求能處理用戶的輸入,并判斷對(duì)錯(cuò),打分統(tǒng)計(jì)正確率。
  • 要求能處理用戶輸入的真分?jǐn)?shù), 如 1/2, 5/12 等
  • 使用 -n 參數(shù)控制生成題目的個(gè)數(shù),例如執(zhí)行下面命令將生成10個(gè)題目Myapp.exe -n 10
PSP2.1Personal Software Process StagesTime (%) Senior Student(/hour)Time (%)(/hour)
· Planning計(jì)劃21.5
· Estimate估計(jì)這個(gè)任務(wù)需要多少時(shí)間3740
· Analysis需求分析 (包括學(xué)習(xí)新技術(shù))11
· Coding Standard代碼規(guī)范0.50.5
· Design具體設(shè)計(jì)1.51
· Coding具體編碼3035
· Test測(cè)試(自我測(cè)試,修改代碼,提交修改)11
Reporting報(bào)告11

源代碼:

#include<iostream> #include<stdlib.h> #include<time.h> using namespace std; int b = 0; int maxNum(int i, int j) {int k;if ((k = i % j) != 0){maxNum(j, k);}elsereturn j; } int INT() {int x, y, z, t, m, n, c, r1, r2;char r3[10], r4[10];srand(time(NULL));memset(r3, 0, sizeof(r3));memset(r4, 0, sizeof(r4));x = rand() % 100;y = rand() % 100;z = rand() % 4;r1 = 0;r2 = 0;switch (z){case 0:cout << x << "+" << y << "=";cin >> r1;cin.get();r2 = x + y;if (r1 == r2){cout << " " << "T" << endl;b = b + 1;}else cout << " " << "F" << endl;break;case 1:if (x<y){t = x;x = y;y = t;}else;cout << x << "-" << y << "=";cin >> r1;cin.get();r2 = x - y;if (r1 == r2){cout << " " << "T" << endl;b = b + 1;}else cout << " " << "F" << endl;break;case 2:cout << x << "*" << y << "=";cin >> r1;r2 = x * y;if (r1 == r2){cout << " " << "T" << endl;b = b + 1;}else cout << " " << "F" << endl;break;case 3:if (y != 0){cout << x << "÷" << y << "=";c = maxNum(x, y);m = x / c;n = y / c;if (n != 1){sprintf_s(r3, "%d/%d", m, n);cin >> r4;if (strcmp(r3, r4) == 0){cout << " " << "T" << endl;b = b + 1;}else cout << " " << "F" << endl;}else{cin >> r2;if (r2 == m) {cout << " " << "T" << endl;b = b + 1;}else cout << " " << "F" << endl;}}else {cout << x << "÷" << "1" << "=";cin >> r2;if (r2 == x) {cout << " " << "T" << endl;b = b + 1;}else cout << " " << "F" << endl;}break;default:cout << "wrong" << endl;break;}return 0; }int fra() {int x, y, z, t, m, n, c, r1, r2, i, j;char r3[10], r4[10];srand(time(NULL));memset(r3, 0, sizeof(r3));memset(r4, 0, sizeof(r4));x = rand() % 100;y = rand() % 100;i = rand() % 100;j = rand() % 100;z = rand() % 4;r1 = 0;r2 = 0;if (y == 0 || y == 1)y = 2;if (j == 0 || j == 1)j = 2;switch (z){case 0:cout << x << "/" << y << "+" << i << "/" << j << "=";m = (x*j) + (i*y);n = y*j;c = maxNum(m, n);m = m / c;n = n / c;if (n != 1) {sprintf_s(r3, "%d/%d", m, n);cin >> r4;if (strcmp(r3, r4) == 0){cout << " " << "T" << endl;b = b + 1;}else cout << " " << "F" << endl;}else {cin >> r2;if (r2 == m){cout << " " << "T" << endl;b = b + 1;}else cout << " " << "F" << endl;}break;case 1:if ((x*j)<(i*y)){t = x;x = i;i = t;t = y;y = j;j = t;}else;cout << x << "/" << y << "-" << i << "/" << j << "=";m = (x*j) - (i*y);n = y*j;c = maxNum(m, n);m = m / c;n = n / c;if (n != 1) {sprintf_s(r3, "%d/%d", m, n);cin >> r4;if (strcmp(r3, r4) == 0){cout << " " << "T" << endl;b = b + 1;}else cout << " " << "F" << endl;}else {cin >> r2;if (r2 == m){cout << " " << "T" << endl;b = b + 1;}else cout << " " << "F" << endl;}break;case 2:cout << x << "/" << y << "×" << i << "/" << j << "=";m = x*i;n = y*j;c = maxNum(m, n);m = m / c;n = n / c;if (n != 1) {sprintf_s(r3, "%d/%d", m, n);cin >> r4;if (strcmp(r3, r4) == 0){cout << " " << "T" << endl;b = b + 1;}else cout << " " << "F" << endl;}else {cin >> r2;if (r2 == m){cout << " " << "T" << endl;b = b + 1;}else cout << " " << "F" << endl;}break;case 3:cout << x << "/" << y << "÷" << i << "/" << j << "=";m = x*j;n = y*i;c = maxNum(m, n);m = m / c;n = n / c;if (n != 1) {sprintf_s(r3, "%d/%d", m, n);cin >> r4;if (strcmp(r3, r4) == 0){cout << " " << "T" << endl;b = b + 1;}else cout << " " << "F" << endl;}else {cin >> r2;if (r2 == m){cout << " " << "T" << endl;b = b + 1;}else cout << " " << "F" << endl;}break;default:cout << "wrong" << endl;break;}return 0; } int main(int argc, char*argv[]) {int a, c;if (argc<2)cin >> a;else a = atoi(argv[2]);srand(time(NULL));for (int i = 0; i < a; i++) {c = rand() % 2;switch (c) {case 0:INT(); break;case 1:fra(); break;default:cout << "wrong" << endl;break;}}cout << "grade:" << b;return 0; }

運(yùn)行截圖:

碼云鏈接 https://git.oschina.net/lenmo/sizeyunsuan.git

轉(zhuǎn)載于:https://www.cnblogs.com/lenmo/p/6499027.html

總結(jié)

以上是生活随笔為你收集整理的个人作业1——四则运算题目生成程序(基于控制台)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。