<style> h1{ color:#FF359A } h2{ color:#2828FF } h3{ color:#00DB00; } h4{ color:#FF5809; } </style> <h1>C語言程式設計筆記</h1> <h2> API、套件、網路、數據分析 </h2> <h4> ASCII Code: <br> A=65 a=97 </h4> <h1> Exam1 </h1> <h4> 設計一個C語言的程式用以印出「This is my very first C program!」 </h4> ``` #include <stdio.h> int main() { printf("This is my very first C program!\n"); } ``` <h4> 請撰寫一個程式列印出"我愛c語言" </h4> ``` #include <stdio.h> #include <stdlib.h> int main(void) { printf("我愛C語言\n"); /* 印出字串 */ system("pause"); return 0; } ``` <h1> Exam2 </h1> <h4> 請撰寫一個程式來計算10的平方 </h4> ``` #include <stdio.h> #include <stdlib.h> int main() { int num; int square; num = 10; square = num * num; printf("Square of number %d is %d.\n", num, square); system("PAUSE"); return 0; } ``` <h4> 假設圓半徑為整數5,請寫出顯示半徑及面積(半徑*半徑*3.14159)之程式 </h4> ``` #include<stdio.h> #include<stdlib.h> #define PI 3.14159 /*宣告巨集指定,定義PI=3.14159*/ int main() { const int a=5; /*宣告圓半徑為整數常數*/ //a=a+5; printf("PI=%f\n",PI); /* %f 為浮點數輸出格式 */ printf("圓的半徑為=%d ,面積為=%f \n",a,PI*a*a); return 0; } ``` <h1> Exam3 </h1> <h4> 試撰寫一程式,利用sizeof關鍵字查詢下列各種資料型態所佔的位元組: </h4> ``` #include <stdio.h> #include <stdlib.h> int main(void) { printf("sizeof(unsigned int)=%d\n",sizeof(unsigned int)); printf("sizeof(double)=%d\n",sizeof(double)); printf("sizeof(unsigned short int)=%d\n",sizeof(unsigned short int)); system("pause"); return 0; } /* output-------------------- sizeof(unsigned int)=4 sizeof(double)=8 sizeof(unsigned short int)=2 ``` <h4> 假設某社區管理費以房屋建築坪數為基準,每戶每月收取每坪50元的管理費;此外,因為該社區符合新環保規範,所以內政部獎勵補助該社區每戶每平方公尺每年120元的獎勵金。該社區決議將環保獎勵金於每月收取管理費時扣除。請設計一個C語言程式,幫使用者計算應繳的管理費金額。此題如需要使用浮點數,請一律使用double型態,並以1坪=3.3058平方公尺進行計算。本題的執行結果可參考以下的畫面: </h4> ``` #include <stdio.h> int main() { double ping; printf("How big is your apartment: "); scanf("%lf", &ping); printf("Your management fee is %f.\n", ping*50 - (ping*3.3058*120)/12); } ``` <h1> Exam4 </h1> <h4> 試撰寫一程式,將浮點數變數num=28.47f以下列的格式印出(小數點前面有4位,小數點後有2位,不滿欄位長度時填入0) </h4> ``` #include <stdio.h> #include <stdlib.h> int main(void) { float num=28.47f; printf("num=%07.2f\n",num); system("pause"); return 0; } ``` <h4> 試撰寫一程式,利用scanf()函數輸入兩個整數,然後以printf0函數列印出這兩個整數的乘積。 </h4> ``` #include <stdio.h> #include <stdlib.h> int main(void) { int a,b; printf("請輸入兩個整數: "); scanf("%d %d",&a,&b); printf("%d*%d=%d\n",a,b,a*b); system("pause"); return 0; } ``` <h4> 試撰寫一程式,由使用者先輸入姓氏,再輸入名字,輸出時則先印出名字,再印出姓氏。 </h4> ``` #include <stdio.h> #include <stdlib.h> int main(void) { char first[10]; char last[10]; printf("請輸入姓氏: "); scanf("%s",last); printf("請輸入名字: "); scanf("%s",first); printf("%s %s\n",first,last); system("pause"); return 0; } ``` <h4> 設計一個C語言程式,讓使用者輸入一個整數,使用printf()函式相關的輸出格式控制,將這個整數以5個位置、置右對齊的方式輸出。此程式的執行結果可參考如下 </h4> ``` #include <stdio.h> int main() { int x; printf("Please input an integer: "); scanf("%d", &x); printf("Output:[%5d]\n", x); } ``` <h4> 在台灣買東西大家都會記得要索取統一發票(uniform invoice),因為每單月份的25號會開獎一次,從統一發票號碼的末3碼開始到全部的8碼,只要與開獎號碼相同就可以領取從200元到200萬元不等的獎金。請寫一個程式,讓使用者輸入一組統一發票的號碼,並且將其最後3碼印出。請注意統一發票號碼的格式,其前兩碼為英文字母,後面則接8碼的數字。程式的執行結果可參考以下的畫面: </h4> ``` #include <stdio.h> int main() { int i; // 宣告一個整數變數 i 用於儲存使用者輸入的數值 printf("請輸入統一發票號碼: "); // 提示使用者輸入統一發票號碼 // 使用 scanf 函式從使用者輸入中讀取數據,但使用格式字符串 "%*c%*c%8d" // %*c: 跳過並不儲存兩個字符,這兩個字符通常是兩個英文字母 // %8d: 讀取並儲存一個整數,要求這個整數佔 8 位 scanf("%*c%*c%8d", &i); printf("最後 3 位數字為 %03d。\n", i % 1000); // 輸出最後 3 位數字,並確保以 3 位數的形式輸出 } ``` <h1> Exam5 </h1> <h4> 試計算下列各式,並撰寫程式碼來驗證您計算的結果:<br> (a) 6%4<br> (b) 12%6<br> (c) 12%12<br> (d)35%50 (e) 50%35 </h4> ``` #include <stdio.h> #include <stdlib.h> int main(void) { printf("6%%4=%d\n",6%4); printf("12%%6=%d\n",12%6); printf("12%%12=%d\n",12%12); printf("35%%50=%d\n",35%50); printf("50%%35=%d\n",50%35); system("pause"); return 0; } ``` <h4> 假設 a= 1,b= 2, c= 3,請執行出下列邏輯運算式執行結果: <br> --------------------------------------<br> 等號後面 1 為 true(對),0 為 false(錯)<br> a<b && b<c||c<a = ?<br> !(a==b)&&(!a<b) = ?<br> --------------------------------<br> </h4> ``` #include <stdio.h> #include <stdlib.h> int main() { int a=1,b=2,c=3; /*宣告a、b及c三個整數變數*/ printf("a= %d b= %d c= %d\n",a,b,c); printf("--------------------------------------\n"); printf("等號後面 1 為 true(對),0 為 false(錯)\n"); printf("a<b && b<c||c<a = %d\n",a<b&&b<c||c<a); printf("!(a==b)&&(!a<b) = %d\n",!(a==b)&&(!a<b)); // printf("a=%d",!a); /* 包含關係與邏輯運算子的運算式求值 */ return 0; } ``` <h4> 假設 a=12, b=12,請執行出下列運算關係式執行結果:<br> --------------------------------<br> 1為true(對),0為false(錯)<br> a>b,比較結果為 ? 值<br> a<b,比較結果為 ? 值<br> a>=b,比較結果為 ? 值<br> a<=b,比較結果為 ? 值<br> a==b,比較結果為 ? 值<br> a!=b,比較結果為 ? 值<br> </h4> ``` #include<stdio.h> #include<stdlib.h> int main() { int a=12,b=12; /* 宣告兩個整數變數*/ /*比較運算子運算關係*/ printf("a=%d b=%d \n",a,b); printf("--------------------------------\n"); printf("1為true(對),0為false(錯)\n"); printf("a>b,比較結果為 %d 值\n",a>b); printf("a<b,比較結果為 %d 值\n",a<b); printf("a>=b,比較結果為 %d 值\n",a>=b); printf("a<=b,比較結果為 %d 值\n",a<=b); printf("a==b,比較結果為 %d 值\n",a==b); printf("a!=b,比較結果為 %d 值\n",a!=b); return 0; } ``` <h1> Exam6 </h1> <h4> 百貨公司目前有促銷活動,只要消費達一定的金額,就能獲得以下優惠(僅能擇優使用,不得重複): 1.消費滿3000現折200。 2.消費滿6000消費金額打8折。 3.消費滿10000消費金額打6折。 設計一個C語言程式,讓使用者輸入原始的消費金額(整數值),計算優惠後應付的消費金額(整數值)後輸出。要特別注意的是不論消費金額或優惠後的金額都只能是整數值,但計算優惠金額時可能會產生小數,請四捨五入到個位數。此程式的執行結果可參考如下: The original price: 3000 The price after discount: 2800 The original price: 6537 The price after discount: 5230 The original price: 12345 The price after discount: 7407 The original price: 2500 The price after discount: 2500 </h4> ``` #include <stdio.h> int main() { double price; printf("The original price: "); scanf("%lf", &price); if(price>=10000) { price*=0.6; } else if(price>=6000) { price*=0.8; } else if(price>=3000) { price-=200; } printf("The price after discount: %.0f\n", price); } ``` <h4> 設計一個C語言程式,要求使用者輸入一個整數,並檢查該整數是否為2、3、5及7的倍數,其執行結果可參考以下畫面: Please input an integer: 6 6 is divisible by 2. 6 is divisible by 3. -------------------------------------------- Please input an integer: 70 70 is divisible by 2. 70 is divisible by 5. 70 is divisible by 7. --------------------------------------------- Please input an integer: 13 13 is not divisiblerby 2, 3, 5 and 7. </h4> ``` #include <stdio.h> int main() { int i, divisible=0; printf("Please input an integer: "); scanf(" %d", &i); if(i%2==0) { printf("%d is divisible by 2.\n", i); divisible=1; } if(i%3==0) { printf("%d is divisible by 3.\n", i); divisible=1; } if(i%5==0) { printf("%d is divisible by 5.\n", i); divisible=1; } if(i%7==0) { printf("%d is divisible by 7.\n", i); divisible=1; } if(divisible==0) { printf("%d is not divisible by 2, 3, 5 and 7.\n", i); } } ``` <h4> 試由鍵盤輸入一個字元,然後加以判斷輸入的字元是小寫的a還是小寫的b。若是小寫的a,則印出“您輸入a,若是小寫的b,則印出“您輸入b”,若输入的字元不是a或b,則印出“您輸入的不是a或b。 </h4> ``` #include <stdio.h> #include <stdlib.h> int main(void) { char ch; printf("請輸入字元:"); scanf("%c",&ch); switch(ch) { case 'a': printf("您輸入a\n"); break; case 'b': printf("您輸入b\n"); break; default: printf("您輸入的不是a或b\n"); } system("pause"); return 0; } ``` <h4> 某電信公司的通話費及行動上網費有以下三種計費方式: 方案1:499元不限網路流量吃到飽,且享有100分鐘(含)免費通話,超過部份每分鐘收費5元。 方案2:月租費199元,含2GB上網流量,超過2GB的部份,每1GB加收50元(不足1GB仍以1GB計費),例如當月使用3.2GB加收100元(因為超出1.2G流量,加收50元×2=100元)。至於通話費部份享有30分鐘(含)免費,超過部份每分鐘收費6元。 方案3:月租費259元,含4GB上網流量,超過4GB的部份,每1GB加收60元,例如當月使用8.4GB,加收264元(因為超出4.4GB流量,加收60元×4.4GB=264元)。注意,所計算之金額僅需精確到個位數,金額若有小數,則小數部份無條件捨去。通話費部份享有50分鐘(含)免費,超過部份每分鐘收費4元。 請設計一個程式,用以幫使用者找出最划算的方案(若有兩個以上方案相同划算時,請以免費上網流量多的優先)。使用者必須輸入該月份所使用的上網流量(以MB為單位),以及該月份的通話分鐘數(整數),找出最划算的方案後輸出(若方案的費用相同時,則依方案1、方案2 及方案3 的順序決定)。 請參考以下的執行結果: Data usage (in Megabytes): 2500 Voice usage (in minutes): 100 The total amounts of each programs are 499, 669, and 459. The best deal is program 3. --- Data usage (in Megabytes): 8192 Voice usage (in minutes): 31 The total amounts of each programs are 499, 505, and 499. The best deal is program 1. --- Data usage (in Megabytes): 2048 Voice usage (in minutes): 32 The total amounts of each programs are 499, 211, and 259. The best deal is program 2. --- Data usage (in Megabytes): 6144 Voice usage (in minutes): 0 The total amounts of each programs are 499, 399, and 379. The best deal is program 3. </h4> ``` #include <stdio.h> int main() { int amount, minute; int payment1, payment2, payment3; int error=0; printf("Data usages (in Megabytes): "); scanf("%d", &amount); printf("Vocie usages (in minutes): "); scanf("%d", &minute); payment1=499; if(minute>100) payment1+=(minute-100)*5; payment2 = amount<=2048?199:199+((amount-2048)/1024)*50+(((amount-2048)%1024)>0?50:0); if(minute>30) payment2+=(minute-30)*6; payment3 = amount<=4096?259:259+(amount-4096)/1024.0*60; if(minute>50) payment3+=(minute-50)*4; printf("The total amounts of each program are %d, %d, and %d.\n", payment1, payment2, payment3); printf("The best deal is program "); if((payment1<=payment2)&&(payment1<=payment3)) printf("1.\n"); else if((payment3<=payment2)&&(payment3<payment1)) printf("3.\n"); else printf("2.\n"); } ``` <h1> Exam7 </h1> <h4> 請設計一個C語言程式,讓使用者可以不斷地輸入一個字元,若該字元為阿拉伯數字(從0到9),則印出其對應的英文(從zero、one、two、…到nine);若不是阿拉伯數字,則印出「Wrong!Try Again!」。此程式直到使用者所輸入的字元為「q」時才結束(大小寫皆可),此程式的執行結果可參考以下的輸出內容: Arabic number? 3 three Arabic number? 9 nine Arabic number? X Wrong! Try Again! Arabic number? Q bye Arabic number? q bye </h4> ``` #include <stdio.h> int main() { char c; int quit=0; do { printf("Arabic number? "); scanf(" %c", &c); switch(c) { case '0': printf("zero\n"); break; case '1': printf("one\n"); break; case '2': printf("two\n"); break; case '3': printf("three\n"); break; case '4': printf("four\n"); break; case '5': printf("five\n"); break; case '6': printf("six\n"); break; case '7': printf("seven\n"); break; case '8': printf("eight\n"); break; case '9': printf("nine\n"); break; case 'Q': case 'q': printf("bye\n"); quit=1; break; default: printf("Wrong! Try Again!\n"); } } while(!quit); } ``` <h4> 試撰寫一程式,由鍵盤輸入一個整數,然後判別此數是否為質數(prime若是,則印出“此數是質數”字串,若不是,則印出“此數不是質數”字串(質數是指除了1和它本身之外,沒有其他的數可以整除它的數,例如,2,3,5,7與11等皆為質數)。 </h4> ``` #include <stdio.h> #include <stdlib.h> int main(void) { int i,num,flag=1; printf("請輸入一個整數: "); scanf("%d",&num); for(i=2;i<num;i++) if(num%i==0) { flag=0; break; } if(flag==1) printf("此數是質數\n"); else printf("此數不是質數\n"); system("pause"); return 0; } ``` <h4> 以下是經典的數學謎題 "五猴分桃"問題:海灘上有一堆桃子,五隻猴子來分。第一隻猴子把這堆桃子平均分為五份,多了一個,這隻猴子把多的一個扔入海中,拿走了一份。第二隻猴子把剩下的桃子又平均分成五份,又多了一個,它同樣把多的一個扔入海中,拿走了一份,第三、第四、第五隻猴子都是這樣做的,問海灘上原來最少有多少個桃子? </h4> ``` #include <stdio.h> int main() { int x = 1; // 假設最少有一個桃子 int i; while (1) { int temp =x; intcanShare = 1; // 假設可以平均分配 for (i = 0;i < 5; i++) { if((temp - 1) % 5 == 0) { temp = (temp - 1) / 5 * 4; // 平均分配後保留五分之四 } else{ canShare = 0; // 不能平均分配 break; } } if(canShare) { printf("原始海灘上最少有 %d 個桃子。\n", x); break; } x++; // 嘗試下一個數字 } return 0; } ``` <h1> MidTerm </h1> <h4> 試撰寫一個程式,利用for迴圈計算12-22+32-42+...+472-482+492-502的值。<br> /* output---------------------------<br> 1^2-2^2+3^2-4^2+...+49^2-50^2=-1275<br> ----------------------------------*/<br> </h4> ``` /* hw7_9.c */ #include <stdio.h> #include <stdlib.h> int main(void) { int i,j,sum=0; for(i=1,j=2;i<=49;i+=2,j+=2) sum+=i*i-j*j; printf("1^2-2^2+3^2-4^2+...+49^2-50^2=%d\n",sum); system("pause"); return 0; } /* output--------------------------- 1^2-2^2+3^2-4^2+...+49^2-50^2=-1275 ----------------------------------*/ ``` <h4> 試撰寫一程式,利用while迴圈印出1~10之間所有整數的平方值,最後再印出這些平方值的總和。 /* output------<br> 1*1=1<br> 2*2=4<br> 3*3=9<br> 4*4=16<br> 5*5=25<br> 6*6=36<br> 7*7=49<br> 8*8=64<br> 9*9=81<br> 10*10=100<br> sum=385<br> -------------*/<br> </h4> ``` /* hw7_17.c */ #include <stdio.h> #include <stdlib.h> int main(void) { int num=1,sum=0; while(num<=10) { printf("%d*%d=%d\n",num,num,num*num); sum+=num*num; num++; } printf("sum=%d\n",sum); system("pause"); return 0; } /* output------ 1*1=1 2*2=4 3*3=9 4*4=16 5*5=25 6*6=36 7*7=49 8*8=64 9*9=81 10*10=100 sum=385 ``` <h4> 進制轉換 輸入說明:每1筆測試資料為32-bit的2進制數值,每8-bit中間會有一個空白鍵 輸出說明:將2進制數值轉換成16進制數值輸出(每個Byte有一個底線) /*OUTPUT======================================= 請輸32位元的二進制,並以空白將其分為8位元為一組: 00000001 00011111 00111111 01111111 01_1F_3F_7F =================================================* </h4> ``` #include <stdio.h> #include <stdlib.h> #include <math.h> /* run this program using the consolepauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { charc1[8],c2[8],c3[8],c4[8]; inti; intnum1=0,num2=0; intx=0;//判定每個字串(c1,c2...)是否為0,為0時為1,不為0時為1;x是為了處理 00000000 00000000 00000000 00001010 => 0A printf("請輸32位元的二進制,並以空白將其分為8位元為一組: \n"); scanf("%s",&c1); scanf("%s",&c2); scanf("%s",&c3); scanf("%s",&c4); /////////////C1 for(i=0;i<4;i++){//前四位元處理 num1+= (c1[i]-'0')*pow(2,3-i); } for(i=0;i<4;i++){//後四位元處理 num2+= (c1[i+4]-'0')*pow(2,3-i); } if((num1+num2)>0){ printf("%X",num1); printf("%X_",num2); x=1; }else{ x=0; } /////////////C2 num1=0; num2=0; for(i=0;i<4;i++){//前四位元處理 num1+= (c2[i]-'0')*pow(2,3-i); } for(i=0;i<4;i++){//後四位元處理 num2+= (c2[i+4]-'0')*pow(2,3-i); } if((num1+num2)>0){ printf("%X",num1); printf("%X_",num2); x=1; }else{ if(x==1){ printf("00_");//當上一筆c1不為0時c2印出00 } } /////////////C3 num1=0; num2=0; for(i=0;i<4;i++){//前四位元處理 num1+= (c3[i]-'0')*pow(2,3-i); } for(i=0;i<4;i++){//後四位元處理 num2+= (c3[i+4]-'0')*pow(2,3-i); } if((num1+num2)>0){ printf("%X",num1); printf("%X_",num2); x=1; }else{ if(x==1){ printf("00_");//當上一筆c2不為0時c3印出00 } } /////////////C4 num1=0; num2=0; for(i=0;i<4;i++){//前四位元處理 num1+= (c4[i]-'0')*pow(2,3-i); } for(i=0;i<4;i++){//後四位元處理 num2+= (c4[i+4]-'0')*pow(2,3-i); } if((num1+num2)>0){ printf("%X",num1); printf("%X",num2); }else{ printf("00");//當全部為0(c1=0,c2=0...)時印出00 } } ``` <h4> 請使用C語言設計一個程式,讓使用者輸入一個整數座標(X, Y),座標值,判斷該點位於那一個象限或是在座標軸上。舉例來說,若輸入的座標值為 (0,0) ,則優先輸出為Origin(原點);若輸入的座標值為 (4,0),則輸出即為x-axis(x 軸);若輸入的座標值為 (3,-2),則輸出即為 4th Quadrant(第四象限),請參考以下的執行結果: Please input (X, Y): 4 0 x-axis Please input (X, Y): 3 21 1st Quadrant Please input (X, Y):r-8 2 2nd Quadrant Please input (X, Y): -7 -2 3rd Quadrant Please input (X, Y): 3 -2 4th Quadrant Please input (X, Y): 0 0 Origin Please input (X, Y): 0 5 y-axis </h4> ``` #include <stdio.h> int main() { int x, y; printf("Please input (X, Y): "); scanf("%d %d", &x, &y); if((x==0)&&(y==0)) printf("Origin\n"); else if(x==0) printf("y-axis\n"); else if(y==0) printf("x-axis\n"); else if((x>0)&&(y>0)) printf("1st Quadrant\n"); else if((x<0)&&(y>0)) printf("2nd Quadrant\n"); else if((x<0)&&(y<0)) printf("3rd Quadrand\n"); else printf("4th Quadrant\n"); } #線上系統考題皆由教材參考書籍及自訂教材中出題。 ``` <h4> 試撰寫一程式,利用printf()函數列印出如下的字串(必須包含雙引號): "100/4=25" </h4> ``` /* hw4_4.c */ #include <stdio.h> #include <stdlib.h> int main(void) { printf("\"100/4=25\"\n"); system("pause"); return 0; } /* output------- "100/4=25" --------------*/ ``` <h4> 請宣告 i=10,j=20,num=5,k=30; 且num = num+i 求 i + j + k =? 範例: i + j + k = 60 </h4> ``` #include <stdio.h> #include <stdlib.h> int main() { int i=10,j=20,num=5; int k=30; num = num+i; printf("i + j + k = %d.\n", i+j+k); system("PAUSE"); return 0; } #線上系統考題皆由教材參考書籍及自訂教材中出題。 ``` <h2> 別人的Midterm </h2> <h4> 請設計一個C語言的程式,要求使用者輸入一個偏移量與一個字元,並將偏移前與偏移後的字元與對應的ASCII數值以十進制的整數輸出,其執行結果可參考以下的畫面: Please input a character and an offset: B 3 66(B)+3=69(E) </h4> ``` #include <stdio.h> int main() { char c; int offset; printf("Please input a character and an offset: "); scanf(" %c %d", &c, &offset); printf("%d(%c)+%d=%d(%c)\n", c, c, offset, c+offset, c+offset); } -------------------------------------------- Pleaserinputrarcharacterrandranroffset:rBr3 66(B)+3=69(E) Pleaserinputrarcharacterrandranroffset:rDr5 68(D)+5=73(I) -------------------------------------------- #線上系統考題皆由教材參考書籍及自訂教材中出題。 ``` <h4> 設計一個C語言的程式,讓使用者輸入距離(單位為公尺,僅考慮大於0的正整數)與時間(單位為分鐘,僅考慮大於0的正整數),計算並輸出對應的速度(必須為double型態的浮點數)。速度公式為: 速度=距離/時間,此題單位為公尺/分鐘,其執行結果可參考以下的畫面: distance(m): 60 time(min): 30 speed=r2.000000 m/min </h4> ``` 註:本題如有使用浮點數的需求,請使用double做為相關變數的資料型態。 #include <stdio.h> int main() { int distance, minute; printf("distance(m): "); scanf(" %d", &distance); printf("time(min): "); scanf(" %d", &minute); printf("speed= %f m/min\n", distance/(float)minute); } #線上系統考題皆由教材參考書籍及自訂教材中出題。 ``` <h4> 請使用C語言設計一個程式,根據世界衛生組織計算標準體重之方法,男生標準體重=(身高-80 )*0.7,女生標準體重=(身高-70)*0.6;試寫一個程式可以計算男生女生的標準體重,輸出結果僅顯示到小數點後1位,超過的部份請四捨五入。輸入資料時,以字元M代表男性(male)、F代表女性(female),並可輸入包含有小數的身高資料。請參考以下的執行結果: Please input your gender: M Please input your height: 172.5 The standard weight: 64.8 kg Please input your gender: F Please input your height: 165 The standard weight: 57.0 kg Please input your gender: F Please input your height: 160 The standard weight: 54.0 kg </h4> ``` #include <stdio.h> int main() { double height; char gender; printf("Please input your gender: "); scanf("%c", &gender); printf("Please input your height: "); scanf(" %lf", &height); printf("The standard weight: "); if(gender=='M') printf("%.1f kg\n", (height-80)*0.7); else printf("%.1f kg\n", (height-70)*0.6); } #線上系統考題皆由教材參考書籍及自訂教材中出題。 ``` <h4> 請設計一個C語言程式,使用迴圈以及星號*來印出指定長度的菱形,此程式的執行結果可參考以下的輸出內容: Please input the length of the diamond: 1 ![image](https://hackmd.io/_uploads/SJGx2f4W-l.png) Please input the length of the diamond: 2 ![image](https://hackmd.io/_uploads/HJx0lhMNZbe.png) Please input the length of the diamond: 3 ![image](https://hackmd.io/_uploads/ryUb2zN-We.png) </h4> ``` #include <stdio.h> int main() { int len; int i,j, x,y; printf("Please input the length of the square: "); scanf(" %d", &len); for(i=1;i<=len;i++) { if((i==1)||(i==len)) { for(j=1;j<=len-1;j++) { printf("* "); } printf("*\n"); } else { printf("* "); for(j=1;j<=len-2;j++) { printf(" "); } printf("*\n"); } } } ``` <h1> Exam8 </h1> <h4> 試撰寫intmod(int x, int y)函數,計算x/y的餘數。並利用此函數來計算mod(17,5),即計算17/5的餘數。 </h4> ``` /* hw8_5.c */ #include <stdio.h> #include <stdlib.h> int mod(int,int); int main(void) { int x=17; int y=5; printf("mod(%d,%d)=%d\n",x,y,mod(x,y)); system("pause"); return 0; } int mod(int x,int y) { return x%y; } ``` <h4> 試撰寫voidkitty(int k)函數,而k=3,當主程式呼叫kitty(k)時,螢幕上會顯示出3行的"HelloKitty" </h4> ``` #include <stdio.h> #include <stdlib.h> void kitty(int); int main(void) { int k=3; kitty(k); system("pause"); return 0; } void kitty(int n) { int i; for(i=1;i<=n;i++) printf("Hello Kitty\n"); return; } ``` <h4> 試撰寫int prime(intn),可用來找出第n個質數(第一個質數為2,第二個質數為3,以此類推),並以此函數找出第100 個質數。 </h4> ``` /* hw8_7.c */ #include <stdio.h> #include <stdlib.h> int is_prime(int); int main(void) { int cnt=0,x=2; while(cnt<100) { if(is_prime(x++)) cnt++; } printf("第100個質數是%d\n",x-1); system("pause"); return 0; } int is_prime(int num) { int i; for(i=2;i<=num-1;i++) if(num%i==0) return 0; return 1; } ``` <h1> Exam9 </h1> <h4> 如果質数滿足2p-1(p為正整數)的話,則該質數稱為梅森尼質數(Mersenneprimes)。例如 7是梅森尼質數,因為p=3時,23-1=7。另外·11 就不是梅森尼質數因為我們找不到一個整數p,使得2p-1=11。 現在請您撰寫程式碼,找出前8個梅森尼質數,並於主程式裡測試之。 </h4> ``` /* hw8_10.c */ #include <stdio.h> #include <stdlib.h> int is_prime(int); int power(int,int); int main(void) { int cnt=0,p=1,mer; printf("前8個梅森尼質數:"); while(cnt<8) { mer=power(2,p)-1; if(is_prime(mer)) { printf("%d ",mer); cnt++; } p++; } printf("\n"); system("pause"); return 0; } int is_prime(int num) { int i; if(num==1) return 0; else { for(i=2;i<=num-1;i++) if(num%i==0) return 0; } return 1; } int power(int base,int n) { int i; int pow=1; for(i=1;i<=n;i++) pow*=base; return pow; } ``` <h4> 試撰寫一程式,用來比較計算fb(n)數,遞迴版本和迴圈版本執行fib()的總次數。n值取1~30,執行結果應如下所示: n=1, for迴圈1次,遞迴1次 n=2, for迴圈1次,遞迴1次 n=3, for迴圈3次,遞迴3次 n=4, for迴圈4次,遞迴5次 … n=30, for迴圈30次,遞迴1664079次 </h4> ``` /* hw8_27.c */ #include <stdio.h> #include <stdlib.h> void count_r(void), count_for(void); int fib(int),rfib(int); int cnt1,cnt2; int main(void) { int i; for(i=1;i<=30;i++) { cnt1=0; cnt2=0; fib(i); printf("n=%2d, for迴圈%2d次, ",i,cnt1); rfib(i); printf("遞迴%d次\n",cnt2); } system("pause"); return 0; } void count_for(void) { cnt1++; } void count_r(void) { cnt2++; } int fib(int n) { int i,fn; int fn_1=1,fn_2=1; for(i=1;i<=n;i++) { count_for(); if(n==1 || n==2) return 1; else { fn=fn_1+fn_2; fn_2=fn_1; fn_1=fn; } } return fn; } int rfib(int n) { count_r(); if(n==1 || n==2) return 1; else return rfib(n-1)+rfib(n-2); } ``` <h4> 請參考下列main.c程式來設計以遞迴(recursion)方式完成ab的計算(意即求a的b次方)的rpower()函式。 main.c ---------------------------------------- #include <stdio.h> intmain() { int a, b; printf("a=? "); scanf("%d", &a); printf("b=? "); scanf("%d", &b); printf("The %d-th power of %d is%d.\n", b, a , rpower(a,b) ); } -------------------------------------------------------------------------------- 提示:power(a,b)= power(a,b-1) sum(N-1) × a 此程式執行畫面如下: a=? 2 b=? 5 The 5-th power of 2 is 32. a=? 3 b=? 6 The 6-th power of 3 is 729. </h4> ``` #include <stdio.h> #include <stdio.h> int rpower(int a, int b); int main() { int a, b; printf("a=? "); scanf("%d", &a); printf("b=? "); scanf("%d", &b); printf("The %d-th power of %d is %d.\n", b, a, rpower(a,b) ); } int rpower(int a, int b) { if(b==1) return a; else return rpower(a, b-1)*a; } ``` <h4> 以下關於遞迴的敘述何者正確? </h4> <p> A是一種達到特定條件前,不斷呼叫自己的函式 B利用自我呼叫提升程式的執行效率 C可減少記憶體空間的使用 D可有效提升程式執行的安全 E以上皆正確 正確答案: A </p> <h4> 以下選項何者符合使用函式的優點? </h4> <p> A能夠減少重複的程式碼編寫 B增加程式可讀性 C能建立共享函式庫分享程式碼 D提高開發程式的效率 E以上皆正確 正確答案: E </p> <h4> 假設我們要設計一個名為getUserName()的函式,其作用為取回一個使用者所輸入的姓名(包含\0在內,不超過10個字元的字串)後傳回。以下何者為該函式正確的原型寫法: </h4> <p> A char [10] getUserName(); B char [] getUserName(); C char *getUserName(); D char **getUserName(); E 以上皆不正確 正確答案: C </p> <h4> 試寫一函式 int cla(int n),計算下列數學運算式的值「積項之和s=s+(i*i)」 </h4> ``` #include<stdio.h> #include<stdlib.h> intcla(int n); intmain(int argc, char *argv[]) { int n; //宣告整數變數 printf("請輸入一數字:"); //列印"請輸入一數字:" scanf("%d",&n); //接收以10進位之n值 printf("ans=%d\n",cla(n)); //列印"ans",值為執行cla之結果 return 0; } intcla(int n){ int i,s=0; //宣告變數 for(i=1;i<=n;i++){ // i=1,i<=n,i=i+1 s+=(i*i); //s=s+(i*i);ex:i=3則 (1*1)+(2*2)+(3*3) } return s; //回傳s值 } ``` <h4> (汽車租賃服務)汽車租賃服務收費最低為25美元,可租用汽車8小時,8小時後每小時另外收費5美元。每日最高收費為50美元,不含服務稅。該公司每小時收取0.50美元的服務稅。假設沒有汽車會租出去超過72小時。如果租用汽車的時間超過24小時,則以日計費。請編寫一個程式,計算並印出昨天從該公司租用汽車的三位客戶中,每一位的租賃費用。你應該輸入每位客戶租用汽車的時間(以小時計)。你的程式必須以表格的格式印出結果,並且應該計算及印出昨天的收據總金額。程式必須使用函式calculateCharges來確定每個客戶的收費。輸出結果應該以下列格式顯示: </h4> ![螢幕擷取畫面 2025-12-31 151921](https://hackmd.io/_uploads/B1nRaBMNWl.png) ``` #include <stdio.h> #include <math.h> /* 函式原型宣告:計算租車費用的邏輯 */ double calculateCharges(int hours); int main() { int hour; /* 當前車輛的租借小時數 */ double currentCharge; /* 當前車輛的租費 */ double totalCharges = 0.0; /* 總費用累積 */ double totalHours = 0.0; /* 總小時累積 */ int i; /* 迴圈計數器 */ int first = 1; /* 旗標:用於僅列印一次表頭 */ printf("Enter the hours rented for 3 cars: "); /* 執行 3 次迴圈以讀取 3 輛車的資料 */ for (i = 1; i <= 3; ++i) { scanf("%d", &hour); totalHours += hour; /* 若為第一次迴圈,則列印表格標題 */ if (first) { printf("%5s%15s%15s\n", "Car", "Hours", "Charge"); first = 0; } /* 計算並更新總金額 */ currentCharge = calculateCharges(hour); totalCharges += currentCharge; /* 列印當前車輛的明細 */ printf("%5d%15d%15.2f\n", i, hour, currentCharge); } /* 最後列印總結列 */ printf("%5s%15.1f%15.2f\n", "TOTAL", totalHours, totalCharges); return 0; } /* 計算邏輯:根據租借小時數回傳租費 */ double calculateCharges(int hours) { double tax; double charge; if (hours >= 24) { /* 跨日計算:每 24 小時為一個單位 */ int day = hours / 24; if (hours % 24 == 0) charge = day * 50.0; else charge = (day + 1) * 50.0; } else if (hours < 24 && hours > 8) { /* 8 到 24 小時之間:基本費 25 + 每小時 5 元 (上限 50 元) */ double tcharge = 25.0 + 5.0 * (hours - 8); charge = (tcharge > 50.0) ? 50.0 : tcharge; } else { /* 8 小時以內收取基本費 25 元 */ charge = 25.0; } /* 加上服務稅:每小時 0.5 元 */ tax = 0.50 * (double)hours; charge = tax + charge; return charge; } ``` <h4> 請撰寫 C 程式建立匯率換算函數 double rateExchange(int amount, double rate), //參數分別是台幣金額(amount)和匯率(rate),可以傳回美金兌換成的台幣金額。 </h4> ``` #include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ //(14) 在 C 程式建立匯率換算函數 double rateExchange(int amount, double rate), //參數分別是台幣金額(amount)和匯率(rate),可以傳回台幣兌換成的美金金額。 double rateExchange(int amount, double rate); //宣告原型 int main(int argc, char *argv[]) { int amount; //宣告整數變數 double rate; //宣告雙精度浮點數變數 printf("請輸入美金金額及美金兌台幣匯率:"); //列印"請輸入台幣金額及匯率:" scanf("%d %lf",&amount,&rate); //接收輸入之amount值(整數),rate值(雙精度浮點數) printf("可兌換台幣金額為%.3f\n",rateExchange(amount,rate)); //印出rateExchange值,取至小數點後3位 return 0; } double rateExchange(int amount, double rate){ return amount*rate; //回傳 amount*rate值 } ``` <h1> Exam 10 </h1> <h4> 請設計C語言程式RecursiveSum.c以及RecursiveSum.h,以遞迴(遞迴)方式完成1+2+3+… + N的計算。請參考以下的Main.c程式: #include <stdio.h> int main() { int N; printf("N=? "); scanf("%d", &N); printf("The sum of 1 to %d is %d.\n",N, sum(N) ); } 提示:sum(N) =sum(N-1) + N 此程式執行畫面如下: N=?r5 The sum of the 1 to 5 is 15. N=? 10 The sum of the 1 to 10 is 55. </h4> ``` #include <stdio.h> int sum(int N); int main() { int N; printf("N=? "); scanf("%d", &N); printf("The sum of 1 to %d is %d.\n", N, sum(N) ); } int sum(int N) { if(N==1) return 1; else return N+sum(N-1); } ``` <h4> 試撰寫voidkitty(int k)函數,而k=3,當主程式呼叫kitty(k)時,螢幕上會顯示出3行的"HelloKitty"。 </h4> ``` /* hw8_2.c */ #include <stdio.h> #include <stdlib.h> void kitty(int); int main(void) { int k=3; kitty(k); system("pause"); return 0; } void kitty(int n) { int i; for(i=1;i<=n;i++) printf("Hello Kitty\n"); return; } ``` <h4>請撰寫一個函式來計算體脂肪 BMI 值的公式是 W/(H*H),H 是身高(公尺)、W是體重(公斤)</h4> ``` #include<stdio.h> #include<stdlib.h> /* run thisprogram using the console pauser or add your own getch, system("pause")or input loop */ //(13) 計算體脂肪 BMI 值的公式是 W/(H*H),H 是身高(公尺)、W是體重(公斤) //請建立 double bmi(double,double)函數計算 BMI 值,參數是身高和體重。 double bmi(double h,double w); //格式化值為雙精度浮點數 int main(intargc, char *argv[]) { double h,w; //宣告雙精度浮點數 printf("請輸入身高(m) 體重(kg):"); //列印"請輸入身高(m) 體重(kg):" scanf("%lf %lf",&h,&w); //接收h w之值,最多取至小數點後1位 printf("BMI=%.1f\n",bmi(h,w)); //列印bmi之函式值,最多取至小數點後1位 return 0; } double bmi(double h,double w){ return w/(h*h); //回傳值 w/(h*h) } ``` <h4> 試撰寫一程式,用來比較計算fb(n)數,遞迴版本和迴圈版本執行fib()的總次數。n值取1~30,執行結果應如下所示: n=1, for迴圈1次,遞迴1次 n=2, for迴圈1次,遞迴1次 n=3, for迴圈3次,遞迴3次 n=4, for迴圈4次,遞迴5次 … n=30, for迴圈30次,遞迴1664079次 </h4> ``` /* hw8_27.c */ #include <stdio.h> #include <stdlib.h> void count_r(void), count_for(void); int fib(int),rfib(int); int cnt1,cnt2; int main(void) { int i; for(i=1;i<=30;i++) { cnt1=0; cnt2=0; fib(i); printf("n=%2d, for迴圈%2d次, ",i,cnt1); rfib(i); printf("遞迴%d次\n",cnt2); } system("pause"); return 0; } void count_for(void) { cnt1++; } void count_r(void) { cnt2++; } int fib(int n) { int i,fn; int fn_1=1,fn_2=1; for(i=1;i<=n;i++) { count_for(); if(n==1 || n==2) return 1; else { fn=fn_1+fn_2; fn_2=fn_1; fn_1=fn; } } return fn; } int rfib(int n) { count_r(); if(n==1 || n==2) return 1; else return rfib(n-1)+rfib(n-2); } ``` <h4> 請撰寫 void showNumber(int a, int b)讓使用者分別輸入正整數 a 和 b,然後顯示從 a 到 b 之間所有 3 的倍數。 範例如下: /*---------------------------- 請輸入a b:3 99 3~99之間3的倍數有: 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 </h4> ``` #include <stdio.h> #include <stdio.h> void shownumber(int a,int b); int main(void) { int a,b; printf("請輸入a b:\n"); scanf("%d %d",&a,&b); printf("3~99之間3的倍數有:"); shownumber(a,b); return 0; } shownumber(int a,int b) { int i; for (i=a;i<=b;i=i+1) { if (i%3==0) { printf("%d ",i); } } } ``` <h4> 請選撰寫一個程式可以用遞迴方法求5!。 執行結果如下: 0!=1 1!=1 2!=2 3!=6 4!=24 5!=120 </h4> ``` #include <stdio.h> #include <stdlib.h> /* 宣告遞迴函式 */ int fact(int j); int main() { int i; /* 迴圈從 0 執行到 5 */ for (i = 0; i <= 5; i++) { printf("%d! = %d\n", i, fact(i)); } return 0; } /* 遞迴求解階乘 */ int fact(int j) { int sum; if (j == 0) { sum = 1; /* 基礎情況:0! = 1 */ } else { sum = j * fact(j - 1); /* 遞迴呼叫:n! = n * (n-1)! */ } return sum; } ``` <h4>試利用#define定義巨集函數f(x)=4x2+6x-5,並於主程式中計算f(1.0)、f(2.2)與f(3.14)的值。</h4> ``` /* hw8_31.c */ #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; } ``` <h4> 撰寫intmod(int x, int y)函數,計算x/y的餘數。並利用此函數來計算mod(17,5),即計算17/5的餘數。 </h4> ``` /* hw8_5.c */ #include <stdio.h> #include <stdlib.h> int mod(int,int); int main(void) { int x=17; int y=5; printf("mod(%d,%d)=%d\n",x,y,mod(x,y)); system("pause"); return 0; } int mod(int x,int y) { return x%y; } ``` <h1> Exam11 </h1> <p> 1.關於C語言陣列的說明,以下何者正確? A陣列可以由不同型態的數值組成,但必須先加以宣告 B陣列的大小可以在宣告後,經由再次宣告來加以變更 C陣列的維度可以在宣告後,視情況在程式中加以改變 D陣列可以存放固定數目的數值,但其資料型態必須相同 E以上皆不正確 正確答案: D </p> <h4> 請輸入10個數再進行排序 程式分析:可以利用選擇法,即從後9個比較過程中,選擇一個最小的與第一個元素交換,下次類推,即用第二個元素與後8個進行比較,並進行交換。 /*================================= Example: please input ten num: a[0]=4 a[1]=5 a[2]=6 a[3]=9 a[4]=80 a[5]=67 a[6]=1 a[7]=2 a[8]=3 a[9]=4 4 5 6 9 80 67 1 2 3 4 After sorted 1 2 3 4 4 5 6 9 67 80 =================================*/ </h4> ``` include<math.h> #include<stdlib.h> #include<stdio.h> #define N 10 main(){ inti,j,min,tem,a[N]; /*input data*/ printf("please input ten num:\n"); for(i=0;i<N;i++) { printf("a[%d]=",i); scanf("%d",&a[i]); } printf("\n"); for(i=0;i<N;i++) printf("%5d",a[i]); printf("\n"); /*sort ten num*/ for(i=0;i<N-1;i++) { min=i; for(j=i+1;j<N;j++) if(a[min]>a[j]) min=j; tem=a[i]; a[i]=a[min]; a[min]=tem; } /*output data*/ printf("After sorted \n"); for(i=0;i<N;i++) printf("%5d",a[i]); } /*================================= Example: please inputten num: a[0]=4 a[1]=5 a[2]=6 a[3]=9 a[4]=80 a[5]=67 a[6]=1 a[7]=2 a[8]=3 a[9]=4 4 5 6 9 80 67 1 2 3 4 After sorted 1 2 3 4 4 5 6 9 67 80 -------------------------------- ===============================================*/ ``` <h4> 設計一個C語言程式,宣告一個array如下: int data[5][2]; 請將這個陣列的每一個元素所在的記憶體位址印出。其執行結果可參考以下的畫面: data[0][0]ratr0x7ffff3400000 data[0][1]ratr0x7ffff3400004 data[1][0]ratr0x7ffff3400008 data[1][1]ratr0x7ffff340000c data[2][0]ratr0x7ffff3400010 data[2][1]ratr0x7ffff3400014 data[3][0]ratr0x7ffff3400018 data[3][1]ratr0x7ffff340001c data[4][0]ratr0x7ffff3400020 data[4][1]ratr0x7ffff3400024 註:此處所顯示之記憶體位址僅供參考,其數值依實際執行結果為準。 </h4> ``` #include <stdio.h> int main() { int data[5][2]; int i,j; for(i=0;i<5;i++) { for(j=0;j<2;j++) { printf("data[%d][%d] at %p\n", i, j, &data[i][j]); } } } ``` <h4> 請輸入一個數值到已排好順序的陣列{1,4,6,9,13,16,19,28,40,100}中,並維持原來的規律。 /*=範例========================================== original array is: 1 4 6 9 13 16 19 28 40 100 insert a new number:10 1 4 6 9 10 13 16 19 28 40 100 ===============================================*/ </h4> ``` #include<math.h> #include<stdlib.h> #include<stdio.h> main(){ inta[11]={1,4,6,9,13,16,19,28,40,100}; int temp1,temp2,number,end,i,j; printf("original arrayis:\n"); for(i=0;i<10;i++) printf("%5d",a[i]); printf("\n"); printf("insert a newnumber:"); scanf("%d",&number); end=a[9]; if(number>end) a[10]=number; else {for(i=0;i<10;i++) { if(a[i]>number) {temp1=a[i]; a[i]=number; for(j=i+1;j<11;j++) {temp2=a[j]; a[j]=temp1; temp1=temp2; } break; } } } for(i=0;i<11;i++) printf("%6d",a[i]); } ``` <p> 請問下列何者可以求出名為data的int整數陣列具有多少個元素? Asizeof(data)/sizeof(int); Bsizeof(int)/sizeof(data); Csizeof(data); Delements(data); E以上皆不是 正確答案: A </p> <h4> 試撰寫一函數voidtoLower(char str[]),它可以字串的大寫字母改成小寫印出。字串的輸入請用gets()數,輸出請用puts0函數 </h4> ``` #include <stdio.h> #include <stdlib.h> void toLower(char str[]); int main(void) { char str[100]; printf("請輸入一個字串:"); gets(str); toLower(str); printf("大寫轉換成小寫後,新字串="); puts(str); system("pause"); return 0; } void toLower(char str[]) { int i=0; while(str[i]!='\0') { if(str[i]>=65 && str[i]<=90) str[i]+=32; i++; } } /* output-------------------------------------- 請輸入一個字串:Hello C, Hello World! 大寫轉換成小寫後,新字串=hello c, hello world! ---------------------------------------------*/ ``` <p> 假設scores陣列是用以存放5個學生成績的一維陣列,其宣告為「int scores[5];」,若已知score陣列所配置到記憶體空間,是從0x7fffac0d1000位址開頭。請問score[1]所配置到的記憶體空間為於下列何處? A 0x7fffac0d1000 - 0x7fffac0d1001 B 0x7fffac0d1000 - 0x7fffac0d1003 C 0x7fffac0d1004 - 0x7fffac0d1007 D 0x7fffac0d1004 - 0x7fffac0d1008 E以上皆不正確 正確答案: C </p> --- <h4> 試撰寫一程式,由鍵盤輸入5個正整數存入一陣列,並找出該陣列元素最大(小)值與其索引值,且輸出5個數之平均 /*output------------- 請輸入正整數:5 請輸入正整數:6 請輸入正整數:7 請輸入正整數:8 請輸入正整數:9 arr[]=5 6 7 8 9 最大值為9, 索引值為arr[4] 最小值為5, 索引值為arr[0] 平均值為 7 --------------------*/ </h4> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int arr[5],sum=0; int i; for(i=0;i<5;i++) { printf("請輸入正整數:"); scanf("%d",&arr[i]); sum+=arr[i]; } // int arr[5]={5,20,3,57,6}; int x,max_index=0,max=arr[0]; int min_index=0,min=arr[0]; printf("arr[]="); for(x=0;x<5;x++) { if(arr[x]>max) { max=arr[x]; max_index=x; } if(arr[x]<min) { min=arr[x]; min_index=x; } printf("%d ",arr[x]); } printf("\n最大值為%d, 索引值為arr[%d]\n",max,max_index); printf("最小值為%d, 索引值為arr[%d]\n",min,min_index); printf("平均值為 %d\n",sum/5); system("pause"); return 0; } ``` --- <p> 請問下列何者可以求出名為data的陣列所佔的記憶體空間大小? Asize(data); Blength(data); Csizeof(data); DLength(data); E以上皆不是 正確答案: C </p> --- <p> 假設程式中有一個「int data[10];」以及「int i=0;」的宣告,下列有關此陣列的操作何者有誤? A data[i]++; B data[i++]=i; C data[10-i]++; D i=data[i]++; E以上皆正確 正確答案: C </p> <h1> Exam12 </h1> <p> 1.假設程式中有一個「int data[10];」以及「int i=0;」的宣告,下列有關此陣列的操作何者有誤? 回答錯誤 單選題 (2 分) 0分 A data[i]++; B data[i++]=i; C data[10-i]++; D i=data[i]++; E 以上皆正確 正確答案: C </p> <p> 下表為某地星期一至星期四的時段一、時段二與時段三的氣溫: ![螢幕擷取畫面 2026-01-11 003821](https://hackmd.io/_uploads/rk1ilbxHbe.png) 請將上表的內容直接於程式中以陣列初值方式設定,並依序完成下列各題: a印出陣列內容。 b每日的平均溫度。 c時段一、時段二與時段三的平均氣溫。 d溫度最高的日子與時段。 e溫度最低的日子與時段。 </p> <h4> 設計一個C語言程式,宣告一個array如下: int data[5][2]; 請將這個陣列的每一個元素所在的記憶體位址印出。其執行結果可參考以下的畫面: data[0][0]ratr0x7ffff3400000 data[0][1]ratr0x7ffff3400004 data[1][0]ratr0x7ffff3400008 data[1][1]ratr0x7ffff340000c data[2][0]ratr0x7ffff3400010 data[2][1]ratr0x7ffff3400014 data[3][0]ratr0x7ffff3400018 data[3][1]ratr0x7ffff340001c data[4][0]ratr0x7ffff3400020 data[4][1]ratr0x7ffff3400024 註:此處所顯示之記憶體位址僅供參考,其數值依實際執行結果為準。 </h4> ``` #include <stdio.h> int main() { int data[5][2]; int i,j; for(i=0;i<5;i++) { for(j=0;j<2;j++) { printf("data[%d][%d] at %p\n", i, j, &data[i][j]); } } } #線上系統考題皆由教材參考書籍及自訂教材中出題。 ``` <p> 考慮以下的程式片段,若使用者分別將下列選項中的字串加以輸入,哪個字串會與輸出結果相同? char x[11]; scanf("%10[a-zA-Z]",x); printf("%s\n",x); 單選題 (2 分) 2分 A aAbBcCdDeE B ABCDE12345 C ZXCefgDTA? D #99547HJK: E以上皆不相同 正確答案: A </p> <h4> 一球從100米高度自由落下,每次落地後反跳回原高度的一半;再落下,求它在第10次落地時,共經過多少米?第10次反彈多高? /*==================== the total of road is???.?????? the tenth is ?.?????? meter =========================*/ </h4> ``` #include<math.h> #include<stdlib.h> #include<stdio.h> main(){ float sn=100.0,hn=sn/2; int n; for(n=2;n<=10;n++) //第十次落地(並未說反彈),代表只有九次的反彈。 { sn=sn+2*hn; //回彈再落下,所以2倍 hn=hn/2; //下次反彈的高度 } printf("the total of road is %f\n",sn); printf("the tenth is %f meter\n",hn); } /*================================ the total of road is 299.609375 the tenth is 0.097656 meter ===================================*/ ``` <h4> 請設計一個C語言程式,使用迴圈以及星號*來印出指定長度的菱形,此程式的執行結果可參考以下的輸出內容: Pleaserinputrtherlengthrofrtherdiamond:r1 ![螢幕擷取畫面 2026-01-11 004332](https://hackmd.io/_uploads/HJVpW-lB-l.png) Pleaserinputrtherlengthrofrtherdiamond:r2 ![螢幕擷取畫面 2026-01-11 004353](https://hackmd.io/_uploads/Hkd0ZbgH-x.png) Pleaserinputrtherlengthrofrtherdiamond:r3 ![螢幕擷取畫面 2026-01-11 004406](https://hackmd.io/_uploads/S1UyzblSWg.png) </h4> ``` #include <stdio.h> int main() { int len; int i,j, x,y; printf("Please input the length of the square: "); scanf(" %d", &len); for(i=1;i<=len;i++) { if((i==1)||(i==len)) { for(j=1;j<=len-1;j++) { printf("* "); } printf("*\n"); } else { printf("* "); for(j=1;j<=len-2;j++) { printf(" "); } printf("*\n"); } } } ``` <p> 請參考下列程式碼,以下何者是它的執行結果? for(i=0;i<10;i+=3) printf("%d", i); 回答錯誤 單選題 (2 分) 0分 A 0123456789 B 369 C 36912 D 036912 E 以上皆不正確 正確答案: E </p> <h4> 求a的連加數「s=a+aa+aaa+aaaa+aa...a」的值。例如2+22+222+2222+22222(此時共有5個數相加) /*output============================================== please input a andn(a,n),請用逗號隔開 3,8 a=3,n=8 a+aa+...=37037034 ===================================*/ </h4> ``` #include<math.h> #include<stdlib.h> #include<stdio.h> main(){ int a,n,count=1; long int sn=0,tn=0; printf("please input a and n(a,n),請用逗號隔開\n"); scanf("%d,%d",&a,&n); printf("a=%d,n=%d\n",a,n); while(count<=n) { tn=tn+a; sn=sn+tn; a=a*10; ++count; } printf("a+aa+...=%ld\n",sn); } ``` <p> 請參考以下程式碼,下列何者是正確的結果? int i,j,k=1; for(i=1;i<=5;i++){ for(j=i;j>=0;j--){ k+=j; } } printf("%d\n",k); 單選題 (2 分) 2分 A 35 B 36 C 0 D 18 E 以上皆不正確 正確答案: B </p> <h4> 請編寫一個程式,輸入一個正整數n,然後計算並輸出費波那契數列的前n項。 費波那契數列的定義為:第0項為0,第1項為1,從第2項開始,每一項都是前兩項之和。 要求使用循環結構(例如for循環或while循環)來實現計算費波那契數列。 範例輸出: 請輸入正整數n:10 費波那契數列的前10項為:0、1、1、2、3、5、8、13、21、34 </h4> ``` #include <stdio.h> int main() { int n, i; long long fib1 = 0, fib2 = 1, nextFib; printf("請輸入正整數n:"); scanf("%d", &n); printf("費波那契數列的前%d項為:", n); for (i = 0; i < n; i++) { if (i <= 1) { nextFib = i; } else { nextFib = fib1 + fib2; fib1 = fib2; fib2 = nextFib; } printf("%lld", nextFib); if (i < n - 1) { printf("、"); } } printf("\n"); return 0; } /*---------------------------- 這個題目要求你編寫一個程式來計算並輸出費波那契數列的前n項。費波那契數列是一個數列,其中每一項(從第2項開始)都是前兩項之和。以下是解題步驟: 首先,你需要引入頭文件stdio.h 來使用輸入輸出函數。 然後,宣告變數來儲存使用者輸入的正整數n,以及用於計算費波那契數列的變數。 提示使用者輸入正整數n。 使用迴圈結構(例如for迴圈或while迴圈),從第0項開始迭代計算費波那契數列的每一項,並輸出結果。 在這個範例中,我們使用了兩個變數fib1 和fib2 來儲存當前的兩個費波那契數,以及一個變數 來計算下一個費波那契數。在迭代過程中,我們不斷更新這些變數的值,從而計算出費波那契數列的每一項。 請注意,由於費波那契數列可能會很大,我們使用了long long 類型來儲存數值,以確保足夠的位數。同時,我們在輸出時使用條件語句來處理逗號的輸出,以保證格式正確。 你可以將此程式碼複製到C編譯器中運行,輸入一個正整數n,即可得到相應的費波那契數列前n項。 ``` <p> 請參考以下的程式碼,它限制了使用者只能輸入介於1~5之間(包含1,也包含5)的數字。請問此do while迴圈的測試條件(test_condition)該怎麼寫? int num; do { scanf(" %d", &num); } while( [ test_condition ] ); 回答錯誤 單選題 (2 分) 0分 A num<1 || num>5 B num >= 1 && num<=5 C 1 <= num <= 5 D num <= 1 || num >=5 E 以上都不正確 正確答案: A </p> <h4> 請選撰寫一個程式可以用遞迴方法求5!。 執行結果如下: 0!=1 1!=1 2!=2 3!=6 4!=24 5!=120 </h4> ``` #include<math.h> #include<stdlib.h> #include<stdio.h> int fact(int); main(){ int i; for(i=0;i<=5;i++){ printf("%d!=%d\n",i,fact(i)); } } int fact(int j){ int sum; if(j==0) sum=1; else sum=j*fact(j-1); return sum; } ``` <h4> 請利用下列main.c為主程式,完成printArray及swapTwoElements二個子函式。 main.c --------------------------------------------- #include <stdio.h> int main() { int data[10]; int i,j; printf("Please input 10 numbers:"); for(i=0;i<10;i++) scanf("%d",&data[i]); printf("Which two elements you want toswitch? "); scanf("%d %d", &i, &j); printArray(data, 10); swapTwoElements(data, i, j); printArray(data, 10); } -------------------------------------------------------------- 此程式讓使用者輸入10個數字並放到data陣列中,並透過printArray()函數將值印出,接著讓使用者輸入i與j兩個整數值,並呼叫swapTwoElements()函式,將陣列中索引值為i與j的元素進行交換,請參考以下的執行畫面: Please input 10 numbers: 1 2 3 4 5 6 7 8 9 10 Whichrtworelementsryourwantrtorswap:r3r5 1 2 3 4 5 6 7 8 9 10 1 2 3 6 5 4 7 8 9 10 Please input 10 numbers: 43 23 24 9 25 47 98 12 3 77 Which two elements you want to swap: 1 8 43 23 24 9 25 47 98 12 3 77 43 3 24 9 25 47 98 12 23 77 </h4> ``` #include <stdio.h> #include "Toolbox.h" #define Increasing 0 #define Decreasing 1 void newline(); void sortArray(int[], int, int); void printArray(int[], int); void swapTwoElements(int[], int, int); int main() { int data[10]; int i,j; printf("Please input 10 numbers: "); for(i=0;i<10;i++) scanf("%d", &data[i]); printf("Which two elements you want to switch? "); scanf("%d %d", &i, &j); printArray(data, 10); swapTwoElements(data, i, j); printArray(data, 10); } void newline() { printf("\n"); } void swapTwoElements(int data[], int a, int b) { int temp; temp=data[a]; data[a]=data[b]; data[b]=temp; } void sortArray(int data[], int size, int order) { int i, j, temp; for(i=0;i<size-1;i++) { for(j=i;j<size;j++) { if((data[i]>data[j])&&(order==Increasing)) { temp=data[i]; data[i]=data[j]; data[j]=temp; } else if((data[i]<data[j])&&(order==Decreasing)) { temp=data[i]; data[i]=data[j]; data[j]=temp; } } } } void printArray(int data[], int size) { int i; for(i=0;i<size;i++) { printf("%d ", data[i]); } newline(); } ``` <h4> 請撰寫 C 程式建立匯率換算函數 double rateExchange(int amount, double rate), //參數分別是台幣金額(amount)和匯率(rate),可以傳回美金兌換成的台幣金額。 /*------------------------------------------- 請輸入美金金額及美金兌台幣匯率:400 30.2 可兌換台幣金額為12080.000 --------------------------------------------*/ </h4> ``` #include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ //(14) 在 C 程式建立匯率換算函數 double rateExchange(int amount, double rate), //參數分別是台幣金額(amount)和匯率(rate),可以傳回台幣兌換成的美金金額。 double rateExchange(int amount, double rate); //宣告原型 int main(int argc, char *argv[]) { int amount; //宣告整數變數 double rate; //宣告雙精度浮點數變數 printf("請輸入美金金額及美金兌台幣匯率:"); //列印"請輸入台幣金額及匯率:" scanf("%d %lf",&amount,&rate); //接收輸入之amount值(整數),rate值(雙精度浮點數) printf("可兌換台幣金額為%.3f\n",rateExchange(amount,rate)); //印出rateExchange值,取至小數點後3位 return 0; } double rateExchange(int amount, double rate){ return amount*rate; //回傳 amount*rate值 } ``` --- --- <h2> P3-2 No.3_1 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int num1=12400; double num2=5.234; printf("%d is an interger\n",num1); printf("%f is a double\n",num2); system("pause"); return 0; } ``` <h2> P3-6 N.3_2 溢位的發生 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { short sum,s=32767; sum=s+1; printf("s+1= %d\n",sum); sum=s+2; printf("s+2= %d\n",sum); system("pause"); return 0; } ``` <h2> P3-9 No.3_3 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { char ch='a'; printf("ch= %c\n",ch); printf("ASCII of ch= %d\n",ch); system("pause"); return 0; } ``` <h2> P3-10 No.3-10 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { char ch=90; printf("ch=%c\n",ch) ; system("pause"); return 0; } ``` <h2> P3-11 No.3_5 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { char ch='2'; printf("ch=%c\n",ch); printf("the ASCII of ch is %d\n",ch); system("pause"); return 0; } ``` <h2> P3-12 No.3_6 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int i=298; printf("ASCII (%d) = %c\n",i,i); system("pause"); return 0; } ``` <h2> P3-14 No.3_7 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { char beep='\a'; printf("%c",beep); printf("ASCII of beep=%d"); system("pause"); return 0; } ``` <h2> P3-15 No.3_8 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { char ch='\"'; printf("%cwe are the world%c\n",ch,ch); system("pause"); return 0; } ``` <h2> P3-16 No.3_9 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { float num1=123.45F; float num2=4.56E-3F; printf("num1=%e\n",num1); printf("num2=%f\n",num2); system("pause"); return 0; } ``` <h2> P3-18 No.3_10 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { float num1=123.456789012345F; double num2=123.456789012345; printf("num1=%16.12f\n",num1); printf("num2=%16.12f\n",num2); system("pause"); return 0; } ``` <h2> P3-20 No.3_11 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { char ch; float num; printf("sizeof(2L)=%d\n",sizeof(2L)); printf("sizeof(ch)=%d\n",sizeof(ch)); printf("sizeof(num)=%d\n",sizeof(num)); printf("sizeof(int)=%d\n",sizeof(int)); printf("sizeof(long)=%d\n",sizeof(long)); printf("sizeof(short)=%d\n",sizeof(short)); system("pause"); return 0; } ``` <h2> P3-21 No.3_12 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int n1,n2; float num1=3.002F,num2=3.988F; n1=(int)num1; n2=(int)num2; printf("num1=%f,num2=%f\n",num1,num2); printf("n1=%d,n2=%d\n",n1,n2); system("pause"); return 0; } ``` <h2> P3-22 No.3_12 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int num=5; printf("num/2=%d\n",num/2); printf("(float)num/2=%f\n",(float)num/2); system("pause"); return 0; } ``` <h1> 2025/10/22 </h1> <h2> P5-23 No.11 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { float a; printf("請輸入攝氏溫度\n"); scanf("%f",&a); printf("華氏溫度為%f\n",(float)9/5*a+32); system("pause"); return 0; } ``` [驗算](https://www.metric-conversions.org/zh-hant/temperature/celsius-to-fahrenheit.htm <h2> 5-24 No.15 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int a; int b; printf("請輸入底\n"); scanf("%d",&a); printf("請輸入高\n"); scanf("%d",&b); printf("平行四邊形面積為%d\n",a*b); system("pause"); return 0; } ``` <h2> 5-24 No.18 (有問題) </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { char a='A'; short b=38; float c=10.4f; int d=12; double e=8.4; printf("a*(b*c)+(d/e)=%d\n",a*(b*c)+(d/e)); printf("size=%d\n",sizeof((a*(b*c)+(d/e)))); printf("a-(b*+c)+d-e=%d\n",a-(b*+c)+d-e); printf("size=%d\n",sizeof((a-(b*+c)+d-e))); printf("d+(b+c)+a=(d*a)=%d\n",a-(b*+c)+d-e); printf("size=%d\n",sizeof((a-(b*+c)+d-e))); printf("5-(a+b)/4=%d\n",5-(a+b)/4); printf("size=%d\n",sizeof((5-(a+b)/4))); system("pause"); return 0; } ``` <h2> P6-3 No.6_1 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int num; printf("請輸入一個整數"); scanf("%d",&num); if(num>0) printf("您鍵入的數字大於0\n"); printf("程式結束"); system("pause"); return 0; } ``` <h2> P6-4 No.6_2 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int num; printf("請輸入一個整數"); scanf("%d",&num); if(num>0) printf("您鍵入的數字大於0\n"); if(num<=0) printf("您鍵入的數字小於或等於0\n"); printf("程式結束"); system("pause"); return 0; } ``` <h2> P6-7 No.6_3 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int num; printf("請輸入一個整數"); scanf("%d",&num); if(num>0) printf("您鍵入的數字大於0\n"); else printf("您鍵入的整數小於或等於0\n"); printf("程式結束"); system("pause"); return 0; } ``` <h2> P6-9 No.6_4 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int num; printf("請輸入一個整數"); scanf("%d",&num); if(num%2!=0) { printf("%d不能被2整除,",num); printf("所以%d是奇數\n,\n",num); } else { printf("%d可以被2整除,",num); printf("所以%d是偶數\n",num); } system("pause"); return 0; } ``` <h2> P6-11 No.6_5 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int score; printf("請輸入成績"); scanf("%d",&score); if (score<60) { if(score>=50) printf("請參加補考"); else printf("請重修"); } else printf("本科及格\n"); system("pause"); return 0; } ``` <h2> P6-14 No.6_6 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int score; printf("Your score"); scanf("%d",&score); if (score>=80) printf("%d is A\n",score); else if(score>=70) printf("%d is B\n",score); else if(score>=60) printf("%d is C\n",score); else printf("Failed!!\n"); system("pause"); return 0; } ``` <h2> P6-15 No.6_7 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int num; printf("請輸入一個整數:"); scanf("%d",&num); if (num>=0) if(num<=10) printf("數字介於0~10之間\n"); else printf("數字大於10\n"); system("pause"); return 0; } ``` <h2> P6-16 No.6_8 判斷整數有沒有介於0~10之間 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int num; printf("請輸入一個整數:"); scanf("%d",&num); if (num>=0) { if(num<=10) printf("數字介於0~10之間\n"); } else printf("數字小於0\n"); system("pause"); return 0; } ``` <h2> P6-19 No.6_9 兩數值比誰較大 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int num1,num2,larger; printf("請輸入兩個整數"); scanf("%d %d",&num1,&num2); num1>num2 ? (larger=num1) : (larger=num2); printf("%d數值較大\n",larger); system("pause"); return 0; } ``` <h2> P6-31 No.11 工時薪資判斷 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int num; printf("請輸入工時:"); scanf("%d",&num); if (num==80) printf("%.1f",60*75+15*75*1.25+5*75*1.75); else if(num>75) printf("%d\n",num*75*1.75); else if(num>60) printf("%d\n",num*75*1.25); else printf("%d\n",num*75); system("pause"); return 0; } ``` <h2> P6-31 No.10 座標象限判斷 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { float X,Y; printf("請輸入一個X座標一個Y座標\n"); scanf("%f,%f",&X,&Y); 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&&Y==0) printf("原點\n"); else if (Y==0) printf("X軸\n"); else if (X==0) printf("Y軸\n"); system("pause"); return 0; } ``` <h2> 便當費用計算 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int a,total; printf("一個便當80元"); printf("請輸入要訂幾個便當:"); scanf("%d",&a); if (a>=1) { total = a*80; printf("買了%d個便當,總費用為%d\n",a,total); } else { printf("買了%d個便當,總費用為%d\n",a); } system("pause"); return 0; } ``` <h2> 判斷是否5的倍數 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int a; printf("請任意輸入一個整數\n"); scanf("%d",&a); if(a%5==0) printf("%d是5的倍數\n",a); else printf("%d不是5的倍數\n",a); system("pause"); return 0; } ``` <h2> 購物金額>2000打75折,1000~2000打85折,1000下原價 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int a=0; printf("請輸入購物車總金額:"); scanf("%d",&a); if(a<=0) { printf("輸入金額錯誤"); } else if(a>=2000) a=a*0.75; else if(a>=1000) a=a*0.85; else a=a; printf("實際消費總額:%d元\n",a); system("pause"); return 0; } ``` <h2> 平方數列的和 1*1+2*2+3*3+%d*%d=%d </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int a,b; long c=0; //宣告為常數// printf("請輸入1~100的整數:"); scanf("%d",&a); if(a>=1 && a<=100) { for(b=1;b<=a;b++) c+=(long)b*b; printf("1*1+...+%d*%d=%d\n",a,a,c); } else printf("輸入數字超出範圍"); system("pause"); return 0; } ``` <h1> 2025/10/29 </h1> <h2> P6-23 No.6_10 Switch case 運算式選擇 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int a,b; char oper; printf("請輸入運算式(例如:3+2):"); scanf("%d %c %d",&a,&oper,&b); switch(oper) { case '+': printf("%d+%d=%d\n",a,b,a+b); break; case '-': printf("%d-%d=%d\n",a,b,a-b); break; case '*': printf("%d*%d=%d\n",a,b,a*b); break; case '/': printf("%d/%d=%.3f\n",a,b,(float)a/b); break; default: printf("無法辨識的運算式"); } system("pause"); return 0; } ``` <h2> P6-25 No.6_11 輸入成績'A'、'a',印出相對內容 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { char grade; printf("Input grade:"); scanf("%c",&grade); switch(grade) { case 'A': case 'a': printf("Excellent!\n"); break; case 'B': case 'b': printf("Good!\n"); break; case 'C': case 'c': printf("Be study hard!\n"); break; default: printf("Failed!\n"); } system("pause"); return 0; } ``` <h2> P6-26 N6_12不加Break的影響 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { char grade; printf("Input grade:"); scanf("%c",&grade); switch(grade) { case 'A': case 'a': printf("Excellent!\n"); case 'B': case 'b': printf("Good!\n"); case 'C': case 'c': printf("Be study hard!\n"); default: printf("Failed!\n"); } system("pause"); return 0; } ``` <h2> 飲料點餐 Switch Case </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int a; printf(" (1)珍珠奶茶\n"); printf(" (2)葡萄柚多多\n"); printf(" (3)四季春加珍波椰\n"); printf("請輸入你要喝的飲料編號:"); scanf("%d",&a); printf("===================================================================\n"); switch(a) { case 1: printf("(1)珍珠奶茶1杯50元\n"); break; case 2: printf("(2)葡萄柚多多1杯70元\n"); break; case 3: printf("(3)四季春加珍波椰1杯55元\n"); break; default: printf("沒有你要的飲料喔!\n"); } printf("===================================================================\n"); system("pause"); return 0; } ``` <h2> P6-29 No.6_13 Goto標籤 不建議用 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int i=0,sum=0 ; start: i++; //i=i+1 sum+=i; //sum=sum+i printf("%d",i); if (i<10) { printf("+"); goto start; } printf("=%d\n",sum); system("pause"); return 0; } ``` <h2> P7-6 No.7_1 計算1+2+3+....+10=%d </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int i,sum=0; for(i=1;i<=10;i++) sum=sum+i; printf("1+2+3+...+10=%d\n",sum); system("pause"); return 0; } ``` <h2> P7-8 No.7_2 計算擲10000次骰子,出現3點的次數與機率 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int i,count=0; for(i=1;i<=10000;i++) if((rand()%6+1)==3) count=count+1; printf("擲10000次骰子時,出現3點的次數為%d次\n",count); printf("機率為%f\n",(float)count/10000); system("pause"); return 0; } ``` <h2> rand 介紹 </h2> ``` #include<stdio.h> #include<stdlib.h> #include<time.h> int main(void) { int a,b; srand(time(NULL)); /*通常用來設置隨機生成的種子,以便每次進行程序時生成不同的隨機數。 如果這行被啟用,time(NULL)返回目前時間,使用這個值做為種子,隨機樹生成器就會產生不同的序列。 */ /*產生亂數*/ int x = rand(); a=(x%1000)+1; //這行生成一個隨機數並將其賦值給變數x 。rand()返回一個在0到RAND_MAX(通常是32767)之間的整數。並將其限制在1到1000之間,賦值給a b=(rand()%100)+1; //這行生成一個新的隨機數,並將其限制在1到100之間,賦值給b printf("The Random Number id %d, %d.\n",a,b); /* 回傳一個介於0到RAND_MAX(通常是36767)的整數。 它需要搭配#include <stdlib.h>使用。 x=rand() 將隨機數存進變數X。 a=(x%1000)+1; x%1000會把x的值 取模1000,也就是限制在0到999。 再加上1,使得結果變成1到1000之間的整數。 */ system("pause"); return 0; } ``` <h2> 抽籤 </h2> ``` #include<stdio.h> #include<stdlib.h> #include<time.h> int main(void) { int a,b,c,d,e; srand(time(NULL)); printf("請輸入抽籤範圍的開始數字:"); scanf("%d",&a); printf("請輸入抽籤範圍的結束數字:"); scanf("%d",&b); printf("請輸入要抽幾個數字:"); scanf("%d",&c); for(e=1;e<=c;e++) { d = rand()%b+a; printf("%d\n",d); } system("pause"); return 0; } ``` <h2> 輸入a、b判斷是否小寫a、b </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { char a; printf("請輸入字元:"); scanf("%c",&a); if (a=='a') printf("您輸入a\n"); else if (a=='b') printf("您輸入b\n"); else printf("您輸入的不是a或b\n"); system("pause"); return 0; } ``` <h2> P6-32 No.15 找錢(1000、500、100、50、10、5、1) </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int pay,realpay,repay; int m1000=0,m500=0,m100=0,m50=0,m10=0,m5=0,m1=0; printf("請輸入應付金額"); scanf("%d",&pay); printf("請輸入實付金額"); scanf("%d",&realpay); if(realpay<pay) { printf("金額不足。\n"); } else { repay =realpay-pay; //找的錢 m1000=repay/1000; //要找的千鈔 repay=repay%1000; // 扣掉找的千鈔之後剩多少 m500=repay/500; //要找的五百鈔 repay=repay%500; // 扣掉找的五百鈔之後剩多少 m100=repay/100; //要找的百鈔 repay=repay%100; // 扣掉找的百鈔之後剩多少 m50=repay/50; //要找的50硬幣 repay=repay%50; // 扣掉找的50硬幣之後剩多少 m10=repay/10; //要找的10元硬幣 repay=repay%10; // 扣掉找的10元硬幣之後剩多少 m5=repay/5; //要找的5元硬幣 repay=repay%5; // 扣掉找的5元硬幣之後剩多少 m1=repay; //要找的1元硬幣 printf("總共要找%d張1000元、%d張500元、%d張100元、%d個50元、%d個10元、%d個5元、%d個1元。\n",m1000,m500,m100,m50,m10,m5,m1); } system("pause"); return 0; } ``` <h1> 2025/11/05 </h1> <h2> P7-11 No.7_3 while迴圈的使用 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int i=1,sum=0; while(sum<=100) { sum=sum+i; printf("從1累加到%2d=%2d\n",i,sum); i=i+1; } printf("必須累加到%d\n",i-1); system("pause"); return 0; } ``` <h2> P7-13 No.7_4 無窮迴圈的說明 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int i=1; while(i>0) { printf("i=%d\n",i++); //i=i+1 } system("pause"); return 0; } //Ctrl+c 可以終止 ``` <h2> P7-15 No.7_5 無窮迴圈的應用 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { char ch; while(ch!=17) { ch=getch(); printf("ASCII of ch=%d\n",ch); } printf("您已按了Ctrl+q...\n"); system("pause"); return 0; } ``` <h2> P7-18 No.7_6 do while 設計累加1至n </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int n,i=1,s=0; do { printf("請輸入n值(n>0):"); scanf("%d",&n); } while(n<=0); do { s=s+i; i=i+1; } while(i<=n); printf("1+2+...+%d=%d\n",n,s); system("pause"); return 0; } ``` <h2> P7-20 No.7_7 do while 設計n! </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int n,i=1,f=1; do { printf("請輸入n值(n>0):"); scanf("%d",&n); } while(n<=0); do { f=f*i; i=i+1; } while(i<=n); printf("%d!=%d\n",n,f); system("pause"); return 0; } ``` <h2> 7-39 No.13 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int n,i=0,s=0; do { printf("請輸入n值(正偶數n>0):"); scanf("%d",&n); } while(n%2!=0 || n<0); do { s=s+i; i=i+2; } while(i<=n); printf("%d\n",s); system("pause"); return 0; } ``` <h2> P7-22 No.7_8 空迴圈 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int i; for(i=1;i<=10000;i=i+1); printf("i=%d\n",i); system("pause"); return 0; } ``` <h2> P7-22 No.7_9 For迴圈 九九乘法表 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int i,j; for(i=1;i<=9;i=i+1) { for(j=1;j<=9;j=j+1) printf("%d*%d=%2d ",i,j,i*j); printf("\n"); } system("pause"); return 0; } ``` <h2> P7-27 No.7_10 while迴圈 九九乘法表 </h2> <h2> P7-38 No.9 1²-2²+3²-4²+...+49²-50²的值 </h2> ``` #include <stdio.h> #include <stdlib.h> int main(void) { int i,j,sum=0; for(i=1,j=2;i<=49;i+=2,j+=2) sum+=i*i-j*j; printf("1^2-2^2+3^2-4^2+...+49^2-50^2=%d\n",sum); system("pause"); return 0; } ``` <h1> 2025/11/26 </h1> <h2> P8-2 No.8_1 </h2> ``` #include <stdio.h> #include <stdlib.h> void star(void); int main(void) { star() ; printf("歡迎使用C語言\n"); star(); system("pause"); return 0; } void star(void) { printf("*************\n"); return; } ``` <h2> P8-9 No.8_2 </h2> ``` #include <stdio.h> #include <stdlib.h> int add(int,int); int main (void) { int sum,a=5,b=3; sum=add(a,b); printf("%d+%d=%d\n",a,b,sum); system("pause"); return 0; } int add(int num1,int num2) { int a; a=num1+num2; return a; } ``` <H2> P8-64 No.2 </H2> ``` #include <stdio.h> #include <stdlib.h> void kitty(int); int a,i,k; int main(void) { scanf("%d",&a); kitty(k); system("pause"); return 0; } void kitty(int k) { for(i=1;i<=a;i=i+1) printf("Hello Kitty\n"); return; } ``` <h2> P8-64 No.1 </h2> <h2> P8-19 No.8_8 </h2> ``` #include<stdio.h> #include<stdlib.h> void fac(int),sum(int); int main(void) { fac(5); sum(5); system("pause"); return 0; } void fac(int a) { int i,total=1; for (i=1;i<=a;i=i+1) total=total*i; printf("1*2*...*%d=%d\n",a,total); } void sum(int a) { int i,total=0; for (i=1;i<=a;i=i+1) total=total+i; printf("1+2+...+%d=%d\n",a,total); } ``` <h2> P8-22 No.8_9 Leibniz萊布尼茲函數估算𝝿 </h2> ``` #include<stdio.h> #include<stdlib.h> double Leibniz(int); double power(double,int); int main(void) { int i; for (i=1;i<=10000;i=i+1) printf("Leibniz(%d)=%12.10f\n",i,Leibniz(i)); system("pause"); return 0; } double Leibniz(int n) { int k; double sum=0; for (k=1;k<=n;k=k+1) sum=sum+power(-1.0,k-1)/(2*k-1); return 4*sum; } double power(double base,int n) { int i; double pow=1.0; for (i=1;i<=n;i=i+1) pow=pow*base; return pow; } ``` <h2> P8-25 No.8_10 遞迴函數1 fac </h2> ``` #include<stdio.h> #include<stdlib.h> int fac(int); int main(void) { printf("fac(4)=%d\n",fac(4)); system("pause"); return 0; } int fac(int n) { if (n>0) return (n*fac(n-1)); else return 1; } ``` <h2> P8-28 No.8_11 遞迴函數2 Power </h2> ``` #include<stdio.h> #include<stdlib.h> int power(int,int); int main(void) { printf("power(2,3)=%d\n",power(2,3)); system("pause"); return 0; } int power(int b,int n) { if (n==0) return 1; else return (b*power(b,n-1)); } ``` <h2> P8-31 No.8_12 費氏數列 </h2> ``` #include<stdio.h> #include<stdlib.h> int fib(int); int main(void) { int n; for(n=1;n<=10;n=n+1) printf("fib(%d)=%d\n",n,fib(n)); system("pause"); return 0; } int fib(int n) { if (n==1 || n==2) return 1; else return (fib(n-1)+fib(n-2)); } ``` <h1> 2025/12/10 </h1> <h2> P8-35 No.8_13 區域變數的範例(一) </h2> ``` #include<stdio.h> #include<stdlib.h> int fac(int); int main (void) { int ans; ans=fac(5); printf("fac(5)=%d\n",ans); system("pause"); return 0; } int fac(int n) { int i ,total=1; for (i=1;i<=n;i=i+1) total=total*i; return total; } ``` <h2> P8-36 No.8_14 區域變數的範例(二) </h2> ``` #include<stdio.h> #include<stdlib.h> void func(void); int main(void) { int a = 100; printf("呼叫func()之前,a=%d\n",a); func(); printf("呼叫func()之後,a=%d\n",a); system("pause"); return 0; } void func(void) { int a=300; printf("於func()函數裡,a=%d\n",a); } ``` <h2> P8-38 No.8_15 全域變數的使用範例(一) </h2> ``` #include<stdio.h> #include<stdlib.h> void func(void); int a; int main(void) { a = 100; printf("呼叫func()之前,a=%d\n",a); func(); printf("呼叫func()之後,a=%d\n",a); system("pause"); return 0; } void func(void) { a=300; printf("於func()函數裡,a=%d\n",a); } ``` <h2> P8-40 No.8_16全域變數的使用範例(二) </h2> ``` #include<stdio.h> #include<stdlib.h> void func(void); int a=50; int main(void) { int a = 100; printf("呼叫func()之前,a=%d\n",a); func(); printf("呼叫func()之後,a=%d\n",a); system("pause"); return 0; } void func(void) { a=a+300; printf("於func()函數裡,a=%d\n",a); } ``` <h2> P8-41 No.8_17 全域變數的使用範例(三) </h2> ``` #include<stdio.h> #include<stdlib.h> double pi=3.14; void peri(double); void area(double); int main(void) { double r=1.0; printf("pi=%.2f\n",pi); printf("radius=%.2f\n",r); peri(r); area(r); system("pause"); return 0; } void peri(double r) { printf("圓周長=%.2f\n",2*pi*r); } void area(double r) { printf("圓面積=%.2f\n",pi*r*r); } ``` <h2> P8-43 No.8_18 區域靜態變數使用的範例 </h2> ``` #include<stdio.h> #include<stdlib.h> void func(void); int main (void) { func(); func(); func(); system("pause"); return 0; } void func(void) { static int a=100; printf("In func(),a=%d\n",a); a=a+200; } ``` <h2> P8-45 No.8_19 函數的傳值機制 </h2> ``` #include<stdio.h> #include<stdlib.h> void func(void); int main (void) { func(); func(); func(); system("pause"); return 0; } void func(void) { static int a=100; printf("In func(),a=%d\n",a); a=a+200; } ``` <h1> 20251217 </h1> <h2> P8-49 No.8_20 將左右大括號define重新定義 </h2> ``` #include<stdio.h> #include<stdlib.h> #define BEGIN { #define END } int main(void) BEGIN int i,j; for(i=1;i<=5;i=i+1) BEGIN for(j=1;j<=i;j=j+1) printf("*"); printf("\n"); END system("pause"); return 0; END ``` <h2> P8-50 No.8_21 define很長可以用\分成兩行 </h2> ``` #include<stdio.h> #include<stdlib.h> #define WORD "Think of all the things\ we've shared and seen.\n" int main(void) { printf(WORD); system("pause"); return 0; } ``` <h2> P8-51 define PI No.8_22 define PI </h2> ``` #include<stdio.h> #include<stdlib.h> #define PI 3.14 double area(double); int main(void) { printf("PI=%4.2f, area()=%6.2f\n",PI,area(2.0)); system("pause"); return 0; } double area(double r) { return PI*r*r; } ``` <h2> P8-53 No.8_23 const修飾子 </h2> ``` #include<stdio.h> #include<stdlib.h> const double pi=3.14; double area(double); int main(void) { /*若在此設定pi=3.1416,則編譯時會發生錯誤*/ printf("PI=%4.2f, area()=%6.2f\n",pi,area(2.0)); system("pause"); return 0; } double area(double r) { return pi*r*r; } ``` <h2> P8-54 No.8_24 #define取代簡單的函數 </h2> ``` #include<stdio.h> #include<stdlib.h> #define SQUARE n*n int main(void) { int n; printf("Input an integer:"); scanf("%d",&n); printf("%d*%d=%d\n",n,n,SQUARE); system("pause"); return 0; } ``` <h2>P8-55 No.8_25 使用有引數的巨集 </h2> ``` #include<stdio.h> #include<stdlib.h> #define SQUARE(X) X*X int main(void) { int n; printf("Input an integer:"); scanf("%d",&n); printf("%d*%d=%d\n",n,n,SQUARE(n)); system("pause"); return 0; } ``` <h2> P8-57 No.8_26 運算錯誤的巨集 </h2> ``` #include<stdio.h> #include<stdlib.h> #define SQUARE(X) X*X int main(void) { int n; printf("Input an integer:"); scanf("%d",&n); printf("%d*%d=%d\n",n+1,n+1,SQUARE(n+1)); system("pause"); return 0; } ``` <h2> P8-64 No.3 寫int cub(int x) 可傳回x的3次方 </h2> ``` #include <stdio.h> #include<stdlib.h> int cub(int); int main(void) { printf("%d",cub(2)); system("pause"); return 0; } int cub(int x) { int s; s=x*x*x; return s; } ``` <h2> P8-64 No.4 寫double square(double x)回傳x的平方 </h2> ``` #include <stdio.h> #include<stdlib.h> double square(double); int main(void) { printf("%lf",square(4.0)); system("pause"); return 0; } double square(double x) { double s; s=x*x; return s; } ``` <h2> P8-58 No.27 改成(X)*(X) </h2> ``` #include<stdio.h> #include<stdlib.h> #define SQUARE(X) (X)*(X) int main(void) { int n; printf("Input an integer:"); scanf("%d",&n); printf("%d*%d=%d\n",n+1,n+1,SQUARE(n+1)); system("pause"); return 0; } ``` <h2> P8-63 No.8_28 使用自訂標頭檔 </h2> ``` #define PI 3.14 #define CIRCLE(r) ((PI)*(r)*(r)) #define RECTANGLE(length,height) ((lenth)*(height)) #define TRIANGLE(base,height) ((base*height)/2.) ``` ``` #include<stdio.h> #include<stdlib.h> #include "C:\Users\User\Downloads\area.h" int main(void) { float base,height; printf("請輸入三角形的底:"); scanf("%f",&base); printf("請輸入三角形的高:"); scanf("%f",&height); printf("三角形面積為:%.2f\n",TRIANGLE(base,height)); system("pause"); return 0; } ``` <h2> P8-70 No.31 </h2> ``` #include <stdio.h> #include<stdlib.h> #define f(x) 4*x*x+6*x-5 int main(void) { printf("f(1.0)=%f、f(2.2)=%f、f(3.14)=%f",f(1.0),f(2.2),f(3.14)); system("pause"); return 0; } ``` <h2> P8-70 No.32 定義CUBIX(X) 算5^3、2.4^3 </h2> ``` #include <stdio.h> #include<stdlib.h> #define CUBIC(X) X*X*X int main(void) { printf("5^3=%d、2.4^3=%f、",CUBIC(5),CUBIC(2.4)); system("pause"); return 0; } ``` <h2> P9-3 No.9_1一維矩陣的基本操作 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int i,score[4]; score[0]=78; score[1]=55; score[2]=92; score[3]=80; for(i=0;i<=3;i=i+1) printf("score[%d]=%d\n",i,score[i]); system("pause"); return 0; } ``` <h2> P9-4 No.9_2 故意不設定score[2],並將score[4]索引 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int i,score[4]; score[0]=78; score[1]=55; /*score[2]=92;*/ score[3]=80; for(i=0;i<=4;i=i+1) printf("score[%d]=%d\n",i,score[i]); system("pause"); return 0; } ``` <h2> P9-8 No.9_3 sizeof查詢所佔的位元 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { double data[4]; printf("陣列元素所占比的位元組:%d\n",sizeof(data[0])); printf("整個陣列所占比的位元組:%d\n",sizeof(data)); printf("陣列元素的個數:%d\n",sizeof(data)/sizeof(double)); system("pause"); return 0; } ``` <h2> P9-9 No.9_4 一維陣列內元素的設值 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int i,age[3]; for (i=0;i<3;i=i+1) { printf("請輸入age[%d]的值:",i); scanf("%d",&age[i]); } for(i=0;i<3;i=i+1) printf("age[%d]=%d\n",i,age[i]); system("pause"); return 0; } ``` <h2> P9-10 No.9_5 比較陣列中元素值的大小 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int A[5]={74,48,30,17,62}; int i,min,max; min=max=A[0]; for(i=0;i<5;i=i+1) { if(A[i]>max) max=A[i]; if(A[i]<min) min=A[i]; } printf("陣列裡元素最大值為%d\n",max); printf("陣列裡元素最小值為%d\n",min); system("pause"); return 0; } ``` <h2> P9-11 No.9_6 輸入未定個數的資料到陣列裡 </h2> ``` #include<stdio.h> #include<stdlib.h> #define MAX 10 int main(void) { int score[MAX]; int i=0,num; int sum=0; printf("請輸入成績,要結束請輸入0:\n"); do { printf("請輸入成績:"); scanf("%d",&score[i]); }while(score[i++]>0); num=i-1; for(i=0;i<num;i=i+1) sum=sum+score[i]; printf("平均成績為:%.2f\n",(float)sum/num); system("pause"); return 0; } ``` <h2> P9-13 No.9_7 陣列的界限檢查 </h2> ``` #include<stdio.h> #include<stdlib.h> #define MAX 5 int main(void) { int score[MAX]; int i=0,num; float sum=0; printf("請輸入成績,要結束請輸入0:\n"); do { if(i==MAX) { printf("陣列空間已使用完畢!!\n"); i=i+1; break; } printf("請輸入成績:"); scanf("%d",&score[i]); }while(score[i++]>0); num=i-1; for(i=0;i<num;i=i+1) sum=sum+score[i]; printf("平均成績為:%.2f\n",(float)sum/num); system("pause"); return 0; } ``` <h2> P9-15 No.9_8 陣列的搜尋 </h2> ``` #include<stdio.h> #include<stdlib.h> #define SIZE 6 int main(void) { int i,num,flag=0; int A[SIZE]={33,75,69,41,33,19}; printf("陣列A元素的值為:"); for(i=0;i<SIZE;i=i+1) printf("%d ",A[i]); printf("\n請輸入欲搜尋的整數:"); scanf("%d",&num); for(i=0;i<SIZE;i=i+1) if(A[i]==num) { printf("找到了! A[%d]=%d\n",i,A[i]); flag=1; } if(flag==0) printf("沒有找到相同的值!!\n"); system("pause"); return 0; } ``` <h2> P9-21 No.9_9二維陣列的輸入輸出 </h2> ``` #include <stdio.h> #include<stdlib.h> int main(void) { int i,j,sale[2][4],sum=0; for (i=0;i<2;i=i+1) { for (j=0;j<4;j=j+1) { printf("業務員%d的第%d季業績:",i+1,j+1); scanf("%d",&sale[i][j]); } } printf("***Output***"); for(i=0;i<2;i=i+1) { printf("\n業務員%d的業績分別為",i+1); for (j=0;j<4;j=j+1) { printf("%d ",sale[i][j]); sum=sum+sale[i][j]; } } printf("\n2004年總銷售量為%d部車\n",sum); system("pause"); return 0; } ``` <h2> P9-22 No.9_10 </h2> ``` #include <stdio.h> #include<stdlib.h> #define ROW 2 #define COL 3 int main(void) { int i,j; int A[ROW][COL]={{1,2,3},{5,6,8}}; int B[ROW][COL]={{3,0,2},{3,5,7}}; printf("Matrix A+B=\n"); for(i=0;i<ROW;i=i+1) { for(j=0;j<COL;j=j+1) { printf("%3d",A[i][j]+B[i][j]); } printf("\n"); } system("pause"); return 0; } ``` <h2> P9-24 No.9_11 </h2> ``` #include <stdio.h> #include<stdlib.h> #define ROW 2 #define COL 3 int main(void) { int A[2][4][3]={ { {21,32,65}, {78,94,76}, {79,44,65}, {89,54,73} }, { {32,56,89}, {43,23,32}, {32,56,78}, {94,78,45} } }; int i,j,k,max=A[0][0][0]; for (i=0;i<2;i=i+1) { for(j=0;j<4;j=j+1) { for(k=0;k<3;k=k+1) { if(max<A[i][j][k]) max=A[i][j][k]; } } } printf("max=%d\n",max); system("pause"); return 0; } ``` <h2> P9-28 No.9_12 </h2> ``` #include <stdio.h> #include<stdlib.h> #define SIZE 4 void show(int arr[]); int main(void) { int A[SIZE] ={5,3,6,1}; printf("陣列內容為:"); show (A); system("pause"); return 0; } void show(int arr[]) { int i; for(i=0;i<SIZE;i=i+1) printf("%d ",arr[i]); printf("\n"); } ``` <h2> P9-30 No.9_13 </h2> ``` #include <stdio.h> #include<stdlib.h> void func(int); int main(void) { int a=13; printf("於main()裡,a=%d,a的位址=%p\n",a,&a); func(a); system("pause"); return 0; } void func(int a) { printf("於func()裡,a=%d,a的位址=%p\n",a,&a); } ``` <h2> P9-31 No9_14 </h2> ``` #include <stdio.h> #include<stdlib.h> #define SIZE 3 void func(int arr[]); int main(void) { int i,A[SIZE]={20,8,13}; printf("在main(),陣列A元素的位址為\n"); for(i=0;i<SIZE;i=i+1) { printf("A[%d]=%2d,位址為%p\n",i,A[i],&A[i]); } func(A); system("pause"); return 0; } void func(int arr[]) { int i; printf("\n在func()裡,陣列arr元素的位址為\n"); for(i=0;i<SIZE;i=i+1) { printf("arr[%d]=%2d,位址為%p\n",i,arr[i],&arr[i]); } } ``` <h2> P9-33 No.9_15 </h2> ``` #include <stdio.h> #include<stdlib.h> #define SIZE 3 int main(void) { int i,A[SIZE]={20,8,13}; for(i=0;i<SIZE;i=i+1) { printf("A[%d]=%2d,位址為%p\n",i,A[i],&A[i]); } printf("陣列A的位址=%p\n",A); system("pause"); return 0; } ``` <h2> P9-34 No.9_16 </h2> ``` #include <stdio.h> #include<stdlib.h> #define SIZE 4 void show(int arr[]); void add2(int arr[]); int main(void) { int A[SIZE]={5,3,6,1}; printf("呼叫add2()前,陣列的內容為: "); show(A); add2(A); printf("呼叫add2()後,陣列的內容為: "); show(A); system("pause"); return 0; } void show(int arr[]) { int i; for(i=0;i<SIZE;i=i+1) { printf("%d ",arr[i]); } printf("\n"); } void add2(int arr[]) { int i ; for(i=1;i<SIZE;i=i+1) { arr[i]=arr[i]+2; } } ``` <h2>P9-37 No.9_17 氣泡排序法</h2> ``` #include <stdio.h> #include<stdlib.h> #define SIZE 5 void show(int a[]),bubble(int a[]); int main(void) { int data[SIZE]={26,5,81,7,63}; printf("排序前...\n"); show(data); bubble(data); printf("排序後...\n"); show(data); system("pause"); return 0; } void show(int a[]) { int i; for(i=0;i<SIZE;i=i+1) { printf("%d ",a[i]) ; } printf("\n"); } void bubble(int a[]) { int i,j,temp; for(i=1;i<SIZE;i=i+1) { for(j=0;j<i;j=j+1){ if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } } ``` <h1> 2025/12/31 </h1> <h2> P9-43 No.9_19 尋找二維陣列的最大最小值 </h2> ``` #include<stdio.h> #include<stdlib.h> #define ROW 4 #define COL 3 void search(int a[ROW] [COL],int b[]); int main(void) { int a[ROW][COL]= { {26,5,7}, {10,3,47}, {6,76,8}, {40,4,32}, }; int i,j,b[2]; printf("二維陣列內的元素:\n"); for (i=0;i<ROW;i=i+1) { for (j=0;j<COL;j=j+1) { printf("%02d ",a[i][j]); } printf("\n"); } search(a,b); printf("陣列裡最大值=%02d\n",b[0]); printf("陣列裡最小值=%02d\n",b[1]); system("pause"); return 0; } void search(int arr[ROW][COL],int p[]) { int i,j; p[0]=p[1]=arr[0][0]; for (i=0;i<ROW;i=i+1) { for (j=0;j<COL;j=j+1 ) { if (p[0]<arr[i][j]) p[0]=arr[i][j]; if (p[1]>arr[i][j]) p[1]=arr[i][j]; } } } ``` <h2> P9-47 No.9_20 印出字元及字串的長度 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { char ch='a'; char str1[]="a"; char str2[]="Sweet home"; printf("字元 ch佔了%d個位元組\n",sizeof(ch)); printf("字串 str1佔了%d個位元組\n",sizeof(str1)); printf("字串 str2佔了%d個位元組\n",sizeof(str2)); system("pause"); return 0; } ``` <h2> P9-50 No.9_21 輸入及輸出字串 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { char name[15]; puts("What your name?"); gets(name); puts("Hi"); puts(name); puts("How are you"); system("pause"); return 0; } ``` <h2> P9-51 No.9_22 字串裡的小寫轉大寫 </h2> ``` #include<stdio.h> #include<stdlib.h> void toUpper(char s[]); int main(void) { char str[15]; printf("請輸入一個字串: "); gets(str); toUpper(str); printf("轉換成大寫後: %s\n",str); system("pause"); return 0; } void toUpper(char s[]) { int i=0; while(s[i]!='\0') { if(s[i]>=97 && s[i]<=122) s[i]=s[i]-32; i=i+1; } } ``` <h2> P9-54 No.9_23 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { char S[3][10]={"Tom","Lily","James Lee"}; int i; for (i=0;i<3;i=i+1) { printf("S[%d]=%s\n",i,S[i]); } printf("\n"); for (i=0;i<3;i=i+1) { printf("S[%d]=%p\n",i,S[i]); printf("address of S[%d][0]=%p\n\n",i,&S[i][0]); } system("pause"); return 0; } ``` <h2> P9-56 No.9_24 </h2> ``` #include<stdio.h> #include<stdlib.h> #define MAX 3 #define LENGTH 10 int main(void) { char arr1[MAX][LENGTH]={"Tom","Lily","James Lee"}; char arr2[MAX][LENGTH]; int i,j; for (i=0;i<MAX;i=i+1) { for (j=0;j<LENGTH;j=j+1) { if (arr1[i][j]=='\0') break; else arr2[i][j]=arr1[i][j]; } arr2[i][j]='\0'; } for(i=0;i<MAX;i=i+1) { printf("arr2[%d]=%s\n",i,arr2[i]); } system("pause"); return 0; } ``` <h2>P9-61 No.28</h2> ``` #include<stdio.h> #include<stdlib.h> void toLower(char str[]); int main(void) { char str[15]; printf("請輸入一個字串: "); gets(str); toLower(str); printf("轉換成小寫後: %s\n",str); system("pause"); return 0; } void toLower(char s[]) { int i=0; while(s[i]!='\0') { if(s[i]>=65 && s[i]<=90) s[i]=s[i]+32; i=i+1; } } ``` <h2> P10-5 No.10_1 印出變數於記憶體的位址 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int a,b=5; double c=3.14;; printf("a=%4d,sizeof(a)=%d,位址為%d\n",a,sizeof(a),&a); printf("b=%4d,sizeof(b)=%d,位址為%d\n",b,sizeof(b),&b); printf("c=%4.2f,sizeof(c)=%d,位址為%d\n",c,sizeof(c),&c); system("pause"); return 0; } ``` <h2> P10-9 No.10_2 指標變數的宣告 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int *ptr,num=20; ptr=&num; printf("num=%d,&num=%p\n",num,&num); printf("*ptr=%d,ptr=%p,&ptr=%p\n",*ptr,ptr,&ptr); system("pause"); return 0; } ``` <h2> P10-10 No.10_3 指標變數的使用 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int a=5,b=3; int *ptr; ptr=&a; printf("&a=%p,&ptr=%p,ptr=%p,*ptr=%d\n",&a,&ptr,ptr,*ptr); ptr=&b; printf("&b=%p,&ptr=%p,ptr=%p,*ptr=%d\n",&b,&ptr,ptr,*ptr); system("pause"); return 0; } ``` <h1> 20260107 </h1> <h2> P10-12 No.10_4 指標變數的大小 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int *ptri; char *ptrc ; printf("sizeof(ptri)=%d\n",sizeof(ptri)); printf("sizeof(ptrc)=%d\n",sizeof(ptrc)); printf("sizeof(*ptri)=%d\n",sizeof(*ptri)); printf("sizeof(*ptrc)=%d\n",sizeof(*ptrc)); system("pause"); return 0; } ``` <h2> P10-13 No.10_5 指標的操作 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int a=5,b=10; int *ptr1,*ptr2; ptr1=&a; ptr2=&b; *ptr1=7; *ptr2=32; a=17; ptr1=ptr2; *ptr1=9; ptr1=&a; a=64; *ptr2=*ptr1+5; ptr2=&a; printf("a=%2d,b=%2d,*ptr1=%2d,*ptr2=%2d\n",a,b,*ptr1,*ptr2); printf("ptr1=%p,ptr2=%p\n",ptr1,ptr2); system("pause"); return 0; } ``` <h2> P10-15 No.10_6 錯誤的指標型態 </h2> ``` #include<stdio.h> #include<stdlib.h> int main(void) { int a1=100,*ptri; float a2=3.2f,*ptrf; ptri=&a2; ptrf=&a1; printf("sizeof(a1)=%d\n",sizeof(a1)); printf("sizeof(a2)=%d\n",sizeof(a2)); printf("a1=%d,*ptri=%d\n",a1,*ptri); printf("a2=%.1f,*ptrf=%.1f\n",a2,*ptrf); system("pause"); return 0; } ``` ``` ```