Try   HackMD

C語言教學 Part2 : 條件式&基本架構

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
基本介紹

https://www.codejudger.com/
帳號:aimee01@chsc.tw
密碼:19911223

Step 1: 基礎認識

數學模組

在C語言中,要呼叫數學模組,會以#include來表示:

#include<math.h>

若要用數學模組中的「次方」,會使用pow,若要開根號,會使用sprt的功能。

#include<stdio.h> #include<math.h> int main(){ int a=2,b=4,c; c=pow(a,2); //pow(a,2)為a的兩次方 printf("%d\n",c); c=pow(a,3); //pow(a,3)為a的三次方 printf("%d\n",c); c=pow(b,a); //pow(a,b)為a的b次方 printf("%d\n",c); c=sqrt(a);// c為a的開根號 printf("%d\n",c); c=sqrt(a+b);//c為a+b的開根號 printf("%d\n",c); return 0; }

4
8
16
1
2

TQC 106
請撰寫一程式,讓使用者輸入四個整數,依序分別為點A(x1,y1)及點 B(x2,y2)的座標值,接著計算兩點距離並輸出(四捨五入至小數點後第二位)。

兩點距離公式:=

(x2x1)2+(y2y1)2.

輸入說明:

四個整數

輸出說明:

計算兩點座標距離(四捨五入至小數點後第二位)

範例輸入:

3
2
4
-6

範例輸出:

8.06

參考程式碼:

#include<stdio.h> #include<math.h> int main(){ int x1,y1,x2,y2,pow1,pow2; float sqrt1; scanf("%d",&x1); scanf("%d",&y1); scanf("%d",&x2); scanf("%d",&y2); pow1=pow(x2-x1,2); pow2=pow(y2-y1,2); sqrt1=sqrt(pow1+pow2); printf("%.2f",sqrt1); return 0; }

在C語言的程式中,格式化輸出與python一樣

TQC 107
設計說明:

1.請撰寫一程式,讓使用者輸入六個整數,將每三個整數列印在同一列。
2.為了輸出美觀,每個整數給予10個欄位寬,並分別輸出靠右與靠左對齊。

輸入說明:
六個整數

輸出說明:
每三個整數輸出在同一列共分兩列,且每個整數給予10個欄位寬,分別輸出靠右與靠左對齊

範例輸入:

10
100
100000
100000
100
10

參考程式碼:

#include<stdio.h> int main(){ int a,b,c,d,e,f; scanf("%d",&a); scanf("%d",&b); scanf("%d",&c); scanf("%d",&d); scanf("%d",&e); scanf("%d",&f); printf("%10d %10d %10d\n",a,b,c); printf("%10d %10d %10d\n",d,e,f); printf("%-10d %-10d %-10d\n",a,b,c); printf("%-10d %-10d %-10d\n",d,e,f); return 0; }

TQC 108

請撰寫一程式,讓使用者輸入一整數為圓形的直徑,分別輸出直徑、半徑(四捨五入至小數點後第二位)及面積(四捨五入至小數點後第四位),欄位寬度皆為10個字元且靠左對齊

提示:圓周率請使用3.1415進行運算。

輸入說明

一個整數

輸出說明:
圓的直徑、半徑(四捨五入至小數點後第二位)、面積(四捨五入至小數點後第四位)

範例輸入:

90

範例輸出:

90
45.00
6361.5375

#include<stdio.h> int main(){ int a; scanf("%d",&a); printf("%-10d\n",a); printf("%-10.2f\n",a/2.0); printf("%-10.4f",(a/2.0)*(a/2.0)*3.1415); return 0; }

Step 2: 條件式

在C語言中,條件是有有兩種用法:

if條件式

C語言中的if與python類似,是以大括號所包住,程式範例如下

#include<stdio.h> int main(){ int a; scanf("%d",&a); if (a>0){ printf("大於0"); } else if (a<0){ printf("小於0"); } else{ printf("等於0"); } return 0; }

109

設計說明;
請撰寫一程式,讓使用者輸入分數,判斷此分數是否及格(及格分數為60分以上),若及格,則輸出「pass」;若不及格,則輸出「fail」,再判斷此分數為奇數或偶數,若為奇數,則輸出「odd」;若為偶數,則輸出「even」,若輸入的分數不在0~100中,則輸出「error」。

輸入說明
一個整數

輸出說明
該分數pass或fail與該數為odd或even;若分數不在0~100中,輸出error

範例輸入1
90
範例輸出1
pass
even
範例輸入2
59
範例輸出2
fail
odd
範例輸入3
101
範例輸出3
error

題意說明:
先判斷pass fail,若不符合則印出error, 再去判斷偶數基數
參考程式:

#include<stdio.h> int main(){ int x; scanf("%d",&x); if (60<=x&&x<=100){ //如果大於60且小於100就印出pass printf("pass\n"); if (x%2==0){ //如果是偶數就印出even printf("even"); } else{ printf("odd"); } } else if (0<=x&&60>x){ //如果大於0且小於60就印出fail printf("fail\n"); if (x%2==0){ printf("even"); } else{ printf("odd"); } } else{ //前兩個都不符合 printf("error"); } return 0; }

110

題目說明:
1.請撰寫一程式,讓使用者輸入三個整數a、b、c,並依序輸出(2)~(4)。
2.若a大於等於60且小於100則輸出1,否則輸出0。
3.計算b+1再除以10的值,四捨五入至小數點後第二位。
4.若a大於等於c,則輸出a,否則輸出c。
輸入說明
三個整數

輸出說明
依序輸出設計說明2~4

輸入輸出範例
範例輸入
70
100
60
範例輸出
1
10.10
70

題意說明:
按照上方題意輸出

#include<stdio.h> int main(){ int a,b,c; float d; scanf("%d",&a); //1. scanf("%d",&b); //1. scanf("%d",&c); //1. if (a>=60&&a<100){ //2. printf("1\n"); } else{ printf("0\n"); } d=(b+1)/10.0; //3. printf("%.2f\n",d); if (a>=c){ //4. printf("%d\n",a); } else{ printf("%d\n",c); } return 0; }

自行練習:201 202 203