四则运算生成程序(驾驶员)
四则运算生成程序(驾驶员)
领航员:张国成 博客地址
博主上次作业审查代码写的就是领航员的大整数运算,大约1000位左右的运算并包括取模运算。随机生成算式在那篇代码里只是小儿科,用来显示函数功能的附属品。
一、题目要求:
我们在刚开始上课的时候介绍过一个小学四则运算自动生成程序的例子,请实现它,要求:
1.能够自动生成四则运算练习题
2.可以定制题目数量
3.用户可以选择运算符
4.用户设置最大数(如十以内、百以内等)
5.用户选择是否有括号、是否有小数
6.用户选择输出方式(如输出到文件、打印机等)
7.最好能提供图形用户界面(根据自己能力选做,以完成上述功能为主)
二、分析功能--驾驶员
要求核心思想为随机,所以代核心函数为rand()。rand()可以生成0~RAND_MAX之间的一个随机数,对你所需要的数的广度取模加上最小数可以达到范围随机效果。
(rand() % (number_largest - number_smallest)) + number_smallest;
三、功能实现
1.总体
对于上述功能,设置如下变量:
/*变量数据*/
int subject_sum; //题目数
int subject_operation; //四则运算or二则运算
int subject_line_number; //行题目数
int subject_nowline_number; //当前行题目数
int subject_ifdecimal; //是否有小数
int subject_ifbrackets; //是否有扩号
int subject_iffile; //是否输出到文件
int number_smallest; //最小数
int number_largest; //最大数
FILE *file; //文件地址
char subject_operators[4][4] = {"+","-","*","/"}; //运算符号
设置如下函数及功能:
/*功能函数*/
void Menu(); //功能选择菜单
void Subeject_Output_Screen(); //屏幕输出函数
void Subject_Generate_Screen(); //屏幕生成函数
void Subeject_Output_File(); //文件输出函数
void Subject_Generate_File (); //文件生成函数
2.功能菜单
对于每一个要求做好临界点,记录并判断是否越界,函数如下:
void Menu () //功能选择菜单
{
printf ("请输入生成的题目数(1-10000):\n");
scanf ("%d", &subject_sum);
if (subject_sum < 1 || subject_sum > 10000)
{
printf ("输入错误!\n");
return;
}
printf ("请输入是否有乘除法:是输入1,否输入0\n");
scanf ("%d", &subject_operation);
if (subject_operation < 0 || subject_operation > 1)
{
printf ("输入错误!\n");
return;
}
if (subject_operation == 1)
subject_operation = 4;
else
subject_operation = 2;
printf ("请输入数的范围a,b(0-30000):a,b以空格隔开,保证a<b\n");
scanf ("%d%d", &number_smallest, &number_largest);
if (number_smallest < 0 || number_largest > 30000 || number_smallest >= number_largest)
{
printf ("输入错误!\n");
return;
}
printf ("请输入是否有小数:是输入1,否输入0\n");
cin >> subject_ifdecimal;
if (subject_ifdecimal < 0 || subject_ifdecimal > 1)
{
printf ("输入错误!\n");
return;
}
printf ("请输入是否有括号:是输入1,否输入0\n");
scanf ("%d", &subject_ifbrackets);
if (subject_ifbrackets < 0 || subject_ifbrackets > 1)
{
printf ("输入错误!\n");
return;
}
printf ("请输入每行的题目数(1-5):\n");
scanf ("%d", &subject_line_number);
if (subject_line_number < 1 || subject_line_number > 5)
{
printf ("输入错误!\n");
return;
}
printf ("请输入是否输出到文件:是输入1,否输入0\n");
scanf ("%d", &subject_iffile);
if (subject_iffile < 0 || subject_iffile > 1)
{
printf ("输入错误!\n");
return;
}
if(!subject_iffile)
Subeject_Output_Screen();
else
Subeject_Output_File();
}
3.输出函数
调用算式生成函数,对于输出的算式控制好每行的数目,代码如下:
void Subeject_Output_Screen() //屏幕输出函数
{
printf ("题目生成:\n");
while(subject_sum--)
{
subject_nowline_number++;
Subject_Generate_Screen();
printf("=");
if(subject_nowline_number == subject_line_number)
{
printf ("\n");
subject_nowline_number = 0;
}
else
printf (" ");
}
}
4.算式生成函数
调用rand()函数,随机生成数据,对于是否有小数进行分类输出。小数生成方法为,随机一个0-6的数,作为10.0的幂,以生成的整数作为被除数相除即可得到小数。代码如下:
void Subject_Generate_Screen () //屏幕生成函数
{
/*随机生成数字*/
int number_first = (rand() % (number_largest - number_smallest)) + number_smallest;
int number_second = (rand() % (number_largest - number_smallest)) + number_smallest;
int number_last = (rand() % (number_largest - number_smallest)) + number_smallest;
/*随机生成运算符*/
int number_operator_first = rand() % subject_operation;
int number_operator_second = rand() % subject_operation;
/*是否有括号*/
int number_brackets = 0;
if(subject_ifbrackets == 1)
number_brackets = rand() % 2;
if (!subject_ifdecimal)
{
printf ("%3d %s ", number_first, subject_operators[number_operator_first]);
if (number_brackets)
printf ("(");
else
printf (" ");
printf ("%3d %s %3d", number_second, subject_operators[number_operator_second], number_last);
if (number_brackets)
printf (")");
else
printf (" ");
}
else
{
printf ("%5g %s ", number_first + (rand()%100) / 100.0, subject_operators[number_operator_first]); //将整数型转换成浮点型
if (number_brackets)
printf ("(");
else
printf (" ");
printf ("%5g %s %5g ", number_second + (rand()%100) / 100.0, subject_operators[number_operator_second], number_last + (rand()%100) / 100.0);
if (number_brackets)
printf (")");
else
printf (" ");
}
}
5.文件输出
仅仅对于文件是否存在进行判断,输出方式改为fprintf,其余不变,判断代码片段如下:
char file_name[100];
printf ("请输入文件名:\n");
scanf ("%s", file_name);
if((file=fopen(file_name,"w"))==NULL)
{
printf("文件打开失败!\n");
return ;
}
四、功能实现样例