解答私信@m0_51085291 //2020-12-21

为君一笑 提交于 2020-12-22 07:39:53

编写一个帮助小学生练习数学的程序,帮助小学生练习100以内整数的四则运算:加、减、乘、除。

【基本要求】
(1)电脑随机出 10 道题,每题 10 分,程序结束时显示学生得分;
(2)试题应包含四种数学运算:加、减、乘、除,它们是随机产生的。为每道题随机生成数字,但必须确保参与运算的数字和结果都是小于 100 且大于零的整数。
(3)每道题学生有三次机会输入答案,当学生输入错误答案时,提醒学生重新输入,如果三次机会结束则输出正确答案;
(4)对于每道题,学生第一次输入正确答案得 10 分,第二次输入正确答案得 7 分,第三次输入正确答案得 5 分,否则不得分;
(5)总成绩 90 以上显示“ SMART”,80-90 显示“ GOOD”,70-80 显示“ OK”,60-70 显示“ PASS”,60 以下“ TRY AGAIN ”
(6)十道题做完后,显示学生完成这十道题所用的时间

代码块:

/*说明:如果程序在显示第X题后,光标一直跳动不出题,请耐心等待,
这是由于为了让除法题目能够整除,随机函数在不断调整数字造成的*/

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

char randOperator();
void randExpression(int *num1, int *num2, char ro);
void input(int *res);
int judgeResult(int *num1, int *num2, char ro, int *res);
void outputScore(int *fun());

char randOperator()
{
   
   
	int judgeNum;
	char oper;
	srand((unsigned)time(NULL));
	judgeNum=rand()%4;
	switch(judgeNum)
	{
   
   
	case 0: oper='+'; break;
	case 1: oper='-'; break;
	case 2: oper='*'; break;
	case 3: oper='/'; break;
	default: break;
	}
	return oper;
}//randOperator
void randExpression(int *num1, int *num2, char ro)
{
   
   
	unsigned int seed;
	seed=(unsigned)time(0);
	srand(seed);
	*num1=rand()%100+1;
	*num2=rand()%100+1;
	/*以下代码为了保证除法能够整除*/
	if(ro=='/')
	{
   
   
		while(*num1%*num2!=0)
		{
   
   
			seed=(unsigned)time(0);
			srand(seed);
			*num1=rand()%100+1;
			*num2=rand()%100+1;
		}
	}
	cout<<*num1<<ro<<*num2<<"=?";
}//randExpression
void input(int *res)
{
   
   
	cin>>*res;
}//input
int judgeResult(int *num1, int *num2, char ro, int *res)
{
   
   
	int temp, correctCount, totalScore;
	switch(ro)
	{
   
   
	case '+': temp=*num1+*num2; break;
	case '-': temp=*num1-*num2; break;
	case '*': temp=*num1*(*num2); break;
	case '/': temp=*num1/(*num2); break;
	default: break;
	}
	for(correctCount=0, totalScore=10; correctCount<3; correctCount++)
	{
   
   
		if(temp!=*res)
		{
   
   
			switch(correctCount)
			{
   
   
			case 0: cout<<"     第1次错误,重新输入!"; input(res); break;
			case 1: cout<<"     第2次错误,重新输入!"; input(res); break;
			case 2: cout<<"     第3次错误!"<<endl; totalScore=0; break;
			default: break;
			}
		}
		else
		{
   
   
			switch(correctCount)
			{
   
   
			case 0: totalScore=10; break;
			case 1: totalScore=7; break;
			case 2: totalScore=5; break;
			default: totalScore=0; break;
			}
			break;
		}
	}
	return totalScore;
}//judgeResult
void outputScore(int sco)
{
   
   
	int j=sco/10;
	switch(j)
	{
   
   
	case 0:
	case 1:
	case 2:
	case 3:
	case 4:
	case 5: cout<<"TRY AGAIN"<<endl; break;
	case 6: cout<<"PASS"<<endl; break;
	case 7: cout<<"OK"<<endl; break;
	case 8: cout<<"GOOD"<<endl; break;
	case 9:
	case 10: cout<<"SMART"<<endl; break;
	default: break;
	}
}//outputScore

int main()
{
   
   
	int *number1=new int;
	int *number2=new int;
	int *result=new int;
	int score, i;
	clock_t startTime, endTime;
	startTime=clock();
	for(i=0, score=0; i<10; i++)
	{
   
   
		char oper;
		cout<<"第"<<i+1<<"题:";
		oper=randOperator();
		randExpression(number1, number2, oper);
		input(result);
		score+=judgeResult(number1, number2, oper, result);
		cout<<"Score="<<score<<endl<<endl;
	}
	outputScore(score);
	endTime=clock();
	cout<<"The run time is: "<<(double)(endTime-startTime)/CLOCKS_PER_SEC<<"s"<<endl;
	free(number1);
	free(number2);
	free(result);

	system("pause");
	return 0;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!