首页 > 代码库 > 简单程序实现100以内加减乘除
简单程序实现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以内加减乘除
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。