利用指標實現小學生數學四則運算小軟體 2018年12月20日
阿新 • • 發佈:2019-01-01
利用指標實現小學生數學四則運算小軟體 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);
}
}