一 个人相关信息

| 班级 |软件工程
| ---- | ---- | ---- |
| 作业要求 |个人作业-四则运算题目生成程序
| 作业目标 |用博客发表自己的代码,写一份四则运算代码|
| 学号 |3180701331|

二 题目要求

写一个能自动生成小学四则运算题目的程序,然后在此基础上扩展:

1)除了整数以外,还要支持真分数的四则运算,例如:1/6+1/8=7/24

2)程序要求能处理用户的输入,判断对错,累积分数

3)程序支持可以由用户自行选择加、减、乘、除运算

4)使用-n参数控制生成题目的个数,例如Myapp.exe -n 10,将生成10个题目

三 代码

1.整数加法

int add(int Grade)                       //加法函数
{
	int a, b, c;
	srand((unsigned)time(NULL));
	a = rand() % 100;           //随机生成数字
	b = rand() % 100;
	printf("请回答:\n %d + %d = ", a, b);
	scanf_s("%d", &c);
	if (a + b == c)
	{
		printf("回答正确!\n");
		Grade = 1;
	}
	else
	{
		printf("回答错误!\n正确答案是%d", a + b);
		Grade = 0;
	}
	return Grade;
}

2.整数减法

int sub(int Grade)                             //减法运算
{
	int a, b ,c;
	srand((unsigned)time(NULL));
	a = rand() % 100; //随机生成数字
	b = rand() % 100;
	printf("请回答:\n%d - %d = ", a, b);
	scanf_s("%d", &c);
	if (a - b == c)
	{
		printf("回答正确!\n");
		Grade = 1;
	}
	else
	{
		printf("回答错误!\n正确答案是%d",a-b);
		Grade = 0;
	}
	return Grade;
}

3.整数乘法

int mul(int Grade)
{
	int a, b ,c;
	srand((unsigned)time(NULL));
	a = rand() % 20; //随机生成数字
	b = rand() % 20;
	printf("请回答:\n%d * %d = ", a, b);
	scanf_s("%d", &c);
	if (a * b == c)
	{
		printf("回答正确!\n");
		Grade = 1;
	}
	else
	{
		printf("回答错误!\n正确答案是%d",a*b);
		Grade = 0;
	}
	return Grade;
}

4.整数除法

int div(int Grade)
{
	int a,b,c,m,n,j,k;  //a为被除数,除数为b,c为用户输入商,m为结果商,j为结果余数,k为用户输入余数
	srand((unsigned)time(NULL));
	a = rand() % 20; //随机生成数字
	b = rand() % 20;
	printf("请回答:\n%d / %d 商为=,余数为=", a, b);
	scanf_s("%d,%d", &c,&k);
	m=a/b;
	n = b * m;
	j = a - n;
	if (m == c&&j==k)
	{
		printf("回答正确!\n");
		Grade = 1;
	}
	else
	{
		printf("回答错误!\n正确答案商%d,余数为%d",m,j);
		Grade = 0;
	}
	return Grade;
}

5.求最大公约数

int func(int m, int n) //求最大公约数
{
	int tmp;
	m = abs(m);
	n = abs(n);
	if (m == n)
		return m;
	if (m < n)
	{
		tmp = m;
		m = n;
		n = tmp;
	}
	while (m % n != 0)
	{
		tmp = m;
		m = n;
		n = tmp % n;
	}
	return n;
}

6.分数加法

