构建之法--软件工程学习随笔之二
大三下学期第二周,有时候不得不感叹,在匆忙中时间真的过得好快,一转眼就过去了一周。软件工程的个人项目作业又来了一个新的任务,在上周自动生成30道小学数学加减法题、并能够判断对错的基础上,根据自己情况选择扩展如下的功能:
- 题目避免重复,可制定打印数量和打印方式。
- 是否有乘除法。
- 是否有括号。
- 数值范围。
- 加减有无负数。
- 除法有无余数。
- 是否支持分数(真分数、假分数...)。
- 是否支持小树(精确到多少位?)。
- 打印中每行的间隔。
在上周的作业中,确实小看了这道题目。上周的作业中,就用了随机数发生器的初始化函数srand和随机函数rand,并且完全没有模块化。显然,在这周的作业中是行不通的。
因此,这周不得不采取模块化的方式:
所用函数览表:
void menu (); //目录 void jiben (int m,int n); //基本算式 void fenshu (int m,int n); //分数算式 void zonghe (int m,int n); //综合算式 int gongyue (int m,int n); //分数求最大公约数 void huajian(int m,int n); //分数化简
目录函数:
void menu () //目录 { int a,m = 0,n = 0; printf ("\t请选择题目难度:\n\n"); printf (" 1.基本运算 2.分数运算\n 3.综合运算 4.退出程序\n"); scanf ("%d",&a); switch (a) { case 1:jiben (m,n);break; case 2:fenshu (m,n);break; case 3:zonghe (m,n);break; default :break; } }
采用了switch-case语句。选择所要的操作。
基本算式函数:
void jiben (int m,int n) { int i,x,s,y; srand (time (NULL)); for (i = 0;i < 30;i ++) { x = rand ()%4; m = rand ()%100; n = rand ()%100; if (x == 0) { printf ("%d + %d = ",m,n); scanf ("%d",&s); if (m + n == s) printf ("回答正确。"); else printf ("回答错误,正确答案是:%d。",m + n); } else if (x == 1) { printf ("%d - %d = ",m,n); scanf ("%d",&s); if (m - n == s) printf ("回答正确。"); else printf ("回答错误,正确答案是:%d。",m - n); } else if (x == 2) { printf ("%d * %d = ",m,n); scanf ("%d",&s); if (m * n == s) printf ("回答正确。"); else printf ("回答错误,正确答案是:%d。",m * n); } else { if (n != 0) { printf ("%d / %d = ",m,n); scanf ("%d",&s); scanf("%d",&y); if ((m / n == s)&&(m % n == y)) printf ("回答正确。"); else printf ("回答错误,正确答案是:%d %d。",m / n,m % n); } else break; } printf ("请继续做题...\n"); } }
基本算式函数可以实现两位数的加、减、乘、除算法,同时,除法可以实现商和余数的结果判断。只有在商和余数都回答正确时才提示答案正确,否则答案错误。在所有的基本算式题目中,如果做出回答,会立即判断正误。如果回答错误,会立即给出正确答案,以供重算、反省。
分数算式函数:
void fenshu (int m,int n) //分数算式函数 { int i,a,s,x,y; srand (time (NULL)); for (i = 0;i < 30;i ++) { a = rand ()%4; m = rand ()%10 + 1; n = rand ()%10 + 1; x = rand ()%10 + 1; y = rand ()%10 + 1; if (a == 0) { printf ("%d/%d + %d/%d = ",m,n,x,y); s = gongyue (n,y); m = (s / n) * m + (s / y)*x; n = s; huajian (m,n); } else if (a == 1) { printf ("%d/%d - %d/%d = ",m,n,x,y); s = gongyue (n,y); m = (s / n) * m - (s / y)*x; n = s; huajian (m,n); } else if (a == 3) { printf ("%d/%d * %d/%d = ",m,n,x,y); m = m * x; n = n * y; huajian (m,n); } else { printf ("%d/%d / %d/%d = ",m,n,x,y); m = m * y; n = n * x; huajian (m,n); } printf ("请继续做题...\n"); } }
在分数算式函数中,调用了求最大公约数函数和化简函数:
int gongyue (int m,int n) { int x = m <n ? m :n; while ((x % m) != 0||(x % n) != 0 ) x ++; return x; }
求最大公约数函数中,首先申请一个变量,把所要求公约数的两个分数的分母中较小的一个数放在变量中。做while循环,变量内容逐渐加1,直到该数同时满足整除两个分数的分母时,该变量内容即是最大公约数。并返回最大公约数。
分数化简函数:
void huajian (int m,int n) //分数化简函数 { int a,b,x,y; a = abs(m); b = abs(n); int i = a < b?a : b; if (i > 1) { while (i != 1) { if ((a%i ==0)&&(b%i == 0)) { a = a/i; b = b/i; } i --; } } scanf ("%d%d",&x,&y); if (a == x&&b == y) printf ("回答正确。"); else { if (b == 1) { printf ("回答错误。正确答案是:"); if ((m > 0 &&n < 0)||(m < 0 ||n > 0)) printf ("-"); printf ("%d。",a); } else { printf ("回答错误。正确答案是:"); if ((m > 0 &&n < 0)||(m < 0&&n > 0)) printf ("-"); printf ("%d/%d。",a,b); } } }
在化简函数中,首先申请四个变量,变量a,b用于保存所要化简分数的分子和分母的绝对值,以便后面判断该分数的符号。变量x,y用于输入数据,以便判断结果的正确性。化简过程是找到需要化简得两数的绝对值较小的数,从该数开始到1过程中,发现某数可以被分子和分母的绝对值整除,则分子和分母同时除该数。直到较小数为一为止。最后,输入结果,判断正误。若错误,给出正确结果。
综合算式函数:
void zonghe (int m,int n) { int i,a,s; srand (time (NULL)); for (i = 0;i <30;i ++) { a = rand ()%4; m = rand ()%20; n = rand ()%20; if (a == 0) { printf ("%d + %d ",m,n); a = rand ()%4; n = rand ()%20; if (a == 0) printf ("+ %d = ",n); else if (a == 1) printf ("- %d = ",n); else if (a == 2) printf ("* %d = ",n); else { while (n == 0) n = rand ()%20; printf ("/ %d = ",n); } scanf ("%d",&s); printf ("请继续做题...\n"); } else if (a == 1) { printf ("%d - %d ",m,n); a = rand ()%4; n = rand ()%20; if (a == 0) printf ("+ %d = ",n); else if (a == 1) printf ("- %d = ",n); else if (a == 2) printf ("* %d = ",n); else { while (n == 0) n = rand ()%20; printf ("/ %d = ",n); } scanf ("%d",&s); printf ("请继续做题...\n"); } else if (a == 3) { printf ("%d * %d ",m,n); a = rand ()%4; n = rand ()%20; if (a == 0) printf ("+ %d = ",n); else if (a == 1) printf ("- %d = ",n); else if (a == 2) printf ("* %d = ",n); else { while (n == 0) n = rand ()%20; printf ("/ %d = ",n); } scanf ("%d",&s); printf ("请继续做题...\n"); } else { while (n == 0) n = rand ()%20; printf ("%d / %d ",m,n); a = rand ()%4; n = rand ()%20; if (a == 0) printf ("+ %d = ",n); else if (a == 1) printf ("- %d = ",n); else if (a == 2) printf ("* %d = ",n); else { while (n == 0) n = rand ()%20; printf ("/ %d = ",n); } scanf ("%d",&s); printf ("请继续做题...\n"); } } }
在该函数中,实现了三个数的综合加、减、乘、除运算。由于时间关系,判断正确过程还没写出,等待下周作业中升级。
菜单选择界面:
基本算式界面:
分数算式界面:
综合算式界面:
在这次作业中,实现正负数整数的加、减、乘、除法运算,并判断结果正误。实现了约束数值的取值范围。实现除法的商和余数的计算和判断。实现了分数的加、减、乘、除运算,并判断正误。当然,可能还有一些细节不够完善,还有很多代码非常臃肿,完全可以再细分为几个模块完成,不够简洁。也有一些功能还没有完善,如多项式的结果判断等。期待在下次的作业中做得完美。
附上完整的代码:
// yunsuan.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "stdio.h" #include "stdlib.h" #include "time.h" void menu (); //目录 void jiben (int m,int n); //基本算式 void fenshu (int m,int n); //分数算式 void zonghe (int m,int n); //综合算式 int gongyue (int m,int n); //分数求最大公约数 void huajian(int m,int n); //分数化简 int main(int argc, char* argv[]) { menu (); return 0; } void menu () //目录 { int a,m = 0,n = 0; printf ("\t请选择题目难度:\n\n"); printf (" 1.基本运算 2.分数运算\n 3.综合运算 4.退出程序\n"); scanf ("%d",&a); switch (a) { case 1:jiben (m,n);break; case 2:fenshu (m,n);break; case 3:zonghe (m,n);break; default :break; } } void jiben (int m,int n) { int i,x,s,y; srand (time (NULL)); for (i = 0;i < 30;i ++) { x = rand ()%4; m = rand ()%100; n = rand ()%100; if (x == 0) { printf ("%d + %d = ",m,n); scanf ("%d",&s); if (m + n == s) printf ("回答正确。"); else printf ("回答错误,正确答案是:%d。",m + n); } else if (x == 1) { printf ("%d - %d = ",m,n); scanf ("%d",&s); if (m - n == s) printf ("回答正确。"); else printf ("回答错误,正确答案是:%d。",m - n); } else if (x == 2) { printf ("%d * %d = ",m,n); scanf ("%d",&s); if (m * n == s) printf ("回答正确。"); else printf ("回答错误,正确答案是:%d。",m * n); } else { if (n != 0) { printf ("%d / %d = ",m,n); scanf ("%d",&s); scanf("%d",&y); if ((m / n == s)&&(m % n == y)) printf ("回答正确。"); else printf ("回答错误,正确答案是:%d %d。",m / n,m % n); } else break; } printf ("请继续做题...\n"); } } int gongyue (int m,int n) { int x = m <n ? m :n; while ((x % m) != 0||(x % n) != 0 ) x ++; return x; } void huajian (int m,int n) //分数化简函数 { int a,b,x,y; a = abs(m); b = abs(n); int i = a < b?a : b; if (i > 1) { while (i != 1) { if ((a%i ==0)&&(b%i == 0)) { a = a/i; b = b/i; } i --; } } scanf ("%d%d",&x,&y); if (a == x&&b == y) printf ("回答正确。"); else { if (b == 1) { printf ("回答错误。正确答案是:"); if ((m > 0 &&n < 0)||(m < 0 ||n > 0)) printf ("-"); printf ("%d。",a); } else { printf ("回答错误。正确答案是:"); if ((m > 0 &&n < 0)||(m < 0&&n > 0)) printf ("-"); printf ("%d/%d。",a,b); } } } void fenshu (int m,int n) //分数算式函数 { int i,a,s,x,y; srand (time (NULL)); for (i = 0;i < 30;i ++) { a = rand ()%4; m = rand ()%10 + 1; n = rand ()%10 + 1; x = rand ()%10 + 1; y = rand ()%10 + 1; if (a == 0) { printf ("%d/%d + %d/%d = ",m,n,x,y); s = gongyue (n,y); m = (s / n) * m + (s / y)*x; n = s; huajian (m,n); } else if (a == 1) { printf ("%d/%d - %d/%d = ",m,n,x,y); s = gongyue (n,y); m = (s / n) * m - (s / y)*x; n = s; huajian (m,n); } else if (a == 3) { printf ("%d/%d * %d/%d = ",m,n,x,y); m = m * x; n = n * y; huajian (m,n); } else { printf ("%d/%d / %d/%d = ",m,n,x,y); m = m * y; n = n * x; huajian (m,n); } printf ("请继续做题...\n"); } } void zonghe (int m,int n) { int i,a,s; srand (time (NULL)); for (i = 0;i <30;i ++) { a = rand ()%4; m = rand ()%20; n = rand ()%20; if (a == 0) { printf ("%d + %d ",m,n); a = rand ()%4; n = rand ()%20; if (a == 0) printf ("+ %d = ",n); else if (a == 1) printf ("- %d = ",n); else if (a == 2) printf ("* %d = ",n); else { while (n == 0) n = rand ()%20; printf ("/ %d = ",n); } scanf ("%d",&s); printf ("请继续做题...\n"); } else if (a == 1) { printf ("%d - %d ",m,n); a = rand ()%4; n = rand ()%20; if (a == 0) printf ("+ %d = ",n); else if (a == 1) printf ("- %d = ",n); else if (a == 2) printf ("* %d = ",n); else { while (n == 0) n = rand ()%20; printf ("/ %d = ",n); } scanf ("%d",&s); printf ("请继续做题...\n"); } else if (a == 3) { printf ("%d * %d ",m,n); a = rand ()%4; n = rand ()%20; if (a == 0) printf ("+ %d = ",n); else if (a == 1) printf ("- %d = ",n); else if (a == 2) printf ("* %d = ",n); else { while (n == 0) n = rand ()%20; printf ("/ %d = ",n); } scanf ("%d",&s); printf ("请继续做题...\n"); } else { while (n == 0) n = rand ()%20; printf ("%d / %d ",m,n); a = rand ()%4; n = rand ()%20; if (a == 0) printf ("+ %d = ",n); else if (a == 1) printf ("- %d = ",n); else if (a == 2) printf ("* %d = ",n); else { while (n == 0) n = rand ()%20; printf ("/ %d = ",n); } scanf ("%d",&s); printf ("请继续做题...\n"); } } }