c语言程序设计 随机生成十道题包括加减乘除判断正误并求出平均值选出最大值最小值,将正确结果从大到小和从小到大排序
时间: 2024-03-14 10:47:44 浏览: 70
好的,我来给你写一个随机生成十道加减乘除题目的c语言程序,然后通过用户输入答案进行判断正误,并计算平均值,选出最大值和最小值,并将正确结果从大到小和从小到大排序。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i;
int a, b, c, result, answer;
int correct_count = 0;
int sum = 0;
int max = 0, min = 100;
int results[10];
printf("随机生成十道加减乘除题目,你需要输入答案并判断正误\n");
srand((unsigned)time(NULL)); // 设置种子
for (i = 0; i < 10; i++)
{
a = rand() % 100;
b = rand() % 100;
c = rand() % 4; // 随机生成加减乘除符号
switch (c)
{
case 0:
result = a + b;
printf("%d + %d = ", a, b);
break;
case 1:
result = a - b;
printf("%d - %d = ", a, b);
break;
case 2:
result = a * b;
printf("%d * %d = ", a, b);
break;
case 3:
result = a / b;
printf("%d / %d = ", a, b);
break;
}
scanf("%d", &answer); // 用户输入答案
if (answer == result)
{
printf("正确!\n");
correct_count++;
results[i] = result;
}
else
{
printf("错误!\n");
results[i] = -1;
}
sum += result;
if (result > max)
max = result;
if (result < min)
min = result;
}
printf("你一共回答了 %d 道题,其中 %d 道题回答正确\n", 10, correct_count);
printf("这十道题的平均值是 %d\n", sum / 10);
printf("其中最大值是 %d,最小值是 %d\n", max, min);
// 从大到小排序
for (i = 0; i < 10; i++)
{
int j, temp;
for (j = i + 1; j < 10; j++)
{
if (results[i] < results[j])
{
temp = results[i];
results[i] = results[j];
results[j] = temp;
}
}
}
printf("从大到小排序的结果为:\n");
for (i = 0; i < 10; i++)
{
if (results[i] != -1)
printf("%d ", results[i]);
}
printf("\n");
// 从小到大排序
for (i = 0; i < 10; i++)
{
int j, temp;
for (j = i + 1; j < 10; j++)
{
if (results[i] > results[j])
{
temp = results[i];
results[i] = results[j];
results[j] = temp;
}
}
}
printf("从小到大排序的结果为:\n");
for (i = 0; i < 10; i++)
{
if (results[i] != -1)
printf("%d ", results[i]);
}
printf("\n");
return 0;
}
```
希望这个程序能够帮到你!
阅读全文