利用指针实现小学生数学四则运算小软件 2018年12月20日

利用指针实现小学生数学四则运算小软件 2018年12月20日
以下内容仅供娱乐,欢迎随时探讨,请多指教
利用函数指针、指针函数、数组指针以及指针数组完成小学生数学四则运算小软件,具体要求如下:
1)要具有自动批改功能。
2)要具有统计已完成题目数量和正确率功能。
3)要具有错题记录和回放功能。
4)参与运算的数据范围可以由使用者设定
5)参与运算的数据在使用者设定范围后随机生成。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//一道题的结构体
struct problem {
	int value1;
	char fuhao;
	int value2;
	double answer;
	double result;
};
//一张试卷的结构体
struct testpaper {
	int txuhao;
	int num;
	int dataMin;//范围
	int dataMax;
	struct problem** next;
	/*
	<param name="ap">
	题目集合
	一级指针指向aTopic结构体,二级指针指向一级指针
	申请空间时,需要多少个题,就申请多少个aTopic结构体的一级指针
	当然直接申请aTopic结构体*N的空间大小给aTopic结构体的一级指针也可以
	*/
};
double keep2(double a);//求百分比约小数点后两位小数的函数
struct testpaper* makeTestPaper();//出试卷函数
void doExam(struct testpaper* tp);//做题函数
struct testpaper* correct(struct testpaper* tp);//批改试卷函数
void showTestPaper(struct testpaper* tp,int answer);//展示结果函数
//主函数
int main()
{
	int doAgain=0;
	struct testpaper* tp;
	struct testpaper* worngtp;
	struct testpaper* (*poniter)();//函数指针使用 
	poniter=makeTestPaper;
beforel:
	tp=poniter();//出题
	srand(time(NULL));
	system("cls");
	printf("开始答题:\n"
	       "(除法最多保留两位小数,需要四舍五入)\n"
	       "____________分割线_______________\n");
	doExam(tp);
	worngtp=correct(tp);
	while(worngtp->num != 0) {
		printf("其中做错的题:\n");
		showTestPaper(worngtp,0);
	before:
		printf("\n你是否重做做错的题目?\n1.重做\n2.不做了\n"
		       "3.查看正确答案\n4.再来一张试卷:\n");
		scanf("%d",&doAgain);
		if(doAgain==1) {
			system("cls");
			//把屏幕清空,防止看到前面的答案
			printf("\n重做已经答错的题:\n"
			       "____________分割线_______________\n");
			doExam(worngtp);
			worngtp=correct(worngtp);
		} else if(doAgain==3) {
			printf("错题的正确答案:\n");
			showTestPaper(worngtp,1);
			goto before;
		} else if(doAgain==4) {
			printf("再来一张试卷:\n");
			goto beforel;
		} else {
			printf("____________分割线_______________\n"
			       "程序结束\n"
			       "____________分割线_______________\n");
			break;
		}
	}
	system("pause");
	return 0;
}
//求百分比约小数点后两位小数的函数
double keep2(double a)
{
	double b;
	b=(int)(a*100+0.5)/100.00;
	return b;
}
//出试卷函数
struct testpaper* makeTestPaper()
{
	int tmnum;
	struct testpaper* tp=(struct testpaper*)malloc(sizeof(struct testpaper));
	printf("你想做多少道题目:");
	scanf("%d",&(tp->num));
	tp->next=(struct problem**)malloc(sizeof(struct problem*) * (tp->num));
	printf("你希望题目数据最小为:");
	scanf("%d",&(tp->dataMin));
	do {
		printf("你希望题目数据最大为:");
		scanf("%d",&(tp->dataMax));
	} while(tp->dataMax < tp->dataMin?printf("最大数据小于最小数据,请重新输入\n"):0);
	for(tmnum=0; tmnum < tp->num; tmnum++) {
		struct problem* next=(struct problem*)malloc(sizeof(struct problem));
		next->value1=(rand()%((tp->dataMax - tp->dataMin)==0?1:(tp->dataMax - tp->dataMin))) + tp->dataMin;
		next->value2=(rand()%((tp->dataMax - tp->dataMin)==0?1:(tp->dataMax - tp->dataMin))) + tp->dataMin;
	back:
		switch(rand()%4) {
			case 0:
				next->fuhao='+';
				next->result=(double)(next->value1 + next->value2);
				break;
			case 1:
				next->fuhao='-';
				next->result=(double)(next->value1 - next->value2);
				break;
			case 2:
				next->fuhao='x';
				next->result=(double)(next->value1 * next->value2);
				break;
			case 3:
				if(next->value2==0) goto back;
				next->fuhao='/';
				next->result=keep2(((double)next->value1 / (double)next->value2));
				break;
		}
		tp->next[tmnum]=next;
	}
	tp->txuhao++;
	return tp;
}
//做题函数
void doExam(struct testpaper* tp)
{
	int tmnum;
	for(tmnum=0; tmnum<tp->num; tmnum++) {
		printf("%d %c %d = ",tp->next[tmnum]->value1,tp->next[tmnum]->fuhao,tp->next[tmnum]->value2);
		scanf("%lf",&(tp->next[tmnum]->answer));
	}
}
//批改试卷函数
struct testpaper* correct(struct testpaper* tp)
{
	int tmnum;
	struct testpaper* worngtp=(struct testpaper*)malloc(sizeof(struct testpaper));
	//创建一张用于存放错题的空试卷,当遇到错题时只需要将空试卷的题目指针指向那道题记录
	worngtp->num=0;
	worngtp->next=(struct problem**)malloc(sizeof(struct problem*)*worngtp->num);
	for(tmnum=0; tmnum<tp->num; tmnum++) {
		if(tp->next[tmnum]->answer!=tp->next[tmnum]->result) {
			worngtp->num++;
			worngtp->next=(struct problem**)realloc(worngtp->next,sizeof(struct problem*)*(worngtp->num));
			worngtp->next[worngtp->num-1]=tp->next[tmnum];
		}
	}
	printf("你做了%d道题目\n做对%d道题目\n做错%d道题目\n正确率%.2f%%\n"
	       ,tp->num,tp->num-worngtp->num,worngtp->num,
	       (1-((double)worngtp->num/(double)tp->num))*100);
	if((1-((double)worngtp->num/(double)tp->num))*100 < 60)
		printf("小伙子,雄壮起来!!!\n"
		       "你要加油啊!\n");
	return worngtp;
}
//展示结果函数
void showTestPaper(struct testpaper* tp,int answer)
{
	int tmnum;
	for(tmnum=0; tmnum < tp->num; tmnum++) {
		if(answer)
			printf("%d%c%d=%.2f\n",tp->next[tmnum]->value1,tp->next[tmnum]->fuhao,
			       tp->next[tmnum]->value2,tp->next[tmnum]->result);
		else
			printf("%d%c%d=%.2f\n",tp->next[tmnum]->value1,tp->next[tmnum]->fuhao,
			       tp->next[tmnum]->value2,tp->next[tmnum]->answer);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43310774/article/details/85128054