一、题目要求
我们在刚开始上课的时候介绍过一个小学四则运算自动生成程序的例子,请实现它,要求:
* 能够自动生成四则运算练习题
* 可以定制题目数量
* 用户可以选择运算符
* 用户设置最大数(如十以内、百以内等)
* 用户选择是否有括号、是否有小数
* 用户选择输出方式(如输出到文件、打印机等)
* 最好能提供图形用户界面(根据自己能力选做,以完成上述功能为主)
二、代码地址
左面是领航员,右面是驾驶员。
驾驶员:高放 Coding地址
领航员:关伟琦 博客地址
三、开发
3.1开发工具
开发环境:visual studio 2019
3.2功能实现
3.2.1定制题目数量
void menu1()
{
system(" echo.");
showMenu();
cout << "第1步:请设置题目数量 <1-1000> :" << endl;
cin >> Arithmetic_number;
while ((Arithmetic_number > 1000) || (Arithmetic_number < 1))
{
cout << "您设置的题目数目不符合要求(太多/太少)。 < 1 - 1000 > " << endl;
cout << endl;
cout << "请按确认键重新输入,谢谢!" << endl;
system("Pause >nul");
cin >> Arithmetic_number;
}
}
3.2.2选择运算符
void menu2()
{
system(" echo.");
cout << "第2步:请选择运算符 :" << endl;
cin >> Arithmetic_operators;
}
3.2.3设置最大数
void menu3()
{
system("echo.");
cout << "第3步:请设置最大数 <1-1000> :" << endl;
cin >> Arithmetic_max;
while ((Arithmetic_max > 1000) || (Arithmetic_max < 1))
{
cout << "您设置的最大数不符合要求(太大/太小)。 < 1 - 1000 > " << endl;
cout << endl;
cout << "请按确认键重新输入,谢谢!" << endl;
system("Pause >nul");
cin >> Arithmetic_max;
}
}
3.2.5选择是否有小数
void menu4()
{
system("echo.");
cout << "第4步:请选择是否有小数:(输入 <0> 生成整数 , 输入 <1> 生成小数) " << endl;
cin >> Arithmetic_ifdecimal;
while ((Arithmetic_ifdecimal != 0) && (Arithmetic_ifdecimal != 1))
{
cout << "您输入的数不符合要求。(输入 <0> 生成整数 , 输入 <1> 生成小数) " << endl;
cout << endl;
cout << "请按确认键重新输入!" << endl;
system("Pause >nul");
cin >> Arithmetic_ifdecimal;
}
}
3.2.5选择是否有括号
void menu5()
{
system("echo.");
cout << "第5步:请选择是否有括号:(输入 <0> 无括号 , 输入 <1> 有括号)" << endl;
cin >> Arithmetic_ifbrackets;
while ((Arithmetic_ifbrackets != 0) && (Arithmetic_ifbrackets != 1))
{
cout << "您输入的数不符合要求。(输入 <0> 无括号 , 输入 <1> 有括号) " << endl;
cout << endl;
cout << "请按确认键重新输入!" << endl;
system("Pause >nul");
cin >> Arithmetic_ifbrackets;
}
}
3.2.6选择输出方式
void menu6()
{
system("echo.");
cout << "第6步:请选择是否打印到文件:(输入 <0> 不打印(屏幕显示) , 输入 <1> 打印)" << endl;
cin >> Arithmetic_iffile;
while ((Arithmetic_iffile != 0) && (Arithmetic_iffile != 1))
{
cout << "您输入的数不符合要求。(输入 <0> 不打印(屏幕显示) , 输入 <1> 打印) " << endl;
cout << endl;
cout << "请按确认键重新输入!" << endl;
system("Pause >nul");
cin >> Arithmetic_iffile;
}
}
3.2.7四则运算生成程序
该部分包括两个分支,即生成并写入文件中,生成显示在屏幕中;
生成并写入文件中
void Arithmetic_Output_File()
{
cout << Arithmetic_number << endl;
fopen_s(&fp,FILE_PATH, "at+");
if (fp != NULL)
{
fprintf(fp, "\n");
fprintf(fp, "+----------------以下为*小学四则运算自动生成程序*所生成的四则运算练习题----------------+\n");
for (int i = 0; i < Arithmetic_number; ++i)
{
//随机生成四个整数
int number1 = rand() % Arithmetic_max;
int number2 = rand() % Arithmetic_max;
int number3 = rand() % Arithmetic_max;
int number4 = rand() % Arithmetic_max;
//随机生成四个小数
float number5 = (float)rand() / Arithmetic_max;
float number6 = (float)rand() / Arithmetic_max;
float number7 = (float)rand() / Arithmetic_max;
float number8 = (float)rand() / Arithmetic_max;
//随机生成三个运算符
int operation1 = rand() % Arithmetic_operation;
int operation2 = rand() % Arithmetic_operation;
int operation3 = rand() % Arithmetic_operation;
char cur_operation1 = Arithmetic_operators[operation1];
char cur_operation2 = Arithmetic_operators[operation2];
char cur_operation3 = Arithmetic_operators[operation3];
//随机产生括号()
int barcket1 = rand() % Arithmetic_bracket;
char cur_barckets1 = Arithmetic_brackets[barcket1];
char cur_barckets2 = Arithmetic_brackets[barcket1 + 2];
if (Arithmetic_ifdecimal) //判断是否有小数
{
if (Arithmetic_ifbrackets) //判断是否有括号
{
fprintf(fp, "NO. %2d : %c %.2f %c %.2f %c %c %.2f %c %.2f = \n", i, cur_barckets1, number5, cur_operation1, number6, cur_barckets2, cur_operation2, number7, cur_operation3, number8);
}
else
{
fprintf(fp, "NO. %2d : %.2f %c %.2f %c %.2f %c %.2f = \n", i, number5, cur_operation1, number6, cur_operation2, number7, cur_operation3, number8);
}
}
else
{
if (Arithmetic_ifbrackets)
{
fprintf(fp, "NO. %2d : %c %d %c %d %c %c %d %c %d = \n", i, cur_barckets1, number1, cur_operation1, number2, cur_barckets2, cur_operation2, number3, cur_operation3, number4);
}
else
{
fprintf(fp, "NO. %2d : %d %c %d %c %d %c %d = \n", i, number1, cur_operation1, number2, cur_operation2, number3, cur_operation3, number4);
}
}
}
}
else
{
perror(FILE_PATH);
exit(-1);
}
fprintf(fp, "+--------------------------------------------------------------------------------------+\n");
fprintf(fp, "\n");
}
生成显示在屏幕上
void Arithmetic_Output_Screen()
{
cout << "+----------------以下为*小学四则运算自动生成程序*所生成的四则运算练习题----------------+" << endl;
for (int i = 0; i < Arithmetic_number; ++i)
{
//随机生成四个整数
int number1 = rand() % Arithmetic_max;
int number2 = rand() % Arithmetic_max;
int number3 = rand() % Arithmetic_max;
int number4 = rand() % Arithmetic_max;
//随机生成四个小数
float number5 = (float)rand() / Arithmetic_max;
float number6 = (float)rand() / Arithmetic_max;
float number7 = (float)rand() / Arithmetic_max;
float number8 = (float)rand() / Arithmetic_max;
//随机生成三个运算符
int operation1 = rand() % Arithmetic_operation;
int operation2 = rand() % Arithmetic_operation;
int operation3 = rand() % Arithmetic_operation;
char cur_operation1 = Arithmetic_operators[operation1];
char cur_operation2 = Arithmetic_operators[operation2];
char cur_operation3 = Arithmetic_operators[operation3];
//随机产生括号()
int barcket1 = rand() % Arithmetic_bracket;
char cur_barckets1 = Arithmetic_brackets[barcket1];
char cur_barckets2 = Arithmetic_brackets[barcket1 + 2];
if (Arithmetic_ifdecimal)
{
if (Arithmetic_ifbrackets)
{
cout << "NO." << i << " : " << cur_barckets1 << number5 << " " << cur_operation1 << " " << number6 << cur_barckets2 << " " << cur_operation2 << " " << number7 << " " << cur_operation3 << " " << number8 << "=" << endl;
}
else
{
cout << "NO." << i << " : " << number5 << " " << cur_operation1 << " " << number6 << " " << cur_operation2 << " " << number7 << " " << cur_operation3 << " " << number8 << "=" << endl;
}
}
else
{
if (Arithmetic_ifbrackets)
{
cout << "NO." << i << " : " << cur_barckets1 << number1 << " " << cur_operation1 << " " << number2 << cur_barckets2 << " " << cur_operation2 << " " << number3 << " " << cur_operation3 << " " << number4 << "=" << endl;
}
else
{
cout << "NO." << i << " : " << number1 << " " << cur_operation1 << " " << number2 << " " << cur_operation2 << " " << number3 << " " << cur_operation3 << " " << number4 << "=" << endl;
}
}
}
cout << "+--------------------------------------------------------------------------------------+" << endl;
}
四、运行结果
运行结果
五、总结
在本次结对编程中,我担任的角色是驾驶员,我的领航员是关伟琦。这已经是第二次合作写代码,我们配合很有默契,最后的完成了这个项目。这过程中我们也曾遇到过困难,但后来还是一起解决了。但这个项目我们完成的还不是很完美,由于我们之前都没有接触过图形化界面,也查找了很多资料,最后完成了图形化界面的功能,并力争把代码效率质量提高。
对领航员的评价:关伟琦同学很好的扮演了领航员角色,在写代码之前他就已经为我准备了很多需要的资料,包括网上和书上的很多相关内容,为我们的后序编程提供了很大的帮助。正因如此,我们的代码质量大大提高,因为时间有限,这样的合作模式也提高了完成效率。