• 四则运算生成器


    四则运算生成器

    1.代码来源:自己编写

    2.运行环境:VC++6.0

    3.编程语言:c语言

    4.bug:未发现

    5.当前功能:可以生成0-10的四则运算的题,在每道题做完后判断正确性和给出正确答案。

    5.功能扩展的方向:增加复合运算。

    6.gitbub代码地址:https://github.com/selfTboke/project1

    7.实现

    代码如下:

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

    int main()
    {
    int type;
    int left, right,center;
    float result;
    srand(unsigned(time(NULL)));
    while(1)
    {
    //清屏
    system("cls");
    //生成随机数值
    type = rand() % 7;
    left = rand() % 10;
    right = rand() % 10;
    center = rand() % 10;

    switch(type)
    {
    case 0:
    printf("%d + %d = ?\n", left, right);
    scanf("%f", &result);
    if(left + right == result)
    printf("right!\n");
    else
    printf("wrong!The result is %d\n", left + right);
    break;
    case 1:
    //保证被减数大于减数
    if(left < right)
    break;
    printf("%d - %d = ?\n", left, right);
    scanf("%f", &result);
    if(left - right == result)
    printf("right!\n");
    else
    printf("wrong!The result is %d\n", left - right);
    break;
    case 2:
    printf("%d * %d = ?\n", left, right);
    scanf("%f", &result);
    if(left * right == result)
    printf("right!\n");
    else
    printf("wrong!The result is %d\n", left * right);
    break;
    case 3:
    //判断除数是否为0
    if(right == 0)
    break;
    printf("%d / %d = ?\n", left, right);
    printf("除不尽时请保留三位小数:");
    scanf("%f", &result);
    if(fabs(float(left) / right - result) <= 0.001 )
    printf("right!\n");
    else
    printf("wrong!The result is %.3f\n", float(left) / right);
    break;
    case 4:
    printf("%d + %d + %d = ?\n",left,center,right);
    scanf("%f",&result);
    if(left+center+right == result)
    printf("right!\n");
    else
    printf("wrong!The result is %d\n", left+center+right);
    break;
    case 5:
    if(left<(center+right))
    break;
    printf("%d - %d - %d = ?\n",left,center,right);
    scanf("%f",&result);
    if(left-center-right == result)
    printf("right!\n");
    else
    printf("wrong!The result is %d\n", left-center-right);
    break;
    case 6:
    if((left+center)<right)
    break;
    printf("%d + %d - %d = ?\n",left,center,right);
    scanf("%f",&result);
    if(left+center-right == result)

    else
    printf("wrong!The result is %d\n", left+center-right);
    break;
    }
    //防止闪退
    system("pause");

    }

    return 0;
    }

  • 相关阅读:
    使用迭代器模式批量获得数据(C#实现)
    如何从技术上预防抢票软件刷屏
    如何用Tesseract做日文OCR(c#实现)
    我的.net开发百宝箱
    程序员必备基础:Git 命令全方位学习
    Java 异常处理的十个建议
    50道Java集合经典面试题(收藏版)
    记一次接口性能优化实践总结:优化接口性能的八个建议
    100道MySQL数据库经典面试题解析(收藏版)
    800+Java后端经典面试题,希望你找到自己理想的Offer呀~
  • 原文地址:https://www.cnblogs.com/selfTboke/p/7543350.html
Copyright © 2020-2023  润新知