計算機程式(三上)-上課筆記
IDE開發軟件選用
Dev-C++
⬇⬇⬇文章開始⬇⬇⬇
變數資料型態
1 2 3 4 5 6
| char int long short float double
|
變數名稱
變數名稱可以是英文字母、數字或底線
- 名稱中不能有空白字元
- 第一個字元不能是數字
- 不能使用到關鍵字
1 2 3 4 5
| intel_4x _AMD 2dos my dogs int
|
占位符的使用
%d
為整數佔位符,10進製表示,默認有符號,佔4字節
%u
為整數佔位符,10進製表示,無符號表示,最高位算作值的一部分
%o
為整數佔位符,8進製表示
%x
為整數佔位符,16進製表示
%f
為浮點數佔位符
%s
為字符串佔位符
%c
為字符佔位符
程式碼
109/09/26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <stdio.h> #include <stdlib.h>
int main(void) { int number; number = 2; printf("I have %d cats.\n",number); printf("You have %d cats,too.\n",number); system("pause"); return 0; }
|
109/10/03
3.26-12
試寫一程式碼,利用設定字元變數ch為ASCII碼的方式讓電腦發出一個警告音
(警告音的ASCII為7
)
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include <stdio.h> #include <stdlib.h>
int main(void) { char beep='\a';
printf("%c",beep); printf("My ASCII is %d\n",beep);
system("pause"); return 0; }
|
- 輸出結果為:
1 2
| My ASCII is 7 請按任意鍵繼續 . . .
|
3.26-14
請參閱下面的程式碼,然後回答問題:
1 2 3 4 5 6 7 8 9 10 11
| #include <stdio.h> #include <stdlib.h>
int main(void) { unsigned short num=80000; printf("%d\n",num); system("pause"); return 0; }
|
試說明執行此程式的結果,為甚麼是14464
,而不是80000這個數字?
因為unsigned short
的範圍為0~65535(0 ~ 2¹⁶-1)
如果想讓本題第7行的執行結果恰好是80000
,應如何修改程式碼?
int 或 long 的範圍為0~4294967295(0 ~ 2³²-1)
1 2 3 4 5 6 7 8 9 10 11
| #include <stdio.h> #include <stdlib.h>
int main(void) { unsigned int num=80000; printf("%d\n",num); system("pause"); return 0; }
|
3.26-15
請參閱下面的程式碼,然後回答接續問題:
1 2 3 4 5 6 7 8 9 10 11 12
| #include <stdio.h> #include <stdlib.h>
int main(void) { float num1=30000.1F; float num2=0.0004F; printf("%f\n",num1+num2); system("pause"); return 0; }
|
試執行此代碼,您會得到什麼結果?
30000.100586
於數學上,30000.1+0.0004=30000.1004,試說明執行此程式碼後,為什麼得不到完整的這個結果?
double
型態佔8個位元組,數字介於 ±1.7E⁻³⁰⁸ 和 ±1.7E³⁰⁸ 之間
float
型態佔8個位元組,數字介於 ±3.4E⁻³⁸ 和 ±3.4E³⁸ 之間
float
只有7~8個位數的單精確度,double
可達15~16個位數的雙精確度。
如果像讓本題的執行結果恰好為30000.1004
,應如何改進?試撰寫一個完整的程式碼來改進。
1 2 3 4 5 6 7 8 9 10 11 12
| #include <stdio.h> #include <stdlib.h>
int main(void) { double num1=30000.1; double num2=0.0004; printf("%9.4f\n",num1+num2); system("pause"); return 0; }
|
- 輸出結果為:
1 2
| 30000.1004 請按任意鍵繼續 . . .
|
3.26-18
假設浮點數變數num1與num2的值分別為123.39f與3.8e5f,試撰寫一程式,將這兩個程式變數值轉換為整數。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <stdio.h> #include <stdlib.h>
int main(void) { int n1,n2; float num1 = 123.39F; float num2 = 3.8e5F; n1 = (int) num1; n2 = (int) num2; printf("num1 = %d\n",n1); printf("num2 = %d\n",n2); system("pause"); return 0; }
|
- 輸出結果為:
1 2 3
| num1 = 123 num2 = 380000 請按任意鍵繼續 . . .
|
3.26-19
1 2 3 4 5 6 7 8 9 10 11 12
| #include <stdlib.h> #include <stdio.h>
int main(void) { int num1=5; int num2=8; printf("%d\n",num1/num2); system("pause"); return 0; }
|
試解釋第七行的輸出結果為何是0
?
因為5/8是0.625,%d
只輸出整數部分
試修改程式碼,利用型態轉換的方式,使得第七行的輸出結果為0.625000。
1 2 3 4 5 6 7 8 9 10 11 12
| #include <stdlib.h> #include <stdio.h>
int main(void) { double num1=5; double num2=8; printf("%f\n",num1/num2);
system("pause"); return 0; }
|
109/10/17
4.35-7
試撰寫一程式,將浮點數變數num=12.34f以下圖的格式印出
(小數點前面有4位,小數點後有2位,不滿欄位長度時填入0,並印出其變數的正負號)
1 2 3 4 5 6 7 8
| #include <stdio.h> #include <stdlib.h>
int main(void) { double num=12.34; printf("num=%+08.2f",num); }
|
4.36-9
下面的程式碼試想設計從鍵盤讀入一個整數,並設定給變數num存放。此程式於執行時會發生錯誤,試指出錯誤之所在,並試著修正之,使得程式可以正確的執行。
1 2 3 4 5 6 7 8 9 10 11 12
| #include <stdio.h> #include <stdlib.h>
int main(void) { int num; scanf("%d",num); printf("num=%d\n",num); system("pause"); return 0; }
|
- 正確解答
1 2 3 4 5 6 7 8 9 10 11 12
| #include <stdio.h> #include <stdlib.h>
int main(void) { int num; scanf("%d",&num); printf("num=%d\n",num); system("pause"); return 0; }
|
4.36-10
試撰寫一程式,利用scanf()函數輸入兩個整數,然後以printf()函數列印出這兩個整數的乘積。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #include <stdio.h> #include <stdlib.h>
int main(void) { int num1,num2; printf("My num1="); scanf("%d",&num1); printf("My num2="); scanf("%d",&num2); printf("num=%d\n",num1*num2); system("pause"); return 0; }
|
- 鍵盤輸入數字後,輸出結果為:
1 2 3
| My num1=912012 My num2=5 num=4560060
|
4.36-11
試撰寫一程式,由鍵盤輸入學生的學號(整數型態)及年齡(整數型態),輸入完畢後將剛才輸入的內容印出在螢幕中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #include <stdio.h> #include <stdlib.h>
int main(void) { int num1,num2; printf("請輸入您的學號:"); scanf("%d",&num1); printf("請輸入您的年齡:"); scanf("%d",&num2); printf("您的學號是 %d,以及您的年齡是 %d\n",num1,num2); system("pause"); return 0; }
|
- 鍵盤輸入數字後,輸出結果為:
1 2 3
| 請輸入您的學號:912012 請輸入您的年齡:21 您的學號是 912012,以及您的年齡是 21
|
4.36-13
試撰寫一程式,輸入一長度最多為10,且不包括空白的字串,並做出下列的處理。
- 以雙引號將字串包圍
- 以反斜線\將字串包圍,印出時欄寬為20。
- 以反斜線\將字串包圍,印出時欄寬為20,靠左印出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include <stdio.h> #include <stdlib.h>
int main(void) { char chnum[10]; printf("請輸入數字(最多十個):"); scanf("%10s", chnum); printf("1.以雙引號將字串包圍:\n") ; printf("\"%s\"\n",chnum) ; printf("2.以反斜線\將字串包圍,印出時欄寬為20:\n") ; printf("\\%20s\\\n",chnum) ; printf("3.以反斜線\將字串包圍,印出時欄寬為20,靠左印出:\n") ; printf("\\%-20s\\\n",chnum) ; system("pause"); return 0; }
|
- 鍵盤輸入數字後,輸出結果為:
1 2 3 4 5 6 7 8
| 請輸入字元(最多十個):5A912012 1.以雙引號將字串包圍: "5A912012" 2.以反斜線將字串包圍,印出時欄寬為20: \ 5A912012\ 3.以反斜線將字串包圍,印出時欄寬為20,靠左印出: \5A912012 \ 請按任意鍵繼續 . . .
|
4.36-14
試轉寫一程式,由鍵盤輸入一個十進位的整數,然後印出該整數的八進位和十六進位。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <stdio.h> #include <stdlib.h>
int main(void) { int num; printf("請輸入整數:"); scanf("%d",&num);
printf("八進位數字是 %o,以及十六進位數字是 %x\n",num,num); system("pause"); return 0; }
|
- 鍵盤輸入數字後,輸出結果為:
1 2 3
| 請輸入整數:912012 八進位數字是 3365214,以及十六進位數字是 dea8c 請按任意鍵繼續 . . .
|
4.37-17
請先執行下面的程式碼,然後回答接續的問題:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #include <stdio.h> #include <stdlib.h>
int main(void) { char ch1,ch2; printf("請輸入第一個字元:"); scanf("%c",&ch1); printf("請輸入第二個字元:"); scanf("%c",&ch2); printf("ch1=%c,ch2=%c\n",ch1,ch2); system("pause"); return 0; }
|
試說明為甚麼第二個字元無法順利輸入?
因為緩衝區的ch1資料未清除
試修改第11行的格式字串,使得本例中的第二個字元可以順利的輸入。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #include <stdio.h> #include <stdlib.h>
int main(void) { char ch1,ch2; printf("請輸入第一個字元:"); scanf("%c",&ch1); printf("請輸入第二個字元:"); scanf("%c\n",&ch2); printf("ch1=%c,ch2=%c\n",ch1,ch2); system("pause"); return 0; }
|
試撰寫程式碼,利用fflush()
函數來清空緩衝區內的資料,使得本例中的第二個字元可以順利的輸入。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <stdio.h> #include <stdlib.h>
int main(void) { char ch1,ch2;
printf("請輸入第一個字元:"); scanf("%c",&ch1); fflush(stdin); printf("請輸入第二個字元:"); scanf("%c",&ch2); printf("ch1=%c,ch2=%c\n",ch1,ch2); system("pause"); return 0; }
|
109/10/24
5.21-1.C
請寫出下列程式的輸出結果,並撰寫完整的程式碼來驗證之:
1 2 3 4 5 6
| int a=20,b=5; a=20,b=5; a=a%b; b=b*3; printf("a=%d\n",a); printf("b=%d\n",b);
|
5.21-2
下列哪些運算式的運算結果為true?哪些運算式的運算結果為false?
'a'<28
,答案為false
4+3==8-1
,答案為true
8>2
,答案為true
'a'!=97
,答案為false
5.22-3
下列的運算式中,試指出那些是運算元
?哪些是運算子
?
- (6+num)-12+a
- num=(12+ans)-24
- k++
5.22-7
試判別下列的各敘述的執行結果:
- 6+4<9+12 ,答案為
true
- 16+7*6+9 ,答案為
67
- (13-6)/7+8 ,答案為
9
- 7>0 && 6<6 && 12<13 ,答案為
false
- 8>0 || 12<7 ,答案為
true
- 8<=8 ,答案為
true
- 7+7>15 ,答案為
false
- 19+34-6>4 ,答案為
true
5.23-9
設下列各運算式中,a的初值皆為5,b的初值皆為3,num的初值皆為0,試寫出下列各式中,經運算過後的num、a與b之值:
- num=(a++)+b ,答案為
8
- num=(++a)+b ,答案為
9
- num=(a++)+(b++) ,答案為
8
- num=(++a)+(++b) ,答案為
10
- a+=a+(b++) ,答案為
13
5.23-10
設下列各運算式中,a的初值皆為12,b的初值皆為6,試寫出下列各式中,經運算過後的a與b之值:
- a/=b ,答案為
2
- a*=b++ ,答案為
72
- a*=++b ,答案為
84
- a*=b– ,答案為
72
- a%=b ,答案為
0
5.23-11
試撰寫一程式,可由鍵盤輸入攝氏溫度,程式的輸出為華氏溫度,其轉換公式如下:
(華氏溫度=(9/5)*攝氏溫度+32)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <stdio.h> #include <stdlib.h>
int main(void) { float num1,num2; printf("請輸入攝氏溫度:"); scanf("%f",&num1); printf("攝氏溫度為:%3.1f\n",num1); num2=9.0/5*num1+32; printf("華氏溫度為:%3.1f\n",num2);
system ("pause"); return 0; }
|
- 鍵盤輸入數字後,輸出結果為:
1 2 3 4
| 請輸入攝氏溫度:32 攝氏溫度為:32.0 華氏溫度為:89.6 請按任意鍵繼續 . . .
|
5.23-12
根據上題所提供的轉換公式,撰寫轉換華氏(由鍵盤輸入)至攝氏溫度的程式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #include <stdio.h> #include <stdlib.h>
int main(void) { float num1,num2; printf("請輸入華氏溫度:"); scanf("%f",&num1); printf("華氏溫度為:%3.1f\n",num1); num2=(num1-32)*5.0/9; printf("攝氏溫度為:%3.1f\n",num2); system ("pause"); return 0; }
|
- 鍵盤輸入數字後,輸出結果為:
1 2 3 4
| 請輸入華氏溫度:89.6 華氏溫度為:89.6 攝氏溫度為:32.0 請按任意鍵繼續 . . .
|
5.24-15
試撰寫一程式,可由鍵盤輸入平行四邊形的底和高,然後計算其面積。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #include <stdio.h> #include <stdlib.h>
int main(void) { float bottom,high,area; printf("請輸入平行四邊形的底:"); scanf("%f",&bottom); fflush(stdin); printf("請輸入平行四邊形的高:"); scanf("%f",&high); area=bottom*high; printf("平行四邊形的面積為:%5.2f\n",area); system("pause"); return 0; }
|
- 鍵盤輸入數字後,輸出結果為:
1 2 3 4
| 請輸入平行四邊形的底:912 請輸入平行四邊形的高:012 平行四邊形的面積為:10944.00 請按任意鍵繼續 . . .
|
5.24-16
已知圓形體積為4/3πr³,試撰寫一程式,可輸入圓球半徑,經計算後輸出圓形體積。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #include <stdio.h> #include <stdlib.h> #include <math.h>
int main(void) { float r; float pi=3.14159; printf("請輸入圓形半徑:"); scanf("%f",&r); printf("圓形的體積為:%f\n",4.0/3*pi*pow(r,3)); system ("pause"); return 0; }
|
- 鍵盤輸入數字後,輸出結果為:
1 2 3
| 請輸入圓形半徑:1 圓形的體積為:4.188787 請按任意鍵繼續 . . .
|
5.24-17
設有一程式碼,其變數的初值宣告如下:
1 2 3 4 5
| char a='A'; short b=12; float c=12.4f; int d=15; dounle e=13.62;
|
在下面的運算式中,試仿照圖5.4.1與圖5.4.2的畫法,匯出資料型態的轉換過程與資料的運算過程。
a+(b+c)+(d*e)
a+(b*c)+d-e
(b+c)+a*(d*e)
109/10/31
6.30-3
試撰寫一程式,可由鍵盤輸入一個整數,然後判斷它是奇數或偶數。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <stdio.h> #include <stdlib.h>
int main(void) { int num; printf("請輸入一組數字:"); scanf("%d",&num); if (num%2==0) { printf("此數字為\"偶數\"\n"); }else{ printf("此數字為\"奇數\"\n"); } system("pause"); return 0; }
|
- 鍵盤輸入數字後,輸出結果為:
1 2 3
| 請輸入一組數字:912012 此數字為"偶數" 請按任意鍵繼續 . . .
|
6.30-4
試撰寫一程式,可由鍵盤輸入一個整數,然後求此數的絕對值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <stdio.h> #include <stdlib.h> #include <math.h>
int main(void) { int num; printf("請輸入一組整數(可負數):"); scanf("%d",&num); printf("此數值的絕對值為:%d",abs(num)); system("pause"); return 0; }
|
- 鍵盤輸入數字後,輸出結果為:
1 2 3
| 請輸入一組整數(可負數):-912012 此數值的絕對值為:912012 請按任意鍵繼續 . . .
|
6.30-6
試撰寫一程式,可由鍵盤輸入兩個整數,分別代表個人的身高與體重,然後判斷它的體重是否過重。若體重大於90公斤,且身高低於180公分,則印出”體重過重”,否則印出”不會過重”。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include <stdio.h> #include <stdlib.h>
int main(void) { int num1,num2; printf("請輸入您的身高:"); scanf("%d",&num1); fflush(stdin); printf("請輸入您的體重:"); scanf("%d",&num2); if (num1<180&&num2>90){ printf("體重過重\n"); }else{ printf("不會過重\n"); } system("pause"); return 0; }
|
- 鍵盤輸入數字後,輸出結果為:
1 2 3 4
| 請輸入您的身高:176 請輸入您的體重:89 不會過重 請按任意鍵繼續 . . .
|
6.30-7
試撰寫一程式,由程式中宣告並設定三個整數的初值,判斷這三個整數是否能構成三角形的三個邊長(註:三角形兩邊長之和必須大於第三邊)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #include <stdio.h> #include <stdlib.h>
int main(void) { int a,b,c; printf("輸入三角形的邊長a:"); scanf("%d",&a); fflush(stdin); printf("輸入三角形的邊長b:"); scanf("%d",&b); fflush(stdin); printf("輸入三角形的邊長c:"); scanf("%d",&c); fflush(stdin); if (a==0||b==0||c==0) { printf("此三個邊長,不足為三角形。\n"); }else{ if (b+c>a || a+c>b || a+b>c) { printf("此三個邊長,可組為三角形。\n"); } } system("pause"); return 0; }
|
- 鍵盤輸入數字後,輸出結果為:
1 2 3 4 5
| 輸入三角形的邊長a:91 輸入三角形的邊長b:2 輸入三角形的邊長c:12 此三個邊長,可組為三角形。 請按任意鍵繼續 . . .
|
6.31-10
試撰寫一程式,輸入X、Y座標值,判斷該點位於那一個象限或是座標軸上。舉例來說,落輸入的座標值為(3.0,-2.5),輸出即為第四象限;若輸入的座標值為(4.5,0.0),則輸出即為X軸。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include <stdio.h> #include <stdlib.h>
int main(void) { int x,y; printf("請輸入X的座標:"); scanf("%d",&x); fflush(stdin); printf("請輸入Y的座標:"); scanf("%d",&y); fflush(stdin); if(x>0&&y>0){ printf("此座標位於第一象限\n"); }else{ if(x<0&&y>0){ printf("此座標位於第二象限\n"); }else{ if(x<0&&y<0){ printf("此座標位於第三象限\n"); }else{ if(x>0&&y<0){ printf("此座標位於第四象限\n"); }else{ if(x==0){ printf("此座標位於X軸\n"); }else{ if(y==0){ printf("此座標位於Y軸\n"); } } } } } } system("pause"); return 0; }
|
- 鍵盤輸入數字後,輸出結果為:
1 2 3 4
| 請輸入X的座標:912 請輸入Y的座標:012 此座標位於第一象限 請按任意鍵繼續 . . .
|
6.31-12
試利用if-else-if敘述一程式,程式的輸入為學生成績,輸出為成績的等級。學生成績依序下列的分類方式分級。
A級:80~100
B級:60~ 79
C級:0 ~ 59
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <stdio.h> #include <stdlib.h>
int main(void){ int num; printf("請輸入學生的成績:"); scanf("%d",&num); fflush(stdin); printf("學生的成績為:%d\n",num); if(100>=num && num>=80){ printf("%d=A級\n",num);} else if(80>num && num>=60){ printf("%d=B級\n",num);} else if(60>num && num>=0){ printf("%d=C級\n",num);} else{ printf("成績輸入錯誤\n");} system("pause"); return 0; }
|
- 鍵盤輸入數字後,輸出結果為:
1 2 3 4
| 請輸入學生的成績:92 學生的成績為:92 92=A級 請按任意鍵繼續 . . .
|
6.32-16
已知ax²+bx+c=0
,求x的解為多少?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| #include <stdio.h> #include <stdlib.h> #include <math.h>
int main(void){
printf("已知ax^2+bx+c=0,求解\n"); float a,b,c; printf("請輸入a值:"); scanf("%f",&a); fflush(stdin);
printf("請輸入b值:"); scanf("%f",&b); fflush(stdin); printf("請輸入c值:"); scanf("%f",&c); fflush(stdin);
float num=(pow(b,2))-4*a*c; if(num<0){ printf("沒有實根\n"); }else { double x=((-b)+sqrt(b*b-4*a*c))/(2*a); double y=((-b)-sqrt(num))/(2*a); printf("X=%.1f , %.1f\n",x,y); } system("pause"); return 0; }
|
- 鍵盤輸入數字後,輸出結果為:
1 2 3 4 5 6
| 已知ax^2+bx+c=0,求解 請輸入a值:1 請輸入b值:-8 請輸入c值:16 X=4.0 , 4.0 請按任意鍵繼續 . . .
|
6.33-20
由鍵盤輸入數值為1~4,並加以判斷輸入值是否在1~4之間,如果超出此範圍,則印出輸入錯誤
,否則利用switch印出相對應的季節。
1 : 春天
2 : 夏天
3 : 秋天
4 : 冬天
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #include <stdio.h> #include <stdlib.h>
int main(void){ int num; printf("請輸入1~4的數字:"); scanf("%d",&num); fflush(stdin); printf("輸入的數字為:%d\n",num); switch(num){ case 1: printf("春天\n"); break; case 2: printf("夏天\n"); break; case 3: printf("秋天\n"); break; case 4: printf("冬天\n"); break; default: printf("輸入錯誤\n");}
system("pause"); return 0; }
|
- 鍵盤輸入數字後,輸出結果為:
1 2 3 4
| 請輸入1~4的數字:1 輸入的數字為:1 春天 請按任意鍵繼續 . . .
|
6.34-24
試利用goto敘述撰寫一程式,可以計算出1~100之間,所有奇數的總和。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include <stdio.h> #include <stdlib.h>
int main(void){ int num=0; int sum=0; start: num++; if(num<100){ if(num%2==1){ sum+=num; } goto start; } printf("1~100奇數總和: %d\n",sum); system("pause"); return 0; }
|
- 輸出結果為:
1 2
| 1~100奇數總和: 2500 請按任意鍵繼續 . . .
|
109/11/07
7.38-3
試利用for迴圈
計算1+3+5+7+…+n的總和,其中n為奇數
,可由使用者自行輸入。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include <stdlib.h> #include <stdio.h>
int main(void){ int n; int sum=0; printf("請輸入奇數n:"); scanf("%d",&n); if(n%2==0){ printf("數字輸入錯誤\n"); exit(0); }else{ for (int num=1;num<=n;num++){ if(num%2==1){ sum+=num; } } } printf("1到%d的奇數和為:%d\n",n,sum); system("pause"); return 0; }
|
- 輸出結果為:
1 2 3
| 請輸入奇數n:99 1到99的奇數和為:2500 請按任意鍵繼續 . . .
|
7.38-4
試撰寫一程式,利用for迴圈
列印ASCII碼
為41~64
之間的字元。
1 2 3 4 5 6 7 8 9 10 11 12
| #include <stdio.h> #include <stdlib.h>
int main(void){ int a=41,b=64; for (a;a<=b;a+=1){ printf("%c",a); } printf("\n"); system("pause"); return 0; }
|
- 輸出結果為:
1 2
| )*+,-./0123456789:;<=>?@ 請按任意鍵繼續 . . .
|
7.38-5
試撰寫一程式,求整數1~100中,可以同時被3與8整除之所有整數的總和。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #include <stdio.h> #include <stdlib.h>
int main(void){ int sum=0,a=1,b=100; for (a;a<=b;a+=1){ if(a%3==0 && a%8==0){ sum+=a; } } printf("同時被3與8整除總和:%d\n",sum); system("pause"); return 0; }
|
- 輸出結果為:
1 2
| 同時被3與8整除總和:240 請按任意鍵繼續 . . .
|
7.38-6
試撰寫一程式,由鍵盤輸入一個正整數,然後求其所有的因數
,例如輸入24則印出24的所有因數1、2、3、4、6、8、12、24。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <stdio.h> #include <stdlib.h>
int main(void){ int num=1,nice; printf("請輸入一個正整數:"); scanf("%d",&nice); printf("此數的因數:"); for (num;num<=nice;num+=1){ if(nice%num==0){ printf("%d.",num); } } printf("\n"); system("pause"); return 0; }
|
- 輸出結果為:
1 2 3
| 請輸入一個正整數:912012 此數的因數:1.2.3.4.6.12.76001.152002.228003.304004.456006.912012. 請按任意鍵繼續 . . .
|
7.38-8
試撰寫一程式,利用for迴圈
印出1到100之間,求所有可以被7整除,又可以被3整除的數值
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <stdio.h> #include <stdlib.h>
int main(void){ int a=1,b=100; for(a;a<=b;a+=1){ if(a%7==0 && a%3==0){ printf("%d.",a); } } printf("\n"); system("pause"); return 0; }
|
- 輸出結果為:
1 2
| 21.42.63.84. 請按任意鍵繼續 . . .
|
7.38-9
試撰寫一程式,利用for迴圈
計算1²-2²+3²-4²+…+47²-48²+49²-50²的值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <stdio.h> #include <stdlib.h> #include <math.h>
int main(void){ int a=0,b=50,i=0,sum=0; for(a;a<b;a+=2){ i=pow(a+1,2)-pow(a+2,2); sum+=i; } printf("sum=%d\n",sum); system("pause"); return 0; }
|
7.39-13
試利用while迴圈
計算2+4+6+…+n的總和,其中n為整數,可由使用者自行輸入。若輸入的值不是正偶數,則程式會要求使用者再次輸入,直到輸入的數是正偶數為止。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include <stdio.h> #include <stdlib.h>
int main(void){ int a=0,n=0,sum=0; while(1){ printf("請輸入n的數值:"); scanf("%d",&n); if(n>0 && n%2==0){ break; } } while(a<=n){ sum+=a; a+=2; } printf("2+4+6+...+n=%d\n",sum); system("pause"); return 0; }
|
- 輸出結果為:
1 2 3
| 請輸入n的數值:50 2+4+6+...+n=650 請按任意鍵繼續 . . .
|
7.39-14
假設有一條繩子長為3000公尺,每天減去一半的長度,請問需要花費幾天的時間,繩子才會短於5公尺?
1 2 3 4 5 6 7 8 9 10 11 12
| #include <stdio.h> #include <stdlib.h>
int main(void){ int m=3000,n=0; for (m;m>5;m=m/2){ n+=1; } printf("總共花費 %d天 的時間\n",n); system("pause"); return 0; }
|
- 輸出結果為:
1 2
| 總共花費 9天 的時間 請按任意鍵繼續 . . .
|
7.39-17
試撰寫一程式,利用while迴圈印出1~10之間的所有整數的平方值,最後在印出這些平方值的總和。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <stdlib.h> #include <stdio.h> #include <math.h>
int main(void){ int a=1,b=10,num=0,sum=0; while(a<=10){ num=pow(a,2); printf("%d.",num); sum+=num; a+=1; } printf("\n"); printf("1~10的平方值總和= %d\n",sum); system("pause"); return 0; }
|
- 輸出結果為:
1 2 3
| 1.4.9.16.25.36.49.64.81.100. 1~10的平方值總和= 385 請按任意鍵繼續 . . .
|
7.39-18
(a)
試利用do while迴圈
計算2+4+6+…+n的總和,其中n為整數,可由使用者自行輸入。若輸入的值不是正偶數,則程式會要求使用者再次輸入,直到輸入的數是正偶數為止。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include <stdlib.h> #include <stdio.h> #include <math.h>
int main(void){ int a=0,n=0,sum=0; while(1){ printf("請輸入n的數值:"); scanf("%d",&n); if(n>0 && n%2==0){ break; } } do{ a+=2; sum+=a; }while(a<n); printf("2+4+6+...+n=%d\n",sum); system("pause"); return 0; }
|
- 輸出結果為:
1 2 3
| 請輸入n的數值:50 2+4+6+...+n=650 請按任意鍵繼續 . . .
|
(b)
和習題13相比,您覺得哪一種迴圈比較適合用來計算2+4+6+…+n的總和呢?為什麼?
while迴圈
因為while迴圈,由上往下看,看起來比較簡單明瞭,在debug時,也比較容易看出來
7.39-19
試利用do while迴圈來找出最小的n值,使得 1+2+3+…+n 大於等於1000。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <stdlib.h> #include <stdio.h>
int main(void){ int n=1,a=0; do{ n+=1; a+=n; }while(a<1000); printf("%d\n",n); system("pause"); return 0; }
|
7.40-21
於下列的程式碼片段中,原本預期會印出3行星號,但實際上卻只印出了一行。試說明程式錯誤之處,並修正之:
1 2 3 4
| int i; for(i=1;i<=3;i++); printf("*********\n");
|
修正後的程式碼為:
1 2 3 4 5 6 7 8
| #include <stdio.h> #include <stdlib.h> int main(void){ int i; for(i=1;i<=3;i++){ printf("*********\n"); } }
|
- 輸出結果為:
1 2 3
| ********* ********* *********
|
7.40-23
試依下面題意回答:
- 試修改習題19,把do while改以for迴圈來撰寫。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <stdio.h> #include <stdlib.h>
int main(void){
int n=1,a=0; for(a;a<1000;n++){ a+=n; } printf("%d\n",n-1); system("pause"); return 0; }
|
- 試修改習題19,把do while改以while迴圈來撰寫。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #include <stdio.h> #include <stdlib.h>
int main(void){
int n=1,a=0; while(a<1000){ n++; a+=n; } printf("%d\n",n); system("pause"); return 0; }
|
- 試修改習題19,把for、while和do while迴圈來撰寫時,比較起來用哪種迴圈寫起來較方便?為什麼?
while
,程式可縮短。
7.41-26
試利用巢狀迴圈撰寫出一個能產出如下圖結果的程式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <stdio.h> #include <stdlib.h>
int main(void){ int a=1,b=2; for(a;a<=5;a++){ for(b;b<=a;b++){ printf("\n"); a=1; } printf("%d",a); } printf("\n"); }
|
7.41-29
試撰寫一程式,利用break敘述來撰寫4個數字之密碼輸入的過程。使用者有三次的輸入機會,並須滿足下列的條件:
- 如果密碼不對,則會再次的出現”請輸入密碼:”字串。
- 如果三次的輸入都不對,則程式會印出”密碼輸入吃過三次!!”字串,然後結束程式的執行。
- 如果輸入正確,則印出”密碼輸入正確,歡迎進入本系統!!”字串。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include <stdio.h> #include <stdlib.h>
int main(void){ int good,nice,num,a,n=0; while(n<=3){ printf("請輸入密碼:"); scanf("%d",&good); if(good==1234){ printf("密碼輸入正確,歡迎進入本系統!!\n"); break; } n+=1; if(n==3) { printf("密碼輸入吃過三次!!\n"); exit(1); } } system("pause"); return 0; }
|
- 輸出結果為:
1 2 3 4 5
| 請輸入密碼:1 請輸入密碼:2 請輸入密碼:1234 密碼輸入正確,歡迎進入本系統!! 請按任意鍵繼續 . . .
|
7.42-31
試利用continue敘述,找出小於100的整數裡,所有可以被2與3整除,但不能被12整除的整數。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #include <stdio.h> #include <stdlib.h>
int main(void){ int a=1,b=100; for(a;a<b;a++){ if(a%3!=0 || a%2!=0){ continue; } if(a%12==0){ continue; } printf("%d.",a); } printf("\n"); system("pause"); return 0; }
|
- 輸出結果為:
1 2
| 6.18.30.42.54.66.78.90. 請按任意鍵繼續 . . .
|
7.42-33
試撰寫一程式,可由鍵盤輸入一個正整數,並找出此數的最大質數。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #include <stdio.h> #include <stdlib.h>
int main(void){ int good,nice,num,a,n=0; while(1){ printf("請輸入一個正整數:"); scanf("%d",&good); if(good>0) break; } printf("此數的最大質數:"); for(nice=1;nice<=good;nice++){ for (num=1;num<=nice;num+=1){ if(nice%num==0) n+=1; } if(n==1) n=2; if(n==2) a=nice; n=0; } printf("%d",a); printf("\n"); system("pause"); return 0; }
|
- 輸出結果為:
1 2 3
| 請輸入一個正整數:100 此數的最大質數:97 請按任意鍵繼續 . . .
|
109/12/05
8.64-1
試寫一函數void kitty(void)
,當主程式呼叫kitty()時,螢幕上會顯示出"Hello Kitty"
之字串。
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include <stdio.h> #include <stdlib.h>
void kitty(void);
int main(void){ kitty(); return 0; }
void kitty(){ printf("Hello Kitty"); }
|
8.64-3
試撰寫int cub(int x)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <stdio.h> #include <stdlib.h>
int sub(int);
int main(void){ sub(2); }
int sub(int x){ int c=1,n=1; while(n<=3){ c=c*x; n++; } printf("%d",c); }
|
8.64-5
試撰寫 int mod(int x,int y)函數,計算x/y的餘數。並利用此函數來計算mod(17,5),即計算17/5的餘數。
1 2 3 4 5 6 7 8 9 10 11 12
| #include <stdio.h> #include <stdlib.h>
int mod(int,int); int main(void){ int c=mod(17,5); printf("%d",c); } int mod(int x,int y){ int c=x%y; return c; }
|
8.64-7
試撰寫一 int prime(int n ),可用來找出第n個質數(第一個質數為2,第二個質數為3,依此類推),並以此函數找出第100個質數。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| #include <stdio.h> #include <stdlib.h>
int prime(int); int PRIME(int);
int main(void){ int num=prime(100); printf("%d\n",num); }
int prime(int n){ int a=1,b=0; while(1){ a+=1; if(PRIME(a)==1){ b+=1; if(b==n) { return a; } } } }
int PRIME(int pr){ int i; for(i=2;i<=pr-1;i++) if(pr%i==0) return 0; return 1; }
|
8.64-8
設f(X)=3x³+2x-1,試寫一函數 double f(double x),用來回傳f(x)的值,並於主程式裡分別運算f(-3.2)、f(-2.1)、f(0)與f(2.1)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #include <stdio.h> #include <stdlib.h> #include <math.h>
double f(double); int main(void){ printf("f(-3.2) = %.3f\n",f(-3.2)); printf("f(-2.1) = %.3f\n",f(-2.1)); printf("f(0) = %.3f\n",f(0)); printf("f(2.1) = %.3f\n",f(2.1)); } double f(double x){ double f=3*pow(x,3)+2*x-1; return f; }
|
- 輸出結果為:
1 2 3 4
| f(-3.2) = -105.704 f(-2.1) = -32.983 f(0) = -1.000 f(2.1) = 30.983
|
8.65-!11
8.65-12
試依序回答下面的問題:
- 試撰寫一函數 double my_fun(int n),可用來計算下列的數學式:
my_fun(n) = 1/2¹+1/2²+1/2³+…+1/2ⁿ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #include <stdio.h> #include <stdlib.h> #include <math.h> double my_fun(int); int main(void){ printf("my_fun(3)=%f\n",my_fun(3)); printf("my_fun(4)=%f\n",my_fun(4)); printf("my_fun(5)=%f\n",my_fun(5)); printf("my_fun(6)=%f\n",my_fun(6)); } double my_fun(int n){ if(n>0){ return double (1/pow(2,n)+my_fun(n-1)); } else{ return 0; } }
|
- 輸出結果為:
1 2 3 4
| my_fun(3)=0.875000 my_fun(4)=0.937500 my_fun(5)=0.968750 my_fun(6)=0.984375
|
- 如果將n值加大,my_fun(n)的結果會趨近一個定值,試問此定值為多少?
1.000000
- 試問n值最少要多大時,my_fun(n)的結果才會大於0.99999?
17
8.66-14
試依序回答下面的問題:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include <stdio.h> #include <stdlib.h> double my_fun(double,int); double power(double,int); int fac(int); int main(void) { printf("my_fun(0.1,5)=%f\n",my_fun(0.1,5)); printf("my_fun(0.1,8)=%f\n",my_fun(0.1,8)); printf("my_fun(0.2,8)=%f\n",my_fun(0.2,8)); system("pause"); return 0; }
double my_fun(double x,int n) { int a; double b=0.0; for(a=1;a<=n;a++) b+=power(x,a)/fac(a); return b; }
int fac(int d) { int c,total=1; for(c=1;c<=d;c++) total*=c; return total; }
double power(double base, int n) { int e; double pow=1.0; for(e=1;e<=n;e++) pow*=base; return pow; }
|
- 輸出結果為:
1 2 3 4
| my_fun(0.1,5)=0.105171 my_fun(0.1,8)=0.105171 my_fun(0.2,8)=0.221403 請按任意鍵繼續 . . .
|
8.67-16
試撰寫一函數 int find_k(int n),它可用來找出一個k值(k為整數),使得4k+2的值最靠近n。例如,設n=19,若k=4,則4k+2=18;若k=5,則
例如,設
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include <stdio.h> #include <stdlib.h> int find_K(int);
int main(void) { printf("find_K(19)=%d\n",find_K(19)); printf("find_K(23)=%d\n",find_K(23)); printf("find_K(29)=%d\n",find_K(29)); }
int find_K(int n) { int a; a=(n-2)/4; if(n-(4*a+2)<4*(a+1)+2-n) return a; else return a+1; }
|
- 輸出結果為:
1 2 3
| find_K(19)=4 find_K(23)=5 find_K(29)=7
|
8.67-!17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #include <stdio.h> #include <stdlib.h> int find_k(int); int is_prime(int); double Euler(int);
int main(void) { printf("Euler(10)=%f\n",Euler(10)); printf("Euler(100)=%f\n",Euler(100)); printf("Euler(1000)=%f\n",Euler(1000)); printf("Euler(10000)=%f\n",Euler(10000)); }
double Euler(int n) { int a=0,b=3; double Euler=1.0; while(a<n) { if(is_prime(b)) { Euler*=((float)b/find_k(b)); a++; } b++; } return Euler*2; }
int find_k(int n) { int c; c=(n-2)/4; if(n-(4*c+2)<4*(c+1)+2-n) return (4*c+2); else return (4*(c+1)+2); }
int is_prime(int d) { int e; for(e=2;e<=(d-1);e++) if(d%e==0) return 0; return 1; }
|
- 輸出結果為:
1 2 3 4
| Euler(10)=3.101520 Euler(100)=3.133985 Euler(1000)=3.137723 Euler(10000)=3.140723
|
8.68-20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <stdio.h> #include <stdlib.h> int sum(int n); int main(void) { int n; printf("輸入n:"); scanf("%d",&n); printf("sum 1+2+3+...+%d=%d",n,sum(n)); } int sum(int n) { if(n==1) return 1; else return sum(n-1)+n; }
|
- 輸出結果為:
1 2
| 輸入n:50 sum 1+2+3+...+50=1275
|
8.68-23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <stdio.h> #include <stdlib.h> int f(int); int main(void) { printf("f(5)=%d\n",f(5)); system("pause"); return 0; }
int f(int n) { if(n==0) return 3; else return (2*f(n-1)-5); }
|
8.70-31
1 2 3 4 5 6 7 8 9 10 11 12
| #include <stdio.h> #include <stdlib.h> #define f(X) 4*(X)*(X)+6*(X)-5 int main(void) { printf("f(1.0)=%.2f\n",f(1.0)); printf("f(2.2)=%.2f\n",f(2.2)); printf("f(3.14)=%.4f\n",f(3.14)); system("pause"); return 0; }
|
- 輸出結果為:
1 2 3 4
| f(1.0)=5.00 f(2.2)=27.56 f(3.14)=53.2784 請按任意鍵繼續 . . .
|
8.70-%35