天天看點

20160317第二題

代碼複審

我負責的小夥伴是于悅,她用的是c語言,c-free來編寫的四則運算,以下是她的代碼

#include<stdio.h>
 
#include<stdlib.h>
#include<time.h>
main()
{
int a,b,op,os;
 
printf(" [四則運算來挑戰]\n");
aq1:    printf("選擇你想挑戰的運算法則\n");
printf("1.加法 2.減法 3.乘法 4.除法\n");
scanf("%d",&op);
switch(op)
{
aq: case 1:
srand((unsigned)time(NULL));
a=rand()%100+1;b=rand()%100+1;
printf("題目為:%d + %d = ?\n\n",a,b);
printf("1.檢視答案 2.做下一題\n");
scanf("%d",&os);
if(os==1)
{printf("%d + %d =%d\n\n\n",a,b,a+b);
goto aq1;
}
else
goto aq;
break;
at:  case 2:a=rand()%100+1;b=rand()%100+1;
printf("題目為:%d - %d = ?\n\n",a,b);
printf("1.檢視答案 2.做下一題\n");
scanf("%d",&os);
if(os==1)
{printf("%d - %d =%d\n\n\n",a,b,a-b);
goto aq1;
}
else
goto at;
break;
aq2:     case 3:
a=rand()%100+1;b=rand()%100+1;
printf("題目為:%d * %d = ?\n\n",a,b);
printf("1.檢視答案 2.做下一題\n");
scanf("%d",&os);
if(os==1)
{printf("%d * %d =%d\n\n\n",a,b,a*b);
goto aq1;
}
else
goto aq2;
aq3:     case 4:
a=rand()%100+1;b=rand()%100+1;
printf("題目為:%d / %d = ?\n\n",a,b);
printf("1.檢視答案 2.做下一題\n");
scanf("%d",&os);
if(os==1)
{printf("%d / %d =%d\n\n\n",a,b,a/b);
goto aq1;
}
else
goto aq3;
break;
}
}      

代碼的每一行都檢查過了,都是沒有問題的,也沒有一些沒必要的代碼,而且整個程式都可以運作出來。但是她和我出現了同樣的問題,都是沒有注釋。而且代碼的編寫也不規範,都沒有縮進,比如看if,else語句,因為都是左對齊的,很不容易去找到,再結合上面的内容,很不容看明白,這就造成了代碼整體的可讀性不高,不容易進行維護。

下一篇: 自我介紹