四则运算--实现?

四则运算

驾驶员:许晴

领航员:陈婷

一.题目要求

我们在刚开始上课的时候介绍过一个小学四则运算自动生成程序的例子,请实现它,要求:
1.能够自动生成四则运算练习题
2.可以定制题目数量
3.用户可以选择运算符
4.用户设置最大数(如十以内、百以内等)
5.用户选择是否有括号、是否有小数
6.用户选择输出方式(如输出到文件、打印机等)
7.最好能提供图形用户界面(根据自己能力选做,以完成上述功能为主)

二.运算实现

1.实现重点

(1)随机生成函数

srand((unsigned int)time(0));
a=rand()%max+1;

(2)功能子函数

char ASMD();//生成运算符函数
int ct();//生成随机整数函数
double dec();//生成随机小数函数
char ASMD()
{
	char ch;
	int four;
		four = rand() % 4;
		switch (four)
		{
		case 0:ch = '+'; break;
		case 1:ch = '-'; break;
		case 2:ch = '*'; break;
		case 3:ch = '/'; break;
		}
	return ch;
}
int ct()
{
	int ctt;
	ctt = rand() % qmax + 1;
	return ctt;
}
double dec()
{
	double mal;
	int nt;
	nt = rand() % 1000 + 1;
	mal = nt * 0.1;
	for (;;)
	{
		if (mal < qmax)
		{
			break;
		}
	}
	return mal;
}

(3)功能选择菜单

	cout << "How many questions do you want?(0-1000)
";//设置题目数量
cin >> qnum;
for (;;)
{
	if (qnum<0 || qnum>1000)//所输入数在范围以外
	{
		cout << "Please enter the values in the range!
";
		cin >> qnum;
	}
	else
	{
		cout << "Received!There will be " << qnum << " questions!
";
		break;
	}
}
cout << "What is the maximum number?(0-100)
";//设置最大数值
cin >> qmax;
for (;;)
{
	if (qmax<0 || qmax>100)//所输入数据在范围外
	{
		cout << "Please enter the values in the range!
";
		cin >> qmax;
	}
	else
	{
		cout << "Received!Every number is less than " << qmax << "
";
		break;
	}
}
cout << "Do you want any bracket? 1.Yes 2.No
";//设置有无括号
cin >> qbracket;
for (;;)
{
	if (qbracket != 1 && qbracket != 2)
	{
		cout << "Please enter 1 or 2!
";
		cin >> qbracket;
	}
	else if (qbracket == 1)
	{
		cout << "Received!We will have brackets in every question!
";
		break;
	}
	else if (qbracket == 2)
	{
		cout << "Received!We won't have brackets in every question!
";
		break;
	}
}
cout << "Do you want any decimal? 1.Yes 2.No
";//设置有无小数
cin >> qdecimal;
for (;;)
{
	if (qdecimal != 1 && qdecimal != 2)
	{
		cout << "Please enter 1 or 2!
";
		cin >> qdecimal;
	}
	else if (qdecimal == 1)
	{
		cout << "Received!We will have decimals in every question!
";
		break;
	}
	else if (qdecimal == 2)
	{
		cout << "Received!We won't have decimals in every question!
";
		break;
	}
}
cout << "Please select the questions category!
";//设置题目类型
cout << "1.only one kind of operation(no brackets)
";//仅有一个运算符
cout << "2.Two kind of operations mixed
";//有两个运算符
    cout << "3.Three kind of operations mixed
";//有三个运算符
cin >> qtype;

2.整体实现

(1)变量设置

int qnum;//题目数量
int qmax;//题目数值上限
int qbracket;//括号
int qdecimal;//小数
int qtype;//题目运算符类别
int num1, num2, num3, num4;//整数
double cnum1,cnum2,cnum3,cnum4;//小数
char ch1, ch2, ch3;//运算符

(2)运算分类

运算类别分为:有无括号,有无小数,算式中仅有一个运算符,有两个运算符,以及有三个运算符。
以无括号无小数而且有三个运算符这一种运算为例:
if (qtype == 3)//无括号无小数而且有三个运算符
{
for (i = 0; i<qnum; i++)
{
num1 = ct();
num2 = ct();
num3 = ct();
num4 = ct();
ch1 = ASMD();
ch2 = ASMD();
ch3 = ASMD();
if (ch1!= ch2 != ch3)
{
cout << "No." << i + 1 << ":" << num1 << ch1<< num2 << ch2 << num3 << ch3 << num4 << "= " << endl;
}
}
}

(3)举例运行



3.总结

代码地址:HERE
本次我和我的领航员选择的是四则运算器这一个作业,虽然做的不是很好,但我们也是尽力去完成它。我的代码编辑能力极弱,但是我也用了很久去实现代码的功能,虽然更加能实现的并不完全。但是对我个人以及对我们这个结对项目来说,算是一个很大的进步。
我的领航员,陈婷,也尽职尽责做到了她领航员的责任。代码的编辑是不太可能一次就成功的,需要多次的运行测试以及代码删改。我的代码总体来说瑕疵不少,比如一开始的时候代码运行结果就出现了随机结果相同的情况,经过我们的再次的更改运行,才发现我的srand函数使用不当,虽然也是第一次接触随机函数,但是这种小错误也不应该出现。经过她的运行测试和我们的代码删改,最后能实现我们想要的功能。

最后附上一张图
前方高能,能看到这儿的就走吧!

原文地址:https://www.cnblogs.com/xqAmber/p/8863185.html