加减法画面感的优化
1 #include "stdafx.h" 2 #include "stdio.h" 3 #include "stdlib.h" 4 #include "time.h" 5 #include "stdlib.h" 6 int gys(int a,int b); 7 int gbs(int a,int b); 8 int yue(int a,int b); 9 int fen(int a,int b); 10 11 12 int main(int argc, char* argv[]) 13 { bool Decide; 14 float m,n,result,answer; 15 int i=0,flag,flog,r; 16 int a,b,c,d; 17 int u1,u2,v,x,m1,n1,q,p; 18 int result1,answer1; 19 srand(time(0)); 20 while(i<30) 21 { 22 flag=rand()%5; 23 m = rand()%10; 24 n = rand()%10; 25 a = rand()%10; 26 b = rand()%10; 27 c = rand()%10; 28 d = rand()%10; 29 switch (flag) 30 { 31 case 0: 32 system("cls"); 33 printf("\n\n\n\n\n\n\n"); 34 printf(" %0.2f+%0.2f=",m,n); 35 result = m+n; 36 scanf("%f",&answer); 37 if(result == answer) 38 Decide = true; 39 else 40 Decide = false; 41 break; 42 case 1: 43 system("cls"); 44 printf("\n\n\n\n\n\n\n"); 45 printf(" %0.2f-%0.2f=",m,n); 46 result = m-n; 47 scanf("%f",&answer); 48 if(result == answer) 49 Decide = true; 50 else 51 Decide = false; 52 53 break; 54 case 2: 55 system("cls"); 56 printf("\n\n\n\n\n\n\n"); 57 printf(" %0.2f*%0.2f=",m,n); 58 result = m*n; 59 scanf("%f",&answer); 60 if(result == answer) 61 Decide = true; 62 else 63 Decide = false; 64 65 break; 66 case 3: 67 system("cls"); 68 printf("\n\n\n\n\n\n\n"); 69 if (n==0) 70 continue; 71 printf(" %0.2f/%0.2f=",m,n); 72 result= m/n; 73 scanf("%f",&answer); 74 r=(int)m%(int)n; 75 if (r==0) 76 printf(" 结果余数为零\n"); 77 else 78 printf(" 余数是%d\n",r); 79 if(result == answer) 80 Decide = true; 81 else 82 Decide = false; 83 break; 84 case 4: 85 system("cls"); 86 printf("\n\n\n\n\n\n\n"); 87 flog=rand()%4; 88 printf(" 计算结果,格式为分数\n"); 89 system("pause"); 90 if (b==0) 91 continue; 92 if (d==0) 93 continue; 94 switch (flog) 95 { 96 case 0 : 97 system("cls"); 98 printf("\n\n\n\n\n\n\n"); 99 printf(" %d/%d+%d/%d=",a,b,c,d); //分数加法 100 x=gbs(b,d); 101 u1=x/b*a; 102 u2=x/d*c; 103 v=u1+u2; 104 m1=yue(v,x); 105 n1=fen(v,x); 106 result1=m1/n1; 107 scanf("%d/%d",&q,&p); 108 answer1=q/p; 109 if(result1 == answer1) 110 Decide = true; 111 else 112 Decide = false; 113 break; 114 case 1: 115 system("cls"); 116 printf("\n\n\n\n\n\n\n"); 117 printf(" %d/%d*%d/%d=",a,b,c,d); //分数乘法 118 int u3,u4; 119 u3=a*c; 120 u4=b*d; 121 m1=yue(u3,u4); 122 n1=fen(u3,u4); 123 result1=m1/n1; 124 scanf("%d/%d",&q,&p); 125 answer1=q/p; 126 if(result1 == answer1) 127 Decide = true; 128 else 129 Decide = false; 130 break; 131 case 2: 132 system("cls"); 133 printf("\n\n\n\n\n\n\n"); 134 printf(" %d/%d-%d/%d=",a,b,c,d); //分数减法 135 int u5,u6,v1,x1; 136 x1=gbs(b,d); 137 u5=x1/b*a; 138 u6=x1/d*c; 139 v1=u5-u6; 140 m1=yue(v1,x1); 141 n1=fen(v1,x1); 142 result1=m1/n1; 143 scanf("%d/%d",&q,&p); 144 answer1=q/p; 145 if(result1 == answer1) 146 Decide = true; 147 else 148 Decide = false; 149 break; 150 case 3: 151 system("cls"); 152 printf("\n\n\n\n\n\n\n"); 153 printf(" %d/%d/%d/%d=",a,b,c,d); //分数除法 154 int u7,u8; 155 u7=a*d; 156 u8=b*c; 157 m1=yue(u7,u8); 158 n1=fen(u7,u8); 159 result1=m1/n1; 160 scanf("%d/%d",&q,&p); 161 answer1=q/p; 162 if(result1 == answer1) 163 Decide = true; 164 else 165 Decide = false; 166 break; 167 default: printf ("程序运行错误,请稍后重试!\n"); 168 system("pause"); 169 break; 170 } 171 break; 172 173 default: printf ("程序运行错误,请稍后重试!\n"); 174 system("pause"); 175 break; 176 } 177 if (flag==4) 178 { 179 if(Decide == true) 180 { 181 printf(" 答案正确\n"); 182 system("pause"); 183 } 184 else 185 { 186 printf(" 答案错误 正确答案是 %d/%d\n",m1,n1); 187 system("pause"); 188 } 189 } 190 191 else 192 { 193 if(Decide == true) 194 { 195 printf(" 答案正确\n"); 196 system("pause"); 197 } 198 else 199 { 200 printf(" 答案错误 正确答案是 %0.2f\n",result); 201 system("pause"); 202 } 203 } 204 205 206 207 } 208 return 0; 209 } 210 211 212 213 214 int gys(int a,int b) //求最大公约数 215 { 216 return b?gys(b,a%b):a; 217 } 218 219 int gbs(int a,int b) //求最小公倍数 220 { 221 return a*b/gys(a,b); 222 } 223 224 int yue(int a,int b) 225 { 226 int c,fz; 227 c=gys(a,b); 228 fz=a/c; 229 return fz; 230 } 231 232 int fen(int a,int b) 233 { 234 int c,fm; 235 c=gys(a,b); 236 237 fm=b/c; 238 return fm; 239 }
之前呢,加减法的程序功能完成的差不多,但感觉运行出来的结果画面太差了,感觉乱糟糟的。有密集恐惧症的人估计都要吐了吧!所以呢,我用
system("cls");进行了一下清屏,现在看着好多了。
以前程序遇到除数为零的情况时,除数会自动加一,有点简单粗暴。现在更简单了,用了一个continue直接跳出这次循环。顺道复习了一下break和continue的用法和不同点。
break
(1)break语句只能用于由while语句,do-while语句或for语句构成的循环结构中和switch选择结构中。
(2) 在嵌套循环的情况下,break语句只能终止并跳出包含它的最近一层的循环体。
(3) 在嵌套循环的情况下,如果想让break语句跳出最外层的循环体。那么可通过设置一标志变量tag,然后在每层循环后面加上一条语句:if(tag)break;其值为1表示跳出循环体,为零则不跳出。
continue
(1)continue语句只能用于由while语句,do-while语句或for语句构成的循环结构中。
(2)在嵌套循环的情况下,continue语句只对包含它的最里层的循环体语句起作用。