int Sadd(int Grade = 0)       //分数加法
{
	int flag = 0;
	int x1, x2, y1, y2, Mcon, Mcon1;//x1,x2为分子,y1,y2为生成的分母数字用于加法计算.
	int z1 = 0, z2 = 0, z3 = 0, z4 = 0;//z1是输入的分子,z2是分母   z3是答案的分子,z4是答案的分母。
	srand((unsigned)time(NULL));
	x1 = rand() % 10;
	x2 = rand() % 10;
	y1 = rand() % 10;
	y2 = rand() % 10;
	printf("%d/%d+%d/%d=?", x1, y1, x2, y2);
	printf("请输入答案的分子:");
	scanf_s("%d", &z1);
	printf("请输入答案的分母:");
	scanf_s("%d", &z2);
	Mcon1 = func(y1, y2);
	Mcon = y1 * y2;
	z4 = Mcon;
	z3 = Mcon / y1 * x1 + Mcon / y2 * x2;
	if (z1 == (z3 = z3 / Mcon1) && z2 == (z4 = z4 / Mcon1))
	{
		printf("答案正确。\n");
		Grade=1;
	}
	else
	{
		printf("答案错误,正确答案为%d/%d\n", z1, z4);
		Grade = 0;
	}
	return Grade;
}

7.分数减法

int Ssub(int Grade = 0)
{
	int flag = 0;
	int x1, x2, y1, y2, Mcon, Mcon1;//x1,x2为分子,y1,y2为生成的分母数字用于加法计算.
	int z1 = 0, z2 = 0, z3 = 0, z4 = 0;//z1是输入的分子,z2是分母   z3是答案的分子,z4是答案的分母。
	srand((unsigned)time(NULL));
	x1 = rand() % 10;
	x2 = rand() % 10;
	y1 = rand() % 10;
	y2 = rand() % 10;
	printf("%d/%d-%d/%d=?", x1, x2, y1, y2);
	printf("请输入答案的分子:");
	scanf_s("%d", &z1);
	printf("请输入答案的分母:");
	scanf_s("%d", &z2);
	Mcon1 = func(y1, y2);
	Mcon = y1 * y2;
	z4 = Mcon;
	z3 = Mcon / y1 * x1 - Mcon / y2 * x2;
	if (z1 == (z3 = z3 / Mcon1) && z2 == (z4 = z4 / Mcon1))
	{
		printf("答案正确。\n");
		Grade=1;
		}
	else
	{
		printf("答案错误,正确答案为%d/%d\n", z3, z4);
		Grade = 0;
	}
	return Grade;
}

8.分数乘法

int Smul(int Grade = 0)
{
	int x1, x2, y1, y2, Mcon, Mcon1;//x1,x2为分子,y1,y2为生成的分母数字用于加法计算.
	int z1 = 0, z2 = 0, z3 = 0, z4 = 0;//z1是输入的分子,z2是分母   z3是答案的分子,z4是答案的分母。
	srand((unsigned)time(NULL));
	x1 = rand() % 10;
	x2 = rand() % 10;
	y1 = rand() % 10;
	y2 = rand() % 10;
	printf("%d/%d*%d/%d=?", x1, x2, y1, y2);
	printf("请输入答案的分子:");
	scanf_s("%d", &z1);
	printf("请输入答案的分母:");
	scanf_s("%d", &z2);
	Mcon1 = func(y1, y2);
    Mcon = y1 * y2;
	z4 = Mcon;
	z3 = (Mcon / y1 * x1) * (Mcon / y2 * x2);
	if (z1 == (z3 = z3 / Mcon1) && z2 == (z4 = z4 / Mcon1))
	{
		printf("答案正确。\n");
		Grade=1;
	}
	else
	{
		printf("答案错误,正确答案为%d/%d\n", z3, z4);
		Grade = 0;
	}
	return Grade;
}

9.分数乘法

