nidus

导航

四则运算题目生成程序

一、作业信息

|博客班级| AHPU-软件工程导论-计算机18级 (安徽工程大学 - 计算机与信息学院)
|----|----|----|
| 作业要求| 四则运算题目生成程序|
|作业目标| 练习编译能力,提高编译能力|
| 学号| 3180701335|

二.题目要求

写一个能自动生成小学四则运算题目的程序,然后在此基础上扩展:
1)除了整数以外,还要支持真分数的四则运算,例如:1/6+1/8=7/24
2)程序要求能处理用户的输入,判断对错,累积分数
3)程序支持可以由用户自行选择加、减、乘、除运算
4)使用-n参数控制生成题目的个数,例如Myapp.exe -n 10,将生成10个题目

三.代码提交

头文件

#include"stdio.h"
#include"stdlib.h"
#include"time.h"
#include"math.h"
#include"string.h"

主菜单

void menu()
{
	printf("四则运算题目生成程序\n");
	printf("输入要生成的题目类型\n");
	printf("1.加法算法(整数)\n");
	printf("2.减法算法(整数)\n");
	printf("3.乘法算法(整数)\n");
	printf("4.除法算法(整数)\n");
	printf("5.加法算法(真分数)\n");
	printf("6.减法算法(真分数)\n");
	printf("7.乘法算法(真分数)\n");
	printf("8.除法算法(真分数)\n");
	printf("0.退出\n");
	printf("请输入0——8\n");
}

最大公约数

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;
}

整数加法

int IAddition(int G=0)//加法算法题目生成与处理
{
	int i;//i 生成的题目数量
	int x, y, z1 = 0, z2;//x,y为生成的数字用于加法计算,z1是输入的结果,z2是答案。
	printf("请输入要生成的题目数量:");
	scanf_s("%d", &i);
	for (int j = 0; j < i; j++)
	{
		srand((unsigned)time(NULL));
		x = rand() % 100;
		y = rand() % 100;
		z2 = x + y;
		printf("%d+%d=?", x, y);
		printf("请输入答案:");
		scanf_s("%d", &z1);
		if (z1 == z2)
		{
			printf("答案正确。\n");
			G += 1;
		}
		else
			printf("答案错误,正确答案为%d\n", z2);
	}
	return G;

}

整数减法

int ISubtraction(int G=0)
{
	int i;//i 生成的题目数量
	int x, y, z1 = 0, z2;//x,y为生成的数字用于减法计算,z1是输入的结果,z2是答案。
	printf("请输入要生成的题目数量:");
	scanf_s("%d", &i);
	for (int j = 0; j < i; j++)
	{
		srand((unsigned)time(NULL));
		x = rand() % 100;
		y = rand() % 100;
		z2 = x - y;
		printf("%d-%d=?", x, y);
		printf("请输入答案:");
		scanf_s("%d", &z1);
		if (z1 == z2)
		{
			printf("答案正确。\n");
			G += 1;
		}
		else
			printf("答案错误,正确答案为%d\n", z2);
	}
	return G;
}

整数乘法

int IMultiplication(int G=0)
{
	int i;//i 生成的题目数量
	int x, y, z1 = 0, z2;//x,y为生成的数字用于乘法计算,z1是输入的结果,z2是答案。
	printf("请输入要生成的题目数量:");
	scanf_s("%d", &i);
	for (int j = 0; j < i; j++)
	{
		srand((unsigned)time(NULL));
		x = rand() % 100;
		y = rand() % 100;
		z2 = x * y;
		printf("%d*%d=?", x, y);
		printf("请输入答案:");
		scanf_s("%d", &z1);
		if (z1 == z2)
		{
			printf("答案正确。\n");
			G += 1;
		}
		else
			printf("答案错误,正确答案为%d\n", z2);
	}
	return G;
}

整数除法

int IDivision(int G=0)
{
	int i;//i 生成的题目数量
	int x, y, z1 = 0, z2;//x,y为生成的数字用于除法计算,z1是输入的结果,z2是答案。
	printf("请输入要生成的题目数量:");
	scanf_s("%d", &i);
	for (int j = 0; j < i; j++)
	{
		srand((unsigned)time(NULL));
		x = rand() % 100;
		y = rand() % 100;
		z2 = x / y;
		printf("%d/%d=?", x, y);
		printf("请输入答案:");
		scanf_s("%d", &z1);
		if (z1 == z2)
		{
			printf("答案正确。\n");
			G += 1;
		}
		else
			printf("答案错误,正确答案为%d\n", z2);
	}
	return G;
}

