二柱子的四則運算第n版
阿新 • • 發佈:2018-10-31
作業要求:
1.實現線上答題
2.答題結束後,可以判斷對錯
3.並將錯題的結果儲存起來
設計思路:
整理要求,題目數量,資料範圍,四則運算,統計錯誤
程式程式碼:
1 #include<iostream> 2 #include<stdarg.h> 3 #include<stdlib.h> 4 #include<time.h> 5 #include<math.h> 6 using namespace std; 7 int main() 8 { 9 cout<<endl;10 cout<<"歡迎使用四則運算小程式"<<endl; 11 cout<<endl; 12 cout<<endl; 13 Again: 14 double Q1=1; 15 int Q01; 16 int Q2=1; 17 int Q3=100; 18 char Q4='y'; 19 int Q5=1; 20 char Q6='n'; 21 char Q7='n'; 22 double ans=0; 23 int right=0; 24 cout<<" 一.請輸入題目數量:"; 25 cin>>Q1; 26 Q01=floor(Q1);// cout<<"請輸入每行列印題目數(1-5):";// cin>>Q2; 27 cout<<" 二.行間距(正整數):"; 28 cin>>Q5; 29 cout<<" 三.請輸入算式中數值的最大值:"; 30 cin>>Q3; 31 while(1) 32 { 33 cout<<" 四.運算中需要乘除法嗎?需要:y;不需要:n"; 34 cin>>Q4; 35 cout<<" 五.減法中需要有負數嗎?需要y:;不需要:n"; 36 cin>>Q7; 37 38 39 cout<<"`````````````````````````````````````````````````````````````````````````"<<endl; 40 cout<<"開始答題:"<<endl; 41 srand(time(NULL)); 42 if(Q4=='y') 43 { 44 while(1) 45 { 46 if(Q1<1) 47 { 48 cout<<"輸入有誤,請重新輸入題目數量:"; 49 cin>>Q1; 50 Q01=floor(Q1); 51 } 52 else 53 { 54 for(int j=0;j<Q01;j++) 55 { 56 if(j!=0&&j%Q2==0) 57 { 58 for(int i=0;i<Q5;i++) 59 { 60 cout<<endl; 61 } 62 } 63 int num1=rand()%Q3; 64 int num2=rand()%Q3; 65 int sign=rand()%4; 66 switch(sign) 67 { 68 case 0: 69 cout<<j+1<<":"<<" "<<num1<<"+"<<num2<<"="<<"\t"; 70 cin>>ans; 71 if(ans==num1+num2) 72 { 73 cout<<"正確"<<endl; 74 right++; 75 } 76 else 77 { 78 cout<<"答錯了!"<<endl; 79 cout<<"正確答案是:"<<num1+num2<<endl; 80 } 81 break; 82 case 1: 83 if(Q7=='y') 84 { 85 cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<"\t"; 86 cin>>ans; 87 if(ans==num1-num2) 88 { 89 cout<<"正確"<<endl; 90 right++; 91 } 92 else 93 { 94 cout<<"答錯了!"<<endl; 95 cout<<"正確答案是:"<<num1-num2<<endl; 96 } 97 } 98 else 99 { 100 if(num1>num2) 101 { 102 cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<"\t"; 103 cin>>ans; 104 if(ans==num1-num2) 105 { 106 cout<<"正確"<<endl; 107 right++; 108 } 109 else 110 { 111 cout<<"答錯了!"<<endl; 112 cout<<"正確答案是:"<<num1-num2<<endl; 113 } 114 } 115 else 116 { 117 cout<<j+1<<":"<<" "<<num2<<"-"<<num1<<"="<<"\t"; 118 cin>>ans; 119 if(ans==num2-num1) 120 { 121 cout<<"正確"<<endl; 122 right++; 123 } 124 else 125 { 126 cout<<"答錯了!"<<endl; 127 cout<<"正確答案是:"<<num2-num1<<endl; 128 } 129 } 130 } 131 break; 132 case 2: 133 cout<<j+1<<":"<<" "<<num1<<"*"<<num2<<"="<<"\t"; 134 cin>>ans; 135 if(ans==num1*num2) 136 { 137 cout<<"正確"<<endl; 138 right++; 139 } 140 else 141 { 142 cout<<"答錯了!"<<endl; 143 cout<<"正確答案是:"<<num1*num2<<endl; 144 } 145 break; 146 case 3: 147 if(num2!=0) 148 { 149 cout<<j+1<<":"<<" "<<num1<<"/"<<num2<<"="<<"\t"; 150 cin>>ans; 151 if(ans==num1/num2) 152 { 153 cout<<"正確"<<endl; 154 right++; 155 } 156 else 157 { 158 cout<<"答錯了!"<<endl; 159 cout<<"正確答案是:"<<num1/num2<<endl; 160 } 161 } 162 else 163 { 164 j--; 165 } 166 break; 167 } 168 } 169 break; 170 } 171 } 172 cout<<"共答對"<<right<<"道題;"<<"答錯"<<Q01-right<<"道題。"; 173 cout<<"繼續努力!"<<endl; 174 break; 175 } 176 if(Q4=='n') 177 { 178 while(1) 179 { 180 if(Q01<1) 181 { 182 cout<<"輸入有誤,請重新輸入題目數量:"; 183 cin>>Q1; 184 Q01=floor(Q1); 185 } 186 else 187 { 188 for(int j=0;j<Q01;j++) 189 { 190 if(j!=0&&j%Q2==0) 191 { 192 for(int i=0;i<=Q5;i++) 193 { 194 cout<<endl; 195 } 196 } 197 int num1=rand()%Q3; 198 int num2=rand()%Q3; 199 int sign=rand()%2; 200 switch(sign) 201 { 202 case 0: 203 cout<<j+1<<":"<<" "<<num1<<"+"<<num2<<"="<<"\t";cin>>ans; 204 205 if(ans==num1+num2) 206 { 207 cout<<"正確"<<endl; 208 right++; 209 } 210 else 211 { 212 cout<<"答錯了!"<<endl; 213 cout<<"正確答案是:"<<num1+num2<<endl; 214 } 215 break; 216 case 1: 217 if(Q7=='y') 218 { 219 cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<"\t"; 220 cin>>ans; 221 if(ans==num1-num2) 222 { 223 cout<<"正確"<<endl; 224 right++; 225 } 226 else 227 { 228 cout<<"答錯了!"<<endl; 229 cout<<"正確答案是:"<<num1-num2<<endl; 230 } 231 break; 232 } 233 else 234 { 235 if(num1>num2) 236 { 237 cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<"\t"; 238 cin>>ans; 239 if(ans==num1-num2) 240 { 241 cout<<"正確"<<endl; 242 right++; 243 } 244 else 245 { 246 cout<<"答錯了!"<<endl; 247 cout<<"正確答案是:"<<num1-num2<<endl; 248 } 249 break; 250 } 251 else 252 253 { 254 cout<<j+1<<":"<<" "<<num2<<"-"<<num1<<"="<<"\t"; 255 cin>>ans; 256 257 if(ans==num2-num1) 258 { 259 cout<<"正確"; 260 } 261 else 262 { 263 cout<<"答錯了!"<<endl; 264 cout<<"正確答案是:"<<num2-num1<<endl; 265 } 266 break; 267 } 268 } 269 break; 270 } 271 } 272 } 273 break; 274 } 275 cout<<"共答對"<<right<<"道題;"<<"答錯"<<Q01-right<<"道題。"<<endl; 276 cout<<"繼續努力!"<<endl; 277 break; 278 } 279 else 280 { 281 cout<<"輸入有誤,請按要求輸入!"<<endl; 282 } 283 } 284 cout<<endl; 285 while(1) 286 { 287 cout<<"還需要繼續出題還是退出?(y:繼續出題;n:退出)"; 288 cin>>Q6; 289 if(Q6=='y') 290 { 291 goto Again; 292 } 293 if(Q6=='n') 294 { 295 goto Exit; 296 } 297 else 298 { 299 cout<<"輸入有誤,請重新輸入:"; 300 } 301 } 302 Exit: 303 return 0; 304 }
結果截圖:
實驗總結:
剛開始時覺得很難,沒有頭緒,但是將要求一個個列出後,再逐個解決拼接起來就出了完整的程式,雖然有一些錯誤在拼接時,但都一一解決了,將複雜問題簡單化很容易解決。