You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
2.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<stdio.h>
#include<conio.h> //包含_getch()函数,可以自动从缓冲区读取一个字符(不需要按回车)
#include<stdlib.h> //包含随机数函数
#include<time.h> //包含时间函数
int main()
{
while (true)
{
srand(time(NULL)); //time(NULL)读取得到从标准计时点一般是1970年1月1日午夜到当前时间的秒数。
//srand()是随机数发生器的初始化函数
int x, y, score = 0, count = 0, correctAnswer, currentAnswer, flag;
//x:前一个数y:后一个数,score:最终分数,count:答题次数,correntAnswer:正确答案,currentAnswer:当前答案 ,flag:临时量
char choice,op;
//choice:存储选择,op运算符
system("cls");
printf("欢迎进入测验电脑将随机出10道题每道题有三次作答机会但每答错一次对应分数就会减少\n");
printf("按Enter键进入考试其他键退出");
choice = _getch();
if (choice == 13) //Enter键的ascii码
{
for (int i = 0; i < 10; i++)
{
system("cls"); //清屏
printf("现在是第%d道题\n",i + 1);
x = rand() % 51; //产生0~50的随机数
flag = rand() % 2; //flag为0或1
if (flag)
{
op = '+';
}
else
{
op = '-';
}
if (op == '+') //当运算符为加时
{
y = rand() % (50 - x + 1); //第二个数的范围满足两数之和小于等于50
correctAnswer = x + y; //算出正确答案
printf("%d %c %d = \n", x, op, y);
}
else
{
y = rand() % (x + 1); //应为第一个数范围为0~50,所以第二个数比第一小就行了
correctAnswer = x - y; //算出正确答案
printf("%d %c %d = \n", x, op, y);
}
for (int j = 0; j < 3; j++)
{
printf("第%d次作答\n", j + 1);
count++;
printf("请输入您的答案:");
scanf("%d", &currentAnswer);
if (currentAnswer == correctAnswer)
{
printf("恭喜你回答正确!\n");
system("pause"); //暂停
if (count == 1)
{
score = score + 10;
count = 0;
break;
//初始化count并退出循环
}
else if (count == 2)
{
score = score + 7;
count = 0;
break;
//初始化count并退出循环
}
else if (count == 3)
{
score = score + 5;
count = 0;
break;
//初始化count并退出循环
}
count = 0; //初始化
}
else
{
printf("答案错误!\n");
if (count == 3)
{
printf("第%d题的答题次数已用完即将进入下一题",i + 1);
count = 0;
system("pause");
}
}
}
}
system("cls");
printf("答题结束!\n");
printf("您的分数为%d\n",score);
printf("按Enter继续测试其他键退出测试.");
choice = _getch();
if (choice != 13)
return 0;
}
else
{
return 0;
}
}
}