标签:
---恢复内容开始---
成功方面:这次的四则运算增加了运算结果和答对次数,在上次的运算中添加了括号
失败方面:但是因为括号的运算对于我们的编程能来说还是有些难度所以括号的运算并没有成功
一、题目避免重复
二、定制(数量/打印方式)
三、控制参数
1.是否有乘除法
2.是否有负数
3.是否有余数
4.答题控制小数点后2位
5.是否有括号
四、控制数字范围
(新要求)五、成功的表示了正确答案和题目答对次数
1 #include<iostream> 2 #include<stdio.h> 3 #include<time.h> 4 #include<fstream> 5 using namespace std; 6 7 int main() 8 { 9 ofstream outfile; 10 int i, a, b, c, d, e, f, g, l, m, o, x, y, z, count=0; 11 double finalnum = 0, cnum; 12 int number, muldiv, rnum, numf, neg, rem; //muldiv判断乘除,rnum判断随机数,numf数值范围,neg判断有无负数,rem判断有无余数 13 char psign, msign; //符号正负 14 int bra, braran; //bra判断是否有括号 15 int num, signnum; //num数字个数,signnum符号个数 16 int numarray[20]; //szsz数字数组 17 char fh[4] = { ‘+‘, ‘-‘, ‘*‘, ‘/‘ }, signarray[10];//fhsz符号数组 18 cout << "请输入要打印的数量:"; 19 cin >> number; 20 cout << "请输入数值范围:0-"; 21 cin >> numf; 22 cout << "是否有括号: 1、有 2、无 :"; 23 cin >> bra; 24 cout << "加减有无负数: 1、有 2、无 :"; 25 cin >> neg; 26 cout << "是否有乘除法: 1、有 2、无 :"; 27 cin >> muldiv; 28 if (muldiv == 1) 29 { 30 cout << "除法有无余数: 1、有 2、无 :"; 31 cin >> rem; 32 } 33 srand((unsigned)time(NULL));//时间种子,每次出现随机数不同 34 35 if (bra == 1) //判断是否有括号 36 { 37 braran = 3; 38 } 39 if (bra == 2) 40 { 41 braran = 2; 42 } 43 44 if (muldiv == 1) //判断是否有乘除 45 { 46 rnum = 4; 47 } 48 if (muldiv == 2) 49 { 50 rnum = 2; 51 } 52 for (i = 0; i < number; i++) 53 { 54 55 a = rand() % numf; 56 b = rand() % numf; 57 e = rand() % numf; 58 f = rand() % numf; 59 if (neg == 1) 60 { 61 l = rand() % 2; 62 m = rand() % 2; 63 if (l == 0) //加减有负数时,随机生成正负号 64 { 65 psign = ‘-‘; 66 } 67 if (l == 1) 68 { 69 psign = ‘ ‘; 70 } 71 if (m == 0) 72 { 73 msign = ‘-‘; 74 } 75 if (m == 1) 76 { 77 msign = ‘ ‘; 78 } 79 } 80 else 81 { 82 psign = msign = ‘ ‘; 83 } 84 switch (d = rand() % braran) 85 { 86 case 0: //整数 87 { 88 switch (c = rand() % rnum) 89 { 90 case 0:cout << psign << a << "+" << msign << b << "=" << endl; 91 cnum = a + b; 92 cin >> finalnum; 93 if (psign == ‘ ‘&&msign == ‘ ‘) 94 { 95 cnum = a + b; 96 } 97 if (psign == ‘-‘&&msign == ‘ ‘) 98 { 99 cnum = b - a; 100 } 101 if (psign == ‘ ‘&&msign == ‘-‘) 102 { 103 cnum = a - b; 104 } 105 if (psign == ‘-‘&&msign == ‘-‘) 106 { 107 cnum = -a - b; 108 } 109 if (int(finalnum * 100) == int(cnum * 100)) 110 { 111 cout << "答案正确!" << endl; 112 count++; 113 } 114 else 115 { 116 cout << "答案错误!" << endl; 117 cout << "正确答案为:" << cnum << endl; 118 } 119 break; 120 case 1:cout << psign << a << "-" << msign << b << "=" << endl; 121 cnum = a - b; 122 cin >> finalnum; 123 if (psign == ‘ ‘&&msign == ‘ ‘) 124 { 125 cnum = a - b ; 126 } 127 if (psign == ‘-‘&&msign == ‘ ‘) 128 { 129 cnum = -b - a ; 130 } 131 if (psign == ‘ ‘&&msign == ‘-‘) 132 { 133 cnum = a + b ; 134 } 135 if (psign == ‘-‘&&msign == ‘-‘) 136 { 137 cnum = -a + b ; 138 } 139 if (int(finalnum*100) ==int (cnum*100)) 140 { 141 cout << "答案正确!" << endl; 142 count++; 143 } 144 else 145 { 146 cout << "答案错误!" << endl; 147 cout << "正确答案为:" << cnum << endl; 148 } 149 break; 150 case 2:cout << a << "×" << b << "=" << endl; 151 cnum = a * b; 152 cin >> finalnum; 153 if (int(finalnum * 100) == int(cnum * 100)) 154 { 155 cout << "答案正确!" << endl; 156 count++; 157 } 158 else 159 { 160 cout << "答案错误!" << endl; 161 cout <<"正确答案为:"<< cnum << endl; 162 } 163 break; 164 case 3: 165 { 166 if (rem == 2) 167 { 168 while (b == 0) 169 { 170 b = rand() % numf; 171 } 172 o = a%b; 173 while (o != 0) 174 { 175 b = rand() % numf; 176 while (b == 0) 177 { 178 a = rand() % numf; 179 b = rand() % numf; 180 } 181 o = a%b; 182 } 183 } 184 else 185 { 186 while (b == 0) 187 { 188 b = rand() % numf; 189 } 190 } 191 cout << a << "÷" << b << "=" << endl; 192 cnum = a / b; 193 cin >> finalnum; 194 if (int(finalnum * 100) == int(cnum * 100)) 195 { 196 cout << "答案正确!" << endl; 197 count++; 198 } 199 else 200 { 201 cout << "答案错误!" << endl; 202 cout << "正确答案为:" << cnum << endl; 203 } 204 }break; 205 } 206 } break; 207 case 1://真分数 208 { 209 while (a == 0 || b == 0 || a >= b) 210 { 211 a = rand() % numf; 212 b = rand() % numf; 213 214 } 215 while (e == 0 || f == 0 || e >= f) 216 { 217 e = rand() % numf; 218 f = rand() % numf; 219 } 220 switch (g = rand() % rnum) 221 { 222 case 0: 223 { 224 cout << psign << a << "/" << b << "+" << msign << e << "/" << f << "=" << endl; 225 cin >> finalnum; 226 if (psign == ‘ ‘&&msign == ‘ ‘) 227 { 228 cnum = a / double (b) + e / double(f); 229 } 230 if (psign == ‘-‘&&msign == ‘ ‘) 231 { 232 cnum = e / double(f) - a / double (b); 233 } 234 if (psign == ‘ ‘&&msign == ‘-‘) 235 { 236 cnum = a / double (b) - e / double(f); 237 } 238 if (psign == ‘-‘&&msign == ‘-‘) 239 { 240 cnum = -a / double(b) - e / double(f); 241 } 242 if (int(finalnum * 100) == int(cnum * 100)) 243 { 244 cout << "答案正确!" << endl; 245 count++; 246 } 247 else 248 { 249 cout << "答案错误!" << endl; 250 cout << "正确答案为:" << cnum << endl; 251 } 252 break; 253 } 254 case 1: 255 { 256 cout << psign << a << "/" << b << "-" << msign << e << "/" << f << "=" << endl; 257 cin >> finalnum; 258 if (psign == ‘ ‘&&msign == ‘ ‘) 259 { 260 cnum = a / double(b) - e / double(f); 261 } 262 if (psign == ‘-‘&&msign == ‘ ‘) 263 { 264 cnum = -(e / double(f)) - (a / double(b)); 265 } 266 if (psign == ‘ ‘&&msign == ‘-‘) 267 { 268 cnum = (a / double(b)) + (e / double(f)); 269 } 270 if (psign == ‘-‘&&msign == ‘-‘) 271 { 272 cnum = -(a / double(b)) + (e / double(f)); 273 } 274 if (int(finalnum * 100) == int(cnum * 100)) 275 { 276 cout << "答案正确!" << endl; 277 count++; 278 } 279 else 280 { 281 cout << "答案错误!" << endl; 282 cout << "正确答案为:" << cnum << endl; 283 } 284 break; 285 }cout << "一共答对了:" << count << "题" << endl; 286 case 2: 287 { 288 cout << a << "/" << b << "×" << e << "/" << f << "=" << endl; 289 cnum = (a / double(b)) * (e / double(f)); 290 cin >> finalnum; 291 if (int(finalnum * 100) == int(cnum * 100)) 292 { 293 cout << "答案正确!" << endl; 294 count++; 295 } 296 else 297 { 298 cout << "答案错误!" << endl; 299 cout << "正确答案为:" << cnum << endl; 300 } 301 break; 302 } 303 case 3: 304 { 305 cout << a << "/" << b << "÷" << e << "/" << f << "=" << endl; 306 cnum = (a / double(b)) / (e / double(f)); 307 cin >> finalnum; 308 if (int(finalnum * 100) == int(cnum * 100)) 309 { 310 cout << "答案正确!" << endl; 311 count++; 312 } 313 else 314 { 315 cout << "答案错误!" << endl; 316 cout << "正确答案为:" << cnum << endl; 317 } 318 } 319 } break; 320 } break; 321 case 2: 322 { 323 num = 3 + rand() % 8; //有括号时,数字和符号数组 324 signnum = num - 1; 325 for (y = 0; y < num; y++) 326 { 327 numarray[y] = rand() % numf; 328 } 329 for (y = 0; y < signnum; y++) 330 { 331 x = rand() % 4; 332 signarray[y] = fh[x]; 333 } 334 signarray[signnum] = ‘=‘; 335 switch (z = signnum % 2) 336 { 337 case 0: 338 { 339 cout << numarray[0]; 340 cout << signarray[0]; 341 finalnum =finalnum+numarray[0]; 342 for (y = 1; y < signnum; y++) 343 { 344 cout << "("; 345 cout << numarray[y]; 346 cout << signarray[y]; 347 cout << numarray[y + 1]; 348 cout << ")"; 349 switch (y) 350 { 351 case‘+‘:finalnum = finalnum + numarray[y] + numarray[y + 1]; break; 352 case‘-‘:finalnum = finalnum + numarray[y] - numarray[y + 1]; break; 353 case‘*‘:finalnum = finalnum + numarray[y] * numarray[y + 1]; break; 354 case‘/‘:finalnum = finalnum + numarray[y] / numarray[y + 1]; break; 355 } 356 cout << signarray[y + 1]; 357 y = y + 1; 358 }cout << endl; 359 }break; 360 case 1: 361 { 362 for (y = 0; y < signnum; y++) 363 { 364 cout << "("; 365 cout << numarray[y]; 366 cout << signarray[y]; 367 cout << numarray[y + 1]; 368 cout << ")"; 369 switch (y) 370 { 371 case‘+‘:finalnum = finalnum + numarray[y] + numarray[y + 1]; break; 372 case‘-‘:finalnum = finalnum + numarray[y] - numarray[y + 1]; break; 373 case‘*‘:finalnum = finalnum + numarray[y] * numarray[y + 1]; break; 374 case‘/‘:finalnum = finalnum + numarray[y] / numarray[y + 1]; break; 375 } 376 cout << signarray[y + 1]; 377 y = y + 1; 378 } 379 cout << endl; 380 cin >> finalnum; 381 if (int(finalnum * 100) == int(cnum * 100)) 382 { 383 cout << "答案正确!" << endl; 384 count++; 385 } 386 else 387 cout << "答案错误!" << endl; 388 cout << "正确答案为:" << cnum << endl; 389 }break; 390 } 391 } break; 392 } 393 } 394 cout << "一共答对了:" << count << "题" << endl; 395 }
设计思想:
1.我们结对开发所以选择了较好的那个程序。
2.因为俩人分工合作,每个人负责不同的功能
3.我们先用一个数代表运算式的结果,然后再在这个数上根据运算符号添加运算
---恢复内容结束---
标签:
原文地址:http://www.cnblogs.com/zjy666/p/5296108.html