个人项目-小学四则运算 “软件”之初版

作业要求来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2186

github地址:https://github.com/gswyz/-

一、题目要求

像《构建之法》的人物阿超那样,写一个能自动生成小学四则运算题目的命令行 “软件”。具体要求:任何编程语言都可以,命令行程序接受一个数字输入,然后输出相应数目的四则运算题目和答案。例如输入数字是 30, 那就输出 30 道题目和答案。 运算式子必须至少有两个运算符,运算数字是在 100 之内的正整数,答案不能是负数。 如:23 - 3 * 4 = 11

扩展要求:

1) 要求能出和真分数 (二分之一, 十二分之五,等)相关的练习题。

2) 并且要求能处理用户的输入,并判断对错,打分统计。 要求能处理用户输入的真分数, 如 1/2, 5/12 等。


二、设计思路

编程语言:c语言

工具:

设计思路:主函数让用户输入要练习的题目数量;而后随机产生随机数和随即运算符并产生运算式;判别用户输入的结果是否与正确答案一致,提示输入值是否正确,答题继续。答题完毕后,统计错对情况给出判别数据。

类。

四、运算结果

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<windows.h>

float algorithm(float x,char op,float y);
float algorithm2(float x,char op2,float y);
char createnumberop();
void test1(int n);

main()
{
    int chooce,n;
    while(1)
    {
        printf("\t请输入你要选择的习题:\n");
        printf("\n\t1.100以内四则运算 (小数点后保留两位)\n\n");
        do{
            printf("选择为:");
            scanf("%d",&chooce);
            if(chooce!=1)
                printf("【输入有误,请重新输入!】\n");
        }while(chooce!=1);
        printf("\n请输入四则运算的数目:");
        while(1)
            {
            scanf("%d",&n);
            if(n<=0)
                printf("【数目有误,请重新输入!】\n");
            else
                break;
        }
        if(chooce==1)
        {
            test1(n);
        }
    }
}

//第一个运算符计算
float algorithm(float x,char op,float y)
{
    float result;
    switch(op)
    {
    case'+':result = x + y;break;
    case'-':result = x - y;break;
    case'*':result = x * y;break;
    case'/':result = x / y;break;
    }
    return result;
}
//第二个运算符计算
float algorithm2(float x,char op2,float y)
{
    float result;
    switch(op2)
    {
    case'+':result = x + y;break;
    case'-':result = x - y;break;
    case'*':result = x * y;break;
    case'/':result = x / y;break;
    }
    return result;
}

//随机产生运算符
char mark()
{
    int op;
    op=rand() % 4+1;
    switch(op)
    {
    case 1:return'+';
    case 2:return'-';
    case 3:return'*';
    case 4:return'/';
    }
    return 0;
}

//100以内四则运算
void test1(int n)
{
    int i,rightnum=0,wrongnum=0;//rightnumber正确的个数,wrongnumber错误的个数
    float a,b,c,answer,result;
    char op,op2;//运算符
    srand(time(NULL));
    for(i=0;i<n;i++)
    {
        a=(float)(rand() % 100+1);//获取随机数
        b=(float)(rand() % 100+1);
        c=(float)(rand() % 100+1);
        op=mark();//获取随机运算符
        op2=mark();
        if((op=='+' || op=='-') && (op2=='*' || op2=='/')){
        result=algorithm(a,op,algorithm2(b,op2,c));
        }
        else{
        result=algorithm2(algorithm(a,op,b),op2,c);//根据运算符优先级得出结果
    }
        if(result< 0)//如果结果为非正数,则重新获取题目
        {
            i--;
            continue;
        }
        else{
            printf("%.f %c %.f %c %.f = ",a,op,b,op2,c);
            scanf("%f",&answer);

                if((int)(100.0*answer+0.5)/100.0==(int)(100.0*result+0.5)/100.0 && result>=0)//四舍五入精确到小数点后两位
                {
                    printf("【回答正确!】\n\n");
                    rightnum++;
                }
                else
                {
                    printf("【回答错误!答案是:%.2f】\n\n",(int)(100.0*result+0.5)/100.0);
                    wrongnum++;
                }
        }
    }
    printf("你总共答对了%d题,准确率为%.2f%!\n",n,rightnum,(float)rightnum/(float)n*100);
    system("pause");
    system("CLS");
    printf("\n\n");
}

 

 


 五、遇到的问题

 主要问题是不知道怎么进行优先级计算,解决方法是上网去了解其他人的想法,在结合自己的想法进行修改。


 六、个人软件过程耗时估计与统计表

 

PSP2.1 Personal Software Process Stages Time Senior Student Time
Planning 计划 5 15
 Estimate 估计这个任务需要多少时间 10 12
Development 开发 25 30
Analysis 需求分析 (包括学习新技术) 2 50
Design Spec 生成设计文档 4 5
Design Review 设计复审 3 3
Coding Standard 代码规范 1 1
 Design 具体设计 10 12
 Coding 具体编码 10 50
Code Review 代码复审 2 2
Test 测试(自我测试,修改代码,提交修改) 1 1
Reporting 报告 3 3
  测试报告 1 1
  计算工作量 2 3
  并提出过程改进计划 1 1

 

posted @ 2018-10-15 13:04  潘文祥  阅读(166)  评论(1编辑  收藏  举报