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

  • 相关阅读:
    jQuery诞生记-原理与机制
    你所不知的 CSS ::before 和 ::after 伪元素用法
    http中get与post的区别
    Http请求方法
    TCP/IP详解学习笔记(4)-ICMP协议,ping和Traceroute
    TCP/IP详解学习笔记(3)-IP协议,ARP协议,RARP协议
    TCP/IP详解学习笔记(2)-数据链路层
    TCP/IP详解学习笔记(1)-基本概念
    全面解析Java的垃圾回收机制
    深入Java虚拟机:JVM中的Stack和Heap
  • 原文地址:https://www.cnblogs.com/WQY0903/p/7560411.html
Copyright © 2011-2022 走看看