简单程序实现100以内加减乘除
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单程序实现100以内加减乘除相关的知识,希望对你有一定的参考价值。
include <stdio.h>
int main(void)
{
int i = 0, a, b, ret, user_ret;
char symbol; 0(+), 1(-), 2(*), 3(/)
int count, right = 0;
srand(time(NULL));
while (1) {
a = rand() % 100;
b = rand() % 100;
symbol = rand() % 4;
switch (symbol) {
case 0 : {
ret = a + b;
symbol = ‘+‘;
} break;
case 1 : {
ret = a - b;
symbol = ‘-‘;
} break;
case 2 : {
ret = a * b;
symbol = ‘*‘;
} break;
case 3 : {
ret = a / b;
symbol = ‘/‘;
}
}
printf("%d%c%d=", a, symbol, b);
count = 0;
while (1) {
scanf("%d", &user_ret);
if (user_ret == ret) {
printf("回答正确\n");
right ++;
break;
}
printf("错误,再次输入:");
count++; // 答错计数+1
if (count == 3) {
printf("正确答案是:%d\n", ret);
break;
}
}
i++;
printf("是否继续(y/n)?\n");
getchar();
scanf("%c", &symbol);
if (symbol != ‘y‘ && symbol != ‘Y‘)
break;
}
printf("练习结束, 共做题%d, 正确%d, 正确率%.2f%%\n", i, right, (double)right / i * 100);
return 0;
}
int main(void)
{
int i = 0, a, b, ret, user_ret;
char symbol; 0(+), 1(-), 2(*), 3(/)
int count, right = 0;
srand(time(NULL));
while (1) {
a = rand() % 100;
b = rand() % 100;
symbol = rand() % 4;
switch (symbol) {
case 0 : {
ret = a + b;
symbol = ‘+‘;
} break;
case 1 : {
ret = a - b;
symbol = ‘-‘;
} break;
case 2 : {
ret = a * b;
symbol = ‘*‘;
} break;
case 3 : {
ret = a / b;
symbol = ‘/‘;
}
}
printf("%d%c%d=", a, symbol, b);
count = 0;
while (1) {
scanf("%d", &user_ret);
if (user_ret == ret) {
printf("回答正确\n");
right ++;
break;
}
printf("错误,再次输入:");
count++; // 答错计数+1
if (count == 3) {
printf("正确答案是:%d\n", ret);
break;
}
}
i++;
printf("是否继续(y/n)?\n");
getchar();
scanf("%c", &symbol);
if (symbol != ‘y‘ && symbol != ‘Y‘)
break;
}
printf("练习结束, 共做题%d, 正确%d, 正确率%.2f%%\n", i, right, (double)right / i * 100);
return 0;
}
以上是关于简单程序实现100以内加减乘除的主要内容,如果未能解决你的问题,请参考以下文章
求:随机出10道10以内小学加减法的c语言编程 每道题10分最后输出得多少分