生成一段四则运算的C语言代码
时间: 2023-05-15 14:05:34 浏览: 93
以下是一段生成四则运算的C语言代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
int a = rand() % 100;
int b = rand() % 100;
int c = rand() % 100;
int d = rand() % 100;
int result = 0;
char op1, op2, op3;
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:
op3 = '+';
result += d;
break;
case 1:
op3 = '-';
result -= d;
break;
case 2:
op3 = '*';
result *= d;
break;
case 3:
op3 = '/';
result /= d;
break;
}
printf("%d %c %d %c %d %c %d = %d\n", a, op1, b, op2, c, op3, d, result);
return 0;
}
阅读全文