简单四则运算

Posted thevilewind

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单四则运算相关的知识,希望对你有一定的参考价值。

  我结合网上的demo,我写了一个功能并不完整的四则运算

  1 #include<stdio.h>
  2 #include<math.h>
  3 #include<stdlib.h>
  4 #include<time.h>
  5 #include<iostream>
  6 using namespace std;
  7 int add();
  8 int subtraction();
  9 int comop();
 10 int mul();
 11 
 12 int main() {
 13     int x, y, m, n;
 14     float s;
 15     system("cls");
 16     printf("请选择语言 00:中文,01:英文");
 17     cin >> x;
 18     if (x == 0) {
 19 
 20         printf("小学四则运算\n");
 21         printf("0:加法\n"); 
 22         printf("1:减法\n"); 
 23         printf("4:退出程序\n"); 
 24             }else 
 25         {
 26         printf("Primary Artithemetic Practice\n"); 
 27         printf("0:add\n"); 
 28         printf("1:减法\n"); 
 29         printf("退出程序"); 
 30             }
 31     
 32     while (1 == 1) {
 33 
 34     scanf_s("%d", &y);
 35     switch(y){
 36     case 0:
 37         s = add();
 38         break;
 39     case 1:
 40         s =subtraction();
 41         break;
 42     case 2:
 43         s = mul();
 44         break;
 45     case 3:
 46         s = comop();
 47         break;
 48     case 4:
 49         exit(0);
 50         break;
 51     default:
 52         exit(0);
 53         break;
 54     }
 55 }
 56     system("pause");
 57 }
 58     int add() {
 59         srand(time(NULL));
 60         int a, b,i,uanswer,ranswer;
 61         int x = 0;
 62         for (i = 0; i < 5; i++) {
 63             a = rand() % 100;
 64             b = rand() % 100;
 65             ranswer = a + b;
 66             printf("%d+%d=",a,b);
 67             scanf_s("%d",&uanswer);
 68             if (ranswer == uanswer) {
 69                 printf("T");
 70                 x++;
 71             }
 72             else {
 73                 printf("F");
 74                 
 75             }
 76             printf("%d", x);
 77             continue;
 78         }
 79         return 0;
 80     }
 81     int subtraction() {
 82         srand(time(NULL));
 83         int a, b,c, i, uanswer, ranswer;
 84         int x = 0;
 85         for (i = 0; i < 5; i++) {
 86             a = rand() % 100;
 87             b = rand() % 100;
 88             if (a < b) {
 89                 c = a;
 90                 a = b;
 91                 b = c;
 92             }
 93             ranswer = a - b;
 94             printf("%d-%d=", a, b);
 95             scanf_s("%d", &uanswer);
 96             if (ranswer == uanswer) {
 97                 printf("T");
 98                 x++;
 99             }
100             else {
101                 printf("F");
102 
103             }
104             printf("%d", x);
105             continue;
106         }
107         return 0;
108     }
109 
110     int mul() {
111         srand(time(NULL));
112         int a, b, i, uanswer, ranswer;
113         int x = 0;
114         for (i = 0; i < 5; i++) {
115             a = rand() % 100;
116             b = rand() % 100;
117             ranswer = a * b;
118             printf("%d*%d=", a, b);
119             scanf_s("%d", &uanswer);
120             if (ranswer == uanswer) {
121                 printf("T");
122                 x++;
123             }
124             else {
125                 printf("F");
126 
127             }
128             printf("%d", x);
129             continue;
130         }
131         return 0;
132     }
133     int devision() {
134         srand(time(NULL));
135         int a, b, i, uanswer, ranswer;
136         int x = 0;
137         for (i = 0; i < 5; i++) {
138             a = rand() % 100;
139             b = rand() % 100;
140             if (b == 0) 
141             {
142                 b = rand() % 100;
143             }
144             ranswer = a / b;
145             printf("%d/%d=", a, b);
146             scanf_s("%d", &uanswer);
147             if (ranswer == uanswer) {
148                 printf("T");
149                 x++;
150             }
151             else {
152                 printf("F");
153 
154             }
155             printf("%d", x);
156             continue;
157         }
158         return 0;
159     }
160 
161     int comop() {
162         srand(time(NULL));
163         int a, b,c,m,n, i, uanswer, ranswer;
164         int x = 0;
165         for (i = 0; i < 5; i++) {
166             a = rand() % 100;
167             b = rand() % 100;
168             m = rand() % 100;
169             n = rand() % 100;
170             c = rand() % 5;
171             switch (c) {
172             case 0:
173                 ranswer = (a*b) + (m*n);
174                 printf("%d*%d+%d*%d=", a, b, m, n);
175                 scanf_s("%d", &uanswer);
176                 if (ranswer == uanswer) {
177                     printf("T");
178                     x++;
179                 }
180                 else {
181                     printf("F");
182 
183                 }
184                 printf("%d", x);
185                 break;
186 
187             case 1:
188                 ranswer = (a+b)*m;
189                 printf("(%d+%d)*%d=", a, b, m);
190                 scanf_s("%d", &uanswer);
191                 if (ranswer == uanswer) {
192                     printf("T");
193                     x++;
194                 }
195                 else {
196                     printf("F");
197 
198                 }
199                 printf("%d", x);
200                 break;
201             }
202             
203         }
204         return 0;
205     }

 

以上是关于简单四则运算的主要内容,如果未能解决你的问题,请参考以下文章

CSS3的calc()用法简单介绍

为啥这段代码会泄露? (简单的代码片段)

如何在 python 中并行化以下代码片段?

20个简洁的 JS 代码片段

获取 badarith,[erlang,'+',[error,0],[],同时使用 Erlang 片段在 TSUNG 中执行算术运算

代码片段 - Golang 实现简单的 Web 服务器