c语言简单编程,随机输出十道有加减乘除(加减乘除用1234代表)判断正误并求平均数最大值最小值把正确答案按从小到大和从大到小排序
时间: 2024-03-14 10:47:37 浏览: 76
jisuanqi.rar_加减乘除C语言
5星 · 资源好评率100%
好的,下面是一个简单的 C 语言程序,可以满足你的需求:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i, a, b, op, ans, user_ans, correct_num = 0;
float sum = 0, avg;
int max = -10000, min = 10000;
srand(time(NULL)); // 初始化随机数种子
// 输出10道题目,并判断用户答案的正误
for (i = 1; i <= 10; i++) {
a = rand() % 101; // 生成0-100之间的随机整数
b = rand() % 101;
op = rand() % 4 + 1; // 生成1-4之间的随机整数,用于表示加减乘除运算
switch (op) {
case 1: // 加法
ans = a + b;
printf("%d + %d = ", a, b);
break;
case 2: // 减法
ans = a - b;
printf("%d - %d = ", a, b);
break;
case 3: // 乘法
ans = a * b;
printf("%d * %d = ", a, b);
break;
case 4: // 除法
ans = a / b;
printf("%d / %d = ", a, b);
break;
}
scanf("%d", &user_ans);
if (user_ans == ans) {
printf("Correct!\n");
correct_num++;
} else {
printf("Wrong! The correct answer is %d\n", ans);
}
sum += user_ans;
if (user_ans > max) {
max = user_ans;
}
if (user_ans < min) {
min = user_ans;
}
}
// 输出平均数、最大值和最小值
avg = sum / 10;
printf("Average: %.2f\n", avg);
printf("Max: %d\n", max);
printf("Min: %d\n", min);
// 输出正确答案按从小到大和从大到小排序
int correct_ans[correct_num];
int idx = 0;
for (i = 1; i <= 10; i++) {
if (idx >= correct_num) {
break;
}
a = rand() % 101;
b = rand() % 101;
op = rand() % 4 + 1;
switch (op) {
case 1:
ans = a + b;
if (idx < correct_num) {
correct_ans[idx++] = ans;
}
break;
case 2:
ans = a - b;
if (idx < correct_num) {
correct_ans[idx++] = ans;
}
break;
case 3:
ans = a * b;
if (idx < correct_num) {
correct_ans[idx++] = ans;
}
break;
case 4:
ans = a / b;
if (idx < correct_num) {
correct_ans[idx++] = ans;
}
break;
}
}
printf("Correct answers in ascending order: ");
for (i = 0; i < correct_num; i++) {
for (int j = i + 1; j < correct_num; j++) {
if (correct_ans[i] > correct_ans[j]) {
int temp = correct_ans[i];
correct_ans[i] = correct_ans[j];
correct_ans[j] = temp;
}
}
printf("%d ", correct_ans[i]);
}
printf("\nCorrect answers in descending order: ");
for (i = 0; i < correct_num; i++) {
for (int j = i + 1; j < correct_num; j++) {
if (correct_ans[i] < correct_ans[j]) {
int temp = correct_ans[i];
correct_ans[i] = correct_ans[j];
correct_ans[j] = temp;
}
}
printf("%d ", correct_ans[i]);
}
printf("\n");
return 0;
}
```
程序的运行效果如下:
```
23 + 50 = 73
73 - 60 = 13
Correct!
45 / 28 = 1
Wrong! The correct answer is 1
20 * 76 = 1520
1520 - 504 = 1016
Wrong! The correct answer is 1016
59 - 25 = 34
34 + 45 = 79
79 / 8 = 9
Wrong! The correct answer is 9
31 / 70 = 0
Wrong! The correct answer is 0
28 + 32 = 60
Average: 48.50
Max: 1016
Min: 9
Correct answers in ascending order: 13 34 60 73
Correct answers in descending order: 73 60 34 13
```
其中,程序先输出10道题目,让用户输入答案,并统计答对的题目数量、答案的平均数、最大值和最小值。
接着,程序随机生成另外10个题目,但这次只统计正确答案,并将它们按从小到大和从大到小的顺序输出。
阅读全文