四则运算之Right-BICEP单元测试
一、
这篇博客要对上次实现的四则运算进行单元测试,一是检查上次的程序的实现情况,二是巩固单元测试的相关知识。本次进行单元测试用的是Riget-BICEP方法。
Riget-BICEP方法:
1、Right-结果是否正确?
2、B-是否所有的边界条件都是正确的?
3、I-能查一下反向关联吗?
4、C-能用其他手段交叉检查一下结果吗?
5、E-你是否可以强制错误条件发生?
6、P-是否满足性能要求?
对应于以上的六个值得测试的部位,选择适合本程序的测试方法进行测试如下:
1、运算式多少的测试:分为运算式个数比较少和运算式比较多两种情况,看看结果如何
首先是5个运算式:运行时间较快
运算式数量增加为300个:运算时间较长
运算出现错误,错误为求余数是,方程rand()%a中,a出现负数的情况。经过对a的不同值的判断处理,修改程序后,结果为:
2、控制能否有乘除法
没有的情况:
有的情况:
3、控制是否能有有负数出现
不能有负数出现;
4、测试能否有余数出现
不能有余数出现:
5、测试可以有括号生成
6、测试能否输出运算式到文件:
二、根据代码风格规范修改后的程序为
1 //第三次实验 2 #include<iostream> 3 #include<string> 4 #include<stdlib.h> 5 #include<time.h> 6 #include<math.h> 7 #include<fstream> 8 using namespace std; 9 10 int main() 11 { 12 int intExpressieCanYu[1000][10]; //这是一个二维数组,每行记录的是每个运算式中参与的数字 13 int intExpressieNumNum[1000]; //此数组记录二维数组相应行中的列数,及记录每个运算式中参与运算的数字的个数 14 int intNumber; //定义产生的运算式中数的数量(最大为10个数) 15 int intExpressieNum; //定义产生的运算式的数量 16 int intPrintStyle; //定义打印方式(DOS界面打印、输出到文件) 17 int intMultiplyDivide; //定义是否有乘除法,0代表没有,1代表有 18 int intMax, min; //定义四则运算中的数的取值范围 19 int intMinus; //定义是否有负数,0代表没有,1代表有 20 int intRemainder; //定义是否有余数,0代表没有,1代表有 21 int intBracket; //定义是否有括号,0代表没有,1代表有 22 23 int intBiaozhiwei[9]; //标志位,记录各个条件 24 25 cout << "请输入要生成的运算式的数量:"; 26 cin >> intExpressieNum; 27 cout << endl; 28 cout << "请输入打印运算式的方式(0代表DOS界面,1代表输出到文件):"; 29 cin >> intPrintStyle; 30 cout << endl; 31 cout << "运算式中是否含有乘除法(0代表没有,1代表有):"; 32 cin >> intMultiplyDivide; 33 cout << endl; 34 cout << "请输入运算式中数的取值范围(输入方式为:最小值 空格 最大值):"; 35 cin >> min >> intMax; 36 cout << endl; 37 cout << "运算过程能否出现负数(0代表不能,1代表能)"; 38 cin >> intMinus; 39 cout << endl; 40 cout << "定义运算过程是否能够有余数出现(0代表不能出现余数,1代表能出现)"; 41 cin >> intRemainder; 42 cout << endl; 43 cout << "定义括号是否能够参与运算(0代表不能,1代表能)"; 44 cin >> intBracket; 45 cout << endl; 46 47 intBiaozhiwei[0] = intExpressieNum; 48 intBiaozhiwei[1] = intPrintStyle; 49 intBiaozhiwei[2] = intMultiplyDivide; 50 intBiaozhiwei[3] = min; 51 intBiaozhiwei[4] = intMax; 52 intBiaozhiwei[5] = intMinus; 53 intBiaozhiwei[6] = intRemainder; 54 intBiaozhiwei[7] = intBracket; 55 56 57 ofstream outfile; 58 outfile.open("f1.dat", ios::out); 59 //首先根据运算式的数量,生成每个运算式需要的数,并记录在intExpressieCanYu[1000][10]中 60 srand((int)time(0)); //根据时间生成随机数种子 61 int xun_huan_expressieNum; //此变量代表循环时的记录,代表已经循环的次数 62 for (xun_huan_expressieNum = 0; xun_huan_expressieNum < intExpressieNum; xun_huan_expressieNum++) 63 { 64 intNumber = 2 + rand() % 9; //随机生成参与每个式子进行运算的数的个数 65 intExpressieNumNum[xun_huan_expressieNum] = intNumber; //将每个式子中数字的个数存入数组intExpressieNumNum[1000]中 66 67 for (int i = 0; i < intNumber; i++) //根据intNumber生成式子中的每个随机数 68 { 69 intExpressieCanYu[xun_huan_expressieNum][i] = min + rand() % (intMax - min); //此处可能有错 70 } 71 } 72 73 //以上完成了数组的初始化,下面要完成运算式的组装 74 75 srand((int)time(NULL)); 76 for (int i = 0; i < intExpressieNum; i++) //对N个运算式进行循环 77 { 78 79 if (0 == intBracket) //如果没有括号 80 { 81 int yunsuanfugeshu; //变量运算符的个数 82 yunsuanfugeshu = intExpressieNumNum[i] - 1; 83 char yunsuanfu[10]; //此数组存储运算符 84 85 for (int j = 0; j < yunsuanfugeshu; j++) //对运算符数组进行赋值 86 { 87 if (0 == intMultiplyDivide) //如果没有乘除法 88 { 89 int sign_noMD; //数字代表符号 90 sign_noMD = 1 + rand() % 2; 91 if (1 == sign_noMD) 92 { 93 yunsuanfu[j] = '+'; 94 } 95 else 96 { 97 yunsuanfu[j] = '-'; 98 } 99 } 100 else 101 { 102 int sign_yesMD; //数字代表运算符,且含有乘除法 103 sign_yesMD = 1 + rand() % 4; 104 if (1 == sign_yesMD) 105 { 106 yunsuanfu[j] = '+'; 107 } 108 else if (2 == sign_yesMD) 109 { 110 yunsuanfu[j] = '-'; 111 } 112 else if (3 == sign_yesMD) 113 { 114 yunsuanfu[j] = '*'; 115 } 116 else if (4 == sign_yesMD) 117 { 118 yunsuanfu[j] = '/'; 119 } 120 } 121 } 122 123 //运算符不能有连续3个为除号 124 for (int ai = 0; ai < yunsuanfugeshu; ai++) 125 { 126 if (yunsuanfu[ai] == '/') 127 { 128 if (yunsuanfu[ai + 1] == '/') 129 { 130 if (yunsuanfu[ai + 2] == '/') 131 { 132 yunsuanfu[ai + 2] = '+'; 133 } 134 } 135 } 136 } 137 138 //cout << "运算符的个数为:" << yunsuanfugeshu << endl; 139 //输出所有运算符号 140 /*cout << "所有运算符号为:"; 141 for (int fuhaoshu1 = 0; fuhaoshu1 < yunsuanfugeshu; fuhaoshu1++) 142 { 143 cout << yunsuanfu[fuhaoshu1] << " "; 144 } 145 */ 146 //上面的循环是对运算符号的赋值。下面组合运算式 147 //此时已存在数组intExpressieCanYu[i][0-intNumber](参与运算的数字的个数),yunsuanfu[10](存储运算符),yunsuanfugeshu(运算符个数) 148 if (0 == intMultiplyDivide) //如果没有乘除法 149 { 150 if (0 == intMinus) //如果不能有负数出现 151 { 152 int jieguo; 153 jieguo = intExpressieCanYu[i][0]; //结果等于此行数组中的第一个数值 154 155 //下面的循环是对运算式预算过程有无负数的判断 156 for (int nointMinusNum = 0; nointMinusNum < (intExpressieNumNum[i] - 1); nointMinusNum++) 157 { 158 if (yunsuanfu[nointMinusNum] == '+') 159 { 160 jieguo = jieguo + intExpressieCanYu[i][nointMinusNum + 1]; 161 162 } 163 else 164 { 165 jieguo = jieguo - intExpressieCanYu[i][nointMinusNum + 1]; 166 if (jieguo <= 0) 167 { 168 int a; 169 a = jieguo; 170 int b; 171 b = jieguo + intExpressieCanYu[i][nointMinusNum + 1]; 172 if (a < 0) 173 { 174 a = abs(a); 175 intExpressieCanYu[i][nointMinusNum + 1] = b - rand() % (a); 176 } 177 if (a == 0) 178 { 179 intExpressieCanYu[i][nointMinusNum + 1] = b - rand() % b; 180 } 181 182 } 183 } 184 } 185 186 //对运算式进行输出 187 //cout << "运算式中数的个数为:" << intExpressieNumNum[i] << endl; 188 if (0 == intPrintStyle) 189 { 190 int fuhao = 0; 191 for (int shu = 0; shu < intExpressieNumNum[i]; shu++) 192 { 193 cout << intExpressieCanYu[i][shu]; 194 195 for (; fuhao < yunsuanfugeshu;) 196 { 197 cout << yunsuanfu[fuhao]; 198 fuhao++; 199 break; 200 } 201 } 202 } 203 else 204 { 205 206 int fuhao = 0; 207 for (int shu = 0; shu < intExpressieNumNum[i]; shu++) 208 { 209 outfile << intExpressieCanYu[i][shu]; 210 211 for (; fuhao < yunsuanfugeshu;) 212 { 213 outfile << yunsuanfu[fuhao]; 214 fuhao++; 215 break; 216 } 217 } 218 outfile << endl; 219 //outfile.close(); 220 } 221 222 } 223 else //如果能有负数出现 224 { 225 //对运算式进行输出 226 //cout << "运算式中数的个数为:" << intExpressieNumNum[i] << endl; 227 if (0 == intPrintStyle) 228 { 229 int fuhao = 0; 230 for (int shu = 0; shu < intExpressieNumNum[i]; shu++) 231 { 232 cout << intExpressieCanYu[i][shu]; 233 234 for (; fuhao < yunsuanfugeshu;) 235 { 236 cout << yunsuanfu[fuhao]; 237 fuhao++; 238 break; 239 } 240 } 241 } 242 else 243 { 244 245 int fuhao = 0; 246 for (int shu = 0; shu < intExpressieNumNum[i]; shu++) 247 { 248 outfile << intExpressieCanYu[i][shu]; 249 250 for (; fuhao < yunsuanfugeshu;) 251 { 252 outfile << yunsuanfu[fuhao]; 253 fuhao++; 254 break; 255 } 256 } 257 outfile << endl; 258 //outfile.close(); 259 } 260 } 261 } 262 else //如果有乘除法 263 { 264 if (0 == intRemainder) //如果不能有余数 265 { 266 int intExpressieCanYu_linshi[10]; //建立临时数组,下面的循环为给临时数组赋值 267 for (int linshi_chengchu = 0; linshi_chengchu < intExpressieNumNum[i]; linshi_chengchu++) 268 { 269 intExpressieCanYu_linshi[linshi_chengchu] = intExpressieCanYu[i][linshi_chengchu]; 270 } 271 char yunsuanfulinshi[10]; 272 for (int yunsuanfujishu2 = 0; yunsuanfujishu2 < yunsuanfugeshu; yunsuanfujishu2++) 273 { 274 yunsuanfulinshi[yunsuanfujishu2] = yunsuanfu[yunsuanfujishu2]; 275 } 276 for (int chengchui = 0; chengchui < yunsuanfugeshu; chengchui++) //此循环对数组中的余数情况进行调整 277 { 278 if (yunsuanfulinshi[chengchui] == '*') 279 { 280 int jieguo; 281 jieguo = intExpressieCanYu_linshi[chengchui] * intExpressieCanYu_linshi[chengchui + 1]; 282 intExpressieCanYu_linshi[chengchui] = jieguo; 283 intExpressieCanYu_linshi[chengchui + 1] = jieguo; 284 yunsuanfulinshi[chengchui] = '+'; 285 } 286 if (yunsuanfulinshi[chengchui] == '/') 287 { 288 int jieguo; 289 jieguo = intExpressieCanYu_linshi[chengchui] % intExpressieCanYu_linshi[chengchui + 1]; 290 if (jieguo != 0) //如果有余数 291 { 292 int yueshu = 1;//约数 293 int yushupan = 0; 294 while (yushupan < 2) 295 { 296 int jieguoyue; 297 jieguoyue = intExpressieCanYu_linshi[chengchui] % yueshu; 298 if (jieguoyue == 0) 299 { 300 yushupan += 1; 301 if (yushupan == 2) 302 { 303 intExpressieCanYu_linshi[chengchui + 1] = yueshu; //修改临时数组 304 intExpressieCanYu[i][chengchui + 1] = yueshu; //修改原数组 305 yunsuanfulinshi[chengchui] = '+'; 306 break; 307 } 308 } 309 yueshu += 1; 310 } 311 } 312 } 313 } 314 315 if (0 == intMinus) //如果不能有负数 316 { 317 //下面的大循环时修改乘法或除法前面的一个运算符号为加号 318 for (int gaijiahao = 0; gaijiahao < yunsuanfugeshu; gaijiahao++) 319 { 320 if (yunsuanfu[gaijiahao] == '*') 321 { 322 for (int gaijiahao1 = gaijiahao - 1; gaijiahao1 >= 0; gaijiahao1--) 323 { 324 if ((yunsuanfu[gaijiahao1] == '+') || (yunsuanfu[gaijiahao1] == '-')) 325 { 326 yunsuanfu[gaijiahao1] = '+'; 327 break; 328 } 329 } 330 } 331 if (yunsuanfu[gaijiahao] == '/') 332 { 333 for (int gaijiahao2 = gaijiahao - 1; gaijiahao2 >= 0; gaijiahao2--) 334 { 335 if ((yunsuanfu[gaijiahao2] == '+') || (yunsuanfu[gaijiahao2] == '-')) 336 { 337 yunsuanfu[gaijiahao2] = '+'; 338 if (yunsuanfu[gaijiahao2 + 2] == '-') 339 { 340 yunsuanfu[gaijiahao2 + 2] = '+'; 341 } 342 343 break; 344 } 345 } 346 } 347 } 348 //以上为修改乘法和除法符号前边的符号+ 349 //以下为判断正负 350 for (int fuhaochuan = 0; fuhaochuan < yunsuanfugeshu; fuhaochuan++) 351 { 352 if (yunsuanfu[fuhaochuan] == '-') 353 { 354 intExpressieCanYu[i][fuhaochuan + 1] = 1 + rand() % (intExpressieCanYu[i][fuhaochuan]); 355 } 356 } 357 358 /*cout << "运算符数组为:"; 359 for (int linshia = 0; linshia < yunsuanfugeshu; linshia++) 360 { 361 cout << yunsuanfu[linshia]; 362 } 363 cout << endl;*/ 364 //对运算式进行输出 365 //cout << "运算式中数的个数为:" << intExpressieNumNum[i] << endl; 366 //修改数组中的负数为1 367 for (int qi = 0; qi < intExpressieNumNum[i]; qi++) 368 { 369 if (intExpressieCanYu[i][qi] < 0) 370 { 371 intExpressieCanYu[i][qi] = 1; 372 } 373 } 374 if (0 == intPrintStyle) 375 { 376 int fuhao = 0; 377 for (int shu = 0; shu < intExpressieNumNum[i]; shu++) 378 { 379 cout << intExpressieCanYu[i][shu]; 380 381 for (; fuhao < yunsuanfugeshu;) 382 { 383 cout << yunsuanfu[fuhao]; 384 fuhao++; 385 break; 386 } 387 } 388 } 389 else 390 { 391 392 int fuhao = 0; 393 for (int shu = 0; shu < intExpressieNumNum[i]; shu++) 394 { 395 outfile << intExpressieCanYu[i][shu]; 396 397 for (; fuhao < yunsuanfugeshu;) 398 { 399 outfile << yunsuanfu[fuhao]; 400 fuhao++; 401 break; 402 } 403 } 404 outfile << endl; 405 //outfile.close(); 406 } 407 } 408 else //如果能有负数 409 { 410 //对运算式进行输出 411 //cout << "运算式中数的个数为:" << intExpressieNumNum[i] << endl; 412 if (0 == intPrintStyle) 413 { 414 int fuhao = 0; 415 for (int shu = 0; shu < intExpressieNumNum[i]; shu++) 416 { 417 cout << intExpressieCanYu[i][shu]; 418 419 for (; fuhao < yunsuanfugeshu;) 420 { 421 cout << yunsuanfu[fuhao]; 422 fuhao++; 423 break; 424 } 425 } 426 } 427 else 428 { 429 430 int fuhao = 0; 431 for (int shu = 0; shu < intExpressieNumNum[i]; shu++) 432 { 433 outfile << intExpressieCanYu[i][shu]; 434 435 for (; fuhao < yunsuanfugeshu;) 436 { 437 outfile << yunsuanfu[fuhao]; 438 fuhao++; 439 break; 440 } 441 } 442 outfile << endl; 443 //outfile.close(); 444 } 445 } 446 } 447 else //如果可以有余数 448 { 449 if (0 == intMinus) //如果不能有负数 450 { 451 //下面的大循环时修改乘法或除法前面的一个运算符号为加号 452 for (int gaijiahao = 0; gaijiahao < yunsuanfugeshu; gaijiahao++) 453 { 454 if (yunsuanfu[gaijiahao] == '*') 455 { 456 for (int gaijiahao1 = gaijiahao - 1; gaijiahao1 >= 0; gaijiahao1--) 457 { 458 if ((yunsuanfu[gaijiahao1] == '+') || (yunsuanfu[gaijiahao1] == '-')) 459 { 460 yunsuanfu[gaijiahao1] = '+'; 461 } 462 } 463 } 464 if (yunsuanfu[gaijiahao] == '/') 465 { 466 for (int gaijiahao2 = gaijiahao - 1; gaijiahao2 >= 0; gaijiahao2--) 467 { 468 if ((yunsuanfu[gaijiahao2] == '+') || (yunsuanfu[gaijiahao2] == '-')) 469 { 470 yunsuanfu[gaijiahao2] = '+'; 471 } 472 } 473 } 474 } 475 //以上为修改乘法和除法符号前边的符号+ 476 //以下为判断正负 477 for (int fuhaochuan = 0; fuhaochuan < yunsuanfugeshu; fuhaochuan++) 478 { 479 if (yunsuanfu[fuhaochuan] == '-') 480 { 481 intExpressieCanYu[i][fuhaochuan + 1] = 1 + rand() % (intExpressieCanYu[i][fuhaochuan + 1]); 482 } 483 } 484 485 //对运算式进行输出 486 //cout << "运算式中数的个数为:" << intExpressieNumNum[i] << endl; 487 if (0 == intPrintStyle) 488 { 489 int fuhao = 0; 490 for (int shu = 0; shu < intExpressieNumNum[i]; shu++) 491 { 492 cout << intExpressieCanYu[i][shu]; 493 494 for (; fuhao < yunsuanfugeshu;) 495 { 496 cout << yunsuanfu[fuhao]; 497 fuhao++; 498 break; 499 } 500 } 501 } 502 else 503 { 504 505 int fuhao = 0; 506 for (int shu = 0; shu < intExpressieNumNum[i]; shu++) 507 { 508 outfile << intExpressieCanYu[i][shu]; 509 510 for (; fuhao < yunsuanfugeshu;) 511 { 512 outfile << yunsuanfu[fuhao]; 513 fuhao++; 514 break; 515 } 516 } 517 outfile << endl; 518 //outfile.close(); 519 } 520 } 521 else //如果能有负数 522 { 523 //对运算式进行输出 524 //cout << "运算式中数的个数为:" << intExpressieNumNum[i] << endl; 525 if (0 == intPrintStyle) 526 { 527 int fuhao = 0; 528 for (int shu = 0; shu < intExpressieNumNum[i]; shu++) 529 { 530 cout << intExpressieCanYu[i][shu]; 531 532 for (; fuhao < yunsuanfugeshu;) 533 { 534 cout << yunsuanfu[fuhao]; 535 fuhao++; 536 break; 537 } 538 } 539 } 540 else 541 { 542 543 int fuhao = 0; 544 for (int shu = 0; shu < intExpressieNumNum[i]; shu++) 545 { 546 outfile << intExpressieCanYu[i][shu]; 547 548 for (; fuhao < yunsuanfugeshu;) 549 { 550 outfile << yunsuanfu[fuhao]; 551 fuhao++; 552 break; 553 } 554 } 555 outfile << endl; 556 //outfile.close(); 557 } 558 } 559 } 560 561 } 562 } 563 else //如果有括号 564 { 565 //以下各个变量为个数的定义 566 int yunsuanfugeshu; //变量运算符的个数 567 yunsuanfugeshu = intExpressieNumNum[i] - 1; 568 char yunsuanfu[10]; //此数组存储运算符 569 570 int zuidakuohaoshu; //最大括号对数 571 if (intExpressieNumNum[i] > 2) 572 { 573 zuidakuohaoshu = intExpressieNumNum[i] - 2; 574 } 575 else 576 { 577 zuidakuohaoshu = intExpressieNumNum[i] - 1; 578 } 579 580 int suijidekuohaoduishu; //随机的括号的对数 581 if (zuidakuohaoshu > 4) 582 { 583 suijidekuohaoduishu = 1 + rand() % 4; 584 } 585 else 586 { 587 suijidekuohaoduishu = 1 + rand() % zuidakuohaoshu; 588 } 589 int kuohaogeshu = 2 * suijidekuohaoduishu; //总的括号数,不是对数 590 char zifushuzu[30]; //定义字符数组,存储括号和运算符 591 int zifushuzulength = kuohaogeshu + yunsuanfugeshu; //括号和运算符的总长度 592 593 //对字符数组进行初始化 594 for (int chushihua = 0; chushihua < 30; chushihua++) 595 { 596 zifushuzu[chushihua] = 'n'; 597 } 598 599 //以下循环为运算符数组赋值 600 for (int j = 0; j < yunsuanfugeshu; j++) //对运算符数组进行赋值 601 { 602 if (0 == intMultiplyDivide) //如果没有乘除法 603 { 604 int sign_noMD; //数字代表符号 605 sign_noMD = 1 + rand() % 2; 606 if (1 == sign_noMD) 607 { 608 yunsuanfu[j] = '+'; 609 } 610 else 611 { 612 yunsuanfu[j] = '-'; 613 } 614 } 615 else 616 { 617 int sign_yesMD; //数字代表运算符,且含有乘除法 618 sign_yesMD = 1 + rand() % 4; 619 if (1 == sign_yesMD) 620 { 621 yunsuanfu[j] = '+'; 622 } 623 else if (2 == sign_yesMD) 624 { 625 yunsuanfu[j] = '-'; 626 } 627 else if (3 == sign_yesMD) 628 { 629 yunsuanfu[j] = '*'; 630 } 631 else if (4 == sign_yesMD) 632 { 633 yunsuanfu[j] = '/'; 634 } 635 } 636 } 637 638 //以下将几对括号存入二维数组中 639 char kuohaoshuzu[20][2]; 640 for (int kuohaojishu = 0; kuohaojishu < suijidekuohaoduishu; kuohaojishu++) 641 { 642 kuohaoshuzu[kuohaojishu][0] = '('; 643 kuohaoshuzu[kuohaojishu][1] = ')'; 644 } 645 646 //对括号二维数组中的每行进行选择,选择在上面定义的字符数组中的位置 647 //以下不考虑括号在不在字符数组的首位有两种不同的情况,所以在此不考虑在首位的情况 648 srand((int)time(0)); 649 for (int erweishuzuhangshu = 0; erweishuzuhangshu < suijidekuohaoduishu; erweishuzuhangshu++) 650 { 651 int zuosuijishuweizhi; //此变量代表生成一个随机数,此随机数代表左括号的位置 652 int yousuijishuweizhi; //此变量代表生成一个随机数,此随机数代表有括号的位置 653 654 if (zifushuzulength == 3) 655 { 656 zifushuzu[0] = '('; 657 zifushuzu[2] = ')'; 658 } 659 else 660 { 661 int n1 = 0, n2 = 0; //退出循环的标记 662 zuosuijishuweizhi = 1 + rand() % (zifushuzulength - 3); //此处可能出错 663 while (n1 != 1) 664 { 665 if (zifushuzu[zuosuijishuweizhi] == 'n') 666 { 667 zifushuzu[zuosuijishuweizhi] = '('; 668 n1 = 1; 669 } 670 else 671 { 672 zuosuijishuweizhi = 1 + rand() % (zifushuzulength - 3); 673 } 674 } 675 int aa = zuosuijishuweizhi + 2; 676 yousuijishuweizhi = aa + rand() % (zifushuzulength - aa); 677 while (n2 != 1) 678 { 679 if (zifushuzu[yousuijishuweizhi] == 'n') 680 { 681 zifushuzu[yousuijishuweizhi] = ')'; 682 n2 = 1; 683 } 684 else 685 { 686 int aa = zuosuijishuweizhi + 2; 687 yousuijishuweizhi = aa + rand() % (zifushuzulength - aa); 688 } 689 } 690 } 691 692 693 } 694 695 //将运算符数组加入到字符数组中 696 for (int yunsuanfujishu = 0; yunsuanfujishu < yunsuanfugeshu; yunsuanfujishu++) 697 { 698 for (int qq = 0; qq < zifushuzulength; qq++) 699 { 700 if (zifushuzu[qq] == 'n') 701 { 702 zifushuzu[qq] = yunsuanfu[yunsuanfujishu]; 703 break; 704 } 705 } 706 } 707 708 //校正字符数组,使得其中不能有相邻的括号 709 //下面是校正左括号 710 for (int xianlinkuohao = 0; xianlinkuohao < (zifushuzulength - 1); xianlinkuohao++) 711 { 712 if (zifushuzu[xianlinkuohao] == '(' && zifushuzu[xianlinkuohao + 1] == ')') 713 { 714 for (int ji = 1; ji < zifushuzulength; ji++) 715 { 716 if ((zifushuzu[ji] != '(') && (zifushuzu[ji] != ')') && (zifushuzu[ji + 1] != ')') && (zifushuzu[ji - 1] != ')')) 717 { 718 char a; 719 a = zifushuzu[ji]; 720 zifushuzu[ji] = zifushuzu[xianlinkuohao]; 721 zifushuzu[xianlinkuohao] = a; 722 break; 723 } 724 } 725 } 726 if (zifushuzu[xianlinkuohao] == ')' && zifushuzu[xianlinkuohao + 1] == '(') 727 { 728 for (int ji = 1; ji < zifushuzulength; ji++) 729 { 730 if ((zifushuzu[ji] != '(') && (zifushuzu[ji] != ')') && (zifushuzu[ji + 1] != ')') && (zifushuzu[ji - 1] != ')')) 731 { 732 char a; 733 a = zifushuzu[ji]; 734 zifushuzu[ji] = zifushuzu[xianlinkuohao + 1]; 735 zifushuzu[xianlinkuohao + 1] = a; 736 break; 737 } 738 } 739 } 740 } 741 //下面是校正右括号 742 for (int xianlinkuohao = 0; xianlinkuohao < (zifushuzulength - 1); xianlinkuohao++) 743 { 744 if (zifushuzu[xianlinkuohao] == '(' && zifushuzu[xianlinkuohao + 1] == ')') 745 { 746 for (int ji = 1; ji < zifushuzulength; ji++) 747 { 748 if ((zifushuzu[ji] != '(') && (zifushuzu[ji] != ')') && (zifushuzu[ji + 1] != '(') && (zifushuzu[ji - 1] != '(')) 749 { 750 char a; 751 a = zifushuzu[ji]; 752 zifushuzu[ji] = zifushuzu[xianlinkuohao + 1]; 753 zifushuzu[xianlinkuohao + 1] = a; 754 break; 755 } 756 } 757 } 758 if (zifushuzu[xianlinkuohao] == ')' && zifushuzu[xianlinkuohao + 1] == '(') 759 { 760 for (int ji = 1; ji < zifushuzulength; ji++) 761 { 762 if ((zifushuzu[ji] != '(') && (zifushuzu[ji] != ')') && (zifushuzu[ji + 1] != '(') && (zifushuzu[ji - 1] != '(')) 763 { 764 char a; 765 a = zifushuzu[ji]; 766 zifushuzu[ji] = zifushuzu[xianlinkuohao]; 767 zifushuzu[xianlinkuohao] = a; 768 break; 769 } 770 } 771 } 772 } 773 774 775 //cout << "括号的个数为:" << kuohaogeshu<< endl; 776 //cout << "运算符的个数为:" << yunsuanfugeshu<< endl; 777 //cout << "字符数组的个数为:" << zifushuzulength << endl; 778 //对字符数组进行输出 779 /*cout << "字符数组为:"; 780 for (int ii = 0; ii < zifushuzulength; ii++) 781 { 782 cout << zifushuzu[ii] << " "; 783 } 784 cout << endl; 785 */ 786 /*//对运算式进行输出 787 int fuhaojishus0=0; 788 if (zifushuzulength == 3) 789 { 790 cout << "(" << intExpressieCanYu[i][0] << yunsuanfu[0] << intExpressieCanYu[i][1] << ")"; 791 } 792 else 793 { 794 for (int shuchushuzi = 0; shuchushuzi < intExpressieNumNum[i]; shuchushuzi++) 795 { 796 cout << intExpressieCanYu[i][shuchushuzi]; 797 for (; fuhaojishus0 < zifushuzulength;) 798 { 799 while (zifushuzu[fuhaojishus0] == ')') 800 { 801 cout << ")"; 802 fuhaojishus0 += 1; 803 } 804 if (fuhaojishus0 < zifushuzulength) 805 { 806 cout << zifushuzu[fuhaojishus0]; //输出右括号紧邻的运算符或者是输出第一个符号 807 } 808 809 fuhaojishus0 += 1; 810 while (zifushuzu[fuhaojishus0] == '(') 811 { 812 cout << "("; 813 fuhaojishus0 += 1; 814 } 815 break; 816 } 817 } 818 }*/ 819 820 if (0 == intPrintStyle) 821 { 822 int fuhaojishus0 = 0; 823 if (zifushuzulength == 3) 824 { 825 cout << "(" << intExpressieCanYu[i][0] << yunsuanfu[0] << intExpressieCanYu[i][1] << ")"; 826 } 827 else 828 { 829 for (int shuchushuzi = 0; shuchushuzi < intExpressieNumNum[i]; shuchushuzi++) 830 { 831 cout << intExpressieCanYu[i][shuchushuzi]; 832 for (; fuhaojishus0 < zifushuzulength;) 833 { 834 while (zifushuzu[fuhaojishus0] == ')') 835 { 836 cout << ")"; 837 fuhaojishus0 += 1; 838 } 839 if (fuhaojishus0 < zifushuzulength) 840 { 841 cout << zifushuzu[fuhaojishus0]; //输出右括号紧邻的运算符或者是输出第一个符号 842 } 843 844 fuhaojishus0 += 1; 845 while (zifushuzu[fuhaojishus0] == '(') 846 { 847 cout << "("; 848 fuhaojishus0 += 1; 849 } 850 break; 851 } 852 } 853 } 854 } 855 else 856 { 857 int fuhaojishus0 = 0; 858 if (zifushuzulength == 3) 859 { 860 outfile << "(" << intExpressieCanYu[i][0] << yunsuanfu[0] << intExpressieCanYu[i][1] << ")"; 861 outfile << endl; 862 } 863 else 864 { 865 for (int shuchushuzi = 0; shuchushuzi < intExpressieNumNum[i]; shuchushuzi++) 866 { 867 outfile << intExpressieCanYu[i][shuchushuzi]; 868 for (; fuhaojishus0 < zifushuzulength;) 869 { 870 while (zifushuzu[fuhaojishus0] == ')') 871 { 872 outfile << ")"; 873 fuhaojishus0 += 1; 874 } 875 if (fuhaojishus0 < zifushuzulength) 876 { 877 outfile << zifushuzu[fuhaojishus0]; //输出右括号紧邻的运算符或者是输出第一个符号 878 } 879 880 fuhaojishus0 += 1; 881 while (zifushuzu[fuhaojishus0] == '(') 882 { 883 outfile << "("; 884 fuhaojishus0 += 1; 885 } 886 break; 887 } 888 } 889 outfile << endl; 890 } 891 } 892 893 } 894 895 cout << endl; 896 } 897 898 outfile.close(); 899 900 return 0; 901 902 }