四则运算题生成程序
Posted spring_c
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四则运算题生成程序相关的知识,希望对你有一定的参考价值。
1 /** 2 *程序功能:没有实现中文或英文或日文的选择。 3 * 倒计时功能(这个功能我百度了一下 需要windows编程 和 多线程技术支持*实力不够 做不出来 我把它改成了一个记录答题时间) 4 *程序功能:实现了 判断对错 累计分数 记录答题时间(秒为单位) 5 * 支持多个运算符 支持括号 6 * 具体生成算数 是采用的switch分支结构 7 * 8 */ 9 #include<windows.h> 10 #include <algorithm> 11 #include<iostream> 12 #include<cstdlib> 13 #include<ctime> 14 using namespace std; 15 float Practice(); 16 float Addtion(); 17 float Subtract(); 18 float Multiply(); 19 float Modulo(); 20 void menu(); 21 int main() 22 { 23 int count = 0; 24 int right = 0; 25 int wrong = 0; 26 cout.setf(ios::fixed); 27 cout.precision(2); 28 srand((unsigned)time(NULL)); 29 float answer,write=0.00; 30 int check = 1; 31 32 while (check) 33 { 34 // 声明DWORD变量 记录时间 35 DWORD dwStart; 36 DWORD dwEnd; 37 DWORD useTime; 38 int choice; 39 menu(); 40 cin >> choice; 41 switch (choice) 42 { 43 case 1: 44 answer = Addtion(); 45 break; 46 case 2: 47 answer = Subtract(); 48 break; 49 case 3: 50 answer = Multiply(); 51 break; 52 case 4: 53 answer = Modulo(); 54 break; 55 case 5: 56 answer = Practice(); 57 break; 58 default: 59 break; 60 } 61 // 使用GetTickCount()来获取时间 62 dwStart = GetTickCount(); 63 cin >> write; 64 dwEnd = GetTickCount(); 65 useTime = dwEnd - dwStart; 66 cout << "答题时间为:"<< useTime/1000 <<"s"<< endl; 67 if (write == answer) 68 { 69 right++; 70 cout << "回答正确。" <<endl 71 <<"回答问题总分为 "<<++count<<" 回答正确分数为 "<< 72 right<<" 错误分数为 "<<wrong<< endl; 73 } 74 else 75 { 76 wrong++; 77 cout << "回答错误。" << endl 78 << "回答问题总分为 " << ++count << " 回答正确分数为 " << 79 right << " 错误分数为 " << wrong << endl; 80 cout << "正确答案为:"<<answer<<endl; 81 } 82 83 cout << "继续做题请按1, 退出请按0"<<endl; 84 cin >> check; 85 } 86 system("pause"); 87 return 0; 88 } 89 float Practice() 90 { 91 int range; 92 cout << "请输入多少数以内的四则运算:"; 93 cin >> range; 94 cout << endl; 95 int a[4] = { 0 }; 96 srand((unsigned)time(NULL));// 根据系统时间生成随机数 97 for (int i = 0; i < 4; i++) 98 a[i] = (rand() % range) + 1; 99 sort(a,a+4); // 将数组进行升序排列 100 cout << "请输入答案。(小数精确到后2位!)" << endl; 101 switch ((rand() % 16) + 1) 102 { 103 case 1: 104 cout << a[0]<< "*"<< a[1] <<"=" ; 105 return a[0] * a[1]; 106 case 2: 107 cout << a[0] << "+" << a[1] << "="; 108 return a[0] + a[1]; 109 case 3: 110 cout << a[0] << "-" << a[1] << "="; 111 return a[0] - a[1]; 112 case 4: 113 cout << a[0] << "/" << a[1] << "="; 114 return (a[0] / (a[1]*1.0)); 115 case 5: 116 cout << "(" << a[0] << "/" << a[1] << "+" << a[2] << ")" << "*" << a[3] << "="; 117 return (a[0] / (a[1] * 1.0) + a[2]) * a[3]; 118 case 6: 119 cout << "(" << a[0] << "-" << a[1] << "+" << a[2] << ")" << "/" << a[3] << "="; 120 return (a[0] - a[1] + a[2]) /a[3]; 121 case 7: 122 cout << a[0] << "/" << a[1] << "+" << a[2] << "*" << a[3] << "="; 123 return a[0] / (a[1] * 1.0) + a[2] * a[3]; 124 case 8: 125 cout << a[0] << "+" << a[1] << "+" << a[2] << "*" << a[3] << "="; 126 return a[0] + a[1] + a[2] * a[3]; 127 case 9: 128 cout << a[2] << "-" << a[1] << "+" << a[2] << "*" << a[3] << "="; 129 return a[2] - a[1] + a[2] * a[3]; 130 case 10: 131 cout << a[0] << "+" << a[1] << "-" << a[2] << "="; 132 return a[0] + a[1] - a[2]; 133 case 11: 134 cout << a[0] << "*" << a[1] << "-" << a[2] << "="; 135 return a[0] * a[1] - a[2]; 136 case 12: 137 cout << a[0] << "*" << a[1] << "/" << a[2] << "="; 138 return a[0] * a[1] / (a[2]*1.0); 139 case 13: 140 cout << a[0] << "*" << a[1] << "*" << a[2] << "="; 141 return a[0] * a[1] * a[2]; 142 case 14: 143 cout << "(" << a[0] << "-" << a[1] << ")" << "*" << " (" << a[2] << "/" << a[3] << ")" << "="; 144 return (a[0] - a[1]) * (a[2] / (a[3]*1.0)); 145 case 15: 146 cout << a[0] << " /" << a[1] << "+" << a[2] << "/" << a[3] << "="; 147 return a[0] / (a[1]*1.0) + a[2] / (a[3]*1.0); 148 case 16: 149 cout << "(" << a[0] << " /" << a[1] << "+" << a[2] << ")" << "/" << a[3] << "="; 150 return (a[0] / (a[1] * 1.0) + a[2]) / (a[3] * 1.0); 151 default: 152 cout << a[0] << "+" << a[1] << "="; 153 return a[0] + a[1]; 154 155 } 156 } 157 float Addtion() 158 { 159 int range; 160 cout << "请输入多少数以内的四则运算:"; 161 cin >> range; 162 cout << endl; 163 int a[4] = { 0 }; 164 srand((unsigned)time(NULL));// 根据系统时间生成随机数 165 for (int i = 0; i < 4; i++) 166 a[i] = (rand() % range) + 1; 167 sort(a, a + 4); // 将数组进行升序排列 168 cout << "请输入答案。(小数精确到后2位!)" << endl; 169 switch ((rand() % 9) + 1) 170 { 171 case 1: 172 cout << a[0] << "+" << a[1] << "="; 173 return a[0] + a[1]; 174 case 2: 175 cout << a[3] << "+" << a[1] <<"+"<<a[2]<< "="; 176 return a[3] + a[1]+a[2]; 177 case 3: 178 cout << a[3] << "+" << a[1] << "="; 179 return a[3] + a[1]; 180 case 4: 181 cout << a[4] << "+" << a[1] << "+"<<a[2]<<"+"<<a[3]<<"="; 182 return a[4]+a[3]+a[2]+a[1]; 183 case 5: 184 cout << a[0] << "+" << a[1] << "+" << a[2] << "+" << a[3] << "="; 185 return a[0] + a[1] + a[2] + a[3]; 186 case 6: 187 cout << a[3] << "+" << a[1] << "+" << a[2] << "+"<< a[2] << "="; 188 return a[3] + a[1] + a[2] + a[2]; 189 case 7: 190 cout << a[0] << "+" << a[1] << "+" << a[3]<<"/"<<a[3] << "+" << a[3] << "="; 191 return a[0] + a[1] + a[3]/a[3] + a[3]; 192 case 8: 193 cout << a[0] << "+" << a[1] << "+" << a[2] << "+" << a[3] << "="; 194 return a[0] + a[1] + a[2] + a[3]; 195 case 9: 196 cout << a[2] << "+" << a[1]<<"/"<<a[3] << "+" << a[2] << "+" << a[3] << "="; 197 return a[2] + a[1]/a[3] + a[2] + a[3]; 198 default: 199 cout << a[0]<<"/"<<a[3] << "+" << a[1] << "="; 200 return a[0]/a[3] + a[1]; 201 202 } 203 } 204 float Subtract() 205 { 206 int range; 207 cout << "请输入多少数以内的四则运算:"; 208 cin >> range; 209 cout << endl; 210 int a[4] = { 0 }; 211 srand((unsigned)time(NULL));// 根据系统时间生成随机数 212 for (int i = 0; i < 4; i++) 213 a[i] = (rand() % range) + 1; 214 sort(a, a + 4); // 将数组进行升序排列 215 cout << "请输入答案。(小数精确到后2位!)" << endl; 216 switch ((rand() % 9) + 1) 217 { 218 case 1: 219 cout << a[0] << "-" << a[1] << "="; 220 return a[0] - a[1]; 221 case 2: 222 cout << a[3] << "-" << a[1] << "-" << a[2] << "="; 223 return a[3] - a[1] - a[2]; 224 case 3: 225 cout << a[3] << "-" << a[1] << "="; 226 return a[3] - a[1]; 227 case 4: 228 cout << a[4] << "-" << a[1] << "-" << a[2] << "-" << a[3] << "="; 229 return a[4] - a[3] - a[2] - a[1]; 230 case 5: 231 cout << a[0] << "-" << a[1] << "-" << a[2] << "-" << a[3] << "="; 232 return a[0] - a[1] - a[2] - a[3]; 233 case 6: 234 cout << a[3] << "-" << a[1] << "-" << a[2] << "-" << a[2] << "="; 235 return a[3] - a[1] - a[2] - a[2]; 236 case 7: 237 cout << a[0] << "-" << a[1] << "-" << a[3] << "/" << a[3] << "-" << a[3] << "="; 238 return a[0] - a[1] - a[3] / a[3] - a[3]; 239 case 8: 240 cout << a[0] << "-" << a[1] << "-" << a[2] << "-" << a[3] << "="; 241 return a[0] - a[1] - a[2] - a[3]; 242 case 9: 243 cout << a[2] << "-" << a[1] << "/" << a[3] << "-" << a[2] << "-" << a[3] << "="; 244 return a[2] - a[1] / a[3] - a[2] - a[3]; 245 default: 246 cout << a[0] << "/" << a[3] << "-" << a[1] << "="; 247 return a[0] / a[3] - a[1]; 248 249 } 250 } 251 float Modulo() 252 { 253 int range; 254 cout << "请输入多少数以内的四则运算:"; 255 cin >> range; 256 cout << endl; 257 int a[4] = { 0 }; 258 srand((unsigned)time(NULL));// 根据系统时间生成随机数 259 for (int i = 0; i < 4; i++) 260 a[i] = (rand() % range) + 1; 261 sort(a, a + 4); // 将数组进行升序排列 262 cout << "请输入答案。(小数精确到后2位!)" << endl; 263 switch ((rand() % 4) + 1) 264 { 265 case 1: 266 cout << "(" << a[0] << "/" << a[1] << ")" << "+" << "(" << a[2] << "/" << a[3] << ")" << "=" << endl; 267 return (a[0] / (a[1]*1.0)) + (a[2] / (a[3]*1.0)); 268 case 2: 269 cout << "(" << a[0] << "/" << a[0] << ")" << "+" << "(" << a[2] << "/" << a[3] << ")" << "=" << endl; 270 return (a[0] / a[0]) + (a[2] / (a[3]*1.0)); 271 case 3: 272 cout << "(" << a[1] << "/" << a[1] << ")" << "+" << "(" << a[0] << "/" << a[3] << ")" << "=" << endl; 273 return (a[1] / (a[1]*1.0)) + (a[0] / (a[3]*1.0)); 274 case 4: 275 cout << "(" << a[0] / a[2] << ")" << "-" << a[2] << "/" << a[3] << "=" << endl; 276 return a[0] / (a[2]*1.0) + a[2] / (a[3]*1.0); 277 default: 278 cout << "(" << a[1] << "/" << a[1] << ")" << "+" << "(" << a[0] << "/" << a[3] << ")" <<"="<< endl; 279 return (a[1] / (a[1]*1.0)) + (a[0] / (a[3]*1.0)); 280 281 } 282 } 283 float Multiply() 284 { 285 int range; 286 cout << "请输入多少数以内的四则运算:"; 287 cin >> range; 288 cout << endl; 289 int a[4] = { 0 }; 290 srand((unsigned)time(NULL));// 根据系统时间生成随机数 291 for (int i = 0; i < 4; i++) 292 a[i] = (rand() % range) + 1; 293 sort(a, a + 4); // 将数组进行升序排列 294 cout << "请输入答案。(小数精确到后2位!)" << endl; 295 switch ((rand() % 9) + 1) 296 { 297 case 1: 298 cout << a[2] << "*" << a[1] << "="; 299 return a[2] * a[1]; 300 case 2: 301 cout << a[3] << "*" << a[1] << "*" << a[2] << "="; 302 return a[3] * a[1] * a[2]; 303 case 3: 304 cout << a[3] << "*" << a[1] << "="; 305 return a[3] * a[1]; 306 case 4: 307 cout << a[4] << "*" << a[1] << "*" << a[2] << "*" << a[3] << "="; 308 return a[4] * a[3] * a[2] * a[1]; 309 case 5: 310 cout << a[0] << "*" << "(" << a[1] << "/" << a[3] << ")" << "*" << a[2] << "*" << a[3] << "="; 311 return a[0] * (a[1] / (a[3]*1.0)) * a[2] * a[3]; 312 case 6: 313 cout << "(" << a[2] << "/" << a[3] << ")" << "*" << "(" << a[1] << "/" << a[2] << ")" << "*" << a[2] << "="; 314 return (a[2] / (a[3]*1.0)) * (a[1] / (a[2]*1.0)) * a[2]; 315 case 7: 316 cout << a[0] << "*" << a[1] << "*" << "(" << a[3] << "/" << a[3] << ")" << "*" << a[3] << "="; 317 return a[0] * a[1] * (a[3] / (a[3]*1.0)) * a[3]; 318 case 8: 319 cout << a[2] << "*" << a[1] << "*" << a[2] << "*" << a[3] << "="; 320 return a[2] * a[1] * a[2] * a[3]; 321 case 9: 322 cout << a[2] << "*" << "(" << a[1] << "/" << a[3] << ")" << "*" << a[2] << "*" << a[3] << "="; 323 return a[2] * (a[1] / (a[3]*1.0)) * a[2] * a[3]; 324 default: 325 cout << a[0] << "*" << a[3] << "*" << a[1] << "="; 326 return a[0] * a[3] * a[1]; 327 328 } 329 } 330 void menu() 331 { 332 cout << "1 加法运算 " << " 2 减法运算 " << " 3 乘法运算 " << " 4 除法运算 " << " 5 混合运算"<<endl; 333 cout << "请输入你要做的题目类型:"; 334 }
以上是关于四则运算题生成程序的主要内容,如果未能解决你的问题,请参考以下文章