个人作业1——四则运算题目生成程序

四则运算题目生成程序

 

需求分析:

    1.除了整数以外,还要支持真分数的四则运算,真分数的运算,例如:1/6 + 1/8 = 7/24

    2.运算符为 +, −, ×, ÷

    3.要求能处理用户的输入,并判断对错,打分统计正确率。

    4.要求能处理用户输入的真分数, 1/2, 5/12 等

    5.使用 -n 参数控制生成题目的个数,例如执行下面命令将生成10个题目
       Myapp.exe -n 10

设计实现:

1.使用if条件判断,选择进行整式运算还是分式运算

2.获取随机运算符号与随机数

3.分别编写整式运算函数与分式运算函数

4.分别计算正确率

代码说明:

#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

计划

3

4.5

· Estimate

估计这个任务需要多少时间

4

4.5

· Analysis

需求分析 (包括学习新技术)

1

0.5

· Coding Standard

代码规范

0.2

0.2

· Design

具体设计

1

0.5

· Coding

具体编码

1.5

2

· Test

测试(自我测试,修改代码,提交修改)

0.5

1

Reporting

报告

0.5

0.5

 

实验小结:本次实验的知识之前都学习过,但是还是不熟悉,编写函数的时候有很多小问题,还需要多加练习。

码市地址:https://coding.net/u/ltt979/p/zuoye1/git/tree/master

 

posted on 2017-03-07 19:28  廖婷婷979  阅读(203)  评论(1编辑  收藏  举报