四则运算2单元测试

规范后的四则运算2源程序:

//四则运算程序2,从四则运算程序1的基础之上进行功能扩展
//支持真分数运算,题目不重复,最多可支持十个运算数的带括号的运算式
//多项四则运算式以及分数除法未予考虑余数,整数除法可以实现整除功能
//2016,03,09

#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<time.h>
using namespace std;

void main()
{
	srand((int)time(NULL));
	//定义变量,记录用户的功能选择选项
	int minfigure;
	int maxfigure;
	int count;
	int chengchu;
	int remainder_chengchu;
	int negative_jiajian;
	int printstyle;
	//功能设置界面
	cout << "*******************************************************************" << endl;
	cout << "*                         四则运算生成器                          *" << endl;
	cout << "*******************************************************************" << endl;
	cout << "请按照系统提示设置生成器的功能:" << endl;
	cout << "请输入参与四则运算的数值范围(格式如:1  100):" ;
	cin >> minfigure >> maxfigure;
	cout << "请输入生成四则运算式的数量:";
	cin >> count;
	cout << "请选择打印方式(1 屏幕输出  0 输出到文档):";
	cin >> printstyle;
	cout << "请设置系统的下列参数:" << endl;
	cout << "    1 四则运算可否生成乘除法(1 是  0 否):";
	cin >> chengchu;
	if (1 == chengchu)
	{
		cout << "    2 除法是否可有余数(1 是  0 否):";
		cin >> remainder_chengchu;
	}
	else
	{
		cout << "    2 加减法是否可以有负数(1 是  0 否):";
		cin >> negative_jiajian;
	}
	cout << "功能设置完成!" << endl;
	cout << "*******************************************************************" << endl;

	int i, j;
	int array_A[100];
	int array_B[100];
	int Operator[10];
	char operatorFu[9];
	int Operatorfu[9];
	//生成用户指定数量的运算式的因子,为保证算式不重复,让生成的随机数都不重复
	for (i = 0; i < count; i++)
	{
		array_A[i] = minfigure + rand() % maxfigure;
		array_B[i] = minfigure + rand() % maxfigure;
		for (j = 0; j < i; j++)
		{
			if (array_A[i] == array_A[j])
			{
				array_A[i] = minfigure + rand() % maxfigure;
				j = 0;
			}
			if (array_B[i] == array_B[j])
			{
				array_B[i] = minfigure + rand() % maxfigure;
				j = 0;
			}
		}
	}
	//根据用户的选择产生功能分支
	if (1 == printstyle)   //运算式输出到屏幕上
	{
		if (1 == chengchu)  //是否可以生成除法
		{
			if (remainder_chengchu)  //除法可否有余数,分数除法谈论是否有余数无意义
			{
				cout << "*******************************************************************" << endl;
				cout << "生成的四则运算式如下所示:" << endl;
				cout << "*******************************************************************" << endl;
				for (i = 0; i < count; i++)
				{
					if (array_A[i] > array_B[i])   //如果A[i]>B[i],则后续程序生成带括号的多项因子的四则运算
					{
						int m;
						int operator_count = 3 + rand() % 8;   //随机生成多项因子的个数
						int operatorfu_count = operator_count - 1;  //算符的个数比因子的个数少一
						for (m = 0; m < operator_count; m++)
						{
							Operator[m] = minfigure + rand() % maxfigure;   //生成多项因子
						}
						for (m = 0; m < operatorfu_count; m++)
						{
							Operatorfu[m] = rand() % 4;                  //生成多项式的算符
						}
						for (m = 1; m < operator_count; m++)
						{
							if ((Operatorfu[m] / 2)>(Operatorfu[m - 1] / 2))  //乘除用3和4表示,加减用1和2表示,让其除以二得商0和1,可以区分出乘除为同一优先级,加减比乘除优先级低
							{                                                 //若后一个运算符的优先级比前一个运算符的优先级高,则将前一个运算符对应的式子用括号括起来,确保了运算优先级低的加减运算在括号里面
								switch (Operatorfu[m - 1])
								{
								case 0:
									cout << "(" << Operator[m - 1] << " + " << Operator[m] << ")"; break;
								case 1:
									cout << "(" << Operator[m - 1] << " - " << Operator[m] << ")"; break;
								case 2:
									cout << "(" << Operator[m - 1] << " x " << Operator[m] << ")"; break;
								case 3:
									if (0 == Operator[m])
									{
										cout << "(" << Operator[m - 1] << " ÷ " << Operator[m] + 2 << ")";
									}
									break;   //若分子为零时,则让分子加2,即除以2
								}
								switch (Operatorfu[m])
								{
								case 0:
									cout << " + "; m = m + 1; break;
								case 1:
									cout << " - "; m = m + 1; break;
								case 2:
									cout << " x "; m = m + 1; break;
								case 3:
									cout << " ÷ "; m = m + 1; break;
								}
							}
							else
							{
								switch (Operatorfu[m - 1])
								{
								case 0:
									cout << Operator[m - 1] << " + "; break;
								case 1:
									cout << Operator[m - 1] << " - "; break;
								case 2:
									cout << Operator[m - 1] << " x "; break;
								case 3:
									cout << Operator[m - 1] << " ÷ "; break;
								}
							}
						}
						cout << Operator[m - 1] << " = " << endl;         //输出最后一个多项式因子
					}
					else            //如果A[i]<B[i],则产生真分数运算式
					{
						int copyA = array_A[i];
						int copyB = array_B[i];
						int beichushuA = maxfigure;
						int beichushuB = maxfigure;
						int firstA = beichushuA% copyA;
						int firstB = beichushuB% copyB;
						while (firstA != 0)        //求A[i]和最大值的公约数,以便后面化简
						{
							int temp = copyA;
							copyA = beichushuA%copyA;
							beichushuA = temp;
							firstA = beichushuA%copyA;
						}
						while (firstB != 0)
						{
							int temp = copyB;
							copyB = beichushuB%copyB;
							beichushuB = temp;
							firstB = beichushuB%copyB;
						}
						int suanfu = rand() % 4;
						switch (suanfu)
						{
						case 0:
							cout << array_A[i] / copyA << "/" << maxfigure / copyA << " + " << array_B[i] / copyB << "/" << maxfigure / copyB << " = " << endl; break;
						case 1:
							cout << array_A[i] / copyA << "/" << maxfigure / copyA << " - " << array_B[i] / copyB << "/" << maxfigure / copyB << " = " << endl; break;
						case 2:
							cout << array_A[i] / copyA << "/" << maxfigure / copyA << " x " << array_B[i] / copyB << "/" << maxfigure / copyB << " = " << endl; break;
						case 3:
							cout << array_A[i] / copyA << "/" << maxfigure / copyA << " ÷ " << array_B[i] / copyB << "/" << maxfigure / copyB << " = " << endl; break;
							//让A[i]和最大值都除以公约数便可以得到无法再约分的最大公约数
						}
					}
				}
			}
			else
			{
				cout << "*******************************************************************" << endl;
				cout << "生成的四则运算式如下所示:" << endl;
				cout << "*******************************************************************" << endl;
				for (i = 0; i < count; i++)
				{
					if (array_A[i] > array_B[i])
					{
						int m;
						int operator_count = 3 + rand() % 8;
						int operatorfu_count = operator_count - 1;
						for (m = 0; m < operator_count; m++)
						{
							Operator[m] = minfigure + rand() % maxfigure;
						}
						for (m = 0; m < operatorfu_count; m++)
						{
							Operatorfu[m] = rand() % 4;
						}
						for (m = 1; m < operator_count; m++)
						{
							if ((Operatorfu[m] / 2)>(Operatorfu[m - 1] / 2))
							{
								switch (Operatorfu[m - 1])
								{
								case 0:
									cout << "(" << Operator[m - 1] << " + " << Operator[m] << ")"; break;
								case 1:
									cout << "(" << Operator[m - 1] << " - " << Operator[m] << ")"; break;
								case 2:
									cout << "(" << Operator[m - 1] << " x " << Operator[m] << ")"; break;
								case 3:
									if (0 == Operator[m])
									{
										cout << "(" << Operator[m - 1] << " ÷ " << Operator[m] + 2 << ")";
									}
									break;
								}
								switch (Operatorfu[m])
								{
								case 0:
									cout << " + "; m = m + 1; break;
								case 1:
									cout << " - "; m = m + 1; break;
								case 2:
									cout << " x "; m = m + 1; break;
								case 3:
									cout << " ÷ "; m = m + 1; break;
								}
							}
							else
							{
								switch (Operatorfu[m - 1])
								{
								case 0:
									cout << Operator[m - 1] << " + "; break;
								case 1:
									cout << Operator[m - 1] << " - "; break;
								case 2:
									cout << Operator[m - 1] << " x "; break;
								case 3:
									cout << Operator[m - 1] << " ÷ "; break;
								}
							}
						}
						cout << Operator[m - 1] << " = " << endl;
					}
					else
					{
						int copyB = array_B[i];
						int first = array_B[i] % array_A[i];
						while (first != 0)
						{
							int temp = array_A[i];
							array_A[i] = array_B[i] % array_A[i];
							array_B[i] = temp;
							first = array_B[i] % array_A[i];
						}
						int suanfu = rand() % 4;
						switch (suanfu)
						{
						case 0:
							cout << array_A[i] << " + " << array_B[i] << "=" << endl; break;
						case 1:
							cout << array_A[i] << " - " << array_B[i] << "=" << endl; break;
						case 2:
							cout << array_A[i] << " x " << array_B[i] << "=" << endl; break;
						case 3:
							cout << copyB << " ÷ " << array_A[i] << " = " << endl; break;
						}
					}
				}
			}
		}
		else   //不允许生成乘除法,则只有加减法
		{
			if ((minfigure < 0) && (0 == negative_jiajian))  //若加减法不允许出现负数,则应该重新设定数值的范围,让最小值大于零,保证生成的随机数都是大于零的
			{
				cout << "请重新设置数值范围,最小值应大于零以保证加法无负数!" << endl;
				cout << "请输入参与四则运算的数值范围(格式如:1  100):";
				cin >> minfigure >> maxfigure;
				for (i = 0; i < count; i++)     //重新生成的随机数仍是不重复的
				{
					array_A[i] = minfigure + rand() % maxfigure;
					array_B[i] = minfigure + rand() % maxfigure;
					for (j = 0; j < i; j++)
					{
						if (array_A[i] == array_A[j])
						{
							array_A[i] = minfigure + rand() % maxfigure;
							j = 0;
						}
						if (array_B[i] == array_B[j])
						{
							array_B[i] = minfigure + rand() % maxfigure;
							j = 0;
						}
					}
				}
			}
			for (i = 0; i < count; i++)
			{
				if (array_A[i]>array_B[i])
				{
					int suanfu = rand() % 2;
					switch (suanfu)
					{
					case 0:
						cout << array_A[i] << " + " << array_B[i] << "=" << endl; break;
					case 1:
						cout << array_A[i] << " - " << array_B[i] << "=" << endl; break;
					}
				}
				else
				{
					int copyA = array_A[i];
					int copyB = array_B[i];
					int beichushuA = maxfigure;
					int beichushuB = maxfigure;
					int firstA = beichushuA% copyA;
					int firstB = beichushuB% copyB;
					while (firstA != 0)
					{
						int temp = copyA;
						copyA = beichushuA%copyA;
						beichushuA = temp;
						firstA = beichushuA%copyA;
					}
					while (firstB != 0)
					{
						int temp = copyB;
						copyB = beichushuB%copyB;
						beichushuB = temp;
						firstB = beichushuB%copyB;
					}
					int suanfu = rand() % 2;
					switch (suanfu)
					{
					case 0:
						cout << array_A[i] / copyA << "/" << maxfigure / copyA << " + " << array_B[i] / copyB << "/" << maxfigure / copyB << "=" << endl; break;
					case 1:
						cout << array_B[i] / copyB << "/" << maxfigure / copyB << " - " << array_A[i] / copyA << "/" << maxfigure / copyA << "=" << endl; break;
					}
				}
			}
		}
	}
	else  //将最后生成的四则运算式存到文件中,内部实现的功能与输出到屏幕一致,只是输出不一样
	{
		if (1 == chengchu)
		{
			if (remainder_chengchu)
			{
				ofstream fileout;  //定义输出文件流的对象
				fileout.open("biaodashi.txt", ios::out);  //打开文件,该文件为指定路径,则存放在工程的当前目录下
				fileout << "*******************************************************************" << endl;
				fileout << "生成的四则运算式如下所示:" << endl;
				fileout << "*******************************************************************" << endl;
				for (i = 0; i < count; i++)
				{
					if (array_A[i] > array_B[i])
					{
						int m;
						int operator_count = 3 + rand() % 8;
						int operatorfu_count = operator_count - 1;
						for (m = 0; m < operator_count; m++)
						{
							Operator[m] = minfigure + rand() % maxfigure;
						}
						for (m = 0; m < operatorfu_count; m++)
						{
							Operatorfu[m] = rand() % 4;
						}
						for (m = 1; m < operator_count; m++)
						{
							if ((Operatorfu[m] / 2)>(Operatorfu[m - 1] / 2))
							{
								switch (Operatorfu[m - 1])
								{
								case 0:
									fileout << "(" << Operator[m - 1] << " + " << Operator[m] << ")"; break;
								case 1:
									fileout << "(" << Operator[m - 1] << " - " << Operator[m] << ")"; break;
								case 2:
									fileout << "(" << Operator[m - 1] << " x " << Operator[m] << ")"; break;
								case 3:
									if (0 == Operator[m])
									{
										fileout << "(" << Operator[m - 1] << " ÷ " << Operator[m] + 2 << ")";
									}
									break;
								}
								switch (Operatorfu[m])
								{
								case 0:
									fileout << " + "; m = m + 1; break;
								case 1:
									fileout << " - "; m = m + 1; break;
								case 2:
									fileout << " x "; m = m + 1; break;
								case 3:
									fileout << " ÷ "; m = m + 1; break;
								}
							}
							else
							{
								switch (Operatorfu[m - 1])
								{
								case 0:
									fileout << Operator[m - 1] << " + "; break;
								case 1:
									fileout << Operator[m - 1] << " - "; break;
								case 2:
									fileout << Operator[m - 1] << " x "; break;
								case 3:
									fileout << Operator[m - 1] << " ÷ "; break;
								}
							}
						}
						fileout << Operator[m - 1] << " = " << endl;
					}
					else
					{
						int copyA = array_A[i];
						int copyB = array_B[i];
						int beichushuA = maxfigure;
						int beichushuB = maxfigure;
						int firstA = beichushuA% copyA;
						int firstB = beichushuB% copyB;
						while (firstA != 0)
						{
							int temp = copyA;
							copyA = beichushuA%copyA;
							beichushuA = temp;
							firstA = beichushuA%copyA;
						}
						while (firstB != 0)
						{
							int temp = copyB;
							copyB = beichushuB%copyB;
							beichushuB = temp;
							firstB = beichushuB%copyB;
						}
						int suanfu = rand() % 4;
						switch (suanfu)
						{
						case 0:
							fileout << array_A[i] / copyA << "/" << maxfigure / copyA << " + " << array_B[i] / copyB << "/" << maxfigure / copyB << " = " << endl; break;
						case 1:
							fileout << array_A[i] / copyA << "/" << maxfigure / copyA << " - " << array_B[i] / copyB << "/" << maxfigure / copyB << " = " << endl; break;
						case 2:
							fileout << array_A[i] / copyA << "/" << maxfigure / copyA << " x " << array_B[i] / copyB << "/" << maxfigure / copyB << " = " << endl; break;
						case 3:
							fileout << array_A[i] / copyA << "/" << maxfigure / copyA << " ÷ " << array_B[i] / copyB << "/" << maxfigure / copyB << " = " << endl; break;
						}
					}
				}
				fileout.close();   //关闭文件,断开与文件连接
			}
			else
			{
				ofstream fileout;
				fileout.open("biaodashi.txt", ios::out);
				fileout << "*******************************************************************" << endl;
				fileout << "生成的四则运算式如下所示:" << endl;
				fileout << "*******************************************************************" << endl;
				for (i = 0; i < count; i++)
				{
					if (array_A[i] > array_B[i])
					{
						int m;
						int operator_count = 3 + rand() % 8;
						int operatorfu_count = operator_count - 1;
						for (m = 0; m < operator_count; m++)
						{
							Operator[m] = minfigure + rand() % maxfigure;
						}
						for (m = 0; m < operatorfu_count; m++)
						{
							Operatorfu[m] = rand() % 4;
						}
						for (m = 1; m < operator_count; m++)
						{
							if ((Operatorfu[m] / 2)>(Operatorfu[m - 1] / 2))
							{
								switch (Operatorfu[m - 1])
								{
								case 0:
									fileout << "(" << Operator[m - 1] << " + " << Operator[m] << ")"; break;
								case 1:
									fileout << "(" << Operator[m - 1] << " - " << Operator[m] << ")"; break;
								case 2:
									fileout << "(" << Operator[m - 1] << " x " << Operator[m] << ")"; break;
								case 3:
									if (0 == Operator[m])
									{
										fileout << "(" << Operator[m - 1] << " ÷ " << Operator[m] + 2 << ")";
									}
									break;
								}
								switch (Operatorfu[m])
								{
								case 0:
									fileout << " + "; m = m + 1; break;
								case 1:
									fileout << " - "; m = m + 1; break;
								case 2:
									fileout << " x "; m = m + 1; break;
								case 3:
									fileout << " ÷ "; m = m + 1; break;
								}
							}
							else
							{
								switch (Operatorfu[m - 1])
								{
								case 0:
									fileout << Operator[m - 1] << " + "; break;
								case 1:
									fileout << Operator[m - 1] << " - "; break;
								case 2:
									fileout << Operator[m - 1] << " x "; break;
								case 3:
									fileout << Operator[m - 1] << " ÷ "; break;
								}
							}
						}
						fileout << Operator[m - 1] << " = " << endl;
					}
					else
					{
						int copyB = array_B[i];
						int first = array_B[i] % array_A[i];
						while (first != 0)
						{
							int temp = array_A[i];
							array_A[i] = array_B[i] % array_A[i];
							array_B[i] = temp;
							first = array_B[i] % array_A[i];
						}
						int suanfu = rand() % 4;
						switch (suanfu)
						{
						case 0:
							fileout << array_A[i] << " + " << array_B[i] << "=" << endl; break;
						case 1:
							fileout << array_A[i] << " - " << array_B[i] << "=" << endl; break;
						case 2:
							fileout << array_A[i] << " x " << array_B[i] << "=" << endl; break;
						case 3:
							fileout << copyB << " ÷ " << array_A[i] << " = " << endl; break;
						}
					}
				}
				fileout.close();
			}
		}
		else
		{
			if ((minfigure < 0) && (0 == negative_jiajian))
			{
				cout << "请重新设置数值范围,最小值应大于零以保证加法无负数!" << endl;
				cout << "请输入参与四则运算的数值范围(格式如:1  100):";
				cin >> minfigure >> maxfigure;
				for (i = 0; i < count; i++)
				{
					array_A[i] = minfigure + rand() % maxfigure;
					array_B[i] = minfigure + rand() % maxfigure;
					for (j = 0; j < i; j++)
					{
						if (array_A[i] == array_A[j])
						{
							array_A[i] = minfigure + rand() % maxfigure;
							j = 0;
						}
						if (array_B[i] == array_B[j])
						{
							array_B[i] = minfigure + rand() % maxfigure;
							j = 0;
						}
					}
				}
			}
			ofstream fileout;
			fileout.open("biaodashi.txt", ios::out);
			for (i = 0; i < count; i++)
			{
				if (array_A[i]>array_B[i])
				{
					int suanfu = rand() % 2;
					switch (suanfu)
					{
					case 0:
						fileout << array_A[i] << " + " << array_B[i] << "=" << endl; break;
					case 1:
						fileout << array_A[i] << " - " << array_B[i] << "=" << endl; break;
					}
				}
				else
				{
					int copyA = array_A[i];
					int copyB = array_B[i];
					int beichushuA = maxfigure;
					int beichushuB = maxfigure;
					int firstA = beichushuA% copyA;
					int firstB = beichushuB% copyB;
					while (firstA != 0)
					{
						int temp = copyA;
						copyA = beichushuA%copyA;
						beichushuA = temp;
						firstA = beichushuA%copyA;
					}
					while (firstB != 0)
					{
						int temp = copyB;
						copyB = beichushuB%copyB;
						beichushuB = temp;
						firstB = beichushuB%copyB;
					}
					int suanfu = rand() % 2;
					switch (suanfu)
					{
					case 0:
						fileout << array_A[i] / copyA << "/" << maxfigure / copyA << " + " << array_B[i] / copyB << "/" << maxfigure / copyB << "=" << endl; break;
					case 1:
						fileout << array_B[i] / copyB << "/" << maxfigure / copyB << " - " << array_A[i] / copyA << "/" << maxfigure / copyA << "=" << endl; break;
					}
				}
			}
			fileout.close();
		}
	}
}

    Right-BICEP测试要求:

    1.Right-结果是否正确?

    2.B-是否所有的边界条件都是正确的?

    3.I-能查一下反向关联吗?

    4.C-能用其他手段交叉检查一下结果吗?

    5.E-你是否可以强制错误条件发生?

    6.P-是否满足性能要求?

    对四则运算2程序的测试用例如下:

    第一组测试用例:数值范围去1-465,输出数量为15,可生成除数,除数可以有余数

    最大值465为随便输入的,1到465都为整数,是常规的数据,主要用来测试程序能否得到正确的结果,测试截图如下:

   

    第二组测试用例:数值范围-200到164,输出数量12,可以生成乘除法,整数除法无余数

    测试程序是否可以生成负数,对于负数参与运算,看程序能否获得正确的结果,测试结果如下所示:

   

    第三组测试用例:数值范围0-1,输出数量10,可生成乘除法,有余数

    测试除数为零时是否与可以继续执行,测试结果如下所示:

   

    程序出现错误,无法执行,查找程序源代码得知,为保证题目数量不重复,随机产生的每一个数都是不同的,因此,数值的范围必须大于输出的数量程序才可以后续执行。

    修改后的运行结果为:

   

    第四组测试用例:数值范围0-3,输出数量1,可以生成乘除法,无余数

    主要为测试数据除数为零是是否能继续执行,测试结果如下所示:

   

    程序出现未知的错误,程序中若除数为零时,加入了相应的处理语句,但是不知为何还存在错误,该错误暂时还没有得到解决。

   第五组测试数据:数值范围-123-4897,输出数量50,屏幕输出,不可生成乘除法,加减无负数

    主要为测试程序生成负数时,如何保证加减法无负数,测试结果如下所示:

   

    第六组测试数据:数值范围-465 到-12,输出数量5 ,保存到文件,不可生成乘除法,加减有负数

    主要测试程序最后的结果能否保存到文件,测试结果如下所示:

   

   

posted @ 2016-03-13 16:46  鹄立丹香  阅读(195)  评论(1编辑  收藏  举报