四则运算题目生成程序(一)

1、做业信息

博客班级

软件工程app

做业要求 做业要求
做业目标 你理解的做业目标具体内容
学号 3180701218

2、做业要求

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

3、代码

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<math.h>
#define EPSILON 0.000001 //根据精度须要
int N=0;
int n=0;//用户输入搜题数目n
int m=0;//选择搜题种类


void huajian(int *p,int *q)//化简为真分数
{
	int i;
	for(i=*p;i>=1;i--)
	{
		if(((*p)%i==0)&&((*q)%i==0))
		{
			(*p)=(*p)/i;
			(*q)=(*q)/i;
		}
	}
}


void input()//用户输入搜题数目n,选择搜题种类
{
	printf("*———————————*\n");
	printf("|    四则运算生成器    |\n");
	printf("*———————————*\n");
	printf("|        1.加法        |\n");
	printf("|        2.减法        |\n");
	printf("|        3.乘法        |\n");
	printf("|        4.除法        |\n");
	printf("|        5.结束        |\n");
	printf("|  无限小数保留6位小数 |\n");
	printf("*———————————*\n");
	printf("请输入搜题种类:\n");
	scanf("%d",&m);
}


void addtion()//加法
{
	int i,j;
	printf("1.整数运算or2.真分数运算\n");
	scanf("%d",&j);
	srand(time(NULL));
	if(j==1)
	{
		for(i=0;i<n;i++)
		{
			int a,b,c,d;
			a=rand()%N;
			b=rand()%N;
			c=a+b;
			printf("%d+%d=",a,b);
			scanf("%d",&d);
			if(d==c)printf("运算正确!\n");
			else printf("运算错误!正确答案是%d\n",c);
		}
	}
	else
		for(i=0;i<n;i++)
		{
			int a,b;
			int a1,b1;
			float c,d;
			while(1)
			{
				a=rand()%N;
				b=rand()%N;
				a1=rand()%N;
				b1=rand()%N;
				if(b1!=0&&b!=0&&a!=0&&a1!=0&&a!=b&&a1!=b1)
					break;
			}
			huajian(&a,&b);
			huajian(&a1,&b1);
			c=a/(b*1.0)+a1/(b1*1.0);
			printf("%d/%d+%d/%d=",a,b,a1,b1);
			scanf("%f",&d);
			if(fabs(d-c)<EPSILON)
				printf("运算正确!\n");
			else 
				printf("运算错误!正确答案是%f\n",c);
		}
};
void subtract()//减法
{
	int i,j;
	printf("1.整数运算or2.真分数运算\n");
	scanf("%d",&j);
	srand(time(NULL));
	if(j==1)
	{
		for(i=0;i<n;i++)
		{
			int a,b,c,d;
			a=rand()%N;
			b=rand()%N;
			c=a-b;
			printf("%d-%d=",a,b);
			scanf("%d",&d);
			if(d==c)printf("运算正确!\n");
			else printf("运算错误!正确答案是%d\n",c);
		}
	}
	else
		for(i=0;i<n;i++)
		{
			int a,b;
			int a1,b1;
			float c,d;
			while(1)
			{
				a=rand()%N;
				b=rand()%N;
				a1=rand()%N;
				b1=rand()%N;
				if(b1!=0&&b!=0&&a!=0&&a1!=0&&a!=b&&a1!=b1)
					break;
			}
			huajian(&a,&b);
			huajian(&a1,&b1);
			c=a/(b*1.0)-a1/(b1*1.0);
			printf("%d/%d-%d/%d=",a,b,a1,b1);
			scanf("%f",&d);
			if(fabs(d-c)<EPSILON)
				printf("运算正确!\n");
			else 
				printf("运算错误!正确答案是%f\n",c);
		}
};
void multiply()//乘法
{
	int i,j;
	printf("1.整数运算or2.真分数运算\n");
	scanf("%d",&j);
	srand(time(NULL));
	if(j==1)
	{
		for(i=0;i<n;i++)
		{
			int a,b,c,d;
			a=rand()%N;
			b=rand()%N;
			c=a*b;
			printf("%d*%d=",a,b);
			scanf("%f",&d);
			if(d==c)printf("运算正确!\n");
			else printf("运算错误!正确答案是%d\n",c);
		}
	}
	else
		for(i=0;i<n;i++)
		{
			int a,b;
			int a1,b1;
			float c,d;
			while(1)
			{
				a=rand()%N;
				b=rand()%N;
				a1=rand()%N;
				b1=rand()%N;
				if(b1!=0&&b!=0&&a!=0&&a1!=0&&a!=b&&a1!=b1)
					break;
			}
			huajian(&a,&b);
			huajian(&a1,&b1);
			c=a/(b*1.0)*a1/(b1*1.0);
			printf("%d/%d*%d/%d=",a,b,a1,b1);
			scanf("%f",&d);
			if(fabs(d-c)<EPSILON)
				printf("运算正确!\n");
			else 
				printf("运算错误!正确答案是%f\n",c);
		}
};
void divide()//除法【小数大小判断不许确】
{
	int i,j;
	printf("1.整数运算or2.真分数运算\n");
	scanf("%d",&j);
	srand(time(NULL));
	if(j==1)
	{
		for(i=0;i<n;i++)
		{
			int a,b,c,d;
			a=rand()%N;
			b=rand()%N;
			c=a/b;
			printf("%d/%d=",a,b);
			scanf("%f",&d);
			if(d==c)printf("运算正确!\n");
			else printf("运算错误!正确答案是%d\n",c);
		}
	}
	else
		for(i=0;i<n;i++)
		{
			int a,b;
			int a1,b1;
			float c,d;
			while(1)
			{
				a=rand()%N;
				b=rand()%N;
				a1=rand()%N;
				b1=rand()%N;
				if(b1!=0&&b!=0&&a!=0&&a1!=0&&a!=b&&a1!=b1)
					break;
			}
			huajian(&a,&b);
			huajian(&a1,&b1);
			c=a/(b*1.0)/(a1/(b1*1.0));
			printf("(%d/%d)/(%d/%d)=",a,b,a1,b1);
			scanf("%f",&d);
			if(fabs(d-c)<EPSILON)
				printf("运算正确!\n");
			else 
				printf("运算错误!正确答案是%f\n",c);
		}
};

