Coding Style

make a cleaning code

排版

神馬是排版?

先來個例子

這是你剛剛寫好的Code

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
int x[5];
int i,j,temp;
while(1){
printf("Please enter five integers:  ");
scanf("%d %d %d %d %d",&x[1],&x[2],&x[3],&x[4],&x[5]);
for(i=1;i<=5;i++){for(j=1;j<=5;j++){if(x[i]<x[j])
{temp=x[i];
x[i]=x[j];
x[j]=temp;}}}
printf("The largest is %d\nThe smallest is %d\n",x[5],x[1]);}
system("pause");	
return 0;
}

當下寫完的感覺

然後當按下執行後

他......爆了

然後你開始檢查哪裡有錯

看一下剛剛的程式碼

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
int x[5];
int i,j,temp;
while(1){
printf("Please enter five integers:  ");
scanf("%d %d %d %d %d",&x[1],&x[2],&x[3],&x[4],&x[5]);
for(i=1;i<=5;i++){for(j=1;j<=5;j++){
if(x[i]<x[j])
{temp=x[i];
x[i]=x[j];
x[j]=temp;}
}
printf("The largest is %d\nThe smallest is %d\n",x[5],x[1]);}
system("pause");	
return 0;
}

根本看不出來哪裡有錯!

然後求助助教

助教助教 幫我Debug一下 看不出哪裡有錯

助教看了一下你的程式碼

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
int x[5];
int i,j,temp;
while(1){
printf("Please enter five integers:  ");
scanf("%d %d %d %d %d",&x[1],&x[2],&x[3],&x[4],&x[5]);
for(i=1;i<=5;i++){for(j=1;j<=5;j++){
if(x[i]<x[j])
{temp=x[i];
x[i]=x[j];
x[j]=temp;}
}
printf("The largest is %d\nThe smallest is %d\n",x[5],x[1]);}
system("pause");	
return 0;
}

這......助教也看不懂啊

所以請使用排版!!

Start using 排版

  • 分號、括號後,不要再有任何程式碼
  • 每層括號裡面請縮排一次(Tab鍵)
  • 一些符號後面請加上一個空白

先整理大括號

然後......縮排

縮排

再縮排

符號後面加一個空格

然後我們發現

大括號勒?

}

所以要排版

所以要排版

所以要排版

因為很重要 所以要講三次

Coding Style 排版

By JasonPan

Coding Style 排版

  • 385