-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMathsGen.cpp
165 lines (149 loc) · 3.09 KB
/
MathsGen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Maths Exercise Generation by @wangdino
Version: 0.9.0
*/
#include <iostream>
#include <fstream>
#include <chrono>
#include <random>
#include <iomanip>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int num(int nmin, int nmax); // Generate a random integer in a certain range
char oper(int opertype); // Input: 0=random; 1=addition; 2=deduction
bool neg(int num1, int num2); // Check whether solution from a deduction is negative or not: 1=neg; 0=pos(incl.0)
vector<int> ReadConfig(string cfgname); // Function to read in configurations
int main()
{
vector<int> cfg = ReadConfig("Maths.ini");
int exer = cfg[0];
int opertype = cfg[1];
int max1 = cfg[2];
int min1 = cfg[3];
int max2 = cfg[4];
int min2 = cfg[5];
char const* fname = "Output.txt";
ofstream exp(fname);
if (exp.is_open())
{
for (int i = 0; i < exer; i++)
{
int num1 = num(min1, max1);
int num2 = num(min2, max2);
char op = oper(opertype);
if (op=='-' && neg(num1,num2))
{
int swap = num1;
num1 = num2;
num2 = swap;
}
exp << setw(2) << num1 << setw(2) << op << setw(3) << num2 << " =\n";
}
exp.close();
cout << "File generated successfully." << endl;
}
else
{
cout << "File operation error!" << endl;
}
return 0;
}
int num(int nmin, int nmax)
{
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
default_random_engine generator(seed);
uniform_int_distribution<int> distribution(nmin, nmax);
int num = distribution(generator);
return num;
}
char oper(int opertype)
{
if (opertype != 1 && opertype != 2)
{
opertype = num(1, 2);
}
char op;
if (opertype == 1)
{
op = '+';
}
else if (opertype == 2)
{
op = '-';
}
return op;
}
bool neg(int num1, int num2)
{
if (num1 - num2 >= 0)
{
return false;
}
return true;
}
vector<int> ReadConfig(string cfgname)
{
vector<int> out;
int exer = 100, opertype = 0, max1 = 19, min1 = 0, max2 = 9, min2 = 0, tmax = 0;
if (cfgname == "")
{
cfgname = "Maths.ini";
}
ifstream cfg_in(cfgname);
if (cfg_in.is_open() == false)
{
out.push_back(exer);
out.push_back(opertype);
out.push_back(max1);
out.push_back(min1);
out.push_back(max2);
out.push_back(min2);
out.push_back(tmax);
return out;
}
string cfg_line;
istringstream param;
while (getline(cfg_in, cfg_line))
{
param.str(cfg_line.substr(cfg_line.find("=") + 1));
if (cfg_line.find("No. of Exercises") != string::npos)
{
param >> exer;
}
else if (cfg_line.find("Operation Type") != string::npos)
{
param >> opertype;
}
else if (cfg_line.find("Num1 Max") != string::npos)
{
param >> max1;
}
else if (cfg_line.find("Num1 Min") != string::npos)
{
param >> min1;
}
else if (cfg_line.find("Num2 Max") != string::npos)
{
param >> max2;
}
else if (cfg_line.find("Num2 Min") != string::npos)
{
param >> min2;
}
else if (cfg_line.find("Result Max") != string::npos)
{
param >> tmax;
}
param.clear();
}
out.push_back(exer);
out.push_back(opertype);
out.push_back(max1);
out.push_back(min1);
out.push_back(max2);
out.push_back(min2);
out.push_back(tmax);
return out;
}