軟工第一次做業——小學四則運算

題目描述:git

從《構建之法》第一章的 「程序」 例子出發,像阿超那樣,花二十分鐘寫一個能自動生成小學四則運算題目的命令行 「軟件」,知足如下需求:windows

    1. 除了整數之外,還要支持真分數的四則運算,真分數的運算,例如:1/6 + 1/8 = 7/24
    2. 運算符爲 +, −, ×, ÷
    3. 而且要求能處理用戶的輸入,並判斷對錯,打分統計正確率。
    4. 要求能處理用戶輸入的真分數, 如 1/2, 5/12 等
    5. 使用 -n 參數控制生成題目的個數,例如執行下面命令將生成10個題目
      Myapp.exe -n 10

需求分析:app

  1. 可以實現生成整數和分數的式子並實現計算;
  2. 可以控制生成式子的個數;
  3. 可以實現判斷對錯並計算正確率;

設計實現:dom

  1. 經過if實現選擇生成整數或分數的式子並計算;
  2. 顯示正確率

代碼說明:函數

#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#include<string.h>


using namespace std;

#define OPERATOR 4


//獲取隨機運算符
char getopre() {
    char oper[OPERATOR] = { '+','-','*','/' };
    srand((unsigned)time(NULL));
    return oper[rand() % 4];
}
//獲取隨機數
int random(double start, double end)

{
    return (int)(start + (end - start)*rand() / (RAND_MAX + 1.0));
}



//列式子
void equation() {
    int num1;
    int num2;
    int result;
    int answer;
    char oper;
    int right=0;
    float accuracy;
    int i, n;
    printf("請輸入要生成的式子數:");
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        srand((unsigned)time(NULL));
        oper = getopre();
        num1 = random(0, 20);
        num2 = random(1, 20);
        switch (oper)
        {
        case '+':  result = num1 + num2;
            printf("%d+%d=\n", num1, num2); break;
        case '-':  result = num1 - num2;
            printf("%d-%d=\n", num1, num2); break;
        case '*':  result = num1*num2;
            printf("%d*%d=\n", num1, num2); break;
        case '/':  num1 = num1*num2;
                   result = num1 / num2; 
                   printf("%d÷%d=\n", num1, num2);
                   break;
        }
        //printf("%d%c%d=\n", num1, oper, num2);
        printf("請輸入你的答案:");
        scanf("%d", &answer);
        if (result==answer) {
            printf("恭喜你答對了\n\n");
            right++;
        }
        else {
            printf("對不起答錯了\n");
            printf("正確答案爲:%d\n\n", result);
        }
    }
    accuracy = ((float)right /(float) n)*100;
    printf("正確率爲百分之%1.2f\n", accuracy);
}

//真分數列式

void fraction() {
    
    int num1;
    int num2;
    int num3;
    int num4;
    int a, b;
    int answer1, answer2;
    float result;
    float answer;
    char oper;
    int right = 0;
    float accuracy;
    int i, n;
    printf("請輸入要生成的式子數:");
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        srand((unsigned)time(NULL));
        oper = getopre();
        num1 = random(0, 20);
        num2 = random(1, 20);
        num3 = random(0, 20);
        num4 = random(1, 20);
        switch (oper)
        {
        case '+': 
            a = num1*num4+num3*num2;
            b = num2*num4;
            result = (float)a / (float)b;
            printf("%d/%d+%d/%d=\n", num1, num2, num3, num4); break;
        case '-': 
            a = num1*num4 - num3*num2;
            b = num2*num4;
            result = (float)a / (float)b;
            printf("%d/%d-%d/%d=\n", num1, num2, num3, num4); break;
        case '*':  
            a = num1*num3;
            b = num2*num4;
            result = (float)a / (float)b;
            printf("%d/%d*%d/%d=\n", num1, num2,num3, num4); break;
        case '/': 
            a = num1*num4;
            b = num2*num3;
            result =(float) a /(float) b;
            printf("%d/%d÷%d/%d=\n", num1, num2, num3, num4);
            break;
        }
        printf("請輸入你的答案:\n");
        scanf("%d", &answer1);
        printf("/\n");
        scanf("%d", &answer2);
        answer = (float)answer1 / (float)answer2;
        if (result==answer) {
            printf("恭喜你答對了\n\n");
            right++;
        }
        else {
            printf("對不起答錯了\n");
            printf("正確答案爲:%d/%d\n\n", a,b);
        }
    }
    accuracy = ((float)right / (float)n) * 100;
    printf("正確率爲百分之%1.2f\n", accuracy);
}


//主函數

void main() {
    int choose;
    printf("輸入1生成整數算式,其餘生成分式算式:");
    scanf("%d", &choose);
    if (choose == 1) {
        equation();
    }
    else {
        fraction();
    }
}

測試運行:學習

PSP展現:測試

PSP2.1 Personal Software Process Stages Time (%) Senior Student(/hour) Time (%)(/hour)
· Planning 計劃 2 2
· Estimate 估計這個任務須要多少時間 30 31
· Analysis 需求分析 (包括學習新技術) 1 1
· Coding Standard 代碼規範 0.5 0.5
· Design 具體設計 2 1
· Coding 具體編碼 25 27
· Test 測試(自我測試,修改代碼,提交修改) 1 1
Reporting 報告 1 1

碼雲連接:https://git.coding.net/ras/work1.git編碼

相關文章
相關標籤/搜索