定制題目數量這個功能測試:
(1)輸入題目數為負數時:
可正确處理;
(2)輸入題目數量為0時:
可正确處理;
(3)輸入題目數量為小數時:
程式運作出錯;
錯誤分析:
因為代碼中題目數量的變量Ques1定義為int類型,無法處理小數。
解決方法:
将Ques1定義為double類型,然後使用floor()函數,将Ques1取整。
輸入10.1:
可以正常處理。
修改後完整代碼:
1 #include<iostream.h>
2 #include<stdlib.h>
3 #include<time.h>
4 #include<math.h>
5 int main()
6 {
7 double Ques1=1;
8 int Ques01;
9 int Ques2=1;
10 int Ques3=100;
11 char Ques4='y';
12 int Ques5=1;
13 char Ques6='n';
14 char Ques7='n';
15 Again:
16 cout<<"請輸入題目數量:";
17 cin>>Ques1;
18 Ques01=floor(Ques1);
19 cout<<"請輸入每行列印題目數(1-5):";
20 cin>>Ques2;
21 cout<<"請輸入算式中數值的最大值:";
22 cin>>Ques3;
23 while(1)
24 {
25 cout<<"運算中需要乘除法嗎?y:需要;n:不需要";
26 cin>>Ques4;
27 cout<<"減法運算需要有負數嗎?y:需要;n:不需要";
28 cin>>Ques7;
29 cout<<"行間距(正整數):";
30 cin>>Ques5;
31 srand(time(NULL));
32 if(Ques4=='y')
33 {
34 while(1)
35 {
36 if(Ques1<1)
37 {
38 cout<<"輸入有誤,請重新輸入題目數量:";
39 cin>>Ques1;
40 Ques01=floor(Ques1);
41 }
42 else
43 {
44 for(int j=0;j<Ques01;j++)
45 {
46 if(j!=0&&j%Ques2==0)
47 {
48 for(int i=0;i<Ques5;i++)
49 {
50 cout<<endl;
51 }
52 }
53 int num1=rand()%Ques3;
54 int num2=rand()%Ques3;
55 int sign=rand()%4;
56 switch(sign)
57 {
58 case 0:
59 cout<<j+1<<":"<<" "<<num1<<"+"<<num2<<"="<<" ";
60 break;
61 case 1:
62 if(Ques7=='y')
63 {
64 cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<" ";
65 }
66 else
67 {
68 if(num1>num2)
69 {
70 cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<" ";
71 }
72 else
73 {
74 cout<<j+1<<":"<<" "<<num2<<"-"<<num1<<"="<<" ";
75 }
76 }
77 break;
78 case 2:
79 cout<<j+1<<":"<<" "<<num1<<"*"<<num2<<"="<<" ";
80 break;
81 case 3:
82 if(num2!=0)
83 {
84 cout<<j+1<<":"<<" "<<num1<<"/"<<num2<<"="<<" ";
85 }
86 else
87 {
88 j--;
89 }
90 break;
91 }
92 }
93 break;
94 }
95 }
96 break;
97 }
98 if(Ques4=='n')
99 {
100 while(1)
101 {
102 if(Ques01<1)
103 {
104 cout<<"輸入有誤,請重新輸入題目數量:";
105 cin>>Ques1;
106 Ques01=floor(Ques1);
107 }
108 else
109 {
110 for(int j=0;j<Ques01;j++)
111 {
112 if(j!=0&&j%Ques2==0)
113 {
114 for(int i=0;i<=Ques5;i++)
115 {
116 cout<<endl;
117 }
118 }
119 int num1=rand()%Ques3;
120 int num2=rand()%Ques3;
121 int sign=rand()%2;
122 switch(sign)
123 {
124 case 0:
125 cout<<j+1<<":"<<" "<<num1<<"+"<<num2<<"="<<" ";
126 break;
127 case 1:
128 if(Ques7=='y')
129 {
130 cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<" ";
131 break;
132 }
133 else
134 {
135 if(num1>num2)
136 {
137 cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<" ";
138 break;
139 }
140 else
141 {
142 cout<<j+1<<":"<<" "<<num2<<"-"<<num1<<"="<<" ";
143 break;
144 }
145 }
146 break;
147 }
148 }
149 }
150 break;
151 }
152 break;
153 }
154
155 else
156 {
157 cout<<"輸入有誤,請按要求輸入!"<<endl;
158 }
159 }
160 cout<<endl;
161 while(1)
162 {
163 cout<<"還需要繼續出題還是退出?(y:繼續出題;n:退出)";
164 cin>>Ques6;
165 if(Ques6=='y')
166 {
167 goto Again;
168 }
169 if(Ques6=='n')
170 {
171 goto Exit;
172 }
173 else
174 {
175 cout<<"輸入有誤,請重新輸入:";
176 }
177 }
178 Exit:
179 return 0;
180 }