• 结对项目-四则运算 “软件”之升级版


    本作业要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2213 

    我的githup地址为:https://github.com/lianglina98/ruanjian02

    、基本要求

    从个人项目出发,将程序改造成一个单机带用户界面(不是控制台)的程序,这个程序最基本要达到:

    • 自动生成题目,单个题目最多不能超过4个运算符,操作数小于100。
    • 用户可以输入答案
    • 若用户输入答案正确,则提示正确;若答案错误,则提示错误,并要提示正确答案是多少。

    二、扩展方向

    1. 程序可以出带括号的正整数四则运算,支持分数,除法保留两位小数,如:(1/3+1)*2 = 2.67,特别注意:这里是2.67而非2.66(保留两位小数功能实现,不支持出带括号题目)
    2. 用户可以选择出题的个数(最多不能超过5个题目),答题结束可以显示用户答错的题目个数和答对的题目个数(实现)

    三、开发环境调试:Visual C++

    四、代码呈现

    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #include<math.h>
    #include<windows.h>
    float calculate(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 calculate2(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;
    }
    int createnumber()
    {
    return rand() % 100+1;
    }

    int createnumber2()
    {
    return rand() % 20+1;
    }

    char createnumberop()
    {
    int op;
    op=rand() % 4+1;
    switch(op)
    {
    case 1:return'+';
    case 2:return'-';
    case 3:return'*';
    case 4:return'/';
    }
    return 0;
    }

    void exercises(int n)
    {
    int i,right=0,wrong=0;
    float a,b,c,answer,result1,result2;
    char op,op2;
    srand(time(NULL));
    for(i=0;i<n;i++)
    {
    a=(float)createnumber();
    b=(float)createnumber();
    c=(float)createnumber();
    op=createnumberop();
    op2=createnumberop();
    result1=calculate2(calculate(a,op,b),op2,c);
    result2=calculate(a,op,calculate2(b,op2,c));
    if(result1 < 0 || result2 <0)
    {
    i--;
    continue;
    }
    else{
    printf("%.f %c %.f %c %.f = ",a,op,b,op2,c);
    scanf("%f",&answer);
    if((op=='+' || op=='-') && (op2=='*' || op2=='/'))
    {
    if((int)(100.0*answer+0.5)/100.0==(int)(100.0*result2+0.5)/100.0 && result2 >=0)
    {
    printf(" 【回答正确!】 ");
    right++;
    }
    else
    {
    printf(" 【回答错误!答案是:%.2f】 ",(int)(100.0*result2+0.5)/100.0);
    wrong++;
    }
    }
    else
    {
    if((int)(100.0*answer+0.5)/100.0==(int)(100.0*result1+0.5)/100.0 && result1 >=0)
    {
    printf(" 【回答正确!】 ");
    right++;
    }
    else
    {printf(" 【回答错误!答案是:%.2f】 ",(int)(100.0*result1+0.5)/100.0);
    wrong++;
    }
    }
    }
    printf(" 你答对了%d题,答错了%d题 ",right,wrong);
    }
    }

    double gongbeishu(double b,double d)
    {
    double i,max;
    max=b>d?b:d;
    for(i=max;;i++)
    if((int)(i)%(int)(b)==0 && (int)(i)%(int)(d)==0)
    break;
    return i;
    }

    double gongyueshu(double a,double c)
    {
    double i,min;
    min=a<c?a:c;
    for(i=2;i<=min;i++)
    if((int)(a)%(int)(i)==0 && (int)(c)%(int)(i)==0)
    break;
    if(i>min)
    i=1;
    return i;
    }


    int main()
    {
    int n;
    printf(" ——————————————四则运算------------------------------------------- ");
    printf(" | 欢迎进入四则运算小程序! | ");
    printf(" ——————————————--------------------------------------------------- ");
    while(1)
    {
    do{
    printf(" 请输入四则运算的数目:");
    scanf("%d",&n);
    if(n<=0)
    printf("数目有误,请重新输入! ");
    if(n>5)
    printf("最多不能超过5个题目,请重新输入! ");
    }while(n<=0 || n>5);
    exercises(n);
    }
    }

    五、运行结果截图

    六、感想

    我并没有结对,自己的基础不好,不想拖累别人。这个作业我做得很简单,主要的问题就在于计算上,其他基本没有什么问题。

  • 相关阅读:
    学习数据结构的网站
    线程的自动释放、挂起、运行
    centos7 odbc mysql
    c++和c不同
    Linux挂载u盘作为本地yum源安装gcc
    构建基于JAVASCRIPT的移动WEB CMS入门——简介(转载)
    数据库SQL优化大总结之 百万级数据库优化方案
    webgl 刷底色的基本步骤
    前端下载文件流并且获取heads中的filename文件名且解决中文乱码的方法
    Python little knowledge
  • 原文地址:https://www.cnblogs.com/lianglin918/p/9843232.html
Copyright © 2020-2023  润新知