四则运算2
题目要求:
在第一次原有的功能的基础上增加以下功能:
1.避免题目的重复
2.可定制(可定制题目数量\打印方式)
3.控制参数的生成:
a.是否允许乘除发的生成
b.是否允许有括号的生成(最多支持十个数参与运算)
c.可以设定数值范围的范围
d.是否负数参与运算
e.除法是否有余数
解决方法:
1.随机数生成之后并不能满足现在的要求,所以需要转成string。通过string的可加性输出来实现。
2.乘除法的要求以及数值范围的要求较为简单,在判断(0 or 1)的基础上分类辨别。
3.括号实现最为关键:
1.通过循环进行括号嵌套;
int shu = (rand() % (8) + 2);
3.每一次循环执行四种情况下的操作,具体查看代码。首先考虑已形成的生成式变量shi是否为空,再其次考虑在是否有括号的情况下加入生成。
4.通过主函数调用及其具体输出环境的提示实现。
代码:
//2016.3.15
//石家庄铁道大学
#include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>
#include<time.h>
#include <sstream>
using namespace std;
string fuhao(int v)
{
string c;
int n;
if (v)
{
n = (rand() % 100) % 4;
if (n == 0) c = '+';
else if (n == 1) c = '-';
else if (n == 2) c = '*';
else c = '/';
return c;
}
else
{
n = (rand() % 20) % 2;
if (n == 0) c = '+';
else c = '-';
return c;
}
}
string suanshi(int v, int c, int y) //生成算式
{
int n1, n2;
int shu = (rand() % (8) + 2);
string str1, str2;
string shi = "";
int sui;
ofstream fout;
for (int i = 1; i <= shu; i++)
{
n1 = (rand() % (y)) + 1;
str1 = to_string(n1);
n2 = (rand() % (y)) + 1;
str2 = to_string(n2);
if (c)
{
sui = (rand() % (4)) + 1;
switch (sui)
{
case 1:
{
if (shi == "")
{
shi = str1 + fuhao(v) + str2;
}
else
{
shi = str1 + fuhao(v) + shi;
}
}
break;
case 2:
{
if (shi == "")
{
shi = str2 + fuhao(v) + str1;
}
else
{
shi = shi + fuhao(v) + str1;
}
}
break;
case 3:
{
if (shi == "")
{
shi = "(" + str1 + fuhao(v) + str2 + ")";
}
else
{
shi = "(" + str1 + fuhao(v) + shi + ")";
}
}
break;
case 4:
{
if (shi == "")
{
shi = "(" + str2 + fuhao(v) + str1 + ")";
}
else
{
shi = "(" + shi + fuhao(v) + str1 + ")";
}
}
break;
}
}
else
{
sui = (rand() % (2)) + 1;
switch (sui)
{
case 1:
{
if (shi == "")
{
shi = str1 + fuhao(v) + str2;
}
else
{
shi = str1 + fuhao(v) + shi;
}
}
break;
case 2:
{
if (shi == "")
{
shi = str2 + fuhao(v) + str1;
}
else
{
shi = shi + fuhao(v) + str1;
}
}
break;
}
}
}
return shi + "=";
}
void main()
{
int v, kuohao, fanwei;
int c;
int wenjian;
ofstream fl;
cout << "请输入一共有多少个算式:" << endl;
cin >> c;
cout << "是否输出到文件:(1:是, 0:否)" << endl;
cin >> wenjian;
cout << "有无除法:(1:有,0:无)" << endl;
cin >> v;
cout << "是否有括号:(1:有,0:无)" << endl;
cin >> kuohao;
cout << "请输入数值的范围:" << endl;
cout << "1-";
cin >> fanwei;
cout << endl;
if (wenjian)
{
fl.open("file.txt");
fl << "道四则运算题如下:" << endl;
for (int i = 0; i < c; i++)
{
fl << suanshi(v, kuohao, fanwei) << endl;
cout << suanshi(v, kuohao, fanwei) << endl;
}
}
else
{
for (int i = 0; i < c; i++)
{
cout << suanshi(v, kuohao, fanwei) << endl;
}
}
}
结果: