个人项目之 “二柱子四则运算”升级版
果然……还有后续故事……
可怜的二柱子,老师对他的自动出题系统提出了新的要求:(当然,要求是在实现上次的题目基础上的~~)
1、题目可以避免重复;
2、可以定制(打印方式/题目数量)
3、可以控制下列参数:是否有乘除法、是否有括号(最多可以支持10个数参与运算)、数值范围、加减有无负数、除法有无余数等等。。。
一、项目思路
我们要养成在写代码之前仔细分析项目,锻炼严谨的思路可以减少我们出错。为写这个程序我的思路如下:
1、首先为了避免重复,最基本的思路是先把题目存储下来,然后再逐个比较。
我们可以利用数据结构中的链表和指针进行比较和存储,但是我数据结构学的实在是太差,真心一点都回忆不出来(╥╯^╰╥)
本来以为我完不成这个要求了,后来向我同样也是学计算机的高中同学请教了一下,她给了我一种思路。建立一个二维数组来存放操作数和操作符,数组有n行(生成的算式数量)、3列(分别存放第一个操作数、第二个操作数和操作符所代表的数字),然后每次利用随机数生成函数产生随机数和随机的算符的时候,逐行去数组里比较,如果有完全符合的就重新产生随机数,否则就讲数据存放起来供下次比较。
(P.S我的程序基本上是用这种方法查重的,但是后来经过和室友的讨论,我发现这种方法貌似进行二元运算比较方便,但是如果进行含有括号的多元运算就比较吃力。我在想可不可以尝试用string声明一维数组进行存储,每一行代表一个算式。因为时间真的很有限,我没能尝试这种方法,有时间我一定一定试试看。)
2、用户选择打印方式:可以事先让用户选择要不要保存到文件里。要实现这个需要C++的文件输入输出流,因为不常用有点忘,于是我又翻阅了一下大一下的C++程序,可以基本实现这个功能,但是还需要改动和修改。
3、数值范围:本来是打算使用:由rand()%(b-a+1)+a实现范围为a~b的随机数 这种方法,但是在测试时出现了不能产生负数的问题,所以不得不稍加修改,改动的内容参见下一篇博客,在单元测试中我会详细说明改动的代码和原因,这里不再赘余。:)
4、除法是否含有余数:对于这个要求我们特意查了百度对于“余数”的定义,所谓余数,是指整数之间计算时的数据,因此不用考虑真分数的除法是否有余数的问题,无形中给我们减轻了不少的负担。
5、加减法有无负数:对于这个问题,每个人的看法不同。我所理解的是:如果不含负数,则每一个操作数和操作结果中都不含负数。因此我们要考虑每一个操作数的正负问题(因为生成的随机数可以随机产生负数),还要考虑减法的被减数是否大于减数的问题。当然,对于真分数要考虑分子小于分母,分母不能为0等问题。
6、此外,考虑到这个程序的代码量实在是太大了,因此不得不考虑用函数来封装一些功能的模块。
7、最后,还要注意各种用户输入数据时的异常处理问题。(当然,明天的单元测试更是重点要考虑)
二、实验的源代码如下
炒鸡长……(@﹏@)~,主要是逻辑思维真的不行。要不是老师说的事先列好思路,估计我就被自己绕进去了╮(╯_╰)╭
如果有时间我应该再好好修改自己的程序,虽然基本能够实现功能,但是像老师说的代码的可利用性和编程效率太低了,唉……
老师们,勿喷……我在努力的生长……( ̄ ‘i  ̄;)
下面进行分函数的介绍和展示(把下面的模块连接起来就是整个项目的源代码~)
函数的声明:
1 //Zhao Ziyin,2016/3/11,Four arithmetics(2) 2 3 #include<iostream> 4 #include<time.h> 5 #include<string> 6 #include<fstream> 7 using namespace std; 8 //#define N 9 #define random(x) (rand()%x) 10 int MM[10000][3];//临时记录算式的二维数组
查重的函数体,如下:
1 /*查重的函数*/ 2 int WheReap(int op1,int op2,int s_n,int c) 3 { 4 //int MM[10000][3];//临时记录算式的二维数组 5 int i; 6 bool flag=true; 7 for (i = 0; i < c; i++) 8 { 9 if (MM[i][0] == op1 && MM[i][1] == s_n && MM[i][2] == op2) 10 { 11 flag = false;//出现了重复算式 12 break; 13 } 14 } 15 if (flag)//如果没有出现重复,随机数可以赋值 16 { 17 MM[c][0] = op1; 18 MM[c][1] = s_n; 19 MM[c][2] = op2; 20 } 21 return flag; 22 }
若用户只选择加减法运算的函数体,如下:
其中包括“是否含有真分数” “运算是否含有负数”等等……
1 /*加减法运算*/ 2 void AddSub(int n, int r1,int r2) 3 { 4 /*srand((int)time(0));*/ 5 int r=r2-r1, one, two,three,four, sign_num,type, count = 0;//count记录产生的第几个算式 6 char sign; 7 int choose1,choose2; 8 bool flag_one = false, flag_two = false,flag_three=false,flag_four=false; 9 loop1:cout << "是否进行带真分数的练习?请选择:1、有 2、无" << endl; 10 cin >> choose1; 11 if (choose1 != 1 && choose1 != 2) 12 { 13 cout << "没有您选择的功能,请重新选择:" << endl; 14 goto loop1; 15 } 16 else if (choose1 == 1)//含有真分数的题目 17 { 18 loop2: cout << "加减法运算中有无负数?请选择:1、有 2、无" << endl; 19 cin >> choose2; 20 if (choose2 != 1 && choose2 != 2) 21 { 22 cout << "没有您选择的功能,请重新选择:" << endl; 23 goto loop2; 24 } 25 cout << "系统将为您出" << n << "道加减法的运算题目:" << endl; 26 if (choose2 == 1)//可以含有负数 27 { 28 srand((int)time(0)); 29 while (count < n) 30 { 31 one = r1 + rand() % r; 32 two = r1 + rand() % r; 33 three = r1 + rand() % r; 34 four = r1 + rand() % r; 35 if (one < 0)//记录第一个操作数为负数(true) 36 flag_one = true; 37 if (two < 0)//记录第二个操作数为负数(true) 38 flag_two = true; 39 if (three < 0)//记录第三个操作数为负数(true) 40 flag_three = true; 41 if (four < 0)//记录第四个操作数为负数(true) 42 flag_four = true; 43 //随机生成运算符 44 sign_num = rand() % 2; 45 switch (sign_num) 46 { 47 case(0) : sign = '+'; break; 48 case(1) : sign = '-'; break; 49 } 50 //随机生成运算数的类型 51 type = rand() % 4; 52 switch (type) 53 { 54 case(0) ://两个整数 55 { 56 if (!WheReap(one, two, sign_num, count))//如果有重复 57 count--; 58 else//如果没有重复 59 { 60 if (flag_one && !flag_two)//如果op1为负,op2为正 61 cout << count << "、 (" << one << ") " << sign << " " << two << endl; 62 if (!flag_one && flag_two)//如果op1为正,op2为负 63 cout << count << "、 " << one << " " << sign << " (" << two << ")" << endl; 64 if (flag_one && flag_two)//如果op1、op2都为负 65 cout << count << "、 (" << one << ") " << sign << " (" << two << ")" << endl; 66 } 67 break; 68 } 69 case(1) ://op1为真分数 70 { 71 if (one > two)//第一个真分数的分子要小于分母 72 { 73 int tmp; 74 tmp = one; 75 one = two; 76 two = tmp; 77 } 78 bool flag_OT = false; 79 if ((one<0 && two>0) || (one > 0 && two < 0))//记录第一个真分数为负数(true) 80 flag_OT = true; 81 if (two!=0)//分母不为0 82 { 83 if (flag_OT && !flag_three)//如果op1为负,op2为正 84 cout << count << "、 (" << one << "/" << two << ") " << sign << " " << three << endl; 85 if (!flag_OT && flag_three)//如果op1为正,op2为负 86 cout << count << "、 " << one << "/" << two << " " << sign << " (" << three << ")" << endl; 87 if (flag_OT && flag_three)//如果op1、op2都为负 88 cout << count << "、 (" << one << "/" << two << ") " << sign << " (" << three << ")" << endl; 89 } 90 break; 91 92 } 93 case(2) ://op2为真分数 94 { 95 if (three > four)//第二真分数的分子要小于分母 96 { 97 int tmp; 98 tmp = three; 99 three = four; 100 four = tmp; 101 } 102 bool flag_TF = false; 103 if ((three<0 && four>0) || (three > 0 && four < 0))//记录第二个真分数为负数(true) 104 flag_TF = true; 105 if (four!=0)//分母不为0 106 { 107 if (flag_one && !flag_TF)//如果op1为负,op2为正 108 cout << count << "、 (" << one << ") " << sign << " " << three << "/" << four << endl; 109 if (!flag_one && flag_TF)//如果op1为正,op2为负 110 cout << count << "、 " << one << " " << sign << " (" << three << "/" << four << ")" << endl; 111 if (flag_one && flag_TF)//如果op1、op2都为负 112 cout << count << "、 (" << one << ") " << sign << " (" << three << "/" << four << ")" << endl; 113 } 114 break; 115 } 116 case(3) ://两个数都为真分数 117 { 118 if (one > two)//第一个真分数的分子要小于分母 119 { 120 int tmp; 121 tmp = one; 122 one = two; 123 two = tmp; 124 } 125 if (three > four)//第二真分数的分子要小于分母 126 { 127 int tmp; 128 tmp = three; 129 three = four; 130 four = tmp; 131 } 132 bool flag_OT = false; 133 if ((one<0 && two>0) || (one > 0 && two < 0))//记录第一个真分数为负数(true) 134 flag_OT = true; 135 136 bool flag_TF = false; 137 if ((three<0 && four>0) || (three > 0 && four < 0))//记录第二个真分数为负数(true) 138 flag_TF = true; 139 140 if (two!=0 &&four!=0)//分母不为0 141 { 142 if (flag_OT && !flag_TF)//如果op1为负,op2为正 143 cout << count << "、 (" << one << "/" << two << ") " << sign << " " << three << "/" << four << endl; 144 if (!flag_OT && flag_TF)//如果op1为正,op2为负 145 cout << count << "、 " << one << "/" << two << " " << sign << " (" << three << "/" << four << ")" << endl; 146 if (flag_OT && flag_TF)//如果op1、op2都为负 147 cout << count << "、 (" << one << "/" << two << ") " << sign << " (" << three << "/" << four << ")" << endl; 148 } 149 break; 150 } 151 152 } 153 count++; 154 } 155 } 156 if (choose2 == 2)//不可以含有负数 157 { 158 srand((int)time(0)); 159 while (count < n) 160 { 161 one = r1 + rand() % r; 162 two = r1 + rand() % r; 163 three = r1 + rand() % r; 164 four = r1 + rand() % r; 165 //负数变为相反数 166 if (one < 0) 167 one = -one; 168 if (two < 0) 169 two = -two; 170 if (three < 0) 171 three = -three; 172 if (four < 0) 173 four = -four; 174 175 //随机生成运算符 176 sign_num = rand() % 2; 177 switch (sign_num) 178 { 179 case(0) : sign = '+'; break; 180 case(1) : sign = '-'; break; 181 } 182 183 //随机生成运算数的类型 184 type = rand() % 4; 185 switch (type) 186 { 187 case(0) ://两个整数 188 { 189 if (sign == '-') 190 { 191 if (one < two)//被减数大于减数 192 { 193 int tmp; 194 tmp = one; 195 one = two; 196 two = tmp; 197 } 198 } 199 if (!WheReap(one, two, sign_num, count))//如果有重复 200 count--; 201 else//如果没有重复 202 cout << count << "、 " << one << " " << sign << " " << two << endl; 203 break; 204 } 205 case(1) ://op1为真分数 206 { 207 if (sign != '-') 208 { 209 if (one > two)//第一个真分数的分子要小于分母 210 { 211 int tmp; 212 tmp = one; 213 one = two; 214 two = tmp; 215 } 216 if (two != 0)//分母不为0 217 cout << count << "、 " << one << "/" << two << " " << sign << " " << three << endl; 218 } 219 break; 220 } 221 case(2) ://op2为真分数 222 { 223 if (three > four)//第二真分数的分子要小于分母 224 { 225 int tmp; 226 tmp = three; 227 three = four; 228 four = tmp; 229 } 230 if (four != 0) //分母不为0 231 cout << count << "、 " << one << " " << sign << " " << three << "/" << four << endl; 232 break; 233 } 234 case(3) ://两个数都为真分数 235 { 236 if (one > two)//第一个真分数的分子要小于分母 237 { 238 int tmp; 239 tmp = one; 240 one = two; 241 two = tmp; 242 } 243 if (three > four)//第二个真分数的分子要小于分母 244 { 245 int tmp; 246 tmp = three; 247 three = four; 248 four = tmp; 249 } 250 float OT, TF; 251 OT = one / two; 252 TF = three / four; 253 if (two != 0 && four != 0)//分母不为0 254 { 255 if (sign == '-')//减法被减数大于减数 256 { 257 if (OT<TF) 258 cout << count << "、 " << three << "/" << four << " " << sign << " " << one << "/" << two << endl; 259 else if (OT>TF) 260 cout << count << "、 " << one << "/" << two << " " << sign << " " << three << "/" << four << endl; 261 } 262 cout << count << "、 " << one << "/" << two << " " << sign << " " << three << "/" << four << endl; 263 } 264 break; 265 } 266 267 } 268 count++; 269 } 270 } 271 } 272 else if (choose1 == 2)//不含有真分数的题目 273 { 274 int choose3; 275 loop3: cout << "加减法运算中有无负数?请选择:1、有 2、无" << endl; 276 cin >> choose3; 277 if (choose3 != 1 && choose3 != 2) 278 { 279 cout << "没有您选择的功能,请重新选择:" << endl; 280 goto loop3; 281 } 282 cout << "系统将为您出" << n << "道加减法的运算题目:" << endl; 283 if (choose3 == 1)//包含负数 284 { 285 srand((int)time(0)); 286 while (count < n) 287 { 288 one = r1 + rand() % r; 289 two = r1 + rand() % r; 290 if (one < 0)//记录第一个操作数为负数(true) 291 flag_one = true; 292 if (two < 0)//记录第二个操作数为负数(true) 293 flag_two = true; 294 //随机生成运算符 295 sign_num = rand() % 2; 296 switch (sign_num) 297 { 298 case(0) : 299 { 300 sign = '+'; 301 if (!WheReap(one, two, sign_num, count))//如果有重复 302 count--; 303 else//如果没有重复 304 { 305 if (flag_one && !flag_two)//如果op1为负,op2为正 306 cout << count << "、 (" << one << ") " << sign << " " << two << endl; 307 if (!flag_one && flag_two)//如果op1为正,op2为负 308 cout << count << "、 " << one << " " << sign << " (" << two << ")" << endl; 309 if (flag_one && flag_two)//如果op1、op2都为负 310 cout << count << "、 (" << one << ") " << sign << " (" << two << ")" << endl; 311 } 312 break; 313 } 314 case(1) : 315 { 316 sign = '-'; 317 if (!WheReap(one, two, sign_num, count))//如果有重复 318 count--; 319 else//如果没有重复 320 { 321 if (flag_one && !flag_two)//如果op1为负,op2为正 322 cout << count << "、 (" << one << ") " << sign << " " << two << endl; 323 if (!flag_one && flag_two)//如果op1为正,op2为负 324 cout << count << "、 " << one << " " << sign << " (" << two << ")" << endl; 325 if (flag_one && flag_two)//如果op1、op2都为负 326 cout << count << "、 (" << one << ") " << sign << " (" << two << ")" << endl; 327 } 328 break; 329 } 330 } 331 count++; 332 } 333 } 334 else if (choose3 == 2)//不包含负数 335 { 336 srand((int)time(0)); 337 while (count < n) 338 { 339 one = r1 + rand() % r; 340 two = r1 + rand() % r; 341 //负数变为相反数 342 if (one < 0) 343 one = -one; 344 if (two < 0) 345 two = -two; 346 //随机生成运算符 347 sign_num = rand() % 2; 348 switch (sign_num) 349 { 350 case(0) : 351 { 352 sign = '+'; 353 if (!WheReap(one, two, sign_num, count))//如果有重复 354 count--; 355 else//如果没有重复 356 cout << count << "、 " << one << " " << sign << " " << two << endl; 357 break; 358 } 359 case(1) : 360 { 361 sign = '-'; 362 if (one < two)//被减数大于减数 363 { 364 int tmp; 365 tmp = one; 366 one = two; 367 two = tmp; 368 } 369 if (!WheReap(one, two, sign_num, count))//如果有重复 370 count--; 371 else//如果没有重复 372 cout << count << "、 " << one << " " << sign << " " << two << endl; 373 break; 374 } 375 } 376 count++; 377 } 378 } 379 380 } 381 382 }
含有加减乘除四则运算的函数体部分,如下:
1 /*加减乘除运算*/ 2 void FourOper(int n, int r1, int r2) 3 { 4 int r = r2 - r1, one, two, three, four, sign_num, type, count = 0;//count记录产生的第几个算式 5 char sign; 6 int choose1, choose2; 7 bool flag_one = false, flag_two = false, flag_three = false, flag_four = false; 8 loop1:cout << "是否进行带真分数的练习?请选择:1、有 2、无" << endl; 9 cin >> choose1; 10 if (choose1 != 1 && choose1 != 2) 11 { 12 cout << "没有您选择的功能,请重新选择:" << endl; 13 goto loop1; 14 } 15 else if (choose1 == 1)//含有真分数的题目 16 { 17 loop2: cout << "除法运算中有无余数?请选择:1、有 2、无" << endl; 18 cin >> choose2; 19 if (choose2 != 1 && choose2 != 2) 20 { 21 cout << "没有您选择的功能,请重新选择:" << endl; 22 goto loop2; 23 } 24 cout << "系统将为您出" << n << "道按您定制的四则运算题目:" << endl; 25 26 srand((int)time(0)); 27 while (count < n) 28 { 29 one = r1 + rand() % r; 30 two = r1 + rand() % r; 31 three = r1 + rand() % r; 32 four = r1 + rand() % r; 33 34 //随机生成运算符 35 sign_num = rand() % 4; 36 switch (sign_num)//利用随机数随机产生运算符 37 { 38 case(0) ://加法运算 39 { 40 sign = '+'; 41 loop4: type = rand() % 4; 42 switch(type) 43 { 44 case(0) ://两个整数 45 { 46 if (!WheReap(one, two, sign_num, count))//如果有重复 47 count--; 48 else//如果没有重复 49 { 50 if (one < 0)//记录第一个操作数为负数(true) 51 flag_one = true; 52 if (two < 0)//记录第二个操作数为负数(true) 53 flag_two = true; 54 55 if (!flag_one && !flag_two)//如果op1、op2都为正 56 cout << count << "、 " << one << " " << sign << " " << two << endl; 57 if (flag_one && !flag_two)//如果op1为负,op2为正 58 cout << count << "、 (" << one << ") " << sign << " " << two << endl; 59 if (!flag_one && flag_two)//如果op1为正,op2为负 60 cout << count << "、 " << one << " " << sign << " (" << two << ")" << endl; 61 if (flag_one && flag_two)//如果op1、op2都为负 62 cout << count << "、 (" << one << ") " << sign << " (" << two << ")" << endl; 63 } 64 break; 65 } 66 case(1) ://op1为真分数 67 { 68 if (one > two)//第一个真分数的分子要小于分母 69 { 70 int tmp; 71 tmp = one; 72 one = two; 73 two = tmp; 74 } 75 if (one < 0)//记录第一个操作数为负数(true) 76 flag_one = true; 77 if (two < 0)//记录第二个操作数为负数(true) 78 flag_two = true; 79 if (three < 0)//记录第三个操作数为负数(true) 80 flag_three = true; 81 if (four < 0)//记录第四个操作数为负数(true) 82 flag_four = true; 83 bool flag_OT = false; 84 if ((one<0 && two>0) || (one > 0 && two < 0))//记录第一个真分数为负数(true) 85 flag_OT = true; 86 if (two != 0)//分母不为0 87 { 88 if (!flag_OT && !flag_three)//如果op1、op2都为正 89 cout << count << "、 " << one<<"/"<<two << " " << sign << " " << three << endl; 90 if (flag_OT && !flag_three)//如果op1为负,op2为正 91 cout << count << "、 (" << one << "/" << two << ") " << sign << " " << three << endl; 92 if (!flag_OT && flag_three)//如果op1为正,op2为负 93 cout << count << "、 " << one << "/" << two << " " << sign << " (" << three << ")" << endl; 94 if (flag_OT && flag_three)//如果op1、op2都为负 95 cout << count << "、 (" << one << "/" << two << ") " << sign << " (" << three << ")" << endl; 96 } 97 break; 98 } 99 case(2) ://op2为真分数 100 { 101 if (three > four)//第二真分数的分子要小于分母 102 { 103 int tmp; 104 tmp = three; 105 three = four; 106 four = tmp; 107 } 108 if (one < 0)//记录第一个操作数为负数(true) 109 flag_one = true; 110 if (two < 0)//记录第二个操作数为负数(true) 111 flag_two = true; 112 if (three < 0)//记录第三个操作数为负数(true) 113 flag_three = true; 114 if (four < 0)//记录第四个操作数为负数(true) 115 flag_four = true; 116 bool flag_TF = false; 117 if ((three<0 && four>0) || (three > 0 && four < 0))//记录第二个真分数为负数(true) 118 flag_TF = true; 119 if (four != 0)//分母不为0 120 { 121 if (!flag_one && !flag_TF)//如果op1、op2都为正 122 cout << count << "、 " << one << " " << sign << " " << three<<"/"<<four << endl; 123 if (flag_one && !flag_TF)//如果op1为负,op2为正 124 cout << count << "、 (" << one << ") " << sign << " " << three << "/" << four << endl; 125 if (!flag_one && flag_TF)//如果op1为正,op2为负 126 cout << count << "、 " << one << " " << sign << " (" << three << "/" << four << ")" << endl; 127 if (flag_one && flag_TF)//如果op1、op2都为负 128 cout << count << "、 (" << one << ") " << sign << " (" << three << "/" << four << ")" << endl; 129 } 130 break; 131 } 132 case(3) ://两个数都为真分数 133 { 134 if (one > two)//第一个真分数的分子要小于分母 135 { 136 int tmp; 137 tmp = one; 138 one = two; 139 two = tmp; 140 } 141 if (three > four)//第二真分数的分子要小于分母 142 { 143 int tmp; 144 tmp = three; 145 three = four; 146 four = tmp; 147 } 148 if (one < 0)//记录第一个操作数为负数(true) 149 flag_one = true; 150 if (two < 0)//记录第二个操作数为负数(true) 151 flag_two = true; 152 if (three < 0)//记录第三个操作数为负数(true) 153 flag_three = true; 154 if (four < 0)//记录第四个操作数为负数(true) 155 flag_four = true; 156 bool flag_OT = false; 157 if ((one<0 && two>0) || (one > 0 && two < 0))//记录第一个真分数为负数(true) 158 flag_OT = true; 159 160 bool flag_TF = false; 161 if ((three<0 && four>0) || (three > 0 && four < 0))//记录第二个真分数为负数(true) 162 flag_TF = true; 163 if (two != 0 && four != 0)//分母不为0 164 { 165 if (!flag_OT && !flag_TF)//如果op1、op2都为正 166 cout << count << "、 " << one <<"/"<<two<< " " << sign << " " << three << "/" << four << endl; 167 if (flag_OT && !flag_TF)//如果op1为负,op2为正 168 cout << count << "、 (" << one << "/" << two << ") " << sign << " " << three << "/" << four << endl; 169 if (!flag_OT && flag_TF)//如果op1为正,op2为负 170 cout << count << "、 " << one << "/" << two << " " << sign << " (" << three << "/" << four << ")" << endl; 171 if (flag_OT && flag_TF)//如果op1、op2都为负 172 cout << count << "、 (" << one << "/" << two << ") " << sign << " (" << three << "/" << four << ")" << endl; 173 } 174 break; 175 } 176 } 177 break; 178 } 179 case(1) : 180 { 181 sign = '-'; 182 goto loop4; 183 184 } 185 case(2) : 186 { 187 sign = '*'; 188 goto loop4; 189 } 190 case(3) : 191 { 192 sign = '/'; 193 if (one < two) 194 { 195 int tmp; 196 tmp = one; 197 one = two; 198 two = tmp; 199 } 200 if (one < 0)//记录第一个操作数为负数(true) 201 flag_one = true; 202 if (two < 0)//记录第二个操作数为负数(true) 203 flag_two = true; 204 205 if (two != 0) 206 { 207 int yu = one%two; 208 if (choose2 == 1)//除法运算含有余数 209 { 210 if (yu != 0) 211 { 212 if (!flag_one && !flag_two)//如果op1、op2都为正 213 cout << count << "、 " << one << " " << sign << " " << two << endl; 214 if (flag_one && !flag_two)//如果op1为负,op2为正 215 cout << count << "、 (" << one << ") " << sign << " " << two << endl; 216 if (!flag_one && flag_two)//如果op1为正,op2为负 217 cout << count << "、 " << one << " " << sign << " (" << two << ")" << endl; 218 if (flag_one && flag_two)//如果op1、op2都为负 219 cout << count << "、 (" << one << ") " << sign << " (" << two << ")" << endl; 220 } 221 } 222 else if (choose2 == 2)//除法运算不含余数 223 { 224 if (yu == 0) 225 { 226 if (!flag_one && !flag_two)//如果op1、op2都为正 227 cout << count << "、 " << one << " " << sign << " " << two << endl; 228 if (flag_one && !flag_two)//如果op1为负,op2为正 229 cout << count << "、 (" << one << ") " << sign << " " << two << endl; 230 if (!flag_one && flag_two)//如果op1为正,op2为负 231 cout << count << "、 " << one << " " << sign << " (" << two << ")" << endl; 232 if (flag_one && flag_two)//如果op1、op2都为负 233 cout << count << "、 (" << one << ") " << sign << " (" << two << ")" << endl; 234 } 235 } 236 } 237 break; 238 } 239 } 240 count++; 241 } 242 } 243 244 else if (choose1 == 2)//不含有真分数的题目 245 { 246 int choose3; 247 loop3: cout << "除法运算中有无余数?请选择:1、有 2、无" << endl; 248 cin >> choose3; 249 if (choose3 != 1 && choose3 != 2) 250 { 251 cout << "没有您选择的功能,请重新选择:" << endl; 252 goto loop3; 253 } 254 cout << "系统将为您出" << n << "道按您定制的四则运算题目:" << endl; 255 256 srand((int)time(0)); 257 while (count < n) 258 { 259 one = r1 + rand() % r; 260 two = r1 + rand() % r; 261 if (one < 0)//记录第一个操作数为负数(true) 262 flag_one = true; 263 if (two < 0)//记录第二个操作数为负数(true) 264 flag_two = true; 265 //随机生成运算符 266 sign_num = rand() % 4; 267 switch (sign_num) 268 { 269 case(0) : 270 { 271 sign = '+'; 272 loop5: if (one < 0)//记录第一个操作数为负数(true) 273 flag_one = true; 274 if (two < 0)//记录第二个操作数为负数(true) 275 flag_two = true; 276 if (!WheReap(one, two, sign_num, count))//如果有重复 277 count--; 278 else//如果没有重复 279 { 280 if (!flag_one && !flag_two)//如果op1、op2都为正 281 cout << count << "、 " << one << " " << sign << " " << two << endl; 282 if (flag_one && !flag_two)//如果op1为负,op2为正 283 cout << count << "、 (" << one << ") " << sign << " " << two << endl; 284 if (!flag_one && flag_two)//如果op1为正,op2为负 285 cout << count << "、 " << one << " " << sign << " (" << two << ")" << endl; 286 if (flag_one && flag_two)//如果op1、op2都为负 287 cout << count << "、 (" << one << ") " << sign << " (" << two << ")" << endl; 288 } 289 break; 290 } 291 case(1) : 292 { 293 sign = '-'; 294 goto loop5; 295 } 296 case(2) : 297 { 298 sign = '*'; 299 goto loop5; 300 } 301 case(3) : 302 { 303 sign = '/'; 304 if (one < two) 305 { 306 int tmp; 307 tmp = one; 308 one = two; 309 two = tmp; 310 } 311 if (one < 0)//记录第一个操作数为负数(true) 312 flag_one = true; 313 if (two < 0)//记录第二个操作数为负数(true) 314 flag_two = true; 315 if (two != 0) 316 { 317 int yu = one%two; 318 if (choose2 == 1)//除法运算含有余数 319 { 320 if (yu != 0) 321 { 322 if (!flag_one && !flag_two)//如果op1、op2都为正 323 cout << count << "、 " << one << " " << sign << " " << two << endl; 324 if (flag_one && !flag_two)//如果op1为负,op2为正 325 cout << count << "、 (" << one << ") " << sign << " " << two << endl; 326 if (!flag_one && flag_two)//如果op1为正,op2为负 327 cout << count << "、 " << one << " " << sign << " (" << two << ")" << endl; 328 if (flag_one && flag_two)//如果op1、op2都为负 329 cout << count << "、 (" << one << ") " << sign << " (" << two << ")" << endl; 330 } 331 } 332 else if (choose2 == 2)//除法运算不含余数 333 { 334 if (yu == 0) 335 { 336 if (!flag_one && !flag_two)//如果op1、op2都为正 337 cout << count << "、 " << one << " " << sign << " " << two << endl; 338 if (flag_one && !flag_two)//如果op1为负,op2为正 339 cout << count << "、 (" << one << ") " << sign << " " << two << endl; 340 if (!flag_one && flag_two)//如果op1为正,op2为负 341 cout << count << "、 " << one << " " << sign << " (" << two << ")" << endl; 342 if (flag_one && flag_two)//如果op1、op2都为负 343 cout << count << "、 (" << one << ") " << sign << " (" << two << ")" << endl; 344 } 345 } 346 } 347 break; 348 } 349 } 350 count++; 351 } 352 } 353 354 }
用户选择打印方式的函数体,如下:
1 /*输出打印方式*/ 2 int Output(int o) 3 { 4 /*int o; 5 cout << "系统已经记录了您的定制参数!请选择打印的方式:(1、直接输出 2、保存到文件)"<<endl; 6 cin >> o;*/ 7 if (o == 1) 8 cout << "四则运算出题系统欢迎您!!" << endl; 9 else if (o == 2) 10 { 11 cout << "四则运算出题系统欢迎您!!" << endl; 12 ofstream outfile("formula.txt", ios::out); 13 if (!outfile) 14 { 15 cout << "Open error!!" << endl; 16 exit(1); 17 } 18 outfile.close(); 19 } 20 else 21 cout << "输入错误,重新选择!" << endl; 22 23 return 0; 24 }
最后,是主函数部分:
1 void main() 2 { 3 /*srand((int)time(0));*/ 4 int O, F; 5 int N,R,R1,R2,count; 6 cout << "请选择您是否需要保存文档?(1、不需要 2、需要)" << endl; 7 cin >> O; 8 Output(O); 9 cout << "您想为孩子定制什么范围的题目呢?先输入下限,再输入上限(以回车间隔,可含有负数且上限≥下限):"<<endl; 10 cin >> R1; 11 cin >> R2; 12 while (1)//判断是否输入合法 13 { 14 if (R2 < R1) 15 { 16 cout << "您输入的范围不合理,应输入 上限≥下限,请重新输入:" << endl; 17 cin >> R1; 18 cin >> R2; 19 } 20 else 21 break; 22 } 23 cout << "出多少道题呢?请输入个数:"<<endl; 24 cout << "(友情提示:您不要为孩子出太多的题目~1000道以内最好,上限是100000道!谢谢合作!)" << endl; 25 cin >> N; 26 cout << "请按照孩子的学习程度选择题目的难度:是否包含乘除运算?(1、是 2、否)" << endl; 27 cin >> F; 28 while (1)//判断是否输入合法 29 { 30 if (F!=1 && F!=2) 31 { 32 cout << "没有您想定制的功能,请重新输入(只输入1或2即可):" << endl; 33 cin >> F; 34 } 35 else 36 break; 37 } 38 if (F == 1)//包含乘除法 39 FourOper(N, R1, R2); 40 if (F == 2)//不包含乘除法 41 AddSub(N, R1, R2); 42 cout << "感谢您的使用,欢迎下次再来!祝您的孩子学习进步!!<( ̄︶ ̄)>" << endl; 43 44 45 }