四则运算2在四则运算1的基础之上,又添加了新的功能,但是我觉得四则运算2的难度比四则运算1增大了很多,我在编程的过程中,遇到的最大难度就是不知该如何更好的融合各个功能之间的关系。
写到现在,四则运算2主要实现了以下几个功能:
1.题目可不重复
2.打印方式可以选择,既可以在屏幕上输出,又可以保存到文件
3.题目的数量可以由用户控制
4.可以设置四则运算的数值范围
5.可以实现运算式中有无乘除法
6.可以支持十个数参与运算,并且通过括号改变运算的优先级
7.有除法算式的情况下,可以实现整数除法的整除运算,不产生余数
8.加减法可以实现无负数参与运算
根据程序实现的功能,对程序的设计思想简介如下:
1.为保证题目的不重复,在产生随机数时加入了判断语句,将当下产生的随机数与以前产生的随机数作比较,如果重复了,则当下的随机数舍弃,重新生成,重新生成后接着比较,直到产生新的不重复的随机数才继续往下执行。
2.打印方式分为在屏幕上输出与保存至文件两项,保存到文件的程序功能与输出到屏幕的程序功能一致,在输出到文件时,定义了一个txt文档,用文件输出流将最后的结果输出到文件保存。
3.程序开始会让用户设置某些参数,通过定义全局变量的形式,保存用户输入的运算式数量,从而控制产生的运算式数量。
4.设置数值范围与设置数量的方式一样,也定义了全局变量。
5.乘除法的实现是用随机数控制的,只产生0和1两个随机数,再加上switch语句就可以实现只有加减两种运算。
6.利用随机函数生成3-10的一个随机数,这个随机数代表了多项式四则运算中的因子个数,然后分别产生这些随机数和比运算数少一的运算符,比较后一个运算符和前一个运算符的优先级,若后一个运算符的优先级比前一个高,即后面的运算为乘除,则将前一个算式用括号括起来,保证了加减运算可以实现。
7.多项式运算没有实现没有余数,两个整数参与运算时,可以保证整除,分数除法考虑是否能够整除无意义
8.当数值范围定义中含有负数范围时,程序会提示用户再次输入范围,即若最小值小于零,则不能保证每一个加数都是大于零的,则需用户再次输入范围,重新生成随机数。
程序源代码如下所示:
1 //四则运算程序2,从四则运算程序1的基础之上进行功能扩展 2 //支持真分数运算,题目不重复,最多可支持十个运算数的带括号的运算式 3 //多项四则运算式以及分数除法未予考虑余数,整数除法可以实现整除功能 4 //2016,03,09 5 6 #include<iostream> 7 #include<fstream> 8 #include<stdlib.h> 9 #include<time.h> 10 using namespace std; 11 12 void main() 13 { 14 srand((int)time(NULL)); 15 //定义变量,记录用户的功能选择选项 16 int minfigure; 17 int maxfigure; 18 int count; 19 int chengchu; 20 int remainder_chengchu; 21 int negative_jiajian; 22 int printstyle; 23 //功能设置界面 24 cout << "*******************************************************************" << endl; 25 cout << "* 四则运算生成器 *" << endl; 26 cout << "*******************************************************************" << endl; 27 cout << "请按照系统提示设置生成器的功能:" << endl; 28 cout << "请输入参与四则运算的数值范围(格式如:1 100):" ; 29 cin >> minfigure >> maxfigure; 30 cout << "请输入生成四则运算式的数量:"; 31 cin >> count; 32 cout << "请选择打印方式(1 屏幕输出 0 输出到文档):"; 33 cin >> printstyle; 34 cout << "请设置系统的下列参数:" << endl; 35 cout << " 1 四则运算可否生成乘除法(1 是 0 否):"; 36 cin >> chengchu; 37 if (1 == chengchu) 38 { 39 cout << " 2 除法是否可有余数(1 是 0 否):"; 40 cin >> remainder_chengchu; 41 } 42 else 43 { 44 cout << " 2 加减法是否可以有负数(1 是 0 否):"; 45 cin >> negative_jiajian; 46 } 47 cout << "功能设置完成!" << endl; 48 cout << "*******************************************************************" << endl; 49 50 int i, j; 51 int A[100]; 52 int B[100]; 53 int mark[9]; 54 int Operator[10]; 55 char operatorFu[9]; 56 int Operatorfu[9]; 57 //生成用户指定数量的运算式的因子,为保证算式不重复,让生成的随机数都不重复 58 for (i = 0; i < count; i++) 59 { 60 A[i] = minfigure + rand() % maxfigure; 61 B[i] = minfigure + rand() % maxfigure; 62 for (j = 0; j < i; j++) 63 { 64 if (A[i] == A[j]) 65 { 66 A[i] = minfigure + rand() % maxfigure; 67 j = 0; 68 } 69 if (B[i] == B[j]) 70 { 71 B[i] = minfigure + rand() % maxfigure; 72 j = 0; 73 } 74 } 75 } 76 //根据用户的选择产生功能分支 77 if (1 == printstyle) //运算式输出到屏幕上 78 { 79 if (1 == chengchu) //是否可以生成除法 80 { 81 if (remainder_chengchu) //除法可否有余数,分数除法谈论是否有余数无意义 82 { 83 cout << "*******************************************************************" << endl; 84 cout << "生成的四则运算式如下所示:" << endl; 85 cout << "*******************************************************************" << endl; 86 for (i = 0; i < count; i++) 87 { 88 if (A[i] > B[i]) //如果A[i]>B[i],则后续程序生成带括号的多项因子的四则运算 89 { 90 int m; 91 int operator_count = 3 + rand() % 8; //随机生成多项因子的个数 92 int operatorfu_count = operator_count - 1; //算符的个数比因子的个数少一 93 for (m = 0; m < operator_count; m++) 94 { 95 Operator[m] = minfigure + rand() % maxfigure; //生成多项因子 96 } 97 for (m = 0; m < operatorfu_count; m++) 98 { 99 Operatorfu[m] = rand() % 4; //生成多项式的算符 100 } 101 for (m = 1; m < operator_count; m++) 102 { 103 if ((Operatorfu[m] / 2)>(Operatorfu[m - 1] / 2)) //乘除用3和4表示,加减用1和2表示,让其除以二得商0和1,可以区分出乘除为同一优先级,加减比乘除优先级低 104 { //若后一个运算符的优先级比前一个运算符的优先级高,则将前一个运算符对应的式子用括号括起来,确保了运算优先级低的加减运算在括号里面 105 switch (Operatorfu[m - 1]) 106 { 107 case 0: 108 cout << "(" << Operator[m - 1] << " + " << Operator[m] << ")"; break; 109 case 1: 110 cout << "(" << Operator[m - 1] << " - " << Operator[m] << ")"; break; 111 case 2: 112 cout << "(" << Operator[m - 1] << " x " << Operator[m] << ")"; break; 113 case 3: 114 if (0 == Operator[m]) 115 { 116 cout << "(" << Operator[m - 1] << " ÷ " << Operator[m] + 2 << ")"; 117 } 118 break; //若分子为零时,则让分子加2,即除以2 119 } 120 switch (Operatorfu[m]) 121 { 122 case 0: 123 cout << " + "; m = m + 1; break; 124 case 1: 125 cout << " - "; m = m + 1; break; 126 case 2: 127 cout << " x "; m = m + 1; break; 128 case 3: 129 cout << " ÷ "; m = m + 1; break; 130 } 131 } 132 else 133 { 134 switch (Operatorfu[m - 1]) 135 { 136 case 0: 137 cout << Operator[m - 1] << " + "; break; 138 case 1: 139 cout << Operator[m - 1] << " - "; break; 140 case 2: 141 cout << Operator[m - 1] << " x "; break; 142 case 3: 143 cout << Operator[m - 1] << " ÷ "; break; 144 } 145 } 146 } 147 cout << Operator[m - 1] << " = " << endl; //输出最后一个多项式因子 148 } 149 else //如果A[i]<B[i],则产生真分数运算式 150 { 151 int copyA = A[i]; 152 int copyB = B[i]; 153 int beichushuA = maxfigure; 154 int beichushuB = maxfigure; 155 int firstA = beichushuA% copyA; 156 int firstB = beichushuB% copyB; 157 while (firstA != 0) //求A[i]和最大值的公约数,以便后面化简 158 { 159 int temp = copyA; 160 copyA = beichushuA%copyA; 161 beichushuA = temp; 162 firstA = beichushuA%copyA; 163 } 164 while (firstB != 0) 165 { 166 int temp = copyB; 167 copyB = beichushuB%copyB; 168 beichushuB = temp; 169 firstB = beichushuB%copyB; 170 } 171 int suanfu = rand() % 4; 172 switch (suanfu) 173 { 174 case 0: 175 cout << A[i] / copyA << "/" << maxfigure / copyA << " + " << B[i] / copyB << "/" << maxfigure / copyB << " = " << endl; break; 176 case 1: 177 cout << A[i] / copyA << "/" << maxfigure / copyA << " - " << B[i] / copyB << "/" << maxfigure / copyB << " = " << endl; break; 178 case 2: 179 cout << A[i] / copyA << "/" << maxfigure / copyA << " x " << B[i] / copyB << "/" << maxfigure / copyB << " = " << endl; break; 180 case 3: 181 cout << A[i] / copyA << "/" << maxfigure / copyA << " ÷ " << B[i] / copyB << "/" << maxfigure / copyB << " = " << endl; break; 182 //让A[i]和最大值都除以公约数便可以得到无法再约分的最大公约数 183 } 184 } 185 } 186 } 187 else 188 { 189 cout << "*******************************************************************" << endl; 190 cout << "生成的四则运算式如下所示:" << endl; 191 cout << "*******************************************************************" << endl; 192 for (i = 0; i < count; i++) 193 { 194 if (A[i] > B[i]) 195 { 196 int m; 197 int operator_count = 3 + rand() % 8; 198 int operatorfu_count = operator_count - 1; 199 for (m = 0; m < operator_count; m++) 200 { 201 Operator[m] = minfigure + rand() % maxfigure; 202 } 203 for (m = 0; m < operatorfu_count; m++) 204 { 205 Operatorfu[m] = rand() % 4; 206 } 207 for (m = 1; m < operator_count; m++) 208 { 209 if ((Operatorfu[m] / 2)>(Operatorfu[m - 1] / 2)) 210 { 211 switch (Operatorfu[m - 1]) 212 { 213 case 0: 214 cout << "(" << Operator[m - 1] << " + " << Operator[m] << ")"; break; 215 case 1: 216 cout << "(" << Operator[m - 1] << " - " << Operator[m] << ")"; break; 217 case 2: 218 cout << "(" << Operator[m - 1] << " x " << Operator[m] << ")"; break; 219 case 3: 220 if (0 == Operator[m]) 221 { 222 cout << "(" << Operator[m - 1] << " ÷ " << Operator[m] + 2 << ")"; 223 } 224 break; 225 } 226 switch (Operatorfu[m]) 227 { 228 case 0: 229 cout << " + "; m = m + 1; break; 230 case 1: 231 cout << " - "; m = m + 1; break; 232 case 2: 233 cout << " x "; m = m + 1; break; 234 case 3: 235 cout << " ÷ "; m = m + 1; break; 236 } 237 } 238 else 239 { 240 switch (Operatorfu[m - 1]) 241 { 242 case 0: 243 cout << Operator[m - 1] << " + "; break; 244 case 1: 245 cout << Operator[m - 1] << " - "; break; 246 case 2: 247 cout << Operator[m - 1] << " x "; break; 248 case 3: 249 cout << Operator[m - 1] << " ÷ "; break; 250 } 251 } 252 } 253 cout << Operator[m - 1] << " = " << endl; 254 } 255 else 256 { 257 int copyB = B[i]; 258 int first = B[i] % A[i]; 259 while (first != 0) 260 { 261 int temp = A[i]; 262 A[i] = B[i] % A[i]; 263 B[i] = temp; 264 first = B[i] % A[i]; 265 } 266 int suanfu = rand() % 4; 267 switch (suanfu) 268 { 269 case 0: 270 cout << A[i] << " + " << B[i] << "=" << endl; break; 271 case 1: 272 cout << A[i] << " - " << B[i] << "=" << endl; break; 273 case 2: 274 cout << A[i] << " x " << B[i] << "=" << endl; break; 275 case 3: 276 cout << copyB << " ÷ " << A[i] << " = " << endl; break; 277 } 278 } 279 } 280 } 281 } 282 else //不允许生成乘除法,则只有加减法 283 { 284 if ((minfigure < 0) && (0 == negative_jiajian)) //若加减法不允许出现负数,则应该重新设定数值的范围,让最小值大于零,保证生成的随机数都是大于零的 285 { 286 cout << "请重新设置数值范围,最小值应大于零以保证加法无负数!" << endl; 287 cout << "请输入参与四则运算的数值范围(格式如:1 100):"; 288 cin >> minfigure >> maxfigure; 289 for (i = 0; i < count; i++) //重新生成的随机数仍是不重复的 290 { 291 A[i] = minfigure + rand() % maxfigure; 292 B[i] = minfigure + rand() % maxfigure; 293 for (j = 0; j < i; j++) 294 { 295 if (A[i] == A[j]) 296 { 297 A[i] = minfigure + rand() % maxfigure; 298 j = 0; 299 } 300 if (B[i] == B[j]) 301 { 302 B[i] = minfigure + rand() % maxfigure; 303 j = 0; 304 } 305 } 306 } 307 } 308 for (i = 0; i < count; i++) 309 { 310 if (A[i]>B[i]) 311 { 312 int suanfu = rand() % 2; 313 switch (suanfu) 314 { 315 case 0: 316 cout << A[i] << " + " << B[i] << "=" << endl; break; 317 case 1: 318 cout << A[i] << " - " << B[i] << "=" << endl; break; 319 } 320 } 321 else 322 { 323 int copyA = A[i]; 324 int copyB = B[i]; 325 int beichushuA = maxfigure; 326 int beichushuB = maxfigure; 327 int firstA = beichushuA% copyA; 328 int firstB = beichushuB% copyB; 329 while (firstA != 0) 330 { 331 int temp = copyA; 332 copyA = beichushuA%copyA; 333 beichushuA = temp; 334 firstA = beichushuA%copyA; 335 } 336 while (firstB != 0) 337 { 338 int temp = copyB; 339 copyB = beichushuB%copyB; 340 beichushuB = temp; 341 firstB = beichushuB%copyB; 342 } 343 int suanfu = rand() % 2; 344 switch (suanfu) 345 { 346 case 0: 347 cout << A[i] / copyA << "/" << maxfigure / copyA << " + " << B[i] / copyB << "/" << maxfigure / copyB << "=" << endl; break; 348 case 1: 349 cout << B[i] / copyB << "/" << maxfigure / copyB << " - " << A[i] / copyA << "/" << maxfigure / copyA << "=" << endl; break; 350 } 351 } 352 } 353 } 354 } 355 else //将最后生成的四则运算式存到文件中,内部实现的功能与输出到屏幕一致,只是输出不一样 356 { 357 if (1 == chengchu) 358 { 359 if (remainder_chengchu) 360 { 361 ofstream fileout; //定义输出文件流的对象 362 fileout.open("biaodashi.txt", ios::out); //打开文件,该文件为指定路径,则存放在工程的当前目录下 363 fileout << "*******************************************************************" << endl; 364 fileout << "生成的四则运算式如下所示:" << endl; 365 fileout << "*******************************************************************" << endl; 366 for (i = 0; i < count; i++) 367 { 368 if (A[i] > B[i]) 369 { 370 int m; 371 int operator_count = 3 + rand() % 8; 372 int operatorfu_count = operator_count - 1; 373 for (m = 0; m < operator_count; m++) 374 { 375 Operator[m] = minfigure + rand() % maxfigure; 376 } 377 for (m = 0; m < operatorfu_count; m++) 378 { 379 Operatorfu[m] = rand() % 4; 380 } 381 for (m = 1; m < operator_count; m++) 382 { 383 if ((Operatorfu[m] / 2)>(Operatorfu[m - 1] / 2)) 384 { 385 switch (Operatorfu[m - 1]) 386 { 387 case 0: 388 fileout << "(" << Operator[m - 1] << " + " << Operator[m] << ")"; break; 389 case 1: 390 fileout << "(" << Operator[m - 1] << " - " << Operator[m] << ")"; break; 391 case 2: 392 fileout << "(" << Operator[m - 1] << " x " << Operator[m] << ")"; break; 393 case 3: 394 if (0 == Operator[m]) 395 { 396 fileout << "(" << Operator[m - 1] << " ÷ " << Operator[m] + 2 << ")"; 397 } 398 break; 399 } 400 switch (Operatorfu[m]) 401 { 402 case 0: 403 fileout << " + "; m = m + 1; break; 404 case 1: 405 fileout << " - "; m = m + 1; break; 406 case 2: 407 fileout << " x "; m = m + 1; break; 408 case 3: 409 fileout << " ÷ "; m = m + 1; break; 410 } 411 } 412 else 413 { 414 switch (Operatorfu[m - 1]) 415 { 416 case 0: 417 fileout << Operator[m - 1] << " + "; break; 418 case 1: 419 fileout << Operator[m - 1] << " - "; break; 420 case 2: 421 fileout << Operator[m - 1] << " x "; break; 422 case 3: 423 fileout << Operator[m - 1] << " ÷ "; break; 424 } 425 } 426 } 427 fileout << Operator[m - 1] << " = " << endl; 428 } 429 else 430 { 431 int copyA = A[i]; 432 int copyB = B[i]; 433 int beichushuA = maxfigure; 434 int beichushuB = maxfigure; 435 int firstA = beichushuA% copyA; 436 int firstB = beichushuB% copyB; 437 while (firstA != 0) 438 { 439 int temp = copyA; 440 copyA = beichushuA%copyA; 441 beichushuA = temp; 442 firstA = beichushuA%copyA; 443 } 444 while (firstB != 0) 445 { 446 int temp = copyB; 447 copyB = beichushuB%copyB; 448 beichushuB = temp; 449 firstB = beichushuB%copyB; 450 } 451 int suanfu = rand() % 4; 452 switch (suanfu) 453 { 454 case 0: 455 fileout << A[i] / copyA << "/" << maxfigure / copyA << " + " << B[i] / copyB << "/" << maxfigure / copyB << " = " << endl; break; 456 case 1: 457 fileout << A[i] / copyA << "/" << maxfigure / copyA << " - " << B[i] / copyB << "/" << maxfigure / copyB << " = " << endl; break; 458 case 2: 459 fileout << A[i] / copyA << "/" << maxfigure / copyA << " x " << B[i] / copyB << "/" << maxfigure / copyB << " = " << endl; break; 460 case 3: 461 fileout << A[i] / copyA << "/" << maxfigure / copyA << " ÷ " << B[i] / copyB << "/" << maxfigure / copyB << " = " << endl; break; 462 } 463 } 464 } 465 fileout.close(); //关闭文件,断开与文件连接 466 } 467 else 468 { 469 ofstream fileout; 470 fileout.open("biaodashi.txt", ios::out); 471 fileout << "*******************************************************************" << endl; 472 fileout << "生成的四则运算式如下所示:" << endl; 473 fileout << "*******************************************************************" << endl; 474 for (i = 0; i < count; i++) 475 { 476 if (A[i] > B[i]) 477 { 478 int m; 479 int operator_count = 3 + rand() % 8; 480 int operatorfu_count = operator_count - 1; 481 for (m = 0; m < operator_count; m++) 482 { 483 Operator[m] = minfigure + rand() % maxfigure; 484 } 485 for (m = 0; m < operatorfu_count; m++) 486 { 487 Operatorfu[m] = rand() % 4; 488 } 489 for (m = 1; m < operator_count; m++) 490 { 491 if ((Operatorfu[m] / 2)>(Operatorfu[m - 1] / 2)) 492 { 493 switch (Operatorfu[m - 1]) 494 { 495 case 0: 496 fileout << "(" << Operator[m - 1] << " + " << Operator[m] << ")"; break; 497 case 1: 498 fileout << "(" << Operator[m - 1] << " - " << Operator[m] << ")"; break; 499 case 2: 500 fileout << "(" << Operator[m - 1] << " x " << Operator[m] << ")"; break; 501 case 3: 502 if (0 == Operator[m]) 503 { 504 fileout << "(" << Operator[m - 1] << " ÷ " << Operator[m] + 2 << ")"; 505 } 506 break; 507 } 508 switch (Operatorfu[m]) 509 { 510 case 0: 511 fileout << " + "; m = m + 1; break; 512 case 1: 513 fileout << " - "; m = m + 1; break; 514 case 2: 515 fileout << " x "; m = m + 1; break; 516 case 3: 517 fileout << " ÷ "; m = m + 1; break; 518 } 519 } 520 else 521 { 522 switch (Operatorfu[m - 1]) 523 { 524 case 0: 525 fileout << Operator[m - 1] << " + "; break; 526 case 1: 527 fileout << Operator[m - 1] << " - "; break; 528 case 2: 529 fileout << Operator[m - 1] << " x "; break; 530 case 3: 531 fileout << Operator[m - 1] << " ÷ "; break; 532 } 533 } 534 } 535 fileout << Operator[m - 1] << " = " << endl; 536 } 537 else 538 { 539 int copyB = B[i]; 540 int first = B[i] % A[i]; 541 while (first != 0) 542 { 543 int temp = A[i]; 544 A[i] = B[i] % A[i]; 545 B[i] = temp; 546 first = B[i] % A[i]; 547 } 548 int suanfu = rand() % 4; 549 switch (suanfu) 550 { 551 case 0: 552 fileout << A[i] << " + " << B[i] << "=" << endl; break; 553 case 1: 554 fileout << A[i] << " - " << B[i] << "=" << endl; break; 555 case 2: 556 fileout << A[i] << " x " << B[i] << "=" << endl; break; 557 case 3: 558 fileout << copyB << " ÷ " << A[i] << " = " << endl; break; 559 } 560 } 561 } 562 fileout.close(); 563 } 564 } 565 else 566 { 567 if ((minfigure < 0) && (0 == negative_jiajian)) 568 { 569 cout << "请重新设置数值范围,最小值应大于零以保证加法无负数!" << endl; 570 cout << "请输入参与四则运算的数值范围(格式如:1 100):"; 571 cin >> minfigure >> maxfigure; 572 for (i = 0; i < count; i++) 573 { 574 A[i] = minfigure + rand() % maxfigure; 575 B[i] = minfigure + rand() % maxfigure; 576 for (j = 0; j < i; j++) 577 { 578 if (A[i] == A[j]) 579 { 580 A[i] = minfigure + rand() % maxfigure; 581 j = 0; 582 } 583 if (B[i] == B[j]) 584 { 585 B[i] = minfigure + rand() % maxfigure; 586 j = 0; 587 } 588 } 589 } 590 } 591 ofstream fileout; 592 fileout.open("biaodashi.txt", ios::out); 593 for (i = 0; i < count; i++) 594 { 595 if (A[i]>B[i]) 596 { 597 int suanfu = rand() % 2; 598 switch (suanfu) 599 { 600 case 0: 601 fileout << A[i] << " + " << B[i] << "=" << endl; break; 602 case 1: 603 fileout << A[i] << " - " << B[i] << "=" << endl; break; 604 } 605 } 606 else 607 { 608 int copyA = A[i]; 609 int copyB = B[i]; 610 int beichushuA = maxfigure; 611 int beichushuB = maxfigure; 612 int firstA = beichushuA% copyA; 613 int firstB = beichushuB% copyB; 614 while (firstA != 0) 615 { 616 int temp = copyA; 617 copyA = beichushuA%copyA; 618 beichushuA = temp; 619 firstA = beichushuA%copyA; 620 } 621 while (firstB != 0) 622 { 623 int temp = copyB; 624 copyB = beichushuB%copyB; 625 beichushuB = temp; 626 firstB = beichushuB%copyB; 627 } 628 int suanfu = rand() % 2; 629 switch (suanfu) 630 { 631 case 0: 632 fileout << A[i] / copyA << "/" << maxfigure / copyA << " + " << B[i] / copyB << "/" << maxfigure / copyB << "=" << endl; break; 633 case 1: 634 fileout << B[i] / copyB << "/" << maxfigure / copyB << " - " << A[i] / copyA << "/" << maxfigure / copyA << "=" << endl; break; 635 } 636 } 637 } 638 fileout.close(); 639 } 640 } 641 }
运行结果截图如下所示:
(网页提示浏览器不支持,暂时无法上传图片)