2014年1月23日 星期四

高中筆記存檔-C語言筆記(1) 宣告變數、if判斷句的應用

個人難度評分:★☆☆☆☆

這邊主要是放當初高中時,參加三天的學校營隊的程式筆記
建議先閱讀網路上一些教學文後,有一點點小程度的新手觀看

P.S.筆記時間為2014/1/23,blog發布時間為2016/12/8

程式要在哪裡寫?


寫程式的環境叫做IDE(Integrated Development Environment)
我個人習慣使用Dev-C++
下方的範例程式也都會在Dev-C++中運行

操作方式:建立檔案→儲存檔案→編譯程式→運行程式
關於編譯程式(compile)的詳細介紹,我會另外寫一篇文章介紹原理
現在只要知道按下去他就會編譯,編譯成功後,程式就可以跑了

程式長甚麼樣子?


這就是最基礎最基礎的程式
#include <stdio.h>

int main(void){
 printf("Hello, world!\n");
 system("pause");
 return 0;
}

運行結果


人生的第一支程式

#include <stdio.h>

int main(void){
 printf("安安你好OwO我是李健瑋\n");
 system("pause");
 return 0;
}

運行結果


整數宣告與運算

#include<stdio.h>
int main(void){
 int i=0;
 int j=0;
 int k=5;
 
 i=i+1;
 printf("%d\n",i+j);
 printf ("%d\n",k);
 return 0;
}

運行結果


宣告整數、浮點數、字元

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

int main(void){
 int i;
 char ch;
 float j;
 printf("請輸入整數、字元、小數:");
 scanf("%d %c %f",&i,&ch,&j);
 
 printf("你輸入的整數是:%d\n",i);
 printf("你輸入的字元是:%c\n",ch);
 printf("你輸入的小數是:%f\n",j);
 system("pause");
}

運行結果


浮點數除法運算

#include <stdio.h>

int main(void){
 int a,b;
 float c;
 a=7;
 b=2;
 
 c=(float)a/b;
 printf("%f",c);
}

運行結果


浮點數應用:華氏攝氏轉換

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

int main(void){
 double C,F;
 printf("請輸入華氏溫度:");
 scanf("%lf",&F);
 C = (double)(F -32)*5/9;
 printf("你輸入的是華氏 %lf 度 現在是攝氏 %f 度\n",F,C);
 system("pause");
}

運行結果


浮點數應用:算成績平均

#include <stdio.h>
#include <stdlib.h>
int main(void){
 float A,B,C,D,E,F;
 printf("請輸入成績:國文 英文 數學 自然 社會\n");
 scanf("%f %f %f %f %f",&A,&B,&C,&D,&E);
 F = (A+B+C+D+E)/5;
 printf("你的國文分數: %f\n",A);
 printf("你的英文分數: %f\n",B);
 printf("你的數學分數: %f\n",C);
 printf("你的自然分數: %f\n",D);
 printf("你的社會分數: %f\n",E);
 printf("你的總分: %f\n",F*5);
 printf("你的平均分數: %f\n",F);
 system("pause");
}

運行結果


if...else 條件式應用:判斷奇偶數、判斷程式

"/**/"之間包著的是判斷奇偶數程式
#include <stdio.h>
#include <stdlib.h>
int main(void){
/* int a=4;
 if(a%2==1){
  printf("a is odd");
 }
 else{
  printf("a is even");
 }*/
 int score;
 printf("請輸入你的成績:\n");
 scanf("%d",&score);
 if (score>=95)
  { printf("優等\n"); }
 else if (score>=90)
  { printf("甲等\n"); }
 else if (score>=80)
  { printf("乙等\n"); }
 else if (score>=70)
  { printf("丙等\n"); }
 else if (score>=60)
  { printf("丁等\n"); }
 else
  { printf("不及格\n"); }
}

運行結果


if...else 條件式應用:判斷季節

#include <stdio.h>
#include <stdlib.h>
int main(void){
 int month;
 printf("請輸入月份:");
 scanf("%d",&month);
 
 if (month >=1 && month<=3)
  printf(" %d 月是春天\n",month) ;
 else if (month<=6)
  printf(" %d 月是夏天\n",month) ;
 else if (month<=9)
  printf(" %d 月是秋天\n",month) ;
 else if (month<=12)
  printf(" %d 月是冬天\n",month) ;
 else
  printf("請問你是哪一國人?\n");
 system("pause");
}

運行結果


switch 條件式應用:

有一點需要注意一下,switch只能比較"單一"字元/整數/浮點數
不能以任何陣列的格式比較,字串亦然

另外還有case裡面的陳述句結尾記得要加個break,不然會一直往下跑下去

#include <stdio.h>

int main(void){
 int a=5,b=6;
 char ch;
 printf("請輸入+ - * / 任一字元\n");
 scanf("%c",&ch);
 
 switch (ch){
  case '+':
   printf("a+b= %d",a+b);
   break;
  case '-':
   printf("a-b= %d",a+b);
   break;
  case '*':
   printf("a*b= %d",a*b);
   break;
  case '/':
   printf("a/b= %f",(float)a/b);
   break;
 }
}

運行結果


if...else 條件式應用:計算折價

#include <stdio.h>

int main(void){
 int money;
 printf("請輸入消費金額:");
 scanf("%d",&money);
 
 if (money >=10000) 
  printf("打折後應付 %f\n",(float)money * 0.55);
 else if (money >=5000)
  printf("打折後應付 %f\n",(float)money * 0.6); 
 else if (money >=2000)
  printf("打折後應付 %f\n",(float)money * 0.7); 
 else
  printf("無打折優惠,應付 %d\n",money);
 printf("謝謝惠顧,歡迎再來!"); 
}

運行結果


沒有留言:

張貼留言