C語言程式設計的試題及答案

C語言程式設計的試題及答案

  大家在考程式設計師時,C語言程式設計大家有了解嗎?下面瑞文網小編為大家分享了C語言程式設計的試題及答案,供大家參考。

  第一章 基礎知識

  一、填空

  1. 每個 C 程式都必須有且僅有一個________ 函式。

  2. C 語言程式開發到執行通常要經過 6 個階段即編輯、預處理、________、 連結 、載入 和執行。

  3. 軟體是程式,以及______、使用和維護所需要的所有文件。

  4. 國標中規定: “計算機程式是按照具體要求產生的適合於計算機處理的_________”。

  5. 程式設計語言按照書寫形式,以及思維方式的不同一般分為低階語言和________兩大類 。

  6. C 語言是由________組成的。

  7. C 語言的函式可分為主函式 main、標準庫函式和_________。

  8. 一個函式是由兩部分組成的,即:________和 函式體。

  9. 編譯是將 C 語言所編寫的源程式________成機器程式碼, 也稱為建立目的碼程式的過程。

  10. 程式是由某種程式設計語言編制出來, 體現了程式設計者的控制思想和對計算機執行操 作 的要求。不同的任務功能,就會需求不同的軟體程式,如:控制計算機本身軟硬體協調工作 ,並使其裝置充分發揮效力,方便使用者使用的系統軟體程式,稱為作業系統;而為辦公自動 化(OA) 、管理資訊系統(MIS) 、人工智慧、電子商務、網路互聯等等應用而開發的軟體程 序,統稱為_________。

  11. 機器語言是以__________形式表示的機器基本指令的集合, 是計算機系統唯一不需要翻 譯可以直接識別和執行的程式設計語言。 12. 與機器語言相比,使用匯編語言來編寫程式可以用_______來表示指令的操作碼和操作 對 象,也可以用標號和符號來代替地址、常量和變數。

  13. 在編譯程式之前,凡以____開頭的程式碼行都先由預處理程式預處理。

  14. C 程式的執行均是由執行_________開始。 15. 函式體即為包含在{ }內的部分。它分為________和為完成功能任務由若干個 C 語句 組成的執行部分。

  16. C 語言程式中一條簡單語句是以________字元作為結束符的。

  17. C 語言是結構化、________的程式設計語言。

  18. 由於計算機硬體不能直接識別高階語言中的語句, 因此, 必須經過 “_______程式” , 將用高階語言編寫的程式翻譯成計算機硬體所能識別的機器語言程式方可執行。

  19. 用高階語言編寫的程式需翻譯成計算機硬體所能識別的機器語言程式方可執行。 所 以 說,用高階語言進行程式設計,其程式設計效率高,方便易用,但_______沒有低階語言高。

  20. 第一個系統體現結構化程式設計思想的教學工具語言是_______語言。

  解答: 1. main() 2. 編譯 3. 開發 4. 指令序列 5. 高階語言 6. 函式 7. 自定義函式 8. 函式首部 9. 翻譯 10. 應用軟體 11. 二進位制程式碼 12. 助記符 13. # 14. main 函式 15. 宣告部分 16. ;(或分號) 17. 模組化 18. 翻譯 19. 執行速度

  20. PASCAL 二.閱讀程式寫結果 解答: 三.補充程式 解答: 四.改錯題 解答: 五.程式設計一 解答: 六.程式設計二 解答: 第二章 常量、變數和表示式 一.填空 1. 以 16 位計算機為例,若定義 long int a; 則變數 a 佔______個位元組的記憶體空間。 2. 以 16 位計算機為例,若定義 double a; 則變數 a 佔______ 個位元組的記憶體空間。

  3. 以 16 位計算機為例,若定義 long double; 則變數 a 佔 ______ 個位元組的記憶體空間。 4. 在______ 位機,int 型佔 4 個位元組。 、空型別

  5. C 語言的資料型別有:基本資料型別、構造資料型別、列舉型別、______ 。

  6. 在 C 語言中,基本資料型別有:______ 7. 整型常量 046 轉換為十進位制數等於______ 8. 整型常量 054 轉換為十進位制數等於______ 。 。

  。

  9. 整型常量 0x125 轉換為十進位制數等於 ______。 10. 整型常量 0x7C 轉換為十進位制數等於 11. 整型常量 39 轉換為八進位制等於 ______。 ______。 ______。

  12. 實數 473.468 寫成規範化指數形式,則應為

  13. 實數 0.0453 寫成規範化指數形式,則應為 ______。

  14. 將 0x123 轉換為二進位制等於

  ______。

  15. 字元’A’表示成十六進位制等於 ______。 16. 在 C 語言中,稱代表常量的識別符號為 ______。 17. C語言中是用編譯預處理命令______ 來定義符號常量 ,

  18. 在 C 語言中,以反斜槓符(\)開頭,後跟字元的字元序列,稱之為 ______ 主 要用來表示控制及不可見的字元或者一些圖形符號 19. 語句 printf("\106")執行的結果輸出是 ______。 20. 語句 printf(“b”)執行的結果輸出是______。 21. 語句 printf(“\\b”)執行的結果輸出是 ______。 22. 語句 printf("\102")執行的結果輸出是 ______。 23. 若有 char ch1; ch1='a'+4; 則語句 printf("%c",x)的輸出結果為 ______。 24. 字串常量“Hello, everyone! ”佔據的記憶體空間為______ 25. 字串常量“Hello,Bob!”佔據的記憶體空間為 ______ 26. 十進位制整型常量由 0~9 個數據組成,但不能以______ 27. 十六進位制常量由 0~9 和______ 28. unsigned long 型別常量通常以______ 29. 實型常量通常有小數形式和______ 30. C語言中是用編譯預處理命令______ 組成 為字尾 兩種表達方式 來包含其他原始檔 個位元組 個位元組 開始,沒有小數部分

  31. 為了表示字串的結束,系統自動在字串的最後加一個______,但不被輸出

  32. C 語言中,其值可被改變的量稱為變數,它具有三個基本要素:變數名、______ 和變數值 33. C 語言規定,變數的識別符號只能由字母、數字和下劃線組成,且不能以______ 頭。 開

  34. 在 16 位機中,sizeof(“hello\n”)等於 ______。 35. 在 16 位機中,printf(“\“hello\” ”)等於 ______。 36. 字元變數在記憶體中佔用______ 個位元組的儲存空間。 ______。 ______。

  37. 若有定義 int x=27, y=6; 則表示式 x/y 的值為 38. 若有定義 int x=47, y=8; 則表示式 x%y 的值為

  39. 若有定義 char ch1='A',ch2,ch3; 執行 ch2=ch1++; ch3=++ch1; printf("ch1=%c, ch2=% c",ch1,ch2); 則輸出為 ______。 40. 若有定義 char ch1='A',ch2,ch3; 執行 ch2=ch1++; ch3=++ch1; printf("ch2=%c, ch3=% c",ch2,ch3); 則輸出為 ______。 41. 若有定義 int i=5, j, k; 執行 j=i--; k=--i; printf("i=%d, j=%d\n", i, j); 則輸 出為 ______。 42. 表示式 (int)(13.7+25.6)/4%5 的值是 ______。 43. 表示式 3.6-5/2+1.2+5%3 的值是______。 44. 表示式 54%10/2+4.0*(8/5) 的值是 ______。 45. 若定義 int x; double b=5.45; 執行 x=b 後,則 x 的值為______。 46. 若定義 int y; char c='A'; 執行 y=c 後,則 y 的值為 47. 若定義 int a=8; float b=2.5; 則 a/(int)(b)的值為 48. 若 a=5,則執行 a*=7-3 語句後,a 的值是 ______。 ______。 ______。

  49. 若 int a=1,b=0,c=4,d=2; 則表示式(a>b ? a:b)&&(c-d)的值為______。 50. 已知 a = 0 ,表示式 a = 1?b = 1:b = 0; 執行後,b 的值是 51. 若定義 int m=7,n=12; 則表示式 n%=m-m%5 的值是 ______。

  ______。

  52. 若定義 int a=8; float b=4.5;執行 a*=a/=a-b 後,a 的值為 ______。 53. int a=2; !a++的值為 ______。 ______。

  54. int a=0; !a++&++a 的值為

  55. int a=7;float b=4.5;則 printf(“%d”,a*=a/=a-b)的值為 ______。 56. int k=7;float a=2.5,b=4.7;則表示式 a+k%3*(int)(a+b)%2/4 的值為 ______。 57. #define Max(a,b) (((a)-(b))&0x80000000)?(a):(b),int a=2,b=6;printf(“%d\n”,M ax(a,b))的值為 ______。 58. 在 32 位機中,int a=4,b=7; printf(“%0x”,a-b)的值為:______。 59. int x,y,z; x=y=2;z=3;y=x++-1;printf(“%d,%d”,x,y)的值為: ______。

  60. int i=2,j; printf(“%d”,j=i<1)的值為:______ 解答: 1. 0 2. 4 3. 10 4. 32 5. 指標型別 6. int、float、double、char 7. 38 8. 44 9. 293 10. 124 11. 047 12. 4.734680e+002 13. 4.530000e-002 14. 100100011

  15. \x41/41H 16. 符號常量 17. #define 18. 跳脫字元常量 19. F 20. b 21. \b 22. B 23. e 24. 16 25. 11 26. 0 27. A~F 28. U 29. 指數形式 30. #include 31. “\0” 32. 變數型別 33. 數字 34. 7 35. “hello” 36. 1

  37. 4 38. 7 39. ch1=C, ch2=A 40. ch2=A, ch3=C 41. i=3, j=5 42. 4 43. 4.800000 44. 7.000000 45. 5 46. 65 47. 4 48. 20 49. 1 50. 1 51. 2 52. 4 53. 0 54. 0 55. 4 56. 2.500000 57. 2 58. fffffffd

  59. 3,1 60. 0

  二.閱讀程式寫結果 解答:

  三.補充程式 解答:

  四.改錯題 解答:

  五.程式設計一 解答:

  六.程式設計二 解答:

  第三章 順序結構

  一、填空

  1. C 語言的語句可分為 5 類,它們是:控制語句、____________、函式語句、符合語句和空 語句。

  2. 常用於字元輸入的函式為:________________ 。

  3. 常用於字元輸出的函式為:________________。

  4. 常用於字串輸入的函式為:________________。

  5. 常用於字串輸出的函式為:________________。 6. 格式輸入函式的一般形式為:________________ 。 7. 格式輸出函式的一般形式為:________________ 。 8. printf 和 scanf 是標準的庫函式,它們的函式原型在標頭檔案________________中。 9. 有 char ch,則從鍵盤給 ch 賦值的格式輸入語句為:________________。 10. 如果要輸出字串常量“HelloWord! ” ,則格式輸出語句為:________________。 11. 設有定義:long x=-123456L; ,請寫出能夠正確輸出變數 x 值的語句________________ 。 12. 若有 int a,b; ,想透過 scanf("%d%d",&a,&b);輸入語句給 a 賦於 1,給 b 賦於 2,則從鍵 盤

  輸入資料的形式是________________。 13. 若有 int i,j; scanf("i=%d,j=%d";&i,&j); ,要求給 i 賦 10,給 j 賦 20,則應該從鍵盤 輸入_______。 14. 已知 i、j、k 為 int 型變數,若從鍵盤輸入:1,2,3<回車>,使 i 的值為 1、j 的值為 2、 k 的值為 3,請寫出正確的輸入語句________________ 。 15. 有 int m,n,p; scanf("m=%dn=%dp=%d",&m,&n,&p); printf("%d%d%d\n",m,n,p); 若想 從鍵盤上輸入資料,使變數 m 中的值為 123,n 中的值為 456,p 中的值為 789,則正確的輸入是 _____ ___________。 16. 有 int a; float b, c; %f\n", a, b, c); scanf("%2d%3f%4f",&a,&b,&c); printf("\na=%d, b=%f, c=

  , 若 運 行 時 從 鍵 盤 上 輸 入 9876543210< 回 車 > , 則 程 序 的 輸 出 結 果 是 ___________________ __。 17. 有 int m=0256,n=256; 則 printf("%o %o\n",m,n); 的輸出結果是__________________ ___ 。 18. 有 int x=102,y=012; , 則 printf(“%2d,%2d\n”,x,y); 的輸出結果是______________ _______ 。 19. 有定義和語句:int a; char c=10; float f=100.0; double x; a=f/=c*=(x=6.5); , 則 printf("%d %d %3.1f %3.1f\n",a,c,f,x); 的輸出結果是_____________________ 。 20. 若有定義和語句:int m=0xabc,n=0xabc; m-=n; 則 printf("%o\n",m);的輸出結果是___ __________。 21. 若有 int m=32,n=032; ,則 printf("%d,%x\n",m,n); 執行後輸出結果是______________ _____ 。 22. 若有 int a=1234; ,則 printf("%2d\n",a); 的輸出結果是_____________________ 。 23. 若變數已正確說明為 float 型別,要透過語句 scanf(“%f %f %f ”,&a,&b,&c);給 a 賦於 1 0.0,b 賦予 22.0,c 賦予 33.0,不正確的輸入形式是_____________________ 。 A) 10 <回車>22<回車>33<回車> B) 10.0,22.0,33.0<回車> C) 10.0<回車> 22.0 33.0<回車> D) 10 22<回車> 33<回車>

  24. 若有 int x=3,y=1; 則 printf("%d %d\n",x++,y+2); 的輸出結果是__________________ ___ 。 25. 若有定義和語句:double d; float f; long l; int i; i=f=l=d=20/3; 則 printf("% d %ld %f %f \n", i,l,f,d);的輸出結果是(小數點後只寫一位) _____________________。 26. 以下說法中正確的是 _____________________。 A) #define 和 printf 都是 C 語句 B) #define 是 C 語句,而 printf 不是 C) printf 是 C 語句,但#define 不是 D) #define 和 printf 都不是 C 語句 27. 若有 int k=17; ,則 printf("%d,%o,%x \n",k,k,k);的輸出結果是_________________ ____ 。 28. 以下敘述中正確的是_____________________ 。 A) 輸入項可以是一個實型常量,如:scanf("%f",3.5); B) 只有格式控制,沒有輸入項,也能正確輸入資料到記憶體,例如:scakf("a=%d,b=%d"); C) 當輸入資料時,必須指明變數地址,例如:scanf("%f",&f); 29. 若有定義和語句:int u=010,v=0x10,w=10;則 printf("%d,%d,%d\n",u,v,w); 輸出結果是 _____________________。 30. 若有 int a=1,b=4,c=2; float x=10.5, y=4.0, z; z=(a+b)/c+sqrt((double)y)*1.2/c+ x; ,則 printf("%f\n",z); 的輸出結果是_____________________ 。 31. 若有 int a=2,c=5; ,則 printf("a=%%d,b=%%d\n",a,c);的輸出結果是________________ _____ 。 32. 有 char c; int n=100; float f=10; double x; x=f*=n/=(c=50); 則 printf("%d %f\ n",n,x); 的輸出結果是_____________________。 33. 有 int i=0,j=0,k=0; scanf("%d%*d%d",&i,&j,&k); printf("%d%d%d\n",i,j,k); 若 從鍵盤輸入:10 20 30<回車>。輸出結果是_____________________。 34. 若有 int a,b,c; a=25; b=025; c=0x25;,則 printf("%d %d %d\n",a,b,c); 的輸出結果 是___________。 35. 有以下語句段 int n1=10,n2=20; printf(“________________”,n1,n2); 要求按以下格式輸出 n1 和 n2 的值,每個輸出行從第一列開始,請填空。 n1=10 n2=20 36. 若有語句 int i=-19,j=i%4; 則 printf("%d\n",j);的輸出結果是_____________________

  。 37. 有 int a=0; a+=(a=8); 則 printf("%d\n",a);的輸出結果是_____________________。 38. 有 int a=1, b=2; a=a+b; b=a-b; a=a-b;則 printf(“%d,%d\n”, a, b );的輸出結果是 ______________。 39. 有 unsigned short a=65536; int b;,則 printf(“%d\n”,b=a);的輸出結果是________ ______。 40. 語句 printf("\10289\n");的資料結果為:__________________ 。 解答: 1. 表示式語句 2. getchar( ) 3. putchar( ) 4. gets( ) 5. puts( ) 6. scanf(“格式控制字串” ,地址表列); 7. printf(“格式控制字串” ,輸出表列); 8. stdio.h 9. scanf (“%c” ,&ch); 10. printf (“%s” ,HelloWord!); 或者:printf (“HelloWord!”); 11. printf(“x=%1d\n”,x); 12. 1 2 或者 1 2 13. i=10,j=20 14. scanf(“%d,%d,%d”,&i,&j,&k); 15. m=123n=456p=789

  16. a=98, b=765.000000, c=4321.000000 17. 256 400 18. 102,10 19. 1 65 1.5 6.5 20. 0 21. 32,1a 22. 1234 23. B 24. 3 3 25. 6 6 6.0 6.0 26. D 27. 17,21,11 28. C 29. 8,16,10 30. 13.700000 31. a=%d,b=%d 32. 2 20.000000 33. 10300 34. 25 21 37 35. n1=%d\nn2=%d 36. -3 37. 16

  38. 2,1 39. 0 40. B89 二.閱讀程式寫結果 解答: 三.補充程式 解答: 四.改錯題 解答: 五.程式設計一 解答: 六.程式設計二 解答: 第四章 選擇結構 一.填空 1. 在 if, while, case, do, else 等關鍵字中,用於選擇結構的有( ). 2. 已知 a = 1; 那麼 if ( a = 2) printf("%d", 2); else printf("%d", 1); 的輸出結 果是( ). 3. 已知 char a = 'A'; 那麼 if ( a > 65) printf("%s", "bigger"); else printf("%s" , "smaller"); 的輸出結果是( ). 4. 表示“x≥ y≥ z”的 C 語言表示式是 ( 5. 在 C 語言中,用於實現選擇結構的語句有( 。 6. 判斷兩個浮點型變數 x 和 y 是否相等一般用:if( 7. 設有程式段: t=6;a=7;b=8; if(a=b) t=a;,這段程式執行後,t 的結果是( 。 8. 設 a=1,b=2,c=3,d=4,則表示式:a>b?a:c>d?a:d 的結果為( 9. 表示式 98 && 8 && 31 && (98 - 'a')的值為( 10. 若 int a = 3; 則表示式 (a > 2)&& ! ('D'> 69) 的值為( )。 )。 ) 。 )語句和( )。 ) 語句

  <1e-6 )? 。 )

  解答: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 二.閱讀程式寫結果 1. #include void main( ) { int x,y,t; x=7;y=9; if(x

  2. #include void main( ) { int x=1,a=2,b=3; switch(x) { case 1: case 2: case 3: a--; break; b++; break; a++;b++;

  } printf("\na=%d,b=%d\n",a,b); }

  3. #include void main( ) { char ch1 = 'E'; if(ch1 >= 'A') ch1++; else ch1+=32; printf("ch1 = %c\n", ch1); }

  4. #include void main( ) { int x,y,t; x=5;y=3;

  if(x>y) { t=x;x=y;y=t;} printf("%d,%d\n" , x,y ); }

  5. #include int main() { int a,b,c,m; printf("Enter three integers:"); scanf("%d%d%d",&a,&b,&c); if(a<=b) m=a; else m=b; if(c

  6. #include int main() { char ch1='a',ch2='B',ch3='E'; if(ch1>ch2) if(ch2>ch3) else --ch3; printf("ch3=%c\n",ch3); return 0; }

  ch3++;

  7. #include int main() { float x,y; scanf("%f",&x); switch((int)x/10) { case 0: y=1.0; printf("y=%f\n",y);break; case 1: y=2*x+1; printf("y=%f\n",y);break; case 2: y=3*x*x+2; printf("y=%f\n",y);break; default: printf("No definition.\n"); } return 0; } 輸入:15.3<回車> 8. #include int main() { char ch1='A',ch2='B'; switch(ch1) { case 'A': switch(ch2) {

  case 'B': printf("Good!\n");break; case 'A': printf("Better!\n");break; } case 'B': printf("Best!\n"); break; } return 0; }

  9.

  #include void main() { float score; score = 100; if (score<60) else switch( ( int ) score / 10 ) { case 10: case 9: printf("A\n"); case 8: printf("B\n"); case 7: printf("C\n"); case 6: printf("D\n"); break; break;

  printf("E\n");

  default: printf("Error\n"); } }

  10. #include void main() { int i=0,a=2; if(i==0) printf(“**”); else

  printf(“$$”); printf(“*”); }

  11. #include void main() { int m=10,n=0,p=4,q=20; if (n) q=15-m; else q=25+m; else if (p) q=q+p; printf(“q=%d\n”,q); } if (m)

  12. #include void main() { int a=1,b=0; switch(a) { case 1: switch (b) { case 0: printf("**0**"); break; case 1:printf("**1**");break; } case 2: printf("**2**");break; }

  } 13. #include int main( ) { int a=2,b=7,c=5; switch (a>0){ case 1: switch (b<0){ case 1: printf("@"); break ; case 2: printf("!"); break ; } case 0: switch (c==5){ case 1: printf("*") ; break ; default : printf("#") ; break ; } default : printf("&"); } printf("\n"); return 0; }

  14. #include int main() { int a=0,b=1,c=0,d=20; if(!c) d=15;

  if(a) d=d-10; else d=25;

  else if (!b) printf("d=%d\n",d);

  return 0; } 15. #include int main() { int a=1; switch(a){ case 0: printf("**0**");break; case 1:printf("**1**");break; case 2:printf("**2**");break; } return 0; } 16. #include int main() { int x, y; scanf("%d",&x); y = x>12 ? x + 10 : x - 12; return 0; } 輸入:12<回車> 17. #include int main() { float x, y; char op; double r; scanf("%c%f%f",&op,&x,&y); switch(op) { case '+': r = x + y; break; case '-': r = x - y; break; case '*': r = x * y; break; case '/': r = x / y; break; } printf("%.1f", r); return 0; } 輸入:3.5<回車> 18. #include "stdio.h" void main( ) { int a=7; if(a>9 || a<10) a=a%3; printf("a=%d", a); }

  printf("%d\n", y);

  a=a/3;

  else

  19. #include void main() { int a=20; switch(a%3) case 0: printf("0"); case 1: printf("1"); case 2: printf("2"); default: printf("3"); } printf("4"); } 20. #include void main() { int a=2,b=5,c=8,t; if ( a < b ) { t = a; a = b; b = t; } if ( b < c ) { t = b; b = c; c = t; } if ( a < b ) { t = a; a = b; b = t; } printf( "%d,%d\n", a++, --c ); } 解答: 1. 執行結果: 9,7 2. a=1,b=3 3. ch1= F 4. 3,5 5. m=21 6. ch3=D 7. y=31.600000 8. Good! Best! 9. A B

  {

  C 10. *** 11. q=35 12. **0****2** 13. *& 14. d=20 15. **1** 16. 0 17. 0.6 18. a=2 19. 234 20. 8,1 三.補充程式 1. 根據輸入字元的 ASCII 碼來判別字元的類別。由 ASCII 碼錶可知 ASCII 值小於 32 的為控制 字 符。 在“0”和“9”之間的為數字,在“A”和“Z”之間為大寫字母, 在“a”和“z”之 間為小寫字母,其餘則為其它字元。 #include"stdio.h" #include"string.h" int main() { char c; printf("input a character: "); _____________________________________________ if(c<32) printf("This is a control character\n"); _____________________________________________ printf("This is a digit\n"); else if _______________________________________ printf("This is a capital letter\n"); else if(c>='a'&&c<='z')

  printf("This is a small letter\n"); ____________________________________________ printf("This is an other character\n"); return 0; }

  2. 計算器程式。使用者輸入運算數和四則運算子, 輸出計算結果. #include"stdio.h" int main() { float a,b; ____________________________________________; printf("input expression: a+(-,*,/)b \n"); scanf("%f%c%f",&a,&c,&b); switch(c) { case '+': printf("%f\n",a+b); ____________________________________________ case____________________________________________ printf("%f\n",a-b);break; case '*': printf("%f\n",a*b);break; case '/': ____________________________________________;break; default: printf("input error\n"); } return 0; }

  3. 輸入三個整數,輸出最大數和最小數(注:max 總是記錄最大數, 而 min 總是記錄最小數). #include"stdio.h" int main() { int a,b,c,max,min; printf("input three numbers: "); scanf("%d%d%d",&a,&b,&c); if(a>b) { _______________________________________ _______________________________________ } else {max=b;min=a;} ____________________________________________ max=c; if(min>c)

  _____________________________________ printf("max=%d\nmin=%d",max,min); return 0; } 4. #include"stdio.h" int main() { int a; printf("input integer number: "); scanf("%d",&a); _____________________________________________ { case 1:printf("Monday\n");break; _____________________________________________ printf("Tuesday\n"); break; case 3:printf("Wednesday\n");break; case 4:printf("Thursday\n");_____________________________________________ case 5:printf("Friday\n");break; case 6:printf("Saturday\n");break; case 7:printf("Sunday\n");break; _____________________________________________ printf("error\n"); } return 0; } 5. 判斷 a>b、aB\n"); _____________________________________________ printf("A

  #include"stdio.h" int main ( ) { int x,c,m; float y; scanf(“%d”,&x); _________________________________________c=-1; c=_____________________________________________; switch (c) { case –1 : y=0 ; break ; case 0 : y=x ; break ; case 1 : y=10 ; break ; case 2 : case 3 : y=-0.5*x+20 ; _____________________________________________;

  else

  default : y=-2 ; } if (c!=2) printf (“y=%f”,y); _____________________________________________ printf(“error!”); return 0; } 7. 找出 x,y,z 三個數中的最小數,並判斷該數是否等於 b。 #include"stdio.h" int main() { int x=4,y=5,z=8 ,b; int u,v; scanf("%d",&b); u = x

  8. 輸入兩個運算數 x,y 和一個運算子號 op, 輸出該運算結果的值, 例如輸入 3+5↙得到結果 8 . #include"stdio.h" int main ( ) { float x,y,r; char op ; scanf("%f,%c,%f",&x,&op,&y); switch (op) { case '+': r=x+y ; _____________________________________________; _____________________________________________ r=x-y ;break; case '*' :_____________________________________________;break; _____________________________________________ r=x/y; break; }

  printf("%f",r); return 0; }

  9. 根據輸入字元的 ASCII 碼來判別字元的類別。由 ASCII 碼錶可知 ASCII 值小於 32 的為控制 字 符。 在“0”和“9”之間的為數字,在“A”和“Z”之間為大寫字母, 在“a”和“z”之 間為小寫字母,其餘則為其它字元。 #include"stdio.h" int main() { char c; printf("input a character: "); c=getchar(); _____________________________________________ printf("This is a control character\n"); if(c>='0'&&c<='9') printf("This is a digit\n"); _____________________________________________ printf("This is a capital letter\n"); _____________________________________________ printf("This is a small letter\n"); _____________________________________________ printf("This is an other character\n"); return 0; } 10. 找出 a,b,c 三個數中的最大數,並判斷該數是否等於 x。 #include"stdio.h" int main() { int a=8,b=4,c=12,x ; int u,v; scanf(“%d”,&x); u = a

  解答: 1. c=getchar(); (c>='0'&&c<='9') (c>='A'&&c<='Z') else 2. char c; break; '-': printf("%f\n",a/b); 3. max=a; min=b; if(maxb) else if(a

  c=x/10; break; else 7. x:y; u:z; (v==b) else 8. break; case '-': r=x*y; case '/': 9. if(c<32) else if(c>='A'&&c<='Z') else if(c>='a'&&c<='z') else

  10. b:a c:u (v==x) else 四.改錯題 解答: 五.程式設計一 1. 求分段函式

  的值, 輸入 x 值,輸出結果。 2. 從鍵盤上輸入一個數 x,計算並輸出下列分段函式 y 的值。

  3. 某運輸公司運費計算時根據運輸距離打折計算,折扣的計算方法如下:

  (其中,discount 表示折扣,S 表示運輸距離) 。 編寫一完整程式,輸入運輸距離,統計運費並輸出。每公里的運費透過符號常量定義為 2 .5。 4. 程式設計輸入一元二次方程的三個係數,求解一元二次方程的實根,無實根時不用求虛根, 給出相應提示資訊即可。 5. 給定三條邊,判斷其是否能構成三角形,若能構成,判斷其是等腰三角形、等邊三角形 、直角三角形還是普通三角形。 6. 從鍵盤輸入一個三角形的三條邊,判斷是否能組成三角形,若能組成三角形,則輸出它 的面積。

  7. 從鍵盤輸入一個百分制的學生成績,輸出其對應的分數等級,其中 90~100 為 A,80~ 89 為 B,70~79 為 C,60~69 為 D,60 以下為 E。 8. 編寫一個程式,從鍵盤輸入三個整數給 a,b,c,然後交換它們中的數,把 a 中原來的值 給 b,把 b 中原來的值給 c,把 c 中原來的值給 a。 9. 輸入一個字元,判斷其是否為英文字母,若是英文字母,則將其大小寫進行轉換,若不 是英文字母,則不變。 10. 給定三個整數型 x,y,z,輸出三個數中的最大值和最小值。 解答: 1. #include "stdio.h" #include "math.h" void main() { float x,y; printf("input x\n"); scanf("%f",&x); if(x<0) y=1; else if(x<=5) y=2*x; else y=sqrt(x); printf("x=%5.1fy=%8.3f\n",x,y); } 2. #include "stdio.h" #include "math.h" void main() { float x,y; printf("input x\n"); scanf("%f",&x); if(x>20||x>1&&x<10) { printf("無定義\n"); return; } if(x<0) y=-x; else if(x<=1) y=sin(x)+x; else if(x>=10&&x<=20) y=x*x*x; printf("x=%5.1fy=%8.3f\n",x,y); } 3. #include "stdio.h"

  #include "math.h" #define price 2.5 void main() { float discount,cost,s; printf("input distance\n"); scanf("%f",&s); if(s<250) discount=0; else if(s<800) else

  discount=0.02; else if(s<1500)

  discount=0.05;

  discount=0.08; cost=s*(1-discount)*price; printf("distance=%5.1f;cost=%5.1f\n",s,cost); } 4. /*功能:求一元二次方程的解。*/ #include "math.h" #include "stdio.h" main() {float a,b,c,disc,x1,x2; scanf("%f,%f,%f", &a, &b, &c); disc=b*b-4*a*c; if (disc==0) printf("x1=x2=%7.2f\n", -b/(2*a)); /*輸出兩個相等的實根*/ else if (disc>0) { x1=(-b+sqrt(disc))/(2*a); /*求出兩個不相等的實根*/ x2=(-b-sqrt(disc))/(2*a); printf("x1=%7.2f,x2=%7.2f\n", x1, x2); } else printf("無實根\n"); } 5. #include "stdio.h" void main() { float a,b,c; printf("輸入三條邊:\n"); scanf("%f%f%f",&a,&b,&c); if(a+b>c&&a+c>b&&b+c>a) { if(a==b&&b==c) printf("等邊三角形\n"); else if(a==b||b==c||c==a) printf("等腰三角形\n"); else if(a*a==b*b+c*c||b*b==a*a+c*c||c*c==b*b+a*a) printf("直角三角形\n"); } else

  printf("不能構成三角形 \n"); } 6. #include "stdio.h" #include "math.h" void main() { float a, b, c, s, area; printf("輸入三條邊:\n"); scanf("%f%f%f",&a,&b,&c); if(a+b>c&&a+c>b&&b+c>a) { s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); printf("area=%7.2f\n",area); } else printf("不能構成三角形 \n"); } 7. #include void main() { int score; char d; printf("Input score\n"); scanf("%d",&score); if (score>100||score<0) { printf("data error!\n"); return; } switch(score/10) { case 10: case 9:d='A';break; case 8:d='B';break; case 7:d='C';break; case 6:d='D';break; default : d='E';break; } printf("score=%3ddengji=%c\n",score,d); } 8. #include void main() { int a,b,c,t; printf("Input a,b,c\n"); scanf("%d%d%d",&a,&b,&c); t=a;

  a=b; b=c; c=t; printf("a=%d;b=%d;c=%d;\n",a,b,c); } 9. #include void main() { char c1,c2; printf("Input a c1=getchar(); if(c1>='a'&&c1<='z') c2=c1+'A'-'a'; else if(c1>='A'&&c1<='Z') c2=c1+'a'-'A'; else c2=c1; } 10. #include void main() { int a,b,c,max,min; printf("Input a,b,c\n"); scanf("%d%d%d",&a,&b,&c); max=a;min=a; if(b>max) max=b; else if(bmax) max=c; else if(c

  letter: ");

  printf("原字元:%c 轉換後為:%c\n",c1,c2);

  )、do-while 語句、(

  ) 。

  2. break 語句在 C 程式中只能出現在兩種場合:一是用於( )語句中。

  )語句中,二是用於 (

  3. 以下程式段中的語句 printf(“***”)要執行 5 次,請在空白處填入合適的數 。 for( i=1;i<=( );i=i+2 ) printf(“***”); 4. 定義 int k=10;執行語句 while(k<20) k += 1 ; 後,k 的值為( 5. 在迴圈控制結構中 break 語句的作用是( 6. 在迴圈控制結構中 continue 語句的作用是:( )。 )。 )。

  7. 若已知 int a[5][5]; 並在執行過程中 a 陣列已經賦值;要列印次對角線元素,在空白處 填入合適的值/表示式; for (i = 0; i<5; i++) for (j = ( ); j >=0; j--) printf("%5d", a[i][j]); 8. 執行完語句 sum = 0; for (i = 0; i < 10; i++) { if (i % 5 = = 0) sum += i; sum = ( ) 。 9. 在使用 while 語句中需要注意,為了防止“死迴圈”的發生,在迴圈體中一定要有( )語句。 10. do-while 語句中,判斷迴圈條件在( ” ) ),執行迴圈體在 ( ) 。 (填“先” “後

  解答: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 二.閱讀程式寫結果 1. #include void main() { int number , digit; number = 1234; while ( number != 0 ) { digit = number%10 ;

  printf( "%d" , digit ) ; number = number / 10 ; } } 2. #include #define N 5 void main( ) { int i; for(i=1;i void main( ) { int i,s,x; s=0; x=15; if (x%i==0) printf("i=%d,s=%d\n",i,s); }

  for(i=1;i<=5;i++) s= s + i;

  4. #include void main() { int counter=1; while(counter <= 5) printf("%d \n", counter ); counter++; } } 5. #include int main() { int i,sum,m,a; sum=0; m=5; for(i=0;i<=3;i++) { scanf("%d",&a); if(a>m) sum+=a; } printf("sum=%d\n",sum); return 0; }

  {

  輸入:2 10 8 3<回車>

  6. #include int main() { int i,j,k; for(i=1;i<=4;i++) { for (j=1;j<5-i;j++) printf(" "); for(k=1;k<=i;k++) printf("*"); printf("\n"); } return 0; } 7. #include int main() { int i,j; i=1; while(i<5) { for(j=1;j<2*i+1;j++) printf("%c",'#'); printf("\n"); i++; } return 0; } 8. #include int main() { int i=10,m=0,n=0; do { if(i%2!=0) m=m+i; else n=n+i; i--; }while(i>=0); printf("m=%d,n=%d\n",m,n); return 0; }

  9. #include void main() { int sum=0,n; scanf("%d",&n); while(n<=5) { sum+=n; n++; } printf("sum=%d",sum); } 輸入:1<回車> 10. #include void main() { int i, j; for(i=2;i>=0;i--) { for(j=1;j<=i;j++) printf("*"); for(j=0;j<=2-i;j++) printf("!"); printf("\n"); } } 11. #include void main() { int a,b; for(a=1,b=1;a<=100;a++) { if(b>20) break; if(b%4==1) b=b+4; } b=b-5; } printf("a=%d\n",a); } 12. #include void main( )

  { continue;

  { char k; int i; for(i=1;i<3;i++) { scanf("%c",&k); switch(k) { case '0': printf("another\n"); case '1': printf("number\n"); } } } 輸入:01<回車> 13. #include int main() { int i, s = 0; for(i = 1; i < 10; i++) { s += i * i; if(s > 10) break; } printf("i=%d, s=%d\n", i, s); return 0; } 14. #include void main() { char ch; while((ch=getchar())!='\n') { if (ch>='A'&&ch<='Z') ch=ch+32; ch=ch-32; } } 輸入:ABCdef<回車>

  else if (ch>='a'&&ch<='z') printf("%c",ch);

  15. #include int main () { int a, b; for (a = 1, b = 1 ; a <= 100 ; a++)

  { if (b >= 9) break; if (b % 3 == 1) b += 3 ; continue ; } b -= 5; } printf("%d,%d\n", a, b); return 0; } {

  16. #include int main () { int i = 0,j = 0; while (i < 10) i++; while (j++ < 10) ; printf("i=%d,j=%d\n",i,j); return 0; } 17. #include "stdio.h" void main( ) { int i,j,t; for(i=1,j=10;i<3;i++,j--) t=i+j; t++; printf("%d,%d",j,t); } 18. #include "stdio.h" void main() { int i=10,j; do { j = i%2; i--; } while ( i> 4 ); } 19. #include "stdio.h" void main() { int i=7,j; while ( i> 2)

  printf( "%d",j );

  {

  j = i%2; i--; } printf( "%d",i ); }

  printf( "%d",j );

  20. #include void main() { int i,j,t=0; for(i=3;i>0;i--) for(j=0;j<4;j++) t+=j; printf("t=%d",t); } 解答: 1. 4321 2. 1 4 9 16 3. i=6,s=9 4. 1 2 3 4 5 5. sum=18 6. * ** *** ****

  7. ## #### ###### ########

  8. m=25,n=30 9. sum=15 10. **! *!! !!! 11. a=6 12. another number number 13. i=3, s=14 14. abcDEF 15. 4,10 16. i=10,j=11 17. 8,12 18. 010101 19. 101012 20. t=18 三.補充程式 1. 輸出 100 以內能被 3 整除的數 #include"stdio.h" int main() { _____________________________________________; for(_________________________;n<=100; ________________________) { if (n%3!=0) ________________________ printf("%d ",n); } return 0; } 2. 輸出 100 以內的素數。素數是隻能被 1 和本身整除的數。為減少迴圈次數,只需對數 n

  用2 ~ 去除來判斷該數是否素數。 #include"stdio.h" ________________________ int main() { int n,i,k; for(n=2;n<=100; ________________________) { k=sqrt(n); for(i=2;i<=k; ________________________) if(n%i==0) ________________________ if(i>k) printf("\t%2d",n); } return 0; } 3. 輸出 200 以內能被 5 整除的數 #include"stdio.h" int main() { ________________________ for(________________________;n<=200; ________________________) { if (n%5!=0) ________________________ printf("%d ",n); } return 0; } 4. 求輸入的十個整數中正數的個數及其平均值 #include int main() { int i,num=0,a; float sum=0; for(i=0; ________________________;________________________) { scanf("%d",&a); if(a>=0) { ________________________; ________________________; } } printf("%d plus integer's sum: %6.0f\n",num,sum); printf("Mean value:%6.2f\n",sum/num); return 0; }

  5. 求輸入的十個整數中負數的個數及其平均值 #include int main() { int i,num,a; float sum=0; ________________________; ________________________; while(i<10) { scanf("%d",&a); if(a<=0) { ________________________; num++; } ________________________; } printf("%d plus integer's sum: %6.0f\n",num,sum); printf("Mean value:%6.2f\n",sum/num); return 0; } 6. 分解並打印出 X 的各個素數因子.如 18=1*2*3*3. #include int main() { int i,x; scanf(“%d”,&x); printf(“%d=1”,x); for(________________________;i<=x; ________________________) { while________________________ { printf(“*%d”,i); ________________________; } } return 0; } 7. 輸出九九乘法表. #include int main() { int i,j; for (i=1; ________________________;________________________) { for(________________________;________________________;j++) printf("%2d*%2d=%2d", j, i ,i*j);

  printf("\n\n"); } return 0; } 8. 有一分數序列:2/1,3/2,5/3,8/5,13/8,21/13...求出這個數列的前 20 項之和。 #include "stdio.h" int main() { int n,t,number=20; float a=2,b=1, ________________________; for( n=1; ________________________;________________________) { s=s+a/b; t=a; a=a+b; ________________________; } printf("sum is %9.6f\n",s); return 0; } 9. 求 1+2!+3!+...+20!的和. #include "stdio.h" int main() { float n, ________________________,t=1; ________________________;________________________;) { t*=n; ________________________; } printf("1+2!+3!...+20!=%e\n",s); return 0; }

  for(n=1;

  10. 有一分數序列:2/1,3/2,5/3,8/5,13/8,21/13...求出這個數列的前 20 項之和。 #include "stdio.h" int main() { int n,t,number=20; float a=2, ________________________,s=0; for(________________________;________________________;n++) { s=s+a/b; t=a; ________________________; b=t; }

  printf("sum is %9.6f\n",s); return 0; } 解答: 1. int n; n=1; n++ continue; 2. #include"math.h" n++ i++ break; 3. int n; n=1 n++ continue; 4. i<10 i++ num++ sum+=a 5. i=0; num=0; sum+=a; i++;

  6. i=1 i++ (x%i==0) x=x/i; 7. i<=9 i++ j=1 j<=i 8. s=0 n<=20 n++ b=t; 9. s=0 n<=20 n++ s+=t; 10. b=1 n=1 n<=20 a=a+b 四.改錯題 解答:

  五.程式設計一 1. 試編寫程式求 S=1+1*2+1*2*3+1*2*3*4+?+1*2*3*?*10 的值。

  2. 程式設計利用以下公式求π 的值。 (取 n=10000)

  3. 計算 s=1+1/2+1/3+?+1/n 的值,其中 n 的值由鍵盤輸入。

  4. 編寫程式,在螢幕上輸出下面的圖案(要求用 for 迴圈實現) 。 ***** ***** ***** ***** ***** 5. 編寫程式,輸入整數 n,並根據 n 的值列印圖案(要求用 for 迴圈實現) 。 。 n 為中間一行的*號個數。例如,當 n=4時,圖案為 * ** *** **** *** ** * 6. 從鍵盤輸入若干個學生的成績,-1 作為結束標誌,求出平均成績及最高成績並輸出其結 果。 7. 編寫一程式,從鍵盤輸入 n 個數,輸出其最大數和最小數,並輸出它們在序列中的序號。 8. 程式設計實現:輸出由 0、1、2、3 組成的所有的各位數字均不相同的三位數,輸出時每 8 個 數 佔一行。 9. 輸入一行字元,回車作為結束,分別統計出其中英文字母、空格、數字和其它字元的個 數。

  10. 求 s=a+aa+aaa+aaaa+aa...a 的值,其中 a 是一個數字。例如 2+22+222+2222+22222(此時

  共 有 5 個數相加),幾個數相加由鍵盤控制。

  解答: 1. #include void main() { int i; float s=0,term=1; for(i=1;i<=10;i++) { s=s+term; term=term*(i+1); } printf("sum%10.1f\n",s); } 2. #include void main() { int i; float e=1,term=1; for(i=1;i<=100;i++) { term=term*i; e=e+1./term; } printf("e=%10.6f\n",e); } 3. #include void main() { int i,n; float s=0; printf("input n\n"); scanf("%d",&n); for(i=1;i<=n;i++) s=s+1./i; printf("s=%10.6f\n",s); } 4. #include void main() { int i,j,k; for(i=1;i<=5;i++)

  {

  for(k=1;k<=2*i;k++) printf(" "); for(j=1;j<=5;j++) printf("* "); printf("\n");

  } }

  5. #include void main() { int i,j,n; printf("input n\n"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) printf("* "); printf("\n"); } for(i=n-1;i>=1;i--) { for(j=1;j<=i;j++) printf("* "); printf("\n"); } } 6. #include void main() { int i,score,sum=0,max; i=0; printf("please input score:\n"); scanf("%d",&score); max=score; while (score!=-1) { sum=sum+score; if(score>max)max=score; i++; scanf("%d",&score); } printf("max=%d\n",max); printf("average=%f\n",(float)sum/i);

  } 7. #include void main() { int i, n,x,max,min,maxi=1,mini=1; printf("please input n:\n"); scanf("%d",&n); printf("please input x:\n"); scanf("%d",&x); max=x;min=x; for(i=2;i<=n;i++) { scanf("%d",&x); if(x>max) { max=x;maxi=i;} if(x void main() { int i,j,k,n=0; for(i=1;i<=3;i++) for(j=0;j<=3;j++) for(k=1;k<=3;k++) if(i!=j&&i!=k&&j!=k) { n++; printf("%5d",i*100+j*10+k); if(n%8==0)printf("\n"); } } 9. #include void main() { int shuzi=0,zimu=0,kge=0,qita=0; ch=getchar(); while(ch!='\n') { if(ch==' ') kge++; else if(ch>='0'&&ch<='9')

  char ch;

  shuzi++; else if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z') zimu++; else qita++; ch=getchar(); } printf("數字=%d;字母=%d;空格=%d;其它%d",shuzi,zimu,kge,qita); } 10. #include void main() { int Sn=0,a,n,i=0,t; printf("Sn=a+aa+aaa+…+aaa…a\nplease input a,n:"); scanf("%d,%d",&a,&n);t=a; while(i

  ) ;形參實參都是

  4. 定義在一個程式塊(函式)中的變數,稱為( 它的程式塊。

  5. 在函式原型宣告中,必須宣告函式引數的型別,但可以省略( 6. C 語言源程式中主函式名由系統規定為(

  ) 。

  ) ,程式設計師是不能改變的。

  7. 已知函式首部定義為:int stat(int a , float *p) 則此函式的兩個引數型別分別是(

  ) 、 (

  ) 。

  8. 在 c 語言中,進行函式呼叫是,實參和實參之間有嚴格的規定,主要有三種要求分別是: ( ) 相等, ( )一致,並( )對應。 9. 若自定義的函式要求返回一個值,則應在該函式中有一條( )語句。

  10. 若函式頭部定義為 int foo(), 則該函式中 return 語句返回的資料型別是( ) 。 11. 若函式頭部定義為 struct stu * foo(), 則該函式中 return 語句返回的資料型別是( ) 。 12. 若函式頭部定義為 int * foo(), 則該函式中 return 語句返回的資料型別是( ) 。 13. 若函式頭部定義為 float foo(), 則該函式中 return 語句返回的資料型別是( ) 。 14. 若函式頭部定義為 char foo(), 則該函式中 return 語句返回的資料型別是( ) 。 15. 若函式頭部定義為 char * foo(), 則該函式中 return 語句返回的資料型別是( ) 。 16. 若函式頭部定義為 void foo(int a[]),在主程式中有如下語句: int a[5] = {1};? foo(a[5]); 這句函式呼叫的語句是 ( 正確” “錯誤” ) 17. 若函式頭部定義為 void foo(int *a),在主程式中有如下語句: int array[5] = {1};? foo(array); 這樣的語句是( 確” “錯誤” )

  ) 。 (填“

  ) 。 (填“正

  18. 若函式頭部定位為 void foo (char * str), 在主程式中有如下語句: char array[5] = "good";? foo(array); 這樣的語句是( “正確” “錯誤” )

  ) 。 (填

  19. 若一個函式中的主要語句是這樣的, int a = 3; if (a > 3) return -1; return 0; 那 麼 這 個 函 數 的 返 回 值 是 ( ) 。 20. 若函式頭部定義為 void foo (int * a), 主程式中定義 int array[10], 現在要將 arra y 資料傳進陣列進行處理,正確函式呼叫語句是(

  )

  。 解答: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 二.閱讀程式寫結果 1. #include long fun(int x,int n); int main() { int x=3,n=3; long p; p=fun(x,n); printf("p=%ld\n",p); return 0; } long fun(int x,int n) { int i; long p=1; for(i=0;i int isDigit(char ch); int main() {

  char ch; while((ch=getchar())!='\n') { if(isDigit(ch)) putchar(ch); } printf("\n"); return 0; } int isDigit(char ch) { if(ch>='0' && ch<='9') return 1; else return 0; } 輸入:Abc1d23eF45g<回車> 3. #include void odddivisor(int n); int main() { int n; scanf("%d",&n); odddivisor(n); return 0; } void odddivisor(int n) { int i; for(i=3;i<=n/2;i=i+2) if(n%i==0) printf("\n"); } 輸入:15<回車>

  printf("%5d",i);

  4. #include

  void print(); int a=5; int main() { int a=1,b=2; a=a+b; print(); printf("%d %d\n",a,b); return 0; } void print() { int b=3; printf("%d %d\n",a,b); } 5. #include int fun1(int x); void fun2(int x); int main() { int x=1; x=fun1(x);

  printf("%d\n",x); return 0; } int fun1(int x) { x++; fun2(x); return x; } void fun2(int x) { x++; }

  6. #include int fun1(int a,int b,int c); int main() { int a=11,b=21,c=31; fun1(a,b,c); printf("%d %d %d\n",a,b,c); return 0; } int fun1(int a,int b,int c) { a=a+10; b=b+10; c=c+10; } 7. #include void fun(int x); int main() { fun(7); printf("\n"); return 0; } void fun(int x) { if(x/2>1) printf("%5d",x); }

  return c;

  fun(x/2);

  8. #include void fun(int a[]); int main() { int i,a[5]={1,2,3}; fun(a); for(i=0;i<5;i++) printf("%5d",a[i]); printf("\n"); return 0; } void fun(int a[]) { int i; for(i=0;i<5;i++) a[i]+=5; } 9. #include void fun ( int k ) ; void main ( )

  { int w = 5 ; fun ( w ) ; printf ( "\n" ) ; } void fun ( int k ) { if ( k>0 ) fun ( k-1 ) ; printf ( "%d" , k ) ; } 10. #include void f1(void); int a=1; void main( ) { int a=2; f1(); { int a=3; } printf("a3=%d\n",a); } void f1(void) { printf("a1=%d\n",a); }

  printf("a2=%d\n",a);

  11. #include void f(int a, int b, int *c) { a=20; b=10; *c=a+b; } void main() { int a=10,b=20,c=30,d=40; printf("%d,%d,%d\n",a,b,c); }

  f(a,b,&c);

  12. #include void swap(int a,int b); void main() { int a=2,b=3; printf("a=%d,b=%d\n",a,b);

  swap(a,b); printf("a=%d,b=%d\n",a,b); } void swap(int a,int b) { int c; c=a; a=b; b=c; } 13. #include void fun(int a,int b,int c); void main() { int x=10,y=20,z=30; fun(x,y,z); printf("%d,%d,%d\n",x,y,z); } void fun(int a,int b,int c) { a=456;b=567;c=678; } 14. #include float f(int n) { int i;float s=0.0; for(i=1;i int f(int a); void main() { int a=2,i; for(i=0;i<3;i++) printf("%d",f(i));

  } int f(int a) { int b=0,c=3; c++; return(a+b+c); } 16. #include int fun(); void main() { int i,x; for(i=0;i<=2;i++) x=fun(); printf("%d\n",x); } int fun() { int x=3; x++; return x; }

  b++;

  17. #include float add(float x,float y); void main( ) { float a,b,c; a=1.0; b=2.0; printf("%f\n",c); } float add(float x,float y) { float z; z=x+y; return(z); } 18. #include void fun(int x, int cp, int dp) { cp=x++; dp=++x; }

  c=add(a,b);

  void main(void) { int a,c=80, d=-20; a=30; printf("%d,%d\n", c,d); } 19. #include int f(int a,int b); void main() { int i=2,p; p=f(i,i+1); printf("%d\n",p); } int f(int a,int b) { int c; if(a>b) c=1; else if (a==b) c=0; else c=-1; return (c); }

  fun(a,c,d);

  20. #include int fun(int n) { if(n= =1) return 1; else return fun(n-1)+3; } void main() { int i,j=0; for(i=1;i<4;i++) j=j+fun(i); printf("j=%d\n",j); } 21. #include void f(int x,int y) { int t; if(x

  f(a,b);

  f(a,c); f(b,c); printf("%d,%d,%d",a,b,c); } 22. #include int age(int n) { int c; if(n==1) c=10; else c=age(n-1)+2; return(c); } void main() { printf("%d",age(5)); } 23. #include void hello_world(void) { printf("Hello, world!\n"); } void three_hellos(void) { int counter; for (counter =1;counter <= 3;counter++) hello_world(); }

  void main(void) { three_hellos(); } 24. #include int f(int a,int b); void main() { int x,i=1,k=3; x=f(i,k); printf("x=%d \n",x); } int f(int a,int b) { int M=0; while(b!=0) M=M+a; b--; } return M; }

  {

  25. #include f(int b[ ],int m,int n) { int i,s = 0; for(i = m;i < n;i++) s += b[i]; return s; } int main() { int x,a[ ] = {1,2,3,4,5,6,7,8,9}, *p = a; x = f(p,3,7); printf("x=%d\n",x); return 0; } 26. #include void fun(int i, int j) { int x = 7; printf("i = %d; j = %d; x = %d\n", i, j, x); } int main() { int i = 2, x = 5, j = 7; fun(j, 6); printf("i = %d; j = %d; x = %d\n", i, j, x);

  return 0; } 27. #include void f(int a[]) { int i=0; while(a[i]<=10) { printf("%d",a[i]); i++; } } int main() { int a[]={1,7,17,9,11,34}; f(a+1); }

  28. #include void add(int x, int y, int z) { z = x + y; x = x * x; y = y * y; printf("(2) x = %d y = %d z = %d\n", x, y, z); }

  int main() { int x = 2, y = 3, z = 0; printf("(1) x = %d y = %d z = %d\n", x, y, z); add(x, y, z); printf("(3) x = %d y = %d z = %d\n", x, y, z); return 0; }

  29. #include int x1 = 30, x2 = 40; void sub(int x, int y) { x1 = x; x = y; y = x1; }

  int main() { int x3 = 10, x4 = 20; sub(x3, x4); sub(x2, x1); printf(" %d, %d, %d, %d\n", x3, x4, x1, x2); return 0; }

  30. #include int x; void cube() { x = x * x * x; }

  int main() { x = 5; cube(); printf(" %d\n", x); return 0; } 31. #include invert(int *s, int i, int j) { int t; if(i < j) { invert(s, i + 1,j - 1); t = *(s + i); *(s + i) = *(s + j); *(s + j) = t; } } void main( )

  { int a[6] = {10, 6, 23, -90, 0, 3}, i; invert(a, 0, 5); for(i = 0; i < 6; i++) printf("%d, ", a[i]); printf("\n"); } 32. int func(int a[][3])

  {

  int i,j,sum=0; for(i=0;i<3;i++)

  for(j=0;j<3;j++){

  a[i][j]=i+j;

  if(i==j)

  sum = sum+a[i][j];

  }

  return sum;

  }

  int main()

  {

  int a[3][3]={1,3,5,7,9,11,13,15,17};

  int sum;

  sum=func(a);

  printf("sum=%d",sum);

  return 0; }

  33. # include int i=10; void fun(); void main( ) { int i; for(i=2; i>0; i--) fun( ); } void fun() { i*=2; printf("i=%d\n", i); } 34. #include int fun(int n) { if(n<=1) return 1; else

  return fun(n-1)*n; } void main() { int i,j=0; for(i=1;i<=3;i++) j+=fun(i); printf("j=%d\n",j); }

  35. #include int funa(int a, int b) { int t; if (a > b) t = a/b; else t = a%22; return t; } void main() {

  int a=33, b=22,c; c=funa(a,b); printf("c=%d\n", c); }

  36. # include void fun(int i); void main( ) { int i; for(i=2; i>0; i--) fun(i); } void fun(int i) { i*=2; printf("+%d+", i); } 37. # include void fun(int n); void main( ) { int x=879; fun(x); } void fun(int n) { int a,b,c,t; a = n % 10; b = (n/10) % 10; t; } if ( b < c ) { t = b; b = c; c = t; } if ( a < b ) { t = a; a = b; b = t; } printf( "%d", b ); }

  c = (n/100) % 10;

  if ( a < b ) { t = a; a = b; b =

  38. #include int funa(int a, int b) { int t; if (a > b) { t = a; a = b; b = t; } return t; } void main() { int a=33, b=22,c; c=funa(a,b); printf("c=%d\n", c); } 39. #include int swap(int x, int y) { int z; z=x%10; x=y%10; y=z; z=x; return z; } void main() { int a=38, b=29,c;

  c=swap(a,b); printf("%d%d\n", a,c); }

  40. #include void fun(int n) { if(n==1) printf("*\n"); else { printf("-"); fun(n-1); } } void main() { int i,j=0; for(i=1;i<4;i++) fun(i); printf("XiaoXin!"); } 解答: 1. p=27 2. 執行結果:12345 3. 3 4. 5 3 32 5. 2 6. 11 21 31 7. 3 7 5

  8. 6

  7

  8

  5

  5

  9. 012345 10. a1=1 a2=3 a3=2 11. 10,20,30 12. a=2,b=3 a=2,b=3 13. 10,20,30 14. a=1.0000 15. 567 16. 4 17. 3.000000 18. 80,-20 19. -1 20. j=12 21. 4,3,5 22. 18 23. Hello, world! Hello, world! Hello, world! 24. x=3; 25. x=22 26. i = 7; j = 6; x = 7 i = 2; j = 7; x = 5

  27. 7 28. (1) x = 2 y = 3 z = 0 (2) x = 4 y = 9 z = 5 (3) x = 2 y = 3 z = 0 29. 10, 20, 40, 40 30. 125 31. 3,0,-90,23,6,10 32. sum=6 33. i=20 i=40 34. j=9 35. c=1 36. +4++2+ 37. 8 38. c=33 39. 389 40. * -* --* XiaoXin!

  三.補充程式 1. 定義了一個 max 函式,找兩個整數的最大值,其位置在 main 之後. #include "stdio.h" ________________________ int main() {

  int x,y,z; printf("input two numbers:\n"); scanf("%d%d",&x,&y); ________________________; printf("maxmum=%d",z); return 0; } int max(int a,int b) { ________________________; if(a>b)w=a; else w= b; ________________________; } 2. 用函式 nzp 判別一個整數 a 的值,若大於 0 則輸出該值,若小於等於 0 則輸出 0 值。 #include "stdio.h" ________________________nzp(________________________) { if(v>0) ________________________ else printf("%d ",0); } int main() { int a; printf("input number a:\n"); scanf("%d",&a); ________________________ return 0; } 3. 主函式調 f1 計算 n 階乘. #include "stdio.h" ________________________ int main() { int n; long s; scanf(“%d”,&n); ________________________ printf("\ns=%ld\n",s); return 0; } long f2(int n) { ________________________

  int i; for(i=1;i<=n;i++) c=c*i; ________________________ } 4. 主函式調 fun 計算 1+1/2!+1/3!+...+1/30!的和. #include "stdio.h" ________________________ int main() { int n; float s; ________________________ printf("1+1/2!+1/3!...+1/30!=%f\n",s); return 0; } float fun() { int n; float t=1; ________________________ for(n=1;n<=30;n++) { t*=1/n; s+=t; } ________________________ } 5. 陣列 a 中存放了一個學生 5 門課程的成績,求平均成績。 #include "stdio.h" ________________________ int main() { float sco[5],av; int i; printf("\ninput 5 scores:\n"); for(i=0;i<5;i++) scanf("%f",&sco[i]); ________________________ printf("average score is %5.2f",av);

  return 0; } float aver(float a[]) { int i; float s=a[0]; ________________________ for(i=1;i<5;i++) s=s+a[i]; av=s/5; ________________________ } 6. 用遞迴法計算 n! #include "stdio.h" ________________________ int main() { int n; long y; printf("\ninput a inteager number:\n"); scanf("%d",&n); ________________________ printf("%d!=%ld",n,y); return 0; } ________________________ff( int n) { long f; if(n<0) printf("n<0,input error"); else if(n==0||n==1) f=1; else f=ff(n-1)*n; ________________________ }

  7. 輸入正方體的長寬高 l,w,h。求體積及三個面 x*y,x*z,y*z 的面積。 #include "stdio.h" int s1; ________________________ int vs( int a,int b,int c) { ________________________ v=a*b*c; s1=a*b; s2=b*c;

  s3=a*c; ________________________ } int main() { int v,l,w,h; printf("\ninput length,width and height\n"); scanf("%d%d%d",&l,&w,&h); ________________________ printf("v=%d s1=%d s2=%d s3=%d\n",v,s1,s2,s3); return 0; } 8. 輸入兩個正整數 num1,num2,求其最大公約數. #include "stdio.h" ________________________ int main() { int d; int num1,num2; printf("please input two numbers:\n"); scanf("%d,%d",&num1,&num2); ________________________ printf("gongyueshu:%d\n",d); return 0; } int gongyue(int a,int b) { ________________________ while(b) { temp=a%b; a=b; b=temp; } ________________________ } 9. 利用遞迴函式呼叫方式,輸出 6 個數字。 #include "stdio.h" ________________________ int main() { int w=5; fun(w); printf("\n"); return 0; } ________________________fun(________________________)

  { if(k>0) ________________________; printf(“%d”,k); } 10. 主函式調 printstar 列印一串星形圖案. #include "stdio.h" ________________________ int main() { ________________________ ; return 0; } ________________________printstar(________________________) { printf(“*************\n”); }

  解答: 1. int max(int a,int b); z=max(x,y); int w; return w; 2. void int v printf("%d",v); nzp(a); 3. long f2(int n); s=f2(n);

  long c=1; return c; 4. float fun(void); s=fun(); float s=0; return s;

  5. float aver(float a[]); av=aver(sco); float av; return av;

  6. long ff(int n); y=ff(n); long return f; 7. int s2,s3; int v; return v; v=vs(l.w,h); 8. int gongyue(int a,int b); d=gongyue(num1,num2);

  int temp; return a; 9. void fun(int k); void int k; fun(k-1); 10. void printstar(void); printstar(); void void 四.改錯題 解答: 五.程式設計一 解答: 六.程式設計二 1. 程式設計實現:任意輸入兩個正整數,求它們的最大公約數和最小公倍數。要求最大公約數 透過自定義函式來輸出。 2. 程式設計實現:求出 10~10000 之內的.所有素數。要求素數判斷透過自定義函式來實現。 3. 程式設計實現:輸出所有的“水仙花數” 。所謂“水仙花數”是指一個 3 位數,其各位數字立 方和等於該數本身。例如:153 是一個“水仙花數” ,因為 。要求水仙花數的判斷透過自定 義函式來實現。 4. 程式設計實現:打印出 2~1000 之間的所有完數。所謂完數,是指這樣的數,該數的各因子之 和正好等於該數本身,例如:6=1+2+3;28=1+2+4+7+14;因此,6、28 都是完數。要求完數 的 判斷透過自定義函式來實現。 5. 程式設計實現:打印出 10~9999 之間的所有迴文數,要求每 10 個數一行輸出。要求迴文數的 判 斷透過自定義函式來實現。 6. 程式設計實現:打印出 1~9999 之間的所有同構數,所謂同構數是指一個數如果出現在該數平

  方的右端,該數就是同構數。如 5,25 都是同構數。要求同構數的判斷透過自定義函式來實 現 。 7. 給定一 5*5 的整型矩陣,求所有正對角線元素之和,並以矩陣的形式輸出矩陣的值。要 求 :對角線元素之和及矩陣輸出分別透過兩個自定義函式來實現。 8. 程式設計實現:輸入一 4*4 的矩陣的值,求出每行的最大值並輸出其所在列號。要求:透過 通 過自定義函式實現矩陣的輸入輸出。 9. 程式設計實現:任意給定一個十進位制的整數,將其轉換成二進位制,按位將其存放到陣列中, 然後輸出,要求數制轉換透過自定義函式來實現。

  10. 程式設計實現:任意輸入一個正整數,程式設計求出它是幾位數並將其逆序輸出。要求透過自定 義函式實現求是幾位數,該函式原形為:int ni(int x,int a[]);函式返回值是該數是幾位 數,x 為給定的數,a 中存放該數的各個位。 11. 編寫程式從鍵盤輸入 x 和 n 的值,求 xn (n 為大於等於零的整數) ,要求用 n 遞迴函式求 x 。

  12. 編寫實現:輸入兩個正整數 m,n(m>n) ,計算從 m 個元素中任意取 n 個元素的組合,計 算機

  公式為: 要求透過遞迴函式求階乘。

  13. 程式設計實現:從鍵盤任意輸入 10 個不相同的整數,然後,輸入要查詢的數 x,如果在這 10 個 數中找到 x,則列印“Found!” ;如果找不到,則列印“Not found!” 。要求透過自定義函式

  實現查詢功能。 14. 從鍵盤輸入某班 20 個學生的成績和學號,然後從鍵盤任意輸入一個學生的學號,查詢 出 他的成績並輸出,若未找到,給出相應提示。要求透過自定義函式實現查詢功能。 15. 程式設計實現:從鍵盤任意輸入某班 20 個學生的姓名和成績,輸出最高分及不及格學生的 人 數。要求分別用兩個自定義函式求最高分和不及格人數。 16. 程式設計實現:輸入 2 個降序排列的整數數列,將這兩個數列合併存放到另一個數組中,要 求 每個數都一次到位,不得在新陣列中重新排序。要求數列合併透過自定義函式來實現。 17. 程式設計實現:輸入 N 名職工的姓名及計算機水平考試的成績,輸出及格人數和及格的職工 信 息。要求:職工資訊的輸入和輸出分別透過自定義函式來實現。

  18. 程式設計實現:從鍵盤輸入 10 名學生的資訊,包括學號、姓名、成績,用一個排序函式 sort 完成學生成績降序排列,並輸出學生排序後的結果。 19. 程式設計實現:任意輸入一組不多於 20 個的升序排列的若干個整數,任意輸入不多於 10 個 的 整數(-1 作為輸入結束標誌) ,將其插入到該序列中,使其依然有序。要求:透過自定義函 數實現將一個整數插入到一個有序序列中,使其依然有序。 20. 一個班級有 30 個學生,每個學生有學號、姓名及數學、語文、計算機成績、平均成績。 要求程式設計實現以下功能: 1) 從鍵盤上輸入 30 個學生的資訊,平均成績透過公式:平均成績=(數學+語文+計算 機 )/3 計算得出。 2) 輸出所有平均成績大於等於 85 的學生姓名(要求用自定義函式實現該功能)。 解答: 1. #include "stdio.h" int GreatComDiv(int m,int n); int LeaseComMul(int m,int n); void main() { int m,n,t; printf("請輸入 m 和 n 的值:"); scanf("%d%d",&m,&n); if((m<=0)||(n<=0))

  { printf("輸入錯誤!請重新輸入!\n"); scanf("%d%d",&m,&n); } if(m

  int LeaseComMul(int m,int n) {int greatcomdiv; greatcomdiv=GreatComDiv( m, n); return m*n/greatcomdiv; }

  2. #include "stdio.h" int sushu(int n); void main() { int n; printf("input n:\n"); scanf("%d",&n); if (sushu(n)) printf("%d 是素數\n",n); else printf("%d 不是素數\n",n); } int sushu(int n) { int i; for(i=2;i

  return 1; }

  3. 程式設計實現:輸出所有的“水仙花數” 。所謂“水仙花數”是指一個 3 位數,其各位數字立 方和等於該數本身。例如:153 是一個“水仙花數” ,因為 。要求水仙花數的判斷透過自定 義函式來實現。 #include "stdio.h" int fun(int n); void main() {int i; for(i=100;i<=999;i++) if (fun(i))printf("%d\n",i); } int fun(int n) {int a,b,c; a=n/100;b=n/10%10;c=n%10; }

  if (a*a*a+b*b*b+c*c*c==n)return 1;

  return

  0;

  4. #include "stdio.h" int wanshu(int n); void main() { int n; printf("input n:\n"); scanf("%d",&n); if (wanshu(n)) printf("%d 是完數\n",n); else printf("%d 不是完數\n",n); } int wanshu(int n) { int i,s=0; for(i=1;i<=n/2;i++) if(n%i==0) s+=i; if(s==n) return 1; else return 0;

  }

  5. 方法一: #include "stdio.h" int huiwen(int n); void main()

  { int n; printf("input n:\n"); scanf("%d",&n); if (huiwen(n)) printf("%d 是迴文數\n",n); else printf("%d 不是迴文數\n",n);

  } int huiwen(int n) { int a[6],i=0,j; while (n!=0) //從第一個開是放元素 {i++; a[i]=n%10; n=n/10; } for(j=1;j

  方法二、#include int rev(int a);------------------------------------------------------------------------- 1 分 void main() { int x, y,i=0; for(x=1;x<1000;x++)-------------------------------------------------------------2 分 { y=rev(x); if(x==y) { printf("%6d",x);----------------------------------------------------------3 分 --2 分 } } int rev(int a) { int m, b=0;--------------------------------------------------------------------------1 分 while(a!=0) { m=a%10; b=b*10+m; a=a/10;--------------------------------------------------------------------------5 分 } return b;------------------------------------------------------------------------------1 分 i++; if(i%10==0) } printf("\n");----------------------------------------------------------

  6. #include "stdio.h" int tgshu(int n);

  void main() { int n; printf("input n:\n"); scanf("%d",&n); if (tgshu(n)) printf("%d 是同構數\n",n); else printf("%d 不是同構數\n",n); } int tgshu(int n) { int d=1,nn,k=0,t; nn=n*n; t=n; while(t!=0) { d=d*10; t=t/10; if(nn%d==n) return 1;

  } else return 0; }

  7. #include "stdio.h" #define n 5 void out(int a[n][n] ); int he(int a[n][n]); void main() { int a[n][n],i,j,s; printf("input 5*5 elements\n"); for(i=0;i

  } int he(int a[n][n]) { int i,sum=0; for(i=0;i

  8. #include "stdio.h" #define n 4 void out(int a[n][n] ); void inp(int a[n][n]); void main() { int a[n][n],i,j,k; printf("input 5*5 elements\n"); inp(a); for(i=0;i

  }

  9. #include "stdio.h" int change(int x,int a[]) //返回值為二進位制的位數 { int n=-1; while(x!=0) { n++; a[n]=x%2; x=x/2; } return n; } void main() { int x,a[20],i,n; printf("input x\n"); scanf("%d",&x); n=change(x,a); for(i=n;i>=0;i--) printf("%1d",a[i]); printf("\n"); } 10. #include "stdio.h" int ni(int x,int a[]) //返回值為數的位數 { int n=-1; while(x!=0) { n++; a[n]=x%10; x=x/10; } return n; } void main() { int x,a[20],i,n; printf("input x\n"); scanf("%d",&x); n=ni(x,a); for(i=0;i<=n;i++) printf("%1d",a[i]); printf("\n"); } 11. #include "stdio.h" int exp(int x,int n); void main() { int x,n; printf("input x,n:\n"); scanf("%d%d",&x,&n); printf("%d\n",exp(x,n));

  } int exp(int x,int n) { if (n==1)return x; return x*exp(x,n-1); }

  12. #include "stdio.h" long int jch(int n) { if(n==1) return 1; else return n*jch(n-1); } void main() { int m,n,zuhe; printf("input two integers m,n(m>n)\n"); scanf("%d,%d",&m,&n); if(m>=n) zuhe=jch(m)/(jch(n)*jch(m-n)); printf("%d,%d 的組合為:%d",m,n,zuhe); } 13. #include "stdio.h" void serach(int a[],int x) { int i; for (i=0;i<10;i++) if(a[i]==x) { printf("Found\n"); return ; } printf("Not found!\n"); } void main() { int i,a[10],x; printf("input 10 integers \n"); for (i=0;i<10;i++) scanf("%d",&a[i]); printf("input x\n"); scanf("%d",&x); serach(a,x); }

  14. #include "stdio.h" #include "string.h" typedef struct { char no[10]; int score; }studtype; void search(studtype a[],char xno[]) { int i; for (i=0;i<20;i++) if(strcmp(a[i].no,xno)==0) { printf("%d\n",a[i].score); return ; } printf("Not found!\n"); } void main() { studtype stu[20]; char xno[10]; int i; printf("input 20 no,score\n"); for (i=0;i<20;i++) scanf("%s%d",stu[i].no,&stu[i].score); printf("input xno\n"); scanf("%s",xno); search(stu,xno); } 15. 程式設計實現:從鍵盤任意輸入某班 20 個學生的姓名和成績,輸出最高分及不及格學生的 人 數。要求分別用兩個自定義函式求最高分和不及格人數。 #define N 20 #include typedef struct { char name[10]; int score; }datatype; int max(datatype stu[]); int num(datatype stu[]); void main() { datatype stu[N]; int i,max_score,n; /*姓名*/ /*成績*/ /*最高分函式宣告*/ /*不及格人數函式宣告*/

  printf("請輸入 %d 名職工姓名 成績:\n", N); for(i=0;istu[m].score)m=i; return stu[m].score; } int num(datatype stu[]) { int i,n=0; for(i=0;i

  16. 程式設計實現:輸入 2 個降序排列的整數數列,將這兩個數列合併存放到另一個數組中,要 求 每個數都一次到位,不得在新陣列中重新排序。要求數列合併透過自定義函式來實現。 #define M 4 #define N 5 #include void merge(int a[],int b[],int c[]); void main() { int i,a[M],b[N],c[M+N]; printf("請輸入 %d 個降序排列的整數:\n", M); for(i=0;i

  void merge(int a[],int b[],int c[]) { int i,j,k; i=0;j=0;k=0; while(ib[j]) c[k++]=a[i++]; else c[k++]=b[j++]; while(i

  17. 程式設計實現:輸入 N 名職工的姓名及計算機水平考試的成績,輸出及格人數和及格的職工 信 息。要求:職工資訊的輸入和輸出分別透過自定義函式來實現。 #define N 10 #include //#include typedef struct { char name[10]; float score; }datatype; void input(datatype work[]); void print(datatype work[]); void main() { datatype work[N]; input(work); print(work); } void input(datatype work[]) /*input 函式定義*/ { int i; printf("請輸入 %d 名 學生的姓名 成績:\n", N); for(i=0;i

  void print(datatype work[]) /*print 函式定義*/ { int i; for(i=0;i60)printf("%10s\n",work[i].name); }

  18. 程式設計實現:從鍵盤輸入 10 名學生的資訊,包括學號、姓名、成績,用一個排序函式 sort 完成學生成績降序排列,並輸出學生排序後的結果。 #define N 10 #include //#include typedef { char num[20]; char name[20]; float score; }stutype; void input(stutype stu[]); void sort(stutype stu[]); void print(stutype stu[]); int main() { stutype stu[N]; input(stu); sort(stu); print(stu); return 0; } void input(stutype stu[]) /*input 函式定義*/ { int i; printf("請輸入 %d 名學生資訊(學號 姓名 成績):\n", N); for(i=0;i

  k=i; for(j=i+1;j

  printf("****名次 學號 姓名 成績****\n"); for(i=0;i

  19. //程式設計實現:任意輸入一組不多於 20 個的升序排列的若干個整數,任意輸入不多於 10 個 的整數(-1 作為輸入結束標誌) ,將其插入到該序列中,使其依然有序。要求:透過自定義 函式實現將一個整數插入到一個有序序列中,使其依然有序。 #include void ins(int a[],int*n,int x); void main() { int a[30],x,m,i; m=-1; printf("輸入一組不多於 20 個的升序排列的若干個整數,-1 作為輸入結 束標誌\n"); scanf("%d",&x); while(x!=-1) { m++; a[m]=x; scanf("%d",&x); } printf("輸入一組不多於 10 個的升序排列的若干個整數, -1 作為輸入結束標誌\n"); scanf("%d",&x); while(x!=-1) { ins(a,&m,x); scanf("%d",&x); } for(i=0;i<=m;i++)

  printf("%5d",a[i]); } void ins(int a[],int*n,int x) { int i=*n; while(a[i]>x&&i>=0) { a[i+1]=a[i]; i--; } a[i+1]=x; (*n)++; }

  20. 一個班級有 30 個學生,每個學生有學號、姓名及數學、語文、計算機成績、平均成績。 要求程式設計實現以下功能: 1) 從鍵盤上輸入 30 個學生的資訊,平均成績透過公式:平均成績=(數學+語文+計算 機 )/3 計算得出。 2)輸出所有平均成績大於等於 85 的學生姓名(要求用自定義函式實現該功能)。 #include "stdio.h" #define n 30 typedef struct { char no[11],name[9]; int math,chiness,comp; float ave; }studtype; void out(studtype a[]) { int i; for (i=0;i=85)printf("%10s\n",a[i].name); } void main() { studtype stu[n]; int i; printf("input no,name,math,chiness,computer\n"); for (i=0;i

  第七章 陣列 一.填空 1. 陣列是由固定數目的(

  )的變數按一定順序排列而構成的。 )個位元組

  2. 設有陣列定義:char array[]=“China” ;則陣列所佔的儲存空間為( 。 3. 一個二維字元陣列 a[10][20]能夠儲存( )個字串。

  4. 假定在一維陣列 b[10]中,元素 b[5]的指標為 p,則 p+4 所指向的元素為( 5. 一維陣列的下標從( )開始。 )。

  ) 。

  6. 設有陣列 a[10];則首地址可以寫成 (

  7. 設有陣列 int a[10], 陣列元素 a[5]用指標表示的形式是( 8. 設有陣列 int a[10][15], 陣列元素 a[5][3]用指標表示的形式是( 9. 定義 3 行 4 列的整型二維陣列 a 的語句是( ) 。 10. 定義 int b[ ]={5,6,7,8}, y, *p=b; 執行 y=*p++ 以後, y 的值為( 11. 按下面的語句定義陣列 a 後,a [2][0] 的值為( int a[3][2]={2,4,6,8,10}; )。 )。 ) 。

  ) 。

  12. 假設要把“student! ”存入一個字元陣列,則該陣列長度應該至少為( 。 13. 在 C 語言中,如果採用語句 int a[2][3] = {1,2,3,4,5} 完成一個二維陣列的定義和初 始化,那麼 a[0][1] 的值為( ) 。 14. 在 C 語言中,如果採用語句 int a[2][3] = {1,2,3,4,5} 完成一個二維陣列的定義和初 始化,再定義 int *b = a[0],那麼 *(b+2) 的值為 ( ). 15. 若 int a[ ][3]={1,2,3,4,5,6,7,8,9};則*(*a+1)的值是( 16. 按下面的語句定義陣列 b 後,b[2][1]的值為( int b[][2]={2,1,4,3,5,6}。 ) 。 ) 。

  )

  17. 設 int a[]={0,1,2,3,4,5,6,7,8,9},*p=a,則*p+4=( 18. 定義 2 行 3 列 5 層的整型三維維陣列 a 的語句是( 19. 若有 int a[3]={10,12,30};則*(a+2)+3= ( 20. 若有 int a[3]={10,12,30};則 a+1 是(

  ) 。 ) 。 ) 。 )的地址。

  21. 假設 int 型變數佔兩個位元組的儲存單元,若有定義:int x[10]={0,2,4}; 則陣列 x 在記憶體 中所佔位元組數為 ( ) 。 22. 若有定義 int a[][3]={1,2,3,4,5,6,7};,則 a 陣列第一維的大小是( ) 。 23. 陣列 int a[3][3];共定義了( 24. C 語言中,二維陣列的存放方式為按( )個數組元素。 )存放。 (填“行”或“列” )

  25. 定義 int a[10]={11,12,13,14,15}, *p=&a[5]; 假設&a[0]和&a[1]的值分別是 0x2000 和 0 x2004,則 p 的值是( ) 。 26. 定義 int a[10]={11,12,13,14,15}, *p=&a[3]; 假設&a[0]和&a[1]的值分別是 0x2000 和 0 x2004,則表示式 a[0]+*p 的值是( ) 。 27. 對一維陣列初始化時,若有 int ) 。 a[10]={0,1,2,3,4},則 a[5]的值是(

  28. 已知:int a[][3]={1,3,5,7,9,11}; 此時陣列元素 a[1][2]的值為( ) 。 29. 定義 char a[7]= "abcdef"; char b[4]= "ABC",執行語句 strcpy(a,b);printf("%s",a), 輸出結果是( ) 。 30. 定義 int a[10]={1,3,5,7,9,11,13,15,17,19},*p=a;執行語句 printf("%d",*p+7);輸出 結果是( ) 。 31. 已知 int a[5] = { 2, 3, 4, 5, 6 }; int *p = a+2; 則表示式 *p*a[3] 的值是( ) 。 32. 定義 char a[7]= "123456"; char b[4]= "ABC",執行語句 strcpy(a,b); printf("%c",a[ 6]),輸出結果是( ) 。 33. 對整形陣列 int a[5]={1,2,3}; 則執行語句 a[2]=a[2]+a[1]*(*(a+3)); a[2]的值為( )

  34. 定義 char a[5]={65,66,67},*p=a; 則執行語句 printf("%s",p+1);輸出結果為( )。 35. 對於定義 struct TData{ int a[2]; char c; }; struct TData slist*3+=,,,1,2-,’A’-,,,3,4-,’B’---; slist[1].a[0]的值為( )。

  36. 已知:int a[][3]={1,3,5,7,9,11}; 則*(a[1]+1)的值為( ) 。 37. 已知:int a[2][3]={1,3,5,7,9,11,13},*p; p=&a[2]; p=p+3; 則*p 的值為( )。 38. 定義 char a[7]= "123"; char b[4]= "ABC",執行語句 strcat(a,b); printf("%s",a),輸 出結果是( ) 。 39. 對整形陣列 int a[5]={3,3,3}; 則執行語句 a[2]=a[1]>0?(a[4]+1):(a[2]+2); a[2]的值 為( ); 40. 定義 int a[ ]={5,6,7,8}, *p=a; 執行 *(p++); 以後, a[0]的值為( )。 解答: 1. 同類型 2. 6 3. 10 4. b[9] 5. 0 6. a 7. *(a+5) 8. *(*(a+5)+3)

  9. int a[3][4] 10. 5 11. 10 12. 9 13. 2 14. 3 15. 2 16. 6 17. 4 18. int a[2][3][5] 19. 33 20. a[1] 21. 20 22. 3 23. 9 24. 行 25. 0x2014 26. 25 27. 0 28. 11 29. ABC 30. 8

  31. 20 32. 6 33. 3 34. BC 35. 4 36. 5 37. 11 38. 123ABC 39. 1 40. 5 二.閱讀程式寫結果 1. #include void main() { int a[2][3]={{3,2,7},{4,8,6}}; int i,j,m=0; for(i=1;i<=1;i++) for(j=0;j<=i;j++) m=m+a[i][j]; printf("m=%d",m); } 2. #include void main(void) { int array[10] = {1, 2, 4, 5, 6, 0, 0, 0, 0, 0}, i; printf("\n"); for (i=0; i<10; i++) printf("%3d ", array[i]); printf("\n"); for (i=9; i>=2; i--) array[i] = array[i-1]; array[2] = 3; for (i=0; i<10; i++) printf("%3d ", array[i]);

  printf("\n"); } 3. #include void main(void) { char a[]="ABCDEFGH",b[]="abCDefGh"; char *p1,*p2; int k; p1=a; p2=b; for(k=0;k<=7;k++) if (*(p1+k)==*(p2+k)) printf("%c",*(p1+k)); printf("\n"); } 4. #include #include void main() { char a[30]="Hello "; char b[10]="Students"; char c[10]="Teachers"; strcat(a,c); printf("%s,%s.\n",a,b); } 5. #include void inv(int x[],int n); void main() { int i, a[10]={3,7,9,11,0,6,7,5,4,2}; inv(a, 10); printf("The array has been reverted.\n"); for(i=0;i<10;i++) printf("%d,",a[i]); printf("\n"); } void inv(int x[], int n) { int t,i,j,m=(n-1)/2; for(i=0;i<=m;i++) { j=n-1-i; t=x[i]; x[i]=x[j];

  x[j]=t; } } 6. #include void main() { float a[3][3]={1,2,3,4,5,6,7,8,9},sum=0; int i; printf("please input rectangle element:\n"); for(i=0;i<3;i++) sum=sum+a[i][i]; printf("duijiaoxian he is %6.2f",sum); } 7. #include int main() { int i=1,n=3,j,k=3; int a[5]={1,4,5}; while(i<=n&&k>a[i]) i++; for(j=n-1;j>=i;j--) a[j+1] = a[j]; a[i] = k; for(i=0;i<=n;i++) printf(“%2d”,a*i+); return 0; } 8. #include void main() { int a[10]={0,1,2,3,4,5,6,7,8,9}, b[10],i,sum=0; { b[i]=a[i-1]+a[i]; sum=sum+b[i]; } for(i=1;i<10;i++) if(i%3==0) sum=sum+b[i]; printf("sum=%3d",sum); } 9. #include void main() { int a[5]={2,6,1,3,4},n=5,i,j,t; for(i=0,j=1;j

  for(i=1;i<10;i++)

  if(a[i] void funa(int x[],int n); void main() { int i, a[5]={3,7,9,11,0}; funa(a, 5); for(i=0;i<5;i++) printf("%d,",a[i]); printf("\n"); } void funa(int x[], int n) { int t,i,j,m=(n-1)/2; for(i=0;i<=m;i++) { j=n-1-i; t=x[i]; x[i]=x[j]; x[j]=t; } } 11. #include void main() { int a[5]={24,15,33,64,45},n=5,i,j,t; for(i=0;i0;i--) printf("%d,",a[i-1]); } 12. #include void main() { int a[5],i,j,t; for(i=4;i>=0;i--)

  scanf("%d",&a[i]); for(i=0;i<5;i++) a[i]=a[i]+10; for(i=0;i<5;i++) printf("%d,",a[i]); } 輸入:42 31 24 15 16<回車> 13. #include void main() { char a[][5]={"Zhong","Gong", "ren"}; printf("%s\n%s\n", a[0],a[1]); } 14. #include void main() { int a[3][3]={0,1,2,3,4,5,6,7,8},i,j,t; for(i=0;i<3;i++) for(j=0;j<3;j++) a[i][j]+=a[i][0]; for(i=0;i<3;i++) printf("%d,",a[i][i]); } 15. #include void main() { int a[3][3]={1,2,3,4,5,6,7,8,9},i,j; for(i=0;i<3;i++) for(j=0;j<3;j++) a[i][j]+=a[i][2]; while(i>0) { i--; printf("%d,",a[i][i]); } } 16. #include void main() { int i,j; char a[10]={"ABCDEFGHIJ"};

  for(i=0;i<10;i++) a[i]=a[i]+3; }

  printf("%c\n", a[6]);

  17. #include int main() { int i,a[10]={0,1,2,3,4,5,6,7}; for(i=1;i<9;i++) a[i]=a[i-1]+a[i+1]; printf("%d%d\n",a[5],a[8]); return 0; } 18. #include int main() { int i,a[]={1,2,3,4,5,6,7,8,9,10}; for(i=0;i<5;i++) a[i]=i+2; for(i=9;i>=0;i--) if(a[i]%3==0) printf("%d\n",a[i]); return 0; } 19. #include int main() { int num=13,len=0,i=0,a[30]; do { a[len]=num%2; }while(num); for(i=0;i int main() { int a[5]={6,9,12,16}; int x,i=3; scanf("%d",&x); while(i>=0 && x

  num=num/2;

  len++;

  } a[i+1]=x; for(i=0;i<5;i++) printf("%5d",a[i]); printf("\n"); return 0; } 輸入:10<回車> 21. #include int main() { int a[3][3]={{2,3},{4,5,6},{7,8}}; int i=0,j=0,m; m=a[0][0]; for(i=0;i<3;i++) for(j=0;j<3;j++) if(a[i][j] #include int main() { int i; char str[20]="abcdef"; strcpy(str,"opqrst"); str[5]='\0'; for(i=0;i #include int main() { char a[10]="AB",b[10]="LMNP"; int i=0; while(b[i]) { a[i]=b[i]; i++; }

  a[i]='\0'; puts(b); return 0; } 24. #include #include int main() { int i; char str1[30]="abc",str2[3][5]={"defg","hi\0j","klm"}; for(i=1;i<3;i++) strcat(str1,str2[i]); puts(str1); return 0; } 25. #include int main() { int i; char a[] = "Time", b[] = "Tom"; for(i = 0; a[i] != '\0' && b[i] != '\0'; i++){ if (a[i] == b[i]) if (a[i] >= 'a' && a[i] <= 'z') printf("%c", a[i] - 32); else printf("%c", a[i] + 32); else printf("*"); } return 0; } 26. #include int main() { char a[4][10] = {"1234", "abcd", "xyz", "ijkm"}; int i = 3; char (*p)[10] = a; printf("output string: \n"); for (p = a; p < a + 4; p++, i--) printf("%c", *(*p + i)); return 0; } 27. #include #include int main() {

  char str1*30+, str2*+ = “your book”; scanf(“%s”, str1); strcpy(str1, str2); printf(“\n %s\n %d\n”, str1, strlen(str2)); return 0; } 輸入:you are a student<回車> 28. #include #include int main() , char s1*10+=“java”,s2*10+=“basic”,s*10+; if(strcmp(s1,s2)) { strcpy(s,s1); puts(s);} printf(“%d,%s\n”,strlen(s2),strcat(s2,s1)); return 0; } 29. #include < stdio.h > #include < string.h > int main () { char *p = “abcdefgh” , c*10+ = , “XYZ” - ; p += 3 ; puts ( strcat ( c , p ) ) ; printf ( “%d\n” , strlen ( c ) ) ; return 0; } 30. #include int main() { int a[6][6], i, j; for(i=1;i<6;i++) for(j=1;j<6;j++) a[i][j] = (i/j)*(j/i); for(i=1;i<6;i++) { for(j=1;j<6;j++) printf("%2d",a[i][j]); printf("\n"); } return 0; } 31. #include int main() { int i, f[10];

  f[0]= f[1] = 1; for(i=2;i<10;i++) f[i] = f[i-2] + f[i-1]; for(i=0;i<10;i++){ if(i%4 == 0) printf("\n"); printf("%3d",f[i]); } return 0; } 32. #include #include int main() { char a[]="morming", t; int i, j=0; for(i=1;i<7;i++) if(a[j] #include void main() { char s*12+=“abcdef”; scanf(“%s”,s); strcat(s,”xyz”); printf(“%s\n”,s); } 34. #include void main() { int a[3][3]={{3,4},{5,6},{7,8}}; int i,j,t=0; for(i=1;i<3;i++) for(j=0;j void main( ) {

  int i,t,a[5]={1,2,3,4,5}; t=a[0]; for(i=1;i<5;i++) a[i-1]=a[i]; a[4]=t; for(i=0;i<5;i++) printf("%d",a[i]); printf("\n"); } 36. #include void mystrcpy( char s1[ ] , char s2[ ]); void main( ) { char a[50]="I am a teacher."; char b[]="You are a student."; printf( " a = %s \n" , a ); mystrcpy( a , b ); printf( " a = %s \n" , a ); } void mystrcpy( char s1[ ] , char s2[ ]) { int i = 0 ; while( s2[i] != '\0' ) { s1[i] = s2[i]; i++; } s1[i] = '\0' ; } 37. #include void main() { int i,j,k=0,a[3][3]={1,2,3,4,5,6}; for(i=0;i<3;i++) for(j=i;j<3;j++) k=k+a[i][j]; printf(“??%d”??,k); } 38. #include void main() { int i,j=3,a[ ]={1,2,3,4,5,6,7,8,9,10};

  for(i=0;i<5;i++) a[i]=i*(i+1); for(i=0;i<4;i++) j+=a[i]*3; }

  printf("%d\n",j);

  39. #include void main() { char c, string[81]= "How are you?"; int i, n=0, k=0; for(i=0; c=string[i]; i++) { if(c==' ') k=0; else if(k == 0) k = 1; n ++; } } printf("%d\n" , n ); } 40. #include #include void main( ) { char s*16+=“12345\0\t\t\t”; printf(“%d,%d\n”,strlen(s),sizeof(s)); }

  {

  解答: 1. m=12 2. 1 1 2 2 4 3 5 4 6 5 0 6 0 0 0 0 0 0 0 0

  3. CDG 4. Hello Teachers,Students. 5. The array has been reverted. 2,4,5,7,6,0,11,9,7,3,

  6. please input rectangle element: duijiaoxian he is 15.00 7. 1 3 4 5 8. sum=114 9. 22121 10. 0,11,9,7,3, 11. 5,4,3,5,4, 12. 26,25,34,41,52 13. ZhongGong Gong 14. 0,10,20 15. 18,11,4 16. J 17. 2027 18. 9 6 6 3 19. 1101 20. 21. m=0 22. opqr 23. LMNP 24. abchiklm 6 9 10 12 16

  25. t*M 26. 4cyi 27. your book 9 28. java 9,basicjava 29. XYZdefgh 8 30. 1 0 0 0 0 01000 00100 00010 00001 31. 1 1 2 3 5 8 13 21 32. mo 33. hijxyz 34. t=20 35. 23451 36. a= I am a teacher. a= You are a student. 37. 17 38. 63 39. 3 40. 5,16 三.補充程式 1. 求一個 3*3 矩陣對角線元素之和。 #include "stdio.h"

  int main() { float a[3][3],sum=0; int i,j; printf("please input rectangle element:\n"); for(i=0; ________________________;i++) for(j=0;j<3;j++) scanf("%f",________________________); for(i=0; ________________________;i++) ________________________; printf("duijiaoxian he is %6.2f",sum); return 0; } 2. 將一個數組逆序輸出. #include "stdio.h" #define N 5 int main() { int a[N]={9,6,5,4,1},i,temp; printf("\n original array:\n"); for(i=0;i

  for(j=0;j<5;j++) { scanf("%d",________________________); s=s+a[j][i]; } v[i]=s/5.0; ________________________; } l=(v[0]+v[1]+v[2])/3.0; printf("math:%f\nc ________________________,v[2 ]); printf("total:%f\n",l); return 0; }

  languag:%f\ndbase:%f\n",v[0],

  4. 將字串 st2 內容連線到串 st1 中,並計算串 st1 字元長度。 ________________________ #include "stdio.h" int main() { int len; char st1[30]="My name is "; char st2[10]; printf("input string st2 name:\n"); gets(________________________); ________________________; len=________________________; puts(st1); printf(“串 st1 字元長度%d\n”,len); return 0; } 5. 將十進位制整數 base 轉換成 2 進位制。 #include "stdio.h" int main ( ) { int i=0,base,j,num[20] ; scanf(“%d”,&base) ; do { num[i]= ________________________; base=________________________; i++; } while (base!=0); for (________________________;________________________ ;j--) printf(“%d”,num*j+) ; return 0; }

  6. 輸入一個 2×3 矩陣,並 3 行 2 列輸出矩陣。 #include int main() { int i,j; float s[2][3]; printf(“please input 6 numbers:\n”); for(i=0; ________________________;i++) for(j=0;j<3;j++) ________________________; printf(“\n the final matrix is:\n”); for(i=0;i<3;i++) { ________________________ printf("%f",s[j][i]); ________________________ } return 0; } 7. 從鍵盤輸入 10 個學生的成績,求平均分並輸出大於平均分的所有成績。#include int main( ) { int i; ________________________ float a=0.0; printf (" Input 10 student's scores:\n "); for(i=0; i<10; i++) ________________________; for(i=0; i<10; i++) a=a+s[i]; ________________________; printf("average=%2.1f \n scores(> average):\n",a); for(i=0; i<10; i++) if (s[i]>a) ________________________; return 0; } 8. 將一個 2 行 3 列陣列 a 的元素,存到一個 3 行 2 列二維陣列 b 中,並輸出陣列 a,b. #include int main ( ) { int a[2] [3]={{1, 2,3},{4, 5,6}}; int b[3] [2], i, j;

  printf("array a: \n"); for (i=0; i<=1; i++) { for (j=0; j<=2; j++) { printf("%5d", a[i][j]); ________________________; } printf("\n"); } printf("array b:\n"); for (i=0; ________________________; i++) { for (j=0; ________________________; j++) printf("%5d", b[i][j]); ________________________; } return 0; } 9. 計算矩陣上三角陣(不包括對角線元素)各元素之和,並 8 行 2 列輸出矩陣 a. #include int main() { int a[4][4]={{2,3,4},{5,6,7},{8,9,1}}; int i,j,t=0; for(I=0;i<4;i++) for(____________________; ________________________;j++) t+=________________________; printf("t=%d\n",t); for(i=0;i<4;i++) { for(j=0;j<4;j++) { printf(“%3d”, a*i+*j+); if((j+1)%2==0)printf(“\n”); } ________________________; } return 0; } 10. 計算和儲存數列前 20 項,每行輸出 5 項。 #include "stdio.h" int main() { ________________________;

  int i; a[0]=a[1]=1; for(________________________;i<20;i++) a[i]=a[i-1]+a[i-2]; for(i=0; ________________________;i++) { printf(“%6d”,a*i+); ________________________; } return 0; } 解答: 1. i<3 &a[i][j] i<3 sum+=a[i][j] 2. a[i] i

  5. base%2; base/2; j=i-1 j>=0 6. i<2 scanf("%f",&s[i][j] for(j=0;j<2;j++) printf("\n"); 7. int s[10]; scanf("%d",&s[i]); a=a/10; printf("%5d",s[i]); 8. b[j][i]=a[i][j]; i<3 j<2 printf("\n"); 9. j=i+1 j<4 a[i][j] if((i+1)%2==0)printf("\n"); 10. int a[20] i=2

  i<20 if((i+1)%5==0)printf("\n"); 四.改錯題 解答: 五.程式設計一 解答: 六.程式設計二 解答: 第八章 結構體 一.填空 1. 引用結構體變數成員的一般形式是:結構體變數名__________。 2. 定義列舉型別的關鍵字是__________。 3. 設 char 、int、float 分別佔 1、2、4 個位元組,假設有如下定義: struct student { char name[10]; int nl; float ave; }qq; 則變數 qq 在記憶體中所佔的位元組數是__________。 4. 設 char 、int、float 分別佔 1、2、4 個位元組,假設有如下定義: union student { char name[10]; int nl; float ave; }qq; 則變數 qq 在記憶體中所佔的位元組數是__________。 5. 設 char 、int、float 分別佔 1、2、4 個位元組,假設有如下定義: union stu { char a; int b; float c; }; struct { float c; union stu x; } y;

  那麼變數 y 佔用的儲存空間分別為__________個位元組。 6. 設有定義 struct date {

  int day ; char month ; int year ; } dd , *pd = &dd ;

  請寫出:使用指標變數 pd 引用結構體成員 dd .day 的另一種描述形式__________。 7. 結構體變數各成員佔用的記憶體單元起始地址__________。 (填“相同”或“不同” ) 8. 共用體變數各成員佔用的記憶體單元起始地址__________。 (填“相同”或“不同” ) 9. 設 char 、int、float 分別佔 1、2、4 個位元組,假設有如下定義: union stud_type { char name[5]; int num; float score; }]; union stud_type stu[10]]; 則程式執行時,系統將為 stu 分配__________個位元組的記憶體空間。 10. 設 char 、int、float 分別佔 1、2、4 個位元組,假設有如下定義: struct stud_type { char name[5]; int num; float score; }; struct stud_type stu[10]; 則程式執行時,系統將為 stu 分配__________個位元組的記憶體空間。 11. 設 char、float 和 double 型資料佔用的記憶體空間位元組數分別是 1、4 和 8,則變數 stu1 在 內 存中佔用的位元組數是__________。 struct stud_type { char name[10]; float score[5]; double average; }stu1; 12. 寫出一個單鏈表結點的結構體型別定義: 結點名為 worker, 分量 num 為整型, 分量 next

  為 指向下一結點的指標________________________________________。 13. 寫出一個貨品資訊的結構體型別定義:結點名為 goods_type,第一個分量貨品號 num 為整 型,第二個分量貨品名 name 為長度為 20 的字元型陣列,第三個貨品庫存量 amount 為整 型。__ ______________________________________。 14. 在 C 語言中,可以給某個已有的資料型別重新命名。將 int 型重新命名為 INTEGER,應 該表 示為______________________________。 15. 在 C 語言中,給某個已有的資料型別重新命名的形式為:__________ 型別名 16. 結構體變數成員的引用方式是使用__________運算子。 17. 結構體指標變數成員的引用方式是使用__________運算子。 18. 設 struct student{ int no;char name[12];float score[3]; } sl,* p = &sl;用指標 法給 sl 的成員 no 賦值 1234 的語句是____________________。 19. typedef 的功能是____________________。 20. 設 char 、int 分別佔 1、2 個位元組,假設有如下定義: union student { int n;char a[100]; } b; 則 sizeof(b)的值是__________。 解答: 1. ?成員名 2. enum 3. 16 4. 10 5. 8 6. pd->day 或(*pd).day 7. 不同 8. 相同 新名稱。

  9. 50 10. 110 11. 38 12. struct worker { int num; struct worker *next; }; 13. struct goods_type { int num; char name[20];int amount;}; 14. typedef int INTEGER; 15. typedef 16. ? 17. -> 18. p->no=1234 或(*p).no=1234 19. 給某個已有的資料型別重新命名 20. 100 二.閱讀程式寫結果 1. #include struct stri_type { char ch1; char ch2; { int a; int b; }ins; }; int main() { struct stri_type ci; ci.ch1='a'; ci.ch2='A'; ci.ins.a=ci.ch1+ci.ch2; printf("%d,%c\n",ci.ins.a,ci.ins.b); return 0; } 2. #include struct stud_type

  struct

  ci.ins.b=ci.ins.a-ci.ch1;

  { char num[11]; char name[11]; float score[3]; float average; }; int main() { struct stud_type stu={"200601","wang",80.0,85.0,70.0}; stu.average=(stu.score[0]+stu.score[1]+stu.score[2])/3; printf("average=%5.2f\n",stu.average); return 0; } 3. #include union out { int a[2]; struct { int b; int c; }in; int d; }; int main() { union out e; int i; e.in.b=1; e.in.c=2; printf("%5d",e.a[i]); printf("\n"); return 0; }

  e.d=3;

  for(i=0;i<2;i++)

  4. #include #define PI 3.14 struct cir_type { float r; double area; }; int main() { struct cir_type cir={5.0},*p=&cir; p->area=PI*p->r*p->r; printf("area=%.3lf\n",p->area);

  return 0; } 5. #include struct int_char { int i; char ch; }; int main() { struct int_char x={9,'z'}; printf("%d\t%5c\n",x.i+20,x.ch-2); return 0; } 6. #include int main() { struct data { int m; int n; union { int y; int z; }da; }; struct data intdata; intdata.m=3;intdata.n=6; intdata.da.y=intdata.m+intdata.n; intdata.da.z=intdata.m-intdata.n; printf("%5d%5d\n",intdata.da.y,intdata.da.z); return 0; } 7. #include int main() { int i; float sum_average=0; { int num; char name[10]; int score[2]; float average; };

  struct student

  struct student data[2]={{1,"zhangsan",60,70},{2,"Lisi",89,90}}; for(i=0;i<2;i++) { data[i].average=(data[i].score[0]+data[i].score[1])/2.0; sum_average=sum_average+data[i].average; } sum_average=sum_average/2; for(i=0;i<2;i++) if(sum_average>data[i].average) printf("%5d%10s%5.2f",data[i].num,data[i].name,data[i].average); return 0; } 8. #include struct int_data { int d1,d2;}; int main() { struct int_data data[2]={{2,3},{5,6}}; int i; int sum=10; for(i=0;i<2;i++) { scanf("%d%d",&data[i].d1,&data[i].d2); sum=data[i].d1+data[i].d2+sum; } printf("sum=%d \n",sum); return 0; } 輸入:20 30 40 50<回車> 9. #include int main( ) { union exa { struct { int a; int b; }out; int c; int d; }e; e.out.a=10; e.out.b); return 0; }

  e.out.b=20;

  e.c=2;

  e.d=5;

  printf("%d,%d\n",e.out.a,

  10. #include struct st { int a; int b; union { int e; int f; }c; }; int main( ) { struct st y; y.a=10; y.c.e=20; y.c.f=y.a; y.b=y.c.e; printf("%d,%d\n",y.c.e,y.c.f); return 0; } 11. #include struct sta { int a; char b; }; struct stb { int a; char b; struct sta c; }; int main( ) { struct stb y; y.a=10; y.b='X'; y.c.a=y.a*2; y.c.b=y.b--; printf("%d,%c\n",y.c.a,y.c.b); return 0; }

  12. #include union st { int a[2]; int b[2]; int c; }; int main( ) { union st y; y.a[0]=10; y.c=30; return 0;

  y.b[1]=20; printf("%d,%d\n",y.a[0],y.a[1]); }

  13. #include struct two { int n; char ch; }; void main( ) { struct two ex1={5,'t'}; printf("%d,%c\n",ex1.n+10,ex1.ch-1); }

  14. #include struct two { int n; char ch; }; void func(struct two ex2); void main() { struct two ex1={6,'v'};

  func(ex1); printf("%d,%c\n",ex1.n,ex1.ch); getchar(); } void func(struct two ex2) { ex2.n= ex2.n +20; ex2.ch= ex2.ch -1; } 15. #include union exa{ struct{ int a; int b; }out; int c; int d; }; void main() { union exa e; e.c=1; e.d=3; e.out.a=e.c; e.out.b=e.d; printf("%d,%d\n",e.out.a,e.out.b); } 16. #include struct stu { int a; int b; struct poi { int x; int y; }ins; }; void main() { struct stu outs; outs.a=11; outs.b=4; outs.ins.x=outs.a+outs.b; outs.ins.y=outs.a-outs.b; printf("%d,%d", outs.ins.x, outs.ins.y); } 17. #include

  struct abc { int a, b, c; }; int main() { struct abc s[2] = {{1,2,3},{4,5,6}}; int t; t = s[0].a + s[1].c; printf("t=%d \n",t); return 0; } 18. #include struct st { int x, y; } data[2] = {1, 10, 2, 20}; int main() { struct st *p = data ; printf("%d\n", p->y ) ; printf("%d\n", (++p)->x ) ; return 0 ; } 19. #include struct n { int x; char c; }; void func(struct n b) { b.x = 20; b.c = 'y'; } int main() { struct n a = {10, 'x'}; func(a); printf("%d,%c", a.x, a.c); return 0; }

  20. #include int main() { struct EXAMPLE { struct { int x; int y; } in; int a; int b; } e; e.a = 1; e.b = 2; e.in.x = e.a * e.b; e.in.y = e.a + e.b; printf("%d,%d", e.in.x, e.in.y); return 0; }

  解答: 1. 162,A 2. average=78.33 3. 3 2

  4. area=78.500 5. 29 6. 7. -3 -3 x

  1 zhangsan65.00

  8. sum=150

  9. 5,20 10. 10,10 11. 20,X 12. 30,20 13. 15,s 14. 6,v 15. 3,3 16. 15,7 17. t=7 18. 10 2 19. 10,x 20. 2,3 三.補充程式 1. 該程式完成的功能是從螢幕上輸入日期(年、月、日) ,計算其為該年的第幾天。 #include struct date_type { int year; int month; int day; }; int main() { int days=0,i; struct date_type date; int mon_day[12]={31,28,31,30,31,30,31,31,30,31,30,31}; printf("請輸入日期(年 月 日):\n"); scanf("%d%d%d",_____________________________________________); for(i=1;i=3) ____________________________;

  printf(" 所輸入日期是該年第%d 天\n",_________________________); return 0; } 2. 該程式完成的功能是從螢幕上輸入日期(年、月、日) ,計算其為該年的第幾天。 #include struct date_type { int year; int month; int day; }; int main() { int days=0,i; ___________________________; int mon_day[12]={31,28,31,30,31,30,31,31,30,31,30,31}; printf("請輸入日期(年 月 日):\n"); scanf("%d%d%d",&date.year,&date.month,&date.day); for(i=1;________________;i++) days=mon_day[i-1]+days; days=days+date.day; if((date.year%4==0&&date.year%100!=0||date.year%400==0)&&_______________ _) days=days+1; printf(" 所輸入日期是該年第%d 天\n",_______________); return 0; } 3. 該程式完成的功能是從螢幕上輸入日期(年、月、日) ,計算其為該年的第幾天。 #include struct date_type { int year; int month; int day; }; int main() { int days=0,i; struct date_type date; int mon_day[12]={31,28,31,30,31,30,31,31,30,31,30,31}; printf("請輸入日期(年 月 日):\n"); scanf("%d%d%d",_________________________________); for(i=1;____________________;i++) days=mon_day[i-1]+days; days=days+date.day; if((date.year%4==0&&date.year%100!=0||___________________)&&date.month>=3)

  days=days+1; printf(" 所輸入日期是該年第%d 天\n",______________________); return 0; } 4. 該程式實現從鍵盤輸入 1 名職工的資訊:職工號、姓名、月工資。由 print 函式完成這名 職工資訊的輸出。 #include struct employee_type { char num[20]; char name[20]; float pay; }; void print(struct employee_type emp1); int main() { struct employee_type emp; printf("請輸入職工資訊(職工號 姓名 月工資):\n"); scanf("%s%s%f",____________________________); print(_____________________); return 0; } void print(__________________________) { printf("****職工號 姓名 月工資****\n"); printf("%8s%8s%10.2f\n",_____________________________); } 5. 該程式完成從鍵盤輸入 10 名學生資訊(學號、姓名、成績),用一個排序函式 sort 完成 按 學生成績降序排列,並輸出學生成績排行榜。 #include #define N 10 struct stud_type { char name[10]; int num; float score; }; void sort(struct stud_type stu[], int n); int main() { int i; struct stud_type stu[N]; printf("Please input %d student’s name num score:\n",N); for(i=0;i

  sort(stu,N); printf("The sort is:\n"); for(i=0;i #define N 10 struct stud_type { char name[10]; int num; float score; }; void sort(struct stud_type stu[], int n); int main() { int i; struct stud_type stu[N]; printf("Please input %d student’s name num score:\n",N); for(i=0;i

  { int i,k,j; struct stud_type t ; for(i=0;i #define N 10 struct stud_type { char name[10]; int num; float score; }; void sort(struct stud_type stu[], int n); int main() { int i; ________________________; printf("Please input %d student’s name num score:\n",N); for(i=0;i

  ____________; if (k!=i) { t=stu[i]; stu[i]=stu[k]; stu[k]=t; } } } 8. 該程式完成從鍵盤輸入 10 名學生資訊(學號、姓名、成績),用一個排序函式 sort 完成 按 學生成績降序排列,並輸出學生成績排行榜。 #include #define N 10 struct stud_type { char name[10]; int num; float score; }; void sort(struct stud_type stu[], int n); int main() { int i; struct stud_type stu[N]; printf("Please input %d student’s name num score:\n",N); for(i=0;i

  } } 9. 該程式完成從鍵盤輸入 10 名學生資訊(學號、姓名、成績),用一個排序函式 sort 完成 按 學生成績降序排列,並輸出學生成績排行榜。 #include #define N 10 struct stud_type { char name[10]; int num; float score; }; void sort(struct stud_type stu[], int n); int main() { int i; _______________________; printf("Please input %d student’s name num score:\n",N); for(i=0;i int main()

  { enum week_type { mon=1,tue,wed,thu,fri,sta,sun }workday; int i; do { printf("Please input integer(end:-1)\n"); scanf("%d",________________); workday=(________________)i; switch(________________) { case sun:printf("Sunday\n"); break; case mon:printf("Monday\n"); break; case tue:printf("Tuesday\n"); break; case wed:printf("Wednesday\n"); break; case thu:printf("Thursday\n"); break; case fri:printf("Friday\n"); break; case sta:printf("Saturday\n"); break; default:printf("Input error\n"); break; case -1:printf("Goodbye!\n"); } }while(________________); return 0; } 解答: 1. &date.year,&date.month,&date.day days=mon_day[i-1]+days days=days+1 days

  2. struct date_type date i=3 days 3. &date.year,&date.month,&date.day i

  7. struct stud_type stu[N] struct stud_type stu[], int n

  k=i k=j 8. stu[i].name ,&stu[i].num,&stu[i].score struct stud_type stu[],int n struct stud_type t k!=i 9. struct stud_type stu[N] stu[i].name,stu[i].num,stu[i].score k=j stu[k]=t 10. &i enum week_type workday i!=-1

  四.改錯題 解答: 五.程式設計一 解答: 六.程式設計二 解答: 第九章 指標 一.填空 1. 假定在一維陣列 b[10]中,元素 b[5]的指標為 p,則 p+4 所指向的元素為__________。 2. 指標變數是一種專門存放__________的變數。

  3. 函式中的形參和呼叫時的實參都是變數時,傳遞方式為值傳遞;形參和實參都是陣列名 時,傳遞方式為__________。 4. 設有陣列 a[10];則陣列元素 a[3]的地址可以寫成__________。 5. 定義 int b[10]; 若元素 b[5]的指標為 p1,元素 b[9]的指標為 p2,則 p2- p1 的值為______ ____。 6. int *p, a; 則語句 p=&a; 中的運算子&是__________符號。 7. 設 int a[]={0,1,2,3,4,5,6,7,8,9},*p=a,則*p+4= __________。 8. 定義 int a[10]={11,12,13,14,15}, *p=&a[5]; 假設&a[0]和&a[1]的值分別是 2000 和 200 2,則 p 的值是__________。 9. 對變數的訪問方式有直接訪問和間接訪問兩種。其中,將變數 a 的地址存放在另外一個 變 量 p 中,訪問時先從 p 中取出變數 a 的地址,再按照 a 的地址訪問 a 的值,該方式稱為 __________ 訪問。 10. int *p 的含義是____________________。 11. 定義 int a[10]={11,12,13,14,15}, *p=&a[5]; 則表示式 a[0]+*p 的值是__________。 12. 定義 int a[10]={1,3,5,7,9,11,13,15,17,19},*p=a;執行語句 printf("%d",*p+7);輸出 結果是__________。 13. 定義 int m=5,n=9,t,*p=&m,*q=&n;順序執行語句 t=*p;*p=*q;*q=t;此時 m 的值是 _______ ___。 14. 已知 int a[5] = { 2, 3, 4, 5, 6 }; int *p = a+2; 則表示式 *p*a[3] 的值是_____ _____。 15. 假定在一維陣列 array[10]中,元素 array[5]的指標為 p,則 p-4 所指向的元素為________ __。 16. 陣列名代表的是____________________。 17. 若 int a[3][3]={1,2,3,4,5,6,7,8,9};則*(*(a+2)+1)的值是__________。 18. 若 int a[ ][3]={1,2,3,4,5,6,7,8,9};則*(*a+1)的值是__________。 19. 若有 int a[3]={10,12,30};則 a+1 是陣列元素__________的地址。

  20. 函式呼叫時引數有值傳遞和__________傳遞兩種引數傳遞方式。 解答: 1. b[9] 2. 地址 3. 地址傳遞 4. &a[3]或 a+3 5. 4 6. 取地址 7. 4 8. 2010 9. 間接 10. 定義指標變數 p,p 指向整型變數。 11. 11 12. 8 13. 9 14. 20 15. array[1] 16. 陣列元素的首地址 17. 8 18. 2 19. a[1] 20. 地址

  二.閱讀程式寫結果 1. #include int main() { int i=0,a[5]={11,12,13},*q; for(q=a;q void fun(int *p); int main() { int a[5]={1,2,3,4,5},*r=a; fun(r); printf("%d\n",*r); return 0; } void fun(int *p) { p=p+2; printf("%d\n",*p); } 3. #include int main() { int a[3][3]={0,1,2,3,4,5,6,7,8}; int *p,(*q)[3]; int i=0; for(q=a;q #include int main() { char str[20]="I am a student.",*p=str; char *q="You are a teacher.";

  p=p+7; puts(str); return 0; }

  q=q+10;

  strcpy(p,q);

  5. #include void swap(int x, int y) { int z; z=x; x=y; y=z; } void pswap(int *x, int *y) { int z; z=*x; *x=*y; *y=z; } void main() { int a=3, b=2; printf("first:a=%d, b=%d \n", a, b); swap(a,b); printf("second:a=%d,b=%d\n", a,b); pswap(&a,&b); printf("third:a=%d,b=%d",a,b); } 6. #include void f(int a, int b, int *c, int *d) { a=30; b=40; *c=a+b; *d=*d-a; } void main() { int a=10,b=20,c=30,d=40; f(a,b,&c,&d); printf("%d,%d,%d,%d",a,b,c,d); } 7. #include void ast(int *cp, int *dp) { int x=4,y=3; *cp=++x+y;

  *dp=x-y; } void main() { int c, d; ast(&c,&d); printf("%d\n%d\n", c, d); } 8. #include void main() { char s[]="ABCD",*p; for(p=s+1;p void main() { char *p="I am a student."; p=p+3; printf("%s",p); } 10. #include void main( ) { char *p="This is a programe."; p=p+10; printf("%s",p); } 11. #include void add(int *p, int n) { int *pend = p + n; for( ; p

  12. #include int f(int x,int *y); void main() { int a,b,c; a=3; b=5; c=f(a,&b); printf("%d,%d,%d\n",a,b,c); } int f(int x,int *y) { int a=2; x=x+a; *y=x+a; return(a); } 13. #include void fun (int x , int *y ); int main() { int a = 2 , b = 3 , *c = &b; fun(a, c); printf("a=%d, b=%d, c=%d\n", a, b, *c); return 0; } void fun( int x , int *y ) { int a = 4; *y = x + a; x = *y; } 14. #include void sub(int x, int y, int *z) { *z = y - x; } int main() { int a, b, c; sub(10, 5, &a); sub(7, a, &b); sub(a, b, &c); printf(" % 4d, % 4d, % 4d\n", a, b, c); return 0; }

  15. #include int main() { char a[] = "language", *p; p = a; while (*p != 'u') { printf(" % c", *p - 32); p++; } return 0; } 16. #include int stre(char str[]) { int num = 0; while(*(str + num) != '\0') num++; return num; } int main() { char str[10], *p = str; gets(p); printf(" %d\n", stre(p)); return 0; } 輸入:happy<回車> 17. #include void f(int x, int *y) { x=x%10; *y=*y/10+x; } int main() { int a=88,b=99,*c; c=&b; f(a,c); printf("%d,%d\n",a,b); return 0; } 18. #include void main() { char a[][7]={ "Teacher","Tom" , "Good"};

  char *p=a[0]; p=p+3; printf("%s\n%s\n", a[0],p); } 19. #include void main() { char a[][7]={ "Teacher","Tom" , "Good"}; char (*p)[7]=a; printf("%s\n%s\n", a[0],++p); } 20. #include void ast(int x, int y, int *a, int *b) { *a=x+y; *b=*a-y; } void main() { int a, b, c, d; a=10; b=20; ast(a,b,&c,&d); printf("%d,%d\n", c, d); } 解答: 1. 16 2. 3 1 3. 0 3 6 4 7 8

  17

  18

  5

  5

  4. I am a teacher. 5. first:a=3, b=2 second:a=3,b=2 third:a=2,b=3 6. 10,20,70,10 7. 8

  2 8. BCD CD D 9. m a student. 10. programe. 11. 11 12 13 14 15

  12. 3,7,2 13. a=2, b=6, c=6 14. -5, -12, -7

  15. L A N G 16. 5 17. 88,17 18. TeacherTom cherTom 19. TeacherTom Tom 20. 30,10 三.補充程式 1. 該程式實現輸入兩個整數,按先小後大的順序輸出,呼叫函式實現交換位置。 #include void swap ( int *x, int *y ); int main ( ) { int x, y; printf ( "請輸入兩個數(空格分隔):" ); scanf ( "%d %d", &x, &y ); if ( x > y ) swap ( _________ ); printf ( " %4d %4d \n", x, y );

  return 0; } void swap ( int *p1, int *p2 ) { int p; _________; _________; _________; } 2. 該程式實現輸入兩個整數,按先小後大的順序輸出,呼叫函式實現交換位置。 #include void swap ( int *x, int *y ); int main ( ) { int x, y; printf ( "請輸入兩個數(空格分隔):" ); scanf ( "%d %d", &x, &y ); if (_________ ) swap ( &x, &y ); printf ( " %4d %4d \n", ______________ ); return 0; } void swap ( _____________) { _________; p = *p1; *p1 = *p2; *p2 = p; } 3. 該程式實現輸入兩個整數,按先小後大的順序輸出,呼叫函式實現交換位置。 #include void swap ( _______________ ); int main ( ) { int x, y; printf ( "請輸入兩個數(空格分隔):" ); scanf ( "%d %d", __________________ ); if ( x > y ) swap ( ______________ ); printf ( " %4d %4d \n", x, y ); return 0; } void swap ( int *p1, int *p2 )

  { _________________ ; p = *p1; *p1 = *p2; *p2 = p; } 4. 該程式實現輸入兩個整數,按先小後大的順序輸出,呼叫函式實現交換位置。 #include void swap ( _______________ ); int main ( ) { int x, y; printf ( "請輸入兩個數(空格分隔):" ); scanf ( "%d %d", &x, &y ); if (__________) swap ( &x, &y ); printf ( " %4d %4d \n",________________); return 0; } void swap ( int *p1, int *p2 ) { int p; ___________; *p1 = *p2; *p2 = p; } 5. 該程式實現輸入兩個整數,按先小後大的順序輸出,呼叫函式實現交換位置。 #include void swap ( int *x, int *y ); int main ( ) { int x, y; printf ( "請輸入兩個數(空格分隔):" ); scanf ( "%d %d", &x, &y ); if ( x > y ) swap (__________); printf ( " %4d %4d \n",_____________ ); return 0; } void swap ( int *p1, int *p2 ) { int p; p = *p1; _________;

  _________; } 6. 利用指標變數自增的方式輸出一維陣列 b 中的所有元素。 #include #define SIZE 3 int main() { int i, b[]={1,2,3}; int *p; for (_______;________; _________) printf("*p=%d\n",__________); }

  7. 輸入一個八進位制數並轉換為十進位制數。八進位制轉換為十進位制數的方法是按權相加,即將 八進位制每位上的數乘以位權, 然後相加之和即是十進位制數。 利用陣列存放八進位制數的各位數 碼。 #include #define N 6 int main () { int n, temp ; char s [ N ], * p = s; printf ( "請輸入八進位制數:" ); gets (_____); for ( _________; (* p ) != ' \0 '; _________) { temp = * p - '0'; n = n * 8 + temp; } printf ( "對應的十進位制數:%d\n", _______________ ); return 0; } 8. 輸入一個八進位制數並轉換為十進位制數。八進位制轉換為十進位制數的方法是按權相加,即將 八進位制每位上的數乘以位權, 然後相加之和即是十進位制數。 利用陣列存放八進位制數的各位數 碼。 #include #define N 6 int main () {

  int n, temp ; char s [ N ], * p = s; printf ( "請輸入八進位制數:" ); gets ( _________ ); for ( n = 0; ______________; p ++ ) { temp = * p - '0'; _____________; } printf ( "對應的十進位制數:%d\n", ________ ); return 0; } 9. 判斷一個字串是否為迴文(指從左至右和從右至左讀都是一樣的字串) 。 #include #define N 11 int main ( ) { int i = 0, n = 0; char a [ N ] = " "; char * p1, * p2; printf ( "請輸入小於%d 位的整數: ",N-1 ); scanf ( "%s", a ); for (_______; (*p2) != ' \0 '; p2 ++ ) n ++; p1 = a; p2 = a+n-1; for ( i = 1; p1 < p2; i ++ ) { if (__________ ) break; ___________; ___________; } if ( i > n / 2 ) printf ( "Yes, %s 是迴文!\n", a ); else printf ( "No, %s 不是迴文!\n", a ); return 0 ; } 10. 判斷一個字串是否為迴文(指從左至右和從右至左讀都是一樣的字串) 。 #include #define N 11 int main ( ) { int i = 0, n = 0; char a [ N ] = " "; char * p1, * p2; printf ( "請輸入小於%d 位的整數: ",N-1 );

  scanf ( "%s", a ); for (p2=a; (*p2) != ' \0 '; p2 ++ ) ________; p1 = a; p2 = a+n-1; for ( i = 1; p1 < p2; i ++ ) { if (*p1!=*p2) ________; _________; _________; } if ( i > n / 2 ) printf ( "Yes, %s 是迴文!\n", a ); else printf ( "No, %s 不是迴文!\n", a ); return 0 ; } 解答: 1. &x, &y p = *p1 *p1 = *p2 *p2 = p 2. x > y x, y int *p1, int *p2 int p 3. int *x, int *y &x, &y &x, &y int p 4. int *x, int *y x>y

  x, y p = *p1 5. &x, &y x, y *p1 = *p2 *p2 = p 6. p=b p

  break p1 ++ p2 -四.改錯題 解答: 五.程式設計一 解答: 六.程式設計二 解答: 第十章 檔案 一.填空 解答: 二.閱讀程式寫結果 解答: 三.補充程式 解答: 四.改錯題 1. 下列給定程式中,函式 fun()的功能是:從字串 s 中,取出所有大寫字母。程式的功能 是從檔案中讀出字串,進行如上處理後,輸出到螢幕上。 注意:不要改動 main 函式,不得增行或刪行,也不得更改程式的結構。 #include void fun(char s) { int i, j; for(i=j=0;s[i]!='\n';i++) if(s[i]>='A'|| s[i]<='Z') s[j]=s[i]; s[j]='\0'; } int main() { char s[80]; FILE *fp; if(fp=fopen("d:\\content.txt", "r")==NULL) { printf("Cannot open file\n Press any key to exit!"); return 1; } fgets(s,30,fp);

  fun(s); printf("The string after deleted: "); puts(s); printf("\n\n"); return 0; fclose(fp); }

  2. 下列給定程式中,函式 fun()的功能是:從字串 s 中,將大寫字母轉換成小寫字母。程式 的功能是從鍵盤讀入一個字串,進行如上處理後,寫入 D 盤上指定檔案中。 注意:不要改動 main 函式,不得增行或刪行,也不得更改程式的結構。 #include void fun(char s[]) { int i; for(i=0;s[i]!='\n';i++) if(s[i]>='A'&& s[i]<='Z') s[i]=s[i]+32; s[i]='\0'; } int main() { char *s; FILE fp; gets(s); if((fp=fopen("d:\\content.txt", "w"))=NULL) { printf("Cannot open file\n Press any key to exit!"); return 1; } fun(s); fputs(fp, s); printf("\n\n"); fclose(fp); return 0; } 3. 程式功能說明:該程式完成從文字檔案中讀入 10 名學生資訊(學號、姓名、成績),並用 格式輸出顯示到螢幕上。 #include #define N=10 struct stud_type { char name[10];

  int num; float score; } void main() { int i; struct stud_type stu[N]; FILE *fp; If((fp=fopen(''d:\\student.txt'', ''w'')) ==NULL) { printf(''Cannot open file\n Press any key to exit!''); getch(); } for(i=0;i #define N 10 struct stud_type { char *name; int num; float score; } void main() { int i; struct stud_type stu[N]; FILE *fp; If((fp=fopen(''d:\student.txt'', ''w'')) ==NULL) { printf(''Cannot open file\n Press any key to exit!''); getch(); } for(i=0;i

  請改正 。 #include #define N 10 struct student { char name[10]; int num; int age; char addr[15]; } s[N]; void save() int main() { int i; printf("please input student information:\n"); for(i=0;i

  6. 以下程式中有五處錯誤,請改正。 #include #include

  int main( ) { FILE fp; char ch; if((fp = fopen("d:\test.txt","w+")) == NULL) { printf("Cannot open file\n Press any key exit!"); getch( ); return 1; } printf("input a string:\n"); ch = getchar( ); while(ch != \n) { fputs(ch,fp); ch = getchar( ); } rewind(fp); ch = fgetc(fp); while(ch != "EOF") { putchar(ch); ch = fgetc(fp); } printf("\n"); fclose(fp); return 0; } 7. 以下程式中有五處錯誤,請改正。 #include #include int main( ) { FILE *fp; char ch; if(fp = fopen("d:\\test.txt","w+") == NULL) { printf("Cannot open file\n Press any key exit!"); getch( ); return 1; } printf("input a string:\n"); ch = getchar( );

  while(ch != ‘\0’) { fputc(fp, ch); ch = getchar( ); } rewind(fp); ch = fgetc(fp); while(ch !=end) { putchar(ch); ch = fgetc(fp); } printf("\n"); fclose( ); return 0; } 8. 將 d:\lang\tc 資料夾下的檔案 deposit.c 內容輸出到螢幕上。有 5 處錯誤,請改正。 #include #include int main( ) { char ch; FILE fp; if ((fp=fopen(“d:\\lang\\tc\\deposit.c”,”r”))=NULL) , printf(“Cannot open file \n”); break; } while ((ch=getchar())!=EOF) putchar(ch); fclose(ch); return 0; } 9. 將 d:\lang\tc 資料夾下的檔案 deposit.c 內容全部以小寫字母輸出到螢幕上。 #include #include void main( ) { char ch; FILE *fp; if ((fp=fopen("d:\lang\tc\deposit.c","r"))==NULL) { printf("Cannot open file \n"); return 1; } while (ch=fgetc(fp)!=EOF) {

  if ((ch>='A'||ch <='Z')) } fclose(fp); return 0;

  ch=ch+32;

  puts(ch);

  }

  10. 以下程式中使用者由鍵盤輸入一個檔名,然後輸入一串字元(用#結束輸入)存放到此 文 件檔案中形成文字檔案,並將字元的個數寫到檔案尾部。 #include #include void main() { FILE *fp; char ch,fname[32]; int count=0; printf("Input the filename :"); scanf("%s",&fname[32]); if ((fp=fopen("fname","w+"))==NULL) { printf("Can't open file:%s \n",fname); exit(0); } printf("Enter data:\n"); while ((ch=getchar())!="#") { fputc(fp, ch); count++; } fprintf("\n%d\n",count); fclose(fp); }

  11. 下面程式的功能是從 filename1 中將檔案複製到 filename2,其中原始檔和目標檔名均 從鍵盤輸入。有五處錯誤,請改正。 #include #include void main() { FILE *fp1,fp2; char ch,*fname1,*fname2; "); scanf("%s",fname1); if ((fp1=fopen(fname1,"r"))==NULL)

  printf("Input the source file name :

  { printf("Can't open source file:%s \n",fname1); exit(0); } printf("Input the targate file name :"); scanf("%s",fname2); if ((fp2=fopen(fname2,"w"))==NULL) { printf("Can't open targate file:%s \n",*fname2); exit(0); } while((ch=fgetc(*fp1))=EOF) fputc(ch,fp2); fclose(fp1); fclose(fp2); }

  12. 下面程式的功能是從 filename1 中將內容讀出,顯示在螢幕上。其中檔名由鍵盤輸入。 有五處錯誤,請改正。

  #include #include void main() { FILE fp1; char ch,*fname1; printf("Input the file name :"); scanf("%s",fname1); if ((fp1=fopen(fname1,’r’))==NULL) { printf("Can't open source file:%s \n",fname1); exit(0); } while(ch=fgetc(fp1)!=EOF) put(ch); fclose(fp1); }

  13. 下列程式中存在 5 處 C 語言語法錯誤,請識別並改正。程式的功能是在已建立的檔案 write string.txt 中追加一個字串,並將檔案整個內容輸出。 #include #include int main { FILE *fp; char ch,st[20]; if((fp=fopen("d:\\lang\\tc\\writestring.txt","w"))==NULL) { printf("Cannot open file strike any key exit!"); getchar(); return 1; } printf("input a string:\n"); scanf("%c",st); fgets(fp, st); rewind(); while(ch=fputc(fp)!=EOF) putchar(ch); printf("\n"); fclose(fp); return 0; }

  14. 以下程式的功能:把檔案 source.txt 的內容輸出到螢幕上。 (共 5 個錯誤點) #include #include int main() { char *fp; char ch; fp=fopen("source.txt", "w"); if(fp=NULL) { printf("\nCannot open file\nStrike any key exit!\n"); getchar(); exit(1); } ch=fgetc(fp);

  if(ch!=EOF) { printf("%c",ch); ch=fgetc(fp); } fclose(*fp); } 15. 以下程式的功能:把檔案 source1.txt 和檔案 source2.txt 的內容相連,輸出到螢幕上。 共 5 個錯誤點,請改正。 #include #include int main() { FILE *fp1,fp2; char ch; fp1=fopen("source1.txt", "w"); fp2=fopen("source2.txt", "r"); if(fp1==NULL&& fp2==NULL) { printf("\nCannot open file\nStrike any key exit!\n"); getchar(); exit(1); } ch=fgetc(fp2); if(ch!=EOF) { fputc(ch,fp1); ch=fgetc(fp2); } fclose(fp1,fp2); return 0; } 16. 下面程式的功能是統計檔案中字元的個數, 其中有 5 處錯誤, 請查詢並改正 (每個 2 分) #include #include void main() { FILE *fp; int count; char ch; *fp=fopen("source.txt", "w"); if(*fp=NULL) {

  printf("\nCannot open file\nStrike any key exit!\n"); getchar(); exit(1); } ch=fgetc(fp); while(ch!=EOF) { count++; ch=fgetc(fp); } printf("\n 字元個數=%f\n",count); fclose(fp); return 0; } 17. 以下程式的功能:把檔案 source.txt 的內容讀出,壓縮掉其中的空格,存入檔案 target. txt 中,其中有 5 出錯誤,請檢查並改正。 #include #include int main() { FILE *fp1,fp2; char ch; fp1=fopen("source.txt", "r"); fp2=fopen("target.txt", "w"); if(fp1=NULL|| fp2=NULL) { printf("\nCannot open file\nStrike any key exit!\n"); getchar(); exit(1); } ch=fgetc(fp1); if (ch!=EOF) { while (ch!=' ') fputc(ch,fp2); ch=fgetc(fp1); } fclose(fp1,fp2); return 0; }

  18. 程式功能說明:該程式完成從文字檔案中讀入 10 名學生資訊(學號、姓名、成績),求 成 學生平均分,顯示在螢幕上。其中有 5 處錯誤,請檢查並改正。 #include #define N 10; struct stud_type { char name[10]; int num; float score; }; void main() { int i; struct stud_type stu[N]; float s=0; FILE *fp; If((fp=fopen(''d:\\student.txt'', ''w'')) ==NULL) { printf(''Cannot open file\n Press any key to exit!''); getch(); } for(i=0;i

  19. 以下程式的功能:把字串“hello world”及檔案 source.txt 的內容相連,輸出到檔案 target.txt 中。 (共 5 個錯誤點,請改正) #include #include int main() { FILE *fp1,*fp2; char ch; char str= "hello world"; fp1=fopen("d:\source.txt", "r"); fp2=fopen("d:\target.txt", "w");

  if(fp1==NULL&& fp2==NULL) { printf("\nCannot open file\nStrike any key exit!\n"); getchar(); exit(1); } fputs(str, fp2); while(ch=fgetc(fp1)!=EOF) { fputc(ch,fp2); } fclose(fp1); fclose(fp2); return 0; } 20. 下面程式的功能是統計檔案中大寫字元的個數,其中有 5 處錯誤,請查詢並改正。 #include #include int main() { FILE *fp; int count; char ch; *fp=fopen("source.txt", "r"); if(*fp=NULL) { printf("\nCannot open file\nStrike any key exit!\n"); getchar(); return 1; } ch=fgetc(fp); if (ch!=EOF) { if (ch>=’A’&&ch<=’Z’) count++; ch=fgetc(fp); } printf("\n 大寫字元個數=%f\n",count); fclose(); return 0; } 解答: 1. (1)if((fp=fopen(''d:\\content.txt'', ''r''))==NULL) (2)void fun(char *s) (3)for(i=j=0; s[i]!= ?\0?;i++)

  (4)if(s[i]>='A'&& s[i]<='Z') (5) s[j++]=s[i]; 2. (1)for(i=0;s[i]!='\0';i++) (2)char s[80]; (3)FILE *fp; (4)if((fp=fopen("d:\\content.txt", "w"))==NULL) (5)fputs( s,fp); 3. (1)#define N 10 (2)struct stud_type { char name[10]; int num; float score; }; (3) If((fp=fopen(''d:\\student.txt'', ''r'')) ==NULL) (4), (5) fscanf(fp, "%s%d%f", stu[i].name ,&stu[i].num,&stu[i].score); 4. (1)char name[30] (2)struct stud_type { char name[10]; int num; float score; }; (3) If((fp=fopen(''d:\\student.txt'', ''w'')) ==NULL) (4) scanf("%s%d%f", stu[i].name ,&stu[i].num,&stu[i].score); (5) fprintf(fp,"%8s%5d%8.2f\n",stu[i].name,stu[i].num,stu[i].score); 5. void save(); scanf("%s%d%d%s",&s[i].name,&s[i].num,&s[i].age,s[i].addr); FILE *fp; if (fwrite(&s[i],sizeof(struct student),1,fp)=0) fclose(); 6. ① FILE *fp; ② if((fp = fopen("d:\\test.txt","w+")) == NULL) ③ while(ch !=’ \n’) ④ fputc(ch,fp); ⑤ while(ch != EOF) 7. if((fp = fopen("d:\\test.txt","w+") )== NULL) while(ch != ‘\n’) fputc(ch, fp); while(ch !=EOF) fclose(fp );

  8. FILE *fp; if ((fp=fopen("d:\\lang\\tc\\deposit.c","r"))==NULL) return 1; while ((ch=fgetc(fp))!=EOF) fclose(fp); 9. int main( ) if ((fp=fopen("d:\\lang\\tc\\deposit.c","r"))==NULL) while ((ch=fgetc(fp))!=EOF) if ((ch>='A'&&ch <='Z')) ch=ch+32; putchar(ch); 10. scanf("%s",fname); if ((fp=fopen( fname,"w+"))==NULL) while ((ch=getchar())!='#') fputc(ch,fp); fprintf( fp,"\n%d\n",count); 11. 1、FILE *fp1,*fp2; 2、char ch,fname1[32],fname2[32]; 3、printf("Can't open targate file:%s \n",fname2); 4、5、while((ch=fgetc(fp1))!=EOF) 12. 1、FILE *fp1 ; 2、char ch,fname1[32]; 3、if ((fp1=fopen(fname1, "r"))==NULL) 4、while((ch=fgetc(fp1))!=EOF) 5、putchar(ch); 13. 1、if((fp=fopen("d:\\lang\\tc\\writestring.txt","a+"))==NULL) ; 2、scanf("%s",st); 3、fgets(st, fp); 4、rewind(fp); 5、while((ch=fputc(fp))!=EOF) 14. FILE *fp; fp=fopen("source.txt", "r"); if(fp= =NULL) while(ch!=EOF) fclose(fp); 15. FILE *fp1,*fp2; fp1=fopen("source1.txt", "a+"); if(fp1==NULL|| fp2==NULL) while(ch!=EOF) fclose(fp1); fclose(fp2); 16. int main()

  int count=0; fp=fopen("source.txt", "r"); if(fp==NULL) printf("\n 字元個數=%d\n",count); 17. FILE *fp1,*fp2; if(fp1==NULL|| fp2==NULL) while(ch!=EOF) if (ch!=' ') fputc(ch,fp2); fclose(fp1); fclose(fp2); 18. (1)#define N 10 (2)If((fp=fopen(''d:\\student.txt'', ''r'')) ==NULL) (3)fscanf(fp, "%s%d%f", stu[i].name ,&stu[i].num,&stu[i].score); (4) s=s+stu[i].score; (5) fclose(fp); 19. char str[]= "hello world"; fp1=fopen("d:\\source.txt", "r"); fp2=fopen("d:\\target.txt", "w"); if(fp1==NULL|| fp2==NULL) while((ch=fgetc(fp1))!=EOF) 20. fp=fopen("source.txt", "r"); if(fp==NULL) while(ch!=EOF) printf("\n 大寫字元個數=%d\n",count); fclose(fp); 五.程式設計一 解答: 六.程式設計二 解答:

最近訪問