一、程序要求:
1、学生写的程序必须能够判定用户的输入答案是否正确;
2、程序必须能处理四种运算的混合算式;
3、在上次程序的基础上,添加新的功能。
二、程序设计思想:
1、在上次程序的基础之上添加要求;
2、根据助教提出的问题,添加了用户不按照要求输入的情况处理;
3、判断答案正误时,整数比分数容易判断(判断过程不予叙述),所以单独引入分数类,设计函数分别计算分数四则运算的结果并与输入的结果比较判断;
三、源程序代码:
1 //李俏,张莹荧,2016.3.15
2 //随机生成四则运算3 3 4 #include<iostream> 5 #include<stdio.h> 6 #include<stdlib.h> 7 #include<time.h> 8 using namespace std; 9 10 class Fenshu 11 { 12 private: 13 int fenzi; // 分子 14 int fenmu; // 分母 15 public: 16 Fenshu(int fz = 0, int fm = 1) :fenzi(fz), fenmu(fm){} 17 void set(int, int); 18 void simplify(); 19 friend bool judge1(const Fenshu &c1, const Fenshu &c2); //判断两个分数是否相等 20 friend bool judge2(const Fenshu &c1, int z); //判断两个分数是否相等 21 friend Fenshu add(const Fenshu &c1, const Fenshu &c2); //两个分数相加,结果要化简 22 friend Fenshu sub(const Fenshu &c1, const Fenshu &c2); //两个分数相减,结果要化简 23 friend Fenshu mul(const Fenshu &c1, const Fenshu &c2); //两个分数相乘,结果要化简 24 friend Fenshu div(const Fenshu &c1, const Fenshu &c2); //两个分数相除,结果要化简 25 }; 26 27 void Fenshu::set(int m, int n)// 分数赋值 28 { 29 fenzi = m; 30 fenmu = n; 31 } 32 33 void Fenshu::simplify()// 分数化简 34 { 35 int m, n, r; 36 m = abs(fenmu); 37 n = abs(fenzi); 38 while (r = m%n) // 求m,n的最大公约数 39 { 40 m = n; 41 n = r; 42 } 43 fenmu /= n; // 化简 44 fenzi /= n; 45 if (fenmu<0) // 将分母转化为正数 46 { 47 fenmu = -fenmu; 48 fenzi = -fenzi; 49 } 50 } 51 52 Fenshu add(const Fenshu &c1, const Fenshu &c2)// 分数相加 53 { 54 Fenshu t; 55 t.fenzi = c1.fenzi*c2.fenmu + c2.fenzi*c1.fenmu; 56 t.fenmu = c1.fenmu*c2.fenmu; 57 t.simplify(); 58 return t; 59 } 60 61 Fenshu sub(const Fenshu &c1, const Fenshu &c2)// 分数相减 62 { 63 Fenshu t; 64 t.fenzi = c1.fenzi*c2.fenmu - c2.fenzi*c1.fenmu; 65 t.fenmu = c1.fenmu*c2.fenmu; 66 t.simplify(); 67 return t; 68 } 69 70 Fenshu mul(const Fenshu &c1, const Fenshu &c2)// 分数相乘 71 { 72 Fenshu t; 73 t.fenzi = c1.fenzi*c2.fenzi; 74 t.fenmu = c1.fenmu*c2.fenmu; 75 t.simplify(); 76 return t; 77 } 78 79 Fenshu div(const Fenshu &c1, const Fenshu &c2)// 分数相除 80 { 81 Fenshu t; 82 if (!c2.fenzi) return c1; 83 t.fenzi = c1.fenzi*c2.fenmu; 84 t.fenmu = c1.fenmu*c2.fenzi; 85 t.simplify(); 86 return t; 87 } 88 89 bool judge1(const Fenshu &c1, const Fenshu &c2)//判断正误 90 { 91 bool flag; 92 if ((c1.fenzi == c2.fenzi) && (c1.fenmu == c2.fenmu)) 93 { 94 flag = true; 95 return flag; 96 } 97 else 98 { 99 flag = false; 100 return flag; 101 } 102 } 103 104 bool judge2(const Fenshu &c1, int z)//判断正误 105 { 106 bool flag; 107 if (0 == c1.fenzi - z) 108 { 109 flag = true; 110 return flag; 111 } 112 else 113 { 114 flag = false; 115 return flag; 116 } 117 } 118 119 int main() 120 { 121 int first, second, firstm, secondm; 122 int sign, i, j; 123 int num, chengchu, fushu, yushu, fanwei; 124 int ans, ansm, ansy; 125 int right = 0, wrong = 0; 126 Fenshu x1, x2, x, y; 127 128 cout << "请输入题目数量:"; 129 cin >> num; 130 if (num < 0) //题目数必须为正数 131 { 132 cout << "请重新输入有效的题目数:"; 133 cin >> num; 134 }; 135 if (num == 0) 136 { 137 exit(1); 138 } 139 140 cout << "是否有乘除法?1.是,2.否:"; 141 cin >> chengchu; 142 if ((chengchu != 1)||(chengchu != 2)) //只能选择1或2 143 { 144 cout << "请重新输入有效的数值:"; 145 cin >> chengchu; 146 }; 147 if (chengchu == 1) 148 { 149 cout << "除法是否有余数?1.是,2.否:"; 150 cin >> yushu; 151 if ((yushu != 1)||(yushu != 2)) //只能选择1或2 152 { 153 cout << "请重新输入有效的数值:"; 154 cin >> yushu; 155 }; 156 } 157 158 cout << "减法是否有负数?1.是,2.否:"; 159 cin >> fushu; 160 if ((fushu != 1)||(fushu != 2)) //只能选择1或2 161 { 162 cout << "请重新输入有效的数值:"; 163 cin >> fushu; 164 }; 165 cout << "请输入数值范围:"; 166 cin >> fanwei; 167 if (fanwei <= 0) //运算数必须为正数 168 { 169 cout << "请重新输入有效的范围:"; 170 cin >> fanwei; 171 }; 172 173 srand((int)time(NULL)); //用时间做种子,每次产生随机数都不一样 174 175 for (i = 0; i<num; i++) 176 { 177 j = rand() % 2; //选择生成整数运算还是分数运算 178 179 if (j == 0) //选择整数 180 { 181 first = rand() % (fanwei + 1); 182 second = rand() % (fanwei + 1); 183 if (chengchu == 1) 184 { 185 sign = rand() % 4; 186 } 187 else 188 { 189 sign = rand() % 2; 190 } 191 switch (sign) 192 { 193 case 0: //整数加法 194 cout << first << "+" << second << "=" << endl; 195 cout << "请输入结果:"; 196 cin >> ans; 197 if (ans == first + second) 198 { 199 cout << "答对了!" << endl; 200 right = right + 1; 201 } 202 else 203 { 204 cout << "答错了!" << endl; 205 wrong = wrong + 1; 206 } 207 break; 208 case 1: //整数减法 209 if (fushu == 1) 210 { 211 cout << first << "-" << second << "=" << endl; 212 cout << "请输入结果:"; 213 cin >> ans; 214 if (ans == first - second) 215 { 216 cout << "答对了!" << endl; 217 right = right + 1; 218 } 219 else 220 { 221 cout << "答错了!" << endl; 222 wrong = wrong + 1; 223 } 224 } 225 else 226 { 227 if (first>second) 228 { 229 cout << first << "-" << second << "=" << endl; 230 cout << "请输入结果:"; 231 cin >> ans; 232 if (ans == first - second) 233 { 234 cout << "答对了!" << endl; 235 right = right + 1; 236 } 237 else 238 { 239 cout << "答错了!" << endl; 240 wrong = wrong + 1; 241 } 242 } 243 else 244 { 245 cout << second << "-" << first << "=" << endl; 246 cout << "请输入结果:"; 247 cin >> ans; 248 if (ans == second - first) 249 { 250 cout << "答对了!" << endl; 251 right = right + 1; 252 } 253 else 254 { 255 cout << "答错了!" << endl; 256 wrong = wrong + 1; 257 } 258 } 259 } 260 break; 261 case 2: //整数乘法 262 cout << first << "*" << second << "=" << endl; 263 cout << "请输入结果:"; 264 cin >> ans; 265 if (ans == first * second) 266 { 267 cout << "答对了!" << endl; 268 right = right + 1; 269 } 270 else 271 { 272 cout << "答错了!" << endl; 273 wrong = wrong + 1; 274 } 275 break; 276 case 3: //整数除法 277 if (yushu == 1) 278 { 279 if (second != 0) 280 { 281 cout << first << "/" << second << "=" << endl; 282 cout << "请输入结果(商和余数):"; 283 cin >> ans >> ansy; 284 if ((ans == (first / second)) && (ansy == (first%second))) 285 { 286 cout << "答对了!" << endl; 287 right = right + 1; 288 } 289 else 290 { 291 cout << "答错了!" << endl; 292 wrong = wrong + 1; 293 } 294 } 295 else 296 { 297 i = i - 1; 298 } 299 } 300 else 301 { 302 if (second != 0 && (first%second == 0)) 303 { 304 cout << first << "/" << second << "=" << endl; 305 cout << "请输入结果:"; 306 cin >> ans; 307 if (ans == first / second) 308 { 309 cout << "答对了!" << endl; 310 right = right + 1; 311 } 312 else 313 { 314 cout << "答错了!" << endl; 315 wrong = wrong + 1; 316 } 317 } 318 else 319 { 320 i = i - 1; 321 } 322 } 323 break; 324 } 325 } 326 327 else //选择分数 328 { 329 first = rand() % (fanwei + 1); 330 second = rand() % (fanwei + 1);//分子 331 firstm = rand() % (fanwei + 1); 332 secondm = rand() % (fanwei + 1);//分母 333 if (chengchu == 1) 334 { 335 sign = rand() % 4; 336 } 337 else 338 { 339 sign = rand() % 2; 340 } 341 342 switch (sign) 343 { 344 case 0: //分数加法 345 if ((firstm != 0) && (secondm != 0) && (first<firstm) && (second<secondm)) 346 { 347 cout << first << "/" << firstm << " + " << second << "/" << secondm << "=" << endl; 348 cout << "请输入结果(分子和分母):"; 349 cin >> ans >> ansm; 350 x.set(ans, ansm); 351 x1.set(first, firstm); 352 x2.set(second, secondm); 353 y = add(x1, x2); 354 if (true == judge1(x, y)) 355 { 356 cout << "答对了!" << endl; 357 right = right + 1; 358 } 359 else 360 { 361 cout << "答错了!" << endl; 362 wrong = wrong + 1; 363 } 364 } 365 else 366 { 367 i = i - 1; 368 } 369 break; 370 case 1: //分数减法 371 if (fushu == 1) 372 { 373 if ((firstm != 0) && (secondm != 0) && (first<firstm) && (second<secondm)) 374 { 375 cout << first << "/" << firstm << " - " << second << "/" << secondm << "=" << endl; 376 cout << "请输入结果(分子和分母):"; 377 cin >> ans; 378 if (ans != 0) 379 { 380 cin >> ansm; 381 x.set(ans, ansm); 382 x1.set(first, firstm); 383 x2.set(second, secondm); 384 y = sub(x1, x2); 385 if (true == judge1(x, y)) 386 { 387 cout << "答对了!" << endl; 388 right = right + 1; 389 } 390 else 391 { 392 cout << "答错了!" << endl; 393 wrong = wrong + 1; 394 } 395 } 396 else 397 { 398 x1.set(first, firstm); 399 x2.set(second, secondm); 400 y = sub(x1, x2); 401 if (true == judge2(y, ans)) 402 { 403 cout << "答对了!" << endl; 404 right = right + 1; 405 } 406 else 407 { 408 cout << "答错了!" << endl; 409 wrong = wrong + 1; 410 } 411 } 412 } 413 else 414 { 415 i = i - 1; 416 } 417 } 418 else 419 { 420 if ((firstm != 0) && (secondm != 0) && (first<firstm) && (second<secondm) && ((first / firstm)>(second / secondm))) 421 { 422 cout << first << "/" << firstm << " - " << second << "/" << secondm << "=" << endl; 423 cout << "请输入结果(分子和分母):"; 424 cin >> ans; 425 if (ans != 0) 426 { 427 cin >> ansm; 428 x.set(ans, ansm); 429 x1.set(first, firstm); 430 x2.set(second, secondm); 431 y = sub(x1, x2); 432 if (true == judge1(x, y)) 433 { 434 cout << "答对了!" << endl; 435 right = right + 1; 436 } 437 else 438 { 439 cout << "答错了!" << endl; 440 wrong = wrong + 1; 441 } 442 } 443 else 444 { 445 x1.set(first, firstm); 446 x2.set(second, secondm); 447 y = sub(x1, x2); 448 if (true == judge2(y, ans)) 449 { 450 cout << "答对了!" << endl; 451 right = right + 1; 452 } 453 else 454 { 455 cout << "答错了!" << endl; 456 wrong = wrong + 1; 457 } 458 } 459 } 460 else 461 { 462 i = i - 1; 463 } 464 } 465 break; 466 case 2: //分数乘法 467 if ((firstm != 0) && (secondm != 0) && (first<firstm) && (second<secondm)) 468 { 469 cout << first << "/" << firstm << " * " << second << "/" << secondm << "=" << endl; 470 cout << "请输入结果(分子和分母):"; 471 cin >> ans; 472 if (ans != 0) 473 { 474 cin >> ansm; 475 x.set(ans, ansm); 476 x1.set(first, firstm); 477 x2.set(second, secondm); 478 y = sub(x1, x2); 479 if (true == judge1(x, y)) 480 { 481 cout << "答对了!" << endl; 482 right = right + 1; 483 } 484 else 485 { 486 cout << "答错了!" << endl; 487 wrong = wrong + 1; 488 } 489 } 490 else 491 { 492 x1.set(first, firstm); 493 x2.set(second, secondm); 494 y = sub(x1, x2); 495 if (true == judge2(y, ans)) 496 { 497 cout << "答对了!" << endl; 498 right = right + 1; 499 } 500 else 501 { 502 cout << "答错了!" << endl; 503 wrong = wrong + 1; 504 } 505 } 506 } 507 else 508 { 509 i = i - 1; 510 } 511 break; 512 case 3: //分数除法 513 if ((firstm != 0) && (secondm != 0) && (first != 0) && (second != 0) && (first<firstm) && (second<secondm)) 514 { 515 cout << first << "/" << firstm << " / " << second << "/" << secondm << "=" << endl; 516 cout << "请输入结果(分子和分母):"; 517 cin >> ans; 518 if (ans != 0) 519 { 520 cin >> ansm; 521 x.set(ans, ansm); 522 x1.set(first, firstm); 523 x2.set(second, secondm); 524 y = sub(x1, x2); 525 if (true == judge1(x, y)) 526 { 527 cout << "答对了!" << endl; 528 right = right + 1; 529 } 530 else 531 { 532 cout << "答错了!" << endl; 533 wrong = wrong + 1; 534 } 535 } 536 else 537 { 538 x1.set(first, firstm); 539 x2.set(second, secondm); 540 y = sub(x1, x2); 541 if (true == judge2(y, ans)) 542 { 543 cout << "答对了!" << endl; 544 right = right + 1; 545 } 546 else 547 { 548 cout << "答错了!" << endl; 549 wrong = wrong + 1; 550 } 551 } 552 } 553 else 554 { 555 i = i - 1; 556 } 557 break; 558 } 559 } 560 } 561 cout << "总共完成" << num << "道题,做对" << right << "道,做错" << wrong << "道题!" << endl;
562 return 0;
563 }
四、运行结果截图:
五、项目计划日志:
周活动总结表
姓名:张莹荧 日期:2016-03-19
日期/任务 | 听课 | 编写程序 | 阅读课本 | 准备考试 | 日总计 | ||
周日3.13 | 60 | 120 | 120 | 300 | |||
周一 | 300 | 30 | 30 | 120 | 480 | ||
周二 | 300 | 30 | 30 | 120 | 453 | ||
周三 | 200 | 180 | 180 | 560 | |||
周四 | 300 | 120 | 420 | ||||
周五 | 100 | 180 | 30 | 310 | |||
周六 | 60 | 60 | |||||
周总结 | 1200 | 540 | 210 | 660 | 2610 |
阶段时间和效率 周数:2
不包括上一周在内的累计时间
总计 | |||||||
平均 | |||||||
最大 | |||||||
最小 |
以前各周的累计时间
总计 | 1200 | 540 | 210 | 660 | 2610 | ||
平均 | 1200 | 540 | 210 | 660 | 2610 | ||
最大 | 1200 | 540 | 210 | 660 | 2610 | ||
最小 | 1200 | 540 | 210 | 660 | 2610 |
六、时间记录日志:
学生:张莹荧 日期:2016-03-19
教师:王建民 课程: PSP
日期 | 开始时间 | 结束时间 | 中断时间 | 净时间 | 活动 | 备注 |
3.16 |
14:30 16:10 |
16:00 17:40 |
0 0 |
90 90 |
编程 阅读课本 |
中间上网、休息 |
3.17 |
8:00 14:00 |
12:00 15:50 |
40 10 |
200 100 |
上课 | 课间休息 |
3.18 |
14:30 19:00 |
17:40 19:30 |
10 0 |
180 30 |
编程 阅读课本 |
中间休息 |
3.19 |
12:30 19:00 |
13:30 19:30 |
0 0 |
60 30 |
编程 阅读课本 |
|
七、缺陷记录日志:
学生: 张莹荧,李俏
日期: 2016-03-19
教员: 王建民
程序号: 2
日期 | 编号 | 类型 | 引入阶段 | 排除阶段 | 修复时间 | 修复缺陷 |
3.18 | 1 | 计算 | 设计 | 编译 | 30min | |
描述:分数部分判断正误时,分数该怎样计算结果。通过分数化简,通分等过程解决。 | ||||||
3.18 | 2 | 类与对象 | 编码 | 编译 | 5min | |
描述:在引用类中的变量时,出现错误。 | ||||||
3.18 | 3 | 余数 | 编码 | 编译 | 5min | |
描述:在整数除法中,需加上余数的正误判断。 | ||||||
3.19 | 4 | 答案 | 编码 | 编译 | 10min | |
描述:分数的减、乘、除运算,若结果为0,则出错。通过先判断分子是否为0来解决,若为0,则不需输入分母,若不为0,则需要继续输入分母并判断。 |