分数加法

int SAddition(int G=0)
{
	int flag = 0;
	int i;//i 生成的题目数量
	int x1, x2, y1, y2,Mcon,Mcon1;//x1,x2为分子,y1,y2为生成的分母数字用于加法计算.
	int z1=0, z2=0, z3=0, z4=0;//z1是输入的分子,z2是分母   z3是答案的分子,z4是答案的分母。
	printf("请输入要生成的题目数量:");
	scanf_s("%d", &i);
	for (int j = 0; j < i; j++)
	{
		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");
			G += 1;
		}
		else
			printf("答案错误,正确答案为%d/%d\n",z1,z4 );
	}
	return G;
}

分数减法

int SSubtraction(int G=0)
{
	int flag = 0;
	int i;//i 生成的题目数量
	int x1, x2, y1, y2, Mcon,Mcon1;//x1,x2为分子,y1,y2为生成的分母数字用于加法计算.
	int z1 = 0, z2 = 0, z3 = 0, z4 = 0;//z1是输入的分子,z2是分母   z3是答案的分子,z4是答案的分母。
	printf("请输入要生成的题目数量:");
	scanf_s("%d", &i);
	for (int j = 0; j < i; j++)
	{
		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");
			G += 1;
		}
		else
			printf("答案错误,正确答案为%d/%d\n", z3, z4);
	}
	return G;
}

分数乘法

int SMultiplication(int G=0)
{
	
	int i;//i 生成的题目数量
	int x1, x2, y1, y2, Mcon,Mcon1;//x1,x2为分子,y1,y2为生成的分母数字用于加法计算.
	int z1 = 0, z2 = 0, z3 = 0, z4 = 0;//z1是输入的分子,z2是分母   z3是答案的分子,z4是答案的分母。
	printf("请输入要生成的题目数量:");
	scanf_s("%d", &i);
	for (int j = 0; j < i; j++)
	{
		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");
			G += 1;
		}
		else
			printf("答案错误,正确答案为%d/%d\n", z3, z4);
	}
	return G;
}

分数除法

int SDivision(int G=0)
{
	
	int i;//i 生成的题目数量
	int x1, x2, y1, y2, Mcon,Mcon1;//x1,x2为分子,y1,y2为生成的分母数字用于加法计算.
	int z1 = 0, z2 = 0, z3 = 0, z4 = 0;//z1是输入的分子,z2是分母   z3是答案的分子,z4是答案的分母。
	printf("请输入要生成的题目数量:");
	scanf_s("%d", &i);
	for (int j = 0; j < i; j++)
	{
		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");
			G += 1;
		}
		else
			printf("答案错误,正确答案为%d/%d\n", z3, z4);
	}
	return G;
}

主函数


int main()
{

	int number, grade1 = 0, grade2 = 0;
	while (1)
	{
		menu();
		scanf_s("%d", &number);
		switch (number)
		{
		case 1: grade2 = IAddition(grade1); break;
		case 2: grade2 = ISubtraction(grade1); break;
		case 3: grade2 = IMultiplication(grade1); break;
		case 4: grade2 = IDivision(grade1); break;
		case 5: grade2 = SAddition(grade1); break;
		case 6: grade2 = SSubtraction(grade1); break;
		case 7: grade2 = SMultiplication(grade1); break;
		case 8: grade2 = SDivision(grade1); break;
		case 0: exit(0);
		default: printf("error"); break;
		}
		printf("\n最后得分为:%d\n\n", grade2);
	}

}

运行截图





个人小结

1.psp表格

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

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

2、个人小结

经过这次编程练习,发现了许多的不足之处,编程能力有待提高,改错能力也有待提高。争取在这几次的编程练习中大幅度提高编程能力。

posted on 2020-11-08 17:26  NIDUSPRIME  阅读(185)  评论(0编辑  收藏  举报