c语言四则混合运算代码
时间: 2023-08-01 10:06:51 浏览: 423
以下是一个简单的 C 语言四则混合运算代码示例:
```c
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int a, b, c, d, e, f, result;
char op1, op2;
srand((unsigned)time(NULL));
a = rand() % 101;
b = rand() % 101;
c = rand() % 101;
d = rand() % 101;
e = rand() % 101;
f = rand() % 101;
switch (rand() % 4) {
case 0:
op1 = '+';
result = a + b;
break;
case 1:
op1 = '-';
result = a - b;
break;
case 2:
op1 = '*';
result = a * b;
break;
case 3:
op1 = '/';
result = a / b;
break;
}
switch (rand() % 4) {
case 0:
op2 = '+';
result += c;
break;
case 1:
op2 = '-';
result -= c;
break;
case 2:
op2 = '*';
result *= c;
break;
case 3:
op2 = '/';
result /= c;
break;
}
switch (rand() % 4) {
case 0:
op2 = '+';
result += d;
break;
case 1:
op2 = '-';
result -= d;
break;
case 2:
op2 = '*';
result *= d;
break;
case 3:
op2 = '/';
result /= d;
break;
}
switch (rand() % 4) {
case 0:
op2 = '+';
result += e;
break;
case 1:
op2 = '-';
result -= e;
break;
case 2:
op2 = '*';
result *= e;
break;
case 3:
op2 = '/';
result /= e;
break;
}
switch (rand() % 4) {
case 0:
op2 = '+';
result += f;
break;
case 1:
op2 = '-';
result -= f;
break;
case 2:
op2 = '*';
result *= f;
break;
case 3:
op2 = '/';
result /= f;
break;
}
printf("%d %c %d %c %d %c %d %c %d %c %d = %d\n", a, op1, b, op2, c, op2, d, op2, e, op2, f, result);
return 0;
}
```
该代码会生成 6 个随机数,分别进行四则混合运算,并输出运算式及结果。
阅读全文