需求分析
经过对程序设计题目的分析可知,整个程序的设计实现大致分为三个模块,其中每一个模块对应一个函数,他们的功能分别是:自学函数(study),测试函数(test),随机出题函数(suiji),其中自学函数和测试函数函数严格按照题目的要求,随机出题函数则可被自学函数和测试函数所调用。
- 自学函数 主要实现程序运行时学生可以无限制的作题,每做完一题时系统提示是否继续, 学生可自己选择。
- 测试函数 主要实现程序运行时学生可以自己设定想要作题的个数 ,程序并进行自动打分。
- 随机出题函数 运行该函数时由电脑随机出题, 可被自学函数和测试函数所调用。
概要设计
1.程序整体结构图
随机出题函数
自学函数
测试函数
详细设计
本程序有三个自定义函数和一个主函数组成,其中主函数以菜单的形式调用其他的函数来实现要求的所有功能,三个函数都是较为核心的部分,下面分别进行说明。
1.随机抽题函数
由电脑随机产生两个数,并随机产生一个运算符号,组成一个运算,由程序计算出真实的答案,放在变量T中,供自学函数和测试函数调用,具体程序段如下:
void suiji()
{ int c; int a; int b;
fuhao=random(4)+1; /*在1到4之间产生一个随机数,用来表示+,—,*,/ 等运算符号,放在fuhao这个变量当中*/
switch(fuhao) /*由fuhao判断运算是什么*/
{ case 1:
{ a=random(10);
b=random(10);
T=a+b;break;
}
case 2:
{ a=random(10);
b=random(10);
if(a<b)
{c=a;a=b;b=c;}
T=a-b;break;
}
case 3:
{ a=random(10);
b=random(10);
T=a*b;break;
}
case 4:
{do
{ do
{a=random(10);
b=random(10);
if(a<b)
{c=a;a=b;b=c;}
}while(b==0) ; /若b等于0/
}
while((a%b)!=0); /*当a能被b整除时*/
T=a/b;
break;
}
}
switch(fuhao) /*由fuhao决定输出的情况*/
{
case 1: {printf("\n%d + %d = ",a,b);break;}
case 2: {printf("\n%d - %d = ",a,b);break;}
case 3: {printf("\n%d X %d = ",a,b);break;}
case 4: {printf("\n%d / %d = ",a,b);}
}
}
2.自学函数
先调用随机函数 ,产生一个运算,用户给出答案,如果答案错误,系统提示:答案错误,请重新输入答案,直到答案正确为止,当答案正确,系统提示是否继续做题,输入(N/Y)确认,如果输入除N,Y以外的其他代码,系统提示:代码错误,请重新输入,其具体程序段如下:
void study()
{ int timu=1;int xuanze;int Userda;
randomize(); /*保证每次运行程序时产生的随机数序列不同*/
clrscr(); clrscr(); /*每次程序运行时清除以前内容的语句*/
printf("\n-------------------------------------");
printf("\n-------------------------------------");
printf("\n study start :");
printf("\n-------------------------------------");
while(1)
{ printf("\n Question: %d",timu); /*输出题号*/
printf("\n-------------------------------------");
suiji(); /*调用随机抽题函数*/
scanf("%d",&Userda); /*用户输入答案*/
while(Userda!=T)
{ printf("\n-------------------------------------");
printf("\n-------------------------------------");
printf("\n you are Wrong! ");
printf("\n-------------------------------------");
printf("\n Input The Answer Again: ");
scanf("%d",&Userda);
}
printf("\n-------------------------------------");
printf("\n-------------------------------------");
printf("\n Right! ");
printf("\n-------------------------------------");
printf("\n-------------------------------------");
timu++;
Loop2:printf("\nDo you want to continue?(Y/N) "); /*构造Loop循环*/
scanf("%s",&xuanze); /*输入要操作的代码*/
if(xuanze=='y' || xuanze=='Y')
continue; /*继续做题*/
else if(xuanze=='n'||xuanze=='N')
main(); /*停止做题,返回主函数*/
else
{printf("\n-------------------------------------");
printf("\nWrong! Please enter the correct code!\n"); goto Loop2;} /*输入代码错误,回到Loop循环*/
}
}
3.测试函数
程序开始后,先由用户输入想要答题的个数,然后调用随机抽题函数,产生一个运算,用户给出答案,如果答案错误,系统提示:答案错误,请重新输入,直到答案正确为止,当得分大于或等于10时,每答对一题时,得分加10分,答错一题时,减10分,当得分小于10分时,不减分,当答题的个数等于设定的个数时,程序运行结束,在用户界面输出:测试结束,并给出最后得分,具体程序段如下:
. void test()
{int timu=1; int Userda;
randomize();/*保证每次运行程序时产生的随机数序列不同*/
clrscr();clrscr(); /*每次程序运行时清除以前内容的语句*/
printf("\n-------------------------------------");
printf("\n-------------------------------------");
printf("\n test start :");
printf("\n-------------------------------------");
printf("\n Please Input Question Count: "); printf("\n-------------------------------------");
scanf("%d",&h); /*输入想要答题的个数*/
printf("\n-------------------------------------");
while(timu<=h) /*当答题个数小于或等于设定的个数时*/
{ printf("\n Question: %d",timu);
printf("\n-------------------------------------");
suiji(); /*调用随机抽题函数*/
scanf("%d",&Userda);
while(Userda!=T) /*当用户答案不正确时*/
{ if(d>=10) /*如果当前得分大于或等于10*/
d=d-10; /*得分减10分*/
printf("\nWrong! Now Your Score: %d",d);
if(d<U)
return; /*停止本次循环*/
printf("\n Input The Answer Again: ");
scanf("%d",&Userda);
}
d=d+10; /*如果答案正确,得分加10分*/
printf("\n-------------------------------------");
printf("\n Right! Now Your Score: %d",d);
printf("\n-------------------------------------");
timu++; }
printf("\n--------------------------------------------------------");
printf("\n--------------------------------------------------------\n");
printf("\n Test Over!!!\n");
printf("\n--------------------------------------------------------");
printf("\n Your Final Score: %d",d);/*输出最后得分*/
printf("\n-----------------------------------------------------------------------");
printf("\n-----------------------------------------------------------------------");
main(); /*返回住函数*/
}
程序完整代码
#include<stdio.h>
#include <stdlib.h>
int h; int T; int d; int U;
char fuhao;
void menu();void study();void test();void suiji();
main()
{int n;
menu();
printf("\n------------------------------------------------------------------");
printf("\n Please enter the number you want to ride : ");
while(1)
{scanf("%d",&n);
switch(n)
{case 1: study();break;
case 2: test();break;
case 3: exit(0);
default: printf("\n------------------------------------------------------------------");
printf("\nWrong ! Please enter the existent number : ");
}
}
}
void menu()
{ printf("\n ");
printf("\n ");
printf(" -------------- xiao xue sheng CAI jiao xue ruan jian--------------- ");
printf("\n 1 study ");
printf("\n 2 test ");
printf("\n 3 exit ");
}
void suiji()
{ int c; int a; int b;
fuhao=rand(4)+1;
switch(fuhao)
{ case 1:
{ a=rand(10);
b= rand(10);
T=a+b;break;
}
case 2:
{ a=rand(10);
b=rand(10);
if(a<b)
{c=a;a=b;b=c;}
T=a-b;break;
}
case 3:
{ a=rand(10);
b=rand(10);
T=a*b;break;
}
case 4:
{do
{ do
{a=rand(10);
b=rand(10);
if(a<b)
{c=a;a=b;b=c;}
}while(b==0) ;
}
while((a%b)!=0);
T=a/b;
break;
}
}
switch(fuhao)
{
case 1: {printf("\n%d + %d = ",a,b);break;}
case 2: {printf("\n%d - %d = ",a,b);break;}
case 3: {printf("\n%d X %d = ",a,b);break;}
case 4: {printf("\n%d / %d = ",a,b);}
}
}
void study()
{ int timu=1;int xuanze;int Userda;
printf("\n-------------------------------------");
printf("\n-------------------------------------");
printf("\n study start :");
printf("\n-------------------------------------");
while(1)
{ printf("\n Question: %d",timu);
printf("\n-------------------------------------");
suiji();
scanf("%d",&Userda);
while(Userda!=T)
{ printf("\n-------------------------------------");
printf("\n-------------------------------------");
printf("\n you are Wrong! ");
printf("\n-------------------------------------");
printf("\n Input The Answer Again: ");
scanf("%d",&Userda);
}
printf("\n-------------------------------------");
printf("\n-------------------------------------");
printf("\n Right! ");
printf("\n-------------------------------------");
printf("\n-------------------------------------");
timu++;
Loop2:printf("\nDo you want to continue?(Y/N) ");
scanf("%s",&xuanze);
if(xuanze=='y' || xuanze=='Y')
continue;
else if(xuanze=='n'||xuanze=='N')
main();
else
{printf("\n-------------------------------------");
printf("\nWrong! Please enter the correct code!\n"); goto Loop2;}
}
}
void test()
{int timu=1; int Userda;
printf("\n-------------------------------------");
printf("\n-------------------------------------");
printf("\n test start :");
printf("\n-------------------------------------");
printf("\n Please Input Question Count: ");
printf("\n-------------------------------------");
scanf("%d",&h);
printf("\n-------------------------------------");
while(timu<=h)
{ printf("\n Question: %d",timu);
printf("\n-------------------------------------");
suiji();
scanf("%d",&Userda);
while(Userda!=T)
{ if(d>=10)
d=d-10;
printf("\nWrong! Now Your Score: %d",d);
if(d<U)
return;
printf("\n Input The Answer Again: ");
scanf("%d",&Userda);
}
d=d+10;
printf("\n-------------------------------------");
printf("\n Right! Now Your Score: %d",d);
printf("\n-------------------------------------");
timu++; }
printf("\n--------------------------------------------------------");
printf("\n--------------------------------------------------------\n");
printf("\n Test Over!!!\n");
printf("\n--------------------------------------------------------");
printf("\n Your Final Score: %d",d);
printf("\n-----------------------------------------------------------------------");
printf("\n-----------------------------------------------------------------------");
main();
}