zoukankan      html  css  js  c++  java
  • 四则运算器改进

    本篇GitHub地址:https://github.com/WQY0903/class01

    在四则运算器基本功能上添加了判断正误的功能。

    引用自网上的简单四则运算器代码,加以修改。

    代码如下:

    1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <time.h>
     4 #include <math.h>
     5
     6 int main()
     7 {
     8     int i = 0;
     9     int n = 0;
    10     int x = 0;
    11     int type;
    12     char flag;
    13     int a, b;
    14     float result1,result2;
    15     printf("请输入要出的题目数量 ");
    16     scanf_s("%d", &n);
    17     srand(unsigned(time(NULL)));
    18     while (x<n)
    19     {
    20         type = rand() % 4;
    21         a = rand() % 100;
    22         b = rand() % 100;
    23         switch (type)
    24         {
    25         case 0:
    26             printf("%d + %d = ? ", a, b);
    27             result1 = a + b;
    28             break;
    29         case 1:
    30             printf("%d - %d = ? ", a, b);
    31             result1 = a -b;
    32             break;
    33         case 2:
    34             printf("%d * %d = ? ", a, b);
    35             result1 = a * b;
    36             break;
    37         case 3:
    38             printf("%d / %d = ? ", a, b);
    39             result1 = (float)a / b;
    40             break;
    41         }
    42         printf_s("请输入答案:");
    43         scanf_s("%f",&result2);
    44         if (fabs(result1 - result2)<=1e-6)
    45         {
    46             printf_s("计算正确 ");
    47         }
    48         else
    49         {
    50             printf_s("计算错误 ");
    51         }
    52
    53
    54         i++;
    55         while (i >= n)
    56         {
    57            
    58             printf("谢谢使用! ");
    59             fflush(stdin);
    60             getchar();
    61             return 0;
    62         }
    63     }
    64

  • 相关阅读:
    [hihocoder][Offer收割]编程练习赛62
    [laravel]用户异地登录后踢掉之前的登录
    [hihocoder][Offer收割]编程练习赛57
    [hihocoder][Offer收割]编程练习赛58
    线性基
    数组墙 最详细的解题报告
    MySQL中EXPLAIN命令详细解析
    MySQL性能优化
    MySQL中的事务隔离级别
    Java NIO的理解和应用
  • 原文地址:https://www.cnblogs.com/WQY0903/p/7560411.html
Copyright © 2011-2022 走看看