int Sdiv(int Grade = 0)
{
	int x1, x2, y1, y2, Mcon, Mcon1;//x1,x2为分子,y1,y2为生成的分母数字用于加法计算.
	int z1 = 0, z2 = 0, z3 = 0, z4 = 0;//z1是输入的分子,z2是分母   z3是答案的分子,z4是答案的分母。
	srand((unsigned)time(NULL));
	x1 = rand() % 10;
	x2 = rand() % 10;
	y1 = rand() % 10;
	y2 = rand() % 10;
	printf("%d/%d / %d/%d=?", x1, x2, y1, y2);
	printf("请输入答案的分子:");
	scanf_s("%d", &z1);
	printf("请输入答案的分母:");
	scanf_s("%d", &z2);
	Mcon1 = func(y1, y2);
	Mcon = y1 * y2;
	z4 = Mcon;
	z3 = (Mcon / y1 * x1) / (Mcon / y2 * x2);
	if (z1 == (z3 = z3 / Mcon1) && z2 == (z4 = z4 / Mcon1))
	{
		printf("答案正确。\n");
		Grade=1;
		}
		else
	{
			printf("答案错误,正确答案为%d/%d\n", z3, z4);
			Grade = 0;
	}
	return Grade;
}

10.主函数

int main()
{
	int Num, Score, Choice, Grade;                         //题目数量、得分、题目选择、每题得分
	printf("欢迎进入小学四则运算。\n");
	printf("请输入题目数量\n");
	scanf_s("%d", &Num);
	Score = 0;
	for (Num; Num >= 1; Num--)
	{
		printf("\n请选择四则运算的种类,输入1-9\n");
		printf("1.加法运算\n");
		printf("2.减法运算\n");
		printf("3.乘法运算\n");
		printf("4.除法运算\n");
		printf("5.分数加法运算\n");
		printf("9.我想推出这次的训练。\n");
		scanf_s("%d", &Choice);
		Grade = 0;
		switch (Choice)
		{
		case 1:
		{
			printf("这是加法运算\n");
			Score += add(Grade);
		}break;
		case 2:
		{
			printf("这是减法运算\n");
			Score += sub(Grade);
		}break;
		case 3:
		{
			printf("这是乘法运算\n");
			Score += mul(Grade);
		}break;
		case 4:
		{
			printf("这是除法运算\n");
			Score += div(Grade);
		}break;
		case 5:
		{
			printf("这是分数加法运算\n");
			Score += Sadd(Grade);
		}break;
		case 6:
		{
			printf("这是分数减法运算\n");
			Score += Ssub(Grade);
		}break;
		case 7:
		{
			printf("这是分数乘法运算\n");
			Score += Smul(Grade);
		}break;
		case 8:
		{
			printf("这是分数除法运算\n");
			Score += Sdiv(Grade);
		}break;
		case 9:
		{
			printf("我想推出这次的训练。\n");
			Num = 0;
		}break;
		default:printf("非法操作\n"); break;
		}
	}
	printf("您的最终成绩为:%d分\n\n\n\n\n\n\n",Score);
}

四、运行界面






五、个人小结

1.psp表格

psp2.1 | 任务内容 | 计划完成需要的时间(min)| 实际完成需要的时间(min) |

  • | :-: | :-: | :-: | -:
    Planing |计划 |30 |25 |
    Estimate |估计这个任务需要多少时间,
    并规划大致工作步骤 |300 | 350 |
    Development |开发| 300| 300|
    Analysis |需求分析(包括学习新技能)|60|90|
    Design Spec|生成设计文档|10|20|
    Design Review|设计审核|15|15|
    Coding Standard|代码规范|8|10|
    Design|具体设计|10|12|
    Coding|具体编码|300|300|
    Code Review|代码复核|15|20|
    Test|测试(自我测试,修改代码
    提交修改)|60|60|
    Reporting|报告|15|15|
    Test Report|测试报告|6|10|
    Size Measurement|计算工作量|25|25|
    Postmortem & Process
    Improvement|事后总结,并提出过程修改计划|5|5|

2.经历

经过这此在博客上使用markdown进行编写自己的博文,首先感觉很有一种自己的成就感,希望在以后可以用自己的努力,编写更多更加规范的代码,肯定很开心,我更希望以后可以编写更多的代码,通过一次次的训练让自己的能力更进一步。

posted on 2020-11-08 19:23  阳宝冲冲冲  阅读(114)  评论(0编辑  收藏  举报