int main()
{
	printf("想练习多大数之内的运算题目:\n");
	scanf("%d",&N);
	while(1)
	{
		input();
		switch(m)
		{
		case 1:
			printf("请输入搜题数目:\n");
			scanf("%d",&n);
			addtion();
			break;
		case 2:
			printf("请输入搜题数目:\n");
			scanf("%d",&n);
			subtract();
			break;
		case 3:
			printf("请输入搜题数目:\n");
			scanf("%d",&n);
			multiply();
			break;
		case 4:
			printf("请输入搜题数目:\n");
			scanf("%d",&n);
			divide();
			break;
		case 5:
			printf("谢谢使用!:\n");
			return 0;
		default:
			printf("输入指令有误,请从新输入!\n");
			break;
		}
	}
	return 0;
};

4、运行截图

5、总结

1.psp表格

psp2.1 任务内容 计划完成须要的时间(min) 实际完成须要的时间(min)
Planning 计划 10 8
Estimate 估计这个任务须要多少时间,
并规划大体工做步骤
10 12
Development 开发 100 150
Analysis 需求分析(包括学习新技术) 12 5
Design Spec 生成设计文档 5 5
Design Review 设计复审 5 5
Coding Standard 代码规范 3 2
Design 具体设计 10 12
Coding 具体编码 36 21
Code Review 代码复审 5 7
Test 测试(自我测试,修改代码,提交修改) 10 15
Reporting 报告 9 6
Test Report 测试报告 3 2
Size Measurement 计算工做量 2 1
Postmortem & Process Improvement Plan 过后总结,并提出过程改进计划 3 3

2.总结

刚看到这个题目时,我以为很简单,只须要利用随机数就能够了,可是实现过程却遇到了不少困难,尤为是实现真分数运算。该系统将分数转化为小数,经过两个分数差的绝对值与一个很小量比较判断大小,所以会产生误判,而且用户只能输入小数才能被系统识别。例如1/3+3/7=?正确答案是16/21。可是用户只能输入小数,且保留小数点后6位。这样,用户输入不方便,并且系统可能会误判。学习

相关文章
相关标签/搜索