要求实现一个能提供加、减、乘、除运算的小型系统,进行整数的加、减、乘、除等运算。
具体要求:
(1)实现简单的菜单显示,例如:
1加法运算
2减法运算
3乘法运算
4除法运算
5混合运算
0退出
(2)每种运算下,系统随机产生两个整数(1-100之间)参加运算,用户在线下也用这两个整数完成运算,并将运算结果录入系统,系统接收用户录入并与自动计算的结果进行比较,如果相等则显示“very good”,否则显示“wrong!”。
(3)用户选择一种运算后,系统随机产生5道题目,当用户线下运算完毕后,录入系统,系统接收完毕,给出正确率。例如用户答对了3道题,则显示正确率为60%,做错的题,可以考虑两给用户两次答题机会。运算完毕后,系统返回主菜单,供用户再次选择。
(4)考虑对答题时间做限制,超出时间判答错。
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
void menu()//菜单
{
printf("**************1.Add*************\n");
printf("**************2.Subtract********\n");
printf("**************3.Multiply********\n");
printf("**************4.Divide**********\n");
printf("**************5.Mix*************\n");
printf("**************0.exit************\n");
printf("请输入你的选择:\n");
}
int main()
{
srand((unsigned)time(NULL));//生成随机数
//初始化
int result = 0;
int answer = 0;
int input = 0;
float correct = 0;
int i = 0;
int j = 0;
do
{
menu();
scanf("%d", &input);//输入选择
switch (input)
{
case 1://加法
for (i = 0; i < 5; i++)
{
int operand1 = rand() % 100 + 1;
int operand2 = rand() % 100 + 1;
printf("%d+%d=\n", operand1, operand2);
result = operand1 + operand2;
scanf("%d", &answer);//输入答案
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("错了,还有两次机会:\n");
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("错了,最后一次\n");
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("wrong\n");
}
}
}
}
printf("你的正确率是:%.2f%%\n", correct / 5 * 100);//输出正确率
correct = 0;
break;
case 2://减法
for (i = 0; i < 5; i++)
{
int operand1 = rand() % 100 + 1;
int operand2 = rand() % 100 + 1;
printf("%d-%d=\n", operand1, operand2);
result = operand1 - operand2;
scanf("%d", &answer);//输入答案
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("错了,还有两次机会:\n");
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("错了,最后一次\n");
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("wrong\n");
}
}
}
}
printf("你的正确率是:%.2f%%\n", correct / 5 * 100);//输出正确率
correct = 0;
break;
case 3://乘法
for (i = 0; i < 5; i++)
{
int operand1 = rand() % 100 + 1;
int operand2 = rand() % 100 + 1;
printf("%d*%d=\n", operand1, operand2);
result = operand1 * operand2;
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("错了,还有两次机会:\n");
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("错了,最后一次\n");
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("wrong\n");
}
}
}
}
printf("你的正确率是:%.2f%%\n", correct / 5 * 100);//输出正确率
correct = 0;
break;
case 4://除法
for (i = 0; i < 5; i++)
{
int operand1 = rand() % 100 + 1;
int operand2 = rand() % 100 + 1;
//除数不能为0
if (operand2 == 0)
{
operand2 = rand() % 100 + 1;
}
printf("%d/%d=\n", operand1, operand2);
result = operand1 / operand2;
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("错了,还有两次机会:\n");
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("错了,最后一次\n");
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("wrong\n");
}
}
}
}
printf("你的正确率是:%.2f%%\n", correct / 5 * 100);//输出正确率
correct = 0;
break;
case 5:
for (i = 0; i < 5; i++)
{
int operand1 = rand() % 100 + 1;
int operand2 = rand() % 100 + 1;
j = rand() % 4 + 1;//生成随机数,随机计算的方式
if (j == 1)
{
printf("%d+%d=\n", operand1, operand2);
result = operand1 + operand2;
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("错了,还有两次机会:\n");
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("错了,最后一次\n");
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("wrong\n");
}
}
}
}
else if (j == 2)
{
printf("%d-%d=\n", operand1, operand2);
result = operand1 - operand2;
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("错了,还有两次机会:\n");
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("错了,最后一次\n");
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("wrong\n");
}
}
}
}
if (j == 3)
{
printf("%d*%d=\n", operand1, operand2);
result = operand1 * operand2;
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("错了,还有两次机会:\n");
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("错了,最后一次\n");
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("wrong\n");
}
}
}
}
if (j == 4)
{
if (operand2 == 0)
{
operand2 = rand() % 100 + 1;
}
printf("%d/%d=\n", operand1, operand2);
result = operand1 / operand2;
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("错了,还有两次机会:\n");
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("错了,最后一次\n");
scanf("%d", &answer);
if (result == answer)
{
printf("very good\n");
correct += 1;
}
else
{
printf("wrong\n");
}
}
}
}
}
printf("你的正确率是:%.2f%%\n", correct / 5 * 100);
correct = 0;
break;
case 0:
break;
}
}
while (input!=0);//返回菜单
return 0;
}