个人作业1——四则运算题目生成程序(基于控制台)
生活随笔
收集整理的這篇文章主要介紹了
个人作业1——四则运算题目生成程序(基于控制台)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述:
從《構建之法》第一章的 “程序” 例子出發,像阿超那樣,花二十分鐘寫一個能自動生成小學四則運算題目的命令行 “軟件”,滿足以下需求:
- 除了整數以外,還要支持真分數的四則運算,真分數的運算,例如:1/6 + 1/8 = 7/24
- 運算符為 +,-,×,÷
- 并且要求能處理用戶的輸入,并判斷對錯,打分統計正確率。
- 要求能處理用戶輸入的真分數, 如 1/2, 5/12 等
- 使用 -n 參數控制生成題目的個數,例如執行下面命令將生成10個題目Myapp.exe -n 10
| PSP2.1 | Personal Software Process Stages | Time (%) Senior Student(/hour) | Time (%)(/hour) |
| · Planning | 計劃 | 2 | 1.5 |
| · Estimate | 估計這個任務需要多少時間 | 37 | 40 |
| · Analysis | 需求分析 (包括學習新技術) | 1 | 1 |
| · Coding Standard | 代碼規范 | 0.5 | 0.5 |
| · Design | 具體設計 | 1.5 | 1 |
| · Coding | 具體編碼 | 30 | 35 |
| · Test | 測試(自我測試,修改代碼,提交修改) | 1 | 1 |
| Reporting | 報告 | 1 | 1 |
源代碼:
#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; }運行截圖:
碼云鏈接 https://git.oschina.net/lenmo/sizeyunsuan.git
轉載于:https://www.cnblogs.com/lenmo/p/6499027.html
總結
以上是生活随笔為你收集整理的个人作业1——四则运算题目生成程序(基于控制台)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: window.onload 不执行
- 下一篇: Arithmetic_Thinking