C語言程序設計習題解答_第1頁
C語言程序設計習題解答_第2頁
C語言程序設計習題解答_第3頁
C語言程序設計習題解答_第4頁
C語言程序設計習題解答_第5頁
已閱讀5頁,還剩26頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、精選優質文檔-傾情為你奉上C語言程序設計習題解答沈國榮 隋雪莉 閔芳目錄第1章 C語言程序設計概述一、選擇題12345678910CBACCBDBDA二、填空題1.函數、main()函數2./*、*/3.C、.OBJ、.EXE4.順序結構、選擇結構、循環結構三、編程題1.【參考代碼】#include<stdio.h>int main( )printf( "(學校名稱)n" ) ;printf( "(姓名)n" ) ;return 0 ;2.【參考代碼】#include<stdio.h>int main( )printf( &quo

2、t;(學校名稱)n(姓名)n" ) ;return 0 ;第2章 數據類型及其運算一、選擇題12345678910CCCABADDC、BB11121314151617181920CADDDCDC注:第5題B選項為:''' '017' 't'二、填空題1.字母、數字、下劃線2.1、4、4、83.-6.97.68.-609.y%2=110.1、0、1三、程序閱讀題1.b2.03.04.10,25.9,10,9,106.3,1,0,07.3,20,30,1第3章 語句與輸入輸出一、選擇題12345678910BCC

3、CCBBBDA二、填空題1.123.472.D3.回車4.10,2三、程序閱讀題1.2612.203.201,104.y=4630y=46305.*3.,3.142*6.c:dec=120,oct=170,hex=78,ASCII=x7.x=1 y=2 *sum*=310 squared is : 1008.2 48 20.2 20.29.x+y+z=4810.55, ,A四、編程題1.【參考代碼】#include<stdio.h>int main( )char ch;printf("請輸入一個字符:n");scanf("%c",&c

4、h);printf("%c的ASCII碼為:%dn", ch,ch);return 0 ;2.【參考代碼】#include<stdio.h>#define PI 3.1416int main( )double r, h;double cl,cs,cv;printf("請輸入圓的半徑:");scanf("%lf", &r);printf("請輸入圓柱高:");scanf("%lf", &h);cl=2*PI*r;cs=PI*r*r;cv=PI*r*r*h;printf(

5、"圓的周長為:%.4lfn", cl);printf("圓的面積為:%.4lfn", cs);printf("圓柱的體積為:%.4lfn", cv);return 0 ;3.【參考代碼】#include<stdio.h>int main( ) int splitInt,one,ten,hundred;printf("輸入要處理的整數:"); scanf("%d",&splitInt); hundred = splitInt/100;ten = splitInt%100/10;

6、one = splitInt%10;printf("個位:%d,十位:%d,百位:%dn",one,ten,hundred);return 0 ;第4章 選擇結構程序設計一、選擇題123456DCCBBC二、程序閱讀題1.102.2,2,23.64.97,b5.c=-16.88887.20,08.2,19.1,12,22,1-2,210.a=1,b=3三、程序完善題1.a>b、c>x四、編程題1.【參考代碼】#include<stdio.h>int main( )int a, b, c,d,min; printf("輸入4個整數:"

7、;); scanf("%d%d%d%d",&a,&b,&c,&d);if(a < b) min = a; else min = b; if(c < min) min=c; if(d<min) min=d; printf("%dn",min); return 0 ;2.【參考代碼】#include<stdio.h>int main( ) int num,a,b,c,d; printf("請輸入一個四位整數:"); scanf("%d",&num);

8、a=num/1000;b=num%1000/100;c=num%100/10;d=num%10; printf("各位數字之和為:%dn",a+b+c+d);return 0 ;3.【參考代碼】#include<stdio.h>int main( )int x; printf("請輸入x:"); scanf("%d",&x);printf("y的值為:");if(x<0)printf("%dn",x);else if(x<50)printf("%dn&q

9、uot;,3*x-2);else if(x<100)printf("%dn",4*x+1);elseprintf("%dn",5*x);return 0 ;4.【參考代碼】#include<stdio.h>int main( )int dj;float zl,je,yfk;printf("請輸入等級(14): ");scanf("%d",&dj);if (dj>4|dj<1)printf("無此等級的蘋果!n");return 0;printf("

10、請輸入重量(公斤): ");scanf("%f",&zl);printf("n");switch (dj) case 1 : je=5.5*zl; break;case 2 : je=4.3*zl; break;case 3 : je=3.0*zl; break;case 4 : je=2.5*zl; break;printf("您選擇蘋果級別: %d 級n",dj);printf("您購買蘋果重量: %.2f公斤n",zl);printf("您應付金額為: %.2f元n",j

11、e);printf("n");printf("顧客所付金額: ");scanf("%f",&yfk);if (yfk<je)printf("Data Error!n");return 0;printf("應找您: %.2f元n",yfk-je);return 0 ;第5章 循環結構程序設計一、選擇題12345678(1)8(2)91011CBBBABBBCDCB二、程序閱讀題1.1,2,02 .m=4,n=23.A2C4E64.1325.46.k=0,m=57.x=88.1.69.

12、三、程序完善題1.( ch > 'Z' && ch <= 'Z' + 4 ) |(ch > 'z' ) ch - 262.k k/10 continue 3.i + t * 10 s = s + t4.fabs( t ) >= 1e-6 f = -f 5.i<10 j%3 !=0四、編程題1.【參考代碼】#include<stdio.h>int main( )int n , i , j , k ;printf( "Output:n" ) ;for(n = 100 ; n

13、 < 1000 ; n+ )i = n % 10 ; /* 個位 */j = ( n / 10 ) % 10 ; /* 十位 */k = n / 100 ; /* 百位 */if ( n = i * i * i + j * j * j + k * k * k )printf( "%dn" , n ) ;return 0 ;2. 【參考代碼】#include<stdio.h>int main ( ) int i , m , n , t , p , k ; printf( "Please input: " ) ;scanf ( "

14、%d,%d" , &m , &n ) ;if( m < n )t = n ;n = m ;m = t ; p = m * n ;while ( n != 0 ) /* 余數不為0,繼續相除,直到余數為0 */i = m % n ;m = n ;n = i ; k = p / m ;printf( "%d,%dn" , m , k );return 0 ;3. 【參考代碼】#include<stdio.h>int main( )int i , n , t , sum ;t = 1 ;sum = 0 ;printf( "Pl

15、ease input: n = " ) ;scanf( "%d" , &n ) ;for( i = 1 ; i <= n ; i+ ) t = t * i ;sum = sum + t ;printf( "1!+2!+%d!= %dn" , n , sum ) ;return 0 ;4. 【參考代碼】#include<stdio.h>int main( ) int i , m ;double sum = 0 , k = 1 ;printf( "Please input : m=" ) ;scanf(

16、 "%d" , &m ) ;for( i = 1 ; i <= m ; i+ )sum = sum + k / i ;k = -k ;printf( "sum=%4.2fn" , sum ) ;return 0 ;第6章 數組一、選擇題12345678910111213141516DDDBCCBCCDDBDDCD二、程序閱讀題1 82 43 0,24 125 t*M6 mo7 fwo三、程序完善題1 k = i j = i ak = max aj = max2 sum += scorei scorei<avg3 si = si + a

17、ij printf( "n" ) ;4 j = strlen( str ) 1 strj = k5 ( c = getchar( ) ) != '#' numc-'A' += 1 四、編程題1. 【參考代碼】#include<stdio.h>#define N 5int main( )int aN , i , j , r , temp ;printf( "Please input %d numbersn" , N ) ;for( i = 0 ; i < N ; i+ )scanf( "%d&qu

18、ot; , &ai ) ;for( i = 0 ; i < N - 1 ; i+ )r = i ;for( j = i + 1 ; j < N ; j+ )if( aj < ar )r = j ;if( r != i )temp = ar ;ar = ai ;ai = temp ;printf( "The array after sort:n" ) ;for( i = 0 ; i < N ; i+ )printf( "%5d" , ai ) ;printf( "n" ) ;return 0 ;2. 【參

19、考代碼】#include<stdio.h>int main( )int a10 = 1 , 2 , 3 , 6 , 7 , 8 , 9 , 10 ;int x , j , k = 0 ;printf( "Please input :x= " ) ;scanf( "%d" , &x) ; if( x > a7 )a8 = x ;elsefor( j = 0 ; j < 8 ; j+ )if( x < aj )break ;for(k = 8 ; k > j ; k- )ak = ak - 1 ;aj = x ;f

20、or( j = 0 ; j < 9 ; j+ )printf( "%5d" , aj ) ;printf( "n" ) ;return 0 ;3. 【參考代碼】#include<stdio.h>int main( )int a55 = 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21, 22,23,24 ;int i , j ,sum = 0 ;for ( i = 0 ; i < 5 ; i+ )for ( j = 0 ; j < 5 ; j+)printf( &

21、quot;%4d" , aij ) ;printf( "n" ) ;for( i =0 ;i < 5 ; i+ )sum += aii ;printf( " sum=%4dn" , sum ) ;return 0 ;4. 【參考代碼】#include<stdio.h>int main( )char s1100 , s230 ;int i , j ;printf( "Please input s1:" ) ;gets( s1 ) ;printf( "Please input s2:" )

22、;gets( s2 ) ;for( i = 0 ; s1i != '0' ; i+ ) ;for( j = 0 ; s2j != '0' ; j+ , i+ )s1i = s2j ;s1i = '0' ;printf( "Outputns1:" ) ;puts( s1 ) ;return 0 ;5. 【參考代碼】#include<stdio.h>int main( )char s1100 ;int i ;printf( "Please input s1:" ) ;gets( s1 ) ;for(

23、 i = 0 ; s1i != '0' ; i+ ) ;printf( "The length of s1 is %dn" ,i ) ;return 0 ;第7章 函數一、選擇題123456789101112131415BDDABBCBDDAADDA二、程序閱讀題1 max is 22 a=1,b=23 1 114 a=11,b=12,c=25 66 7 8 97 8 178 0 1 2 0 1 2 三、程序完善題1 float area ( float r ) return s2 z = fun( x , y ) z = z * x 3 count =fu

24、n( score ) count+四、編程題1. 【參考代碼】#include<stdio.h>int main( ) void f(int n) ;int n ;printf( "Please input: n= " ) ;scanf( "%d" , &n ) ;if( n <= 0)printf( "Wrong number!n" ) ;elsef( n ) ;return 0 ;void f(int n)if( n % 2 = 1 )printf( "%d is a odd number.n&

25、quot; , n ) ;elseprintf( "%d is a even number.n" , n ) ;2. 【參考代碼】#include < stdio.h >#include < math.h >int main( )void f( int m ) ;int m ;printf( "Please input: m= " ) ; scanf( "%d" , &m ) ;f( m ) ;return 0 ;void f( int m )int i , k ;k = sqrt( m );for(i

26、 = 2 ; i <= k ; i+ )if( m % i = 0 ) break; if (i >= k + 1 ) printf( "%d is a Prime Number.n" , m ) ; else printf( "%d is not a Prime Number.n" , m ) ;3. 【參考代碼】#include<stdio.h>int gys( int m , int n )int r ;r = m % n ;while( r != 0 )m = n ;n = r ;r = m % n ;return n ;

27、int gbs( int m , int n , int r )return m * n / r ;int main( )int m , n , t ;printf( "Please input(m,n):" ) ;scanf( "%d%d" , &m , &n ) ;if( m < n )t = m ;m = n ;n = t ;t = gys( m , n ) ;printf( "gys=%dn" , t ) ;t = gbs( m , n , t ) ;printf( "gbs=%dn"

28、 , t ) ;return 0 ;4. 【參考代碼】#include<stdio.h>int main( )void mystrcat( char s1100 , char s230 ) ;char s1100 , s230 ;printf( "Please input s1:" ) ;gets( s1 ) ;printf( "Please input s2:" ) ;gets( s2 ) ;mystrcat( s1 , s2 ) ;printf( "Outputns1:" ) ;puts( s1 ) ;return 0

29、 ;void mystrcat( char s1100 , char s230 )int i , j ;for( i = 0 ; s1i != '0' ; i+ ) ;for( j = 0 ; s2j != '0' ; j+ , i+ )s1i = s2j ;s1i = '0' ;第8章 編譯預處理一、選擇題123456ADDABA二、程序閱讀題1 6,182 153 5第9章 指針一、選擇題123456789101112DBBCDCCCDBAC1314151617CDDAC二、填空題1.地址 , NULL(或0)2 .char a, *p; ,

30、 scanf("%c", &a); , p=&a;3.*m4.for( k=0; k<10; k+ )5.*(p+i) , pi , *(x+i)6.stri或*( str + i ) , i三、程序閱讀題1.gae2. bcdABCD3. 7, 8, 84. 8 45. 3 14 26. efgh7. w,one8. 7四、程序完善題1.ai 或 *( a + i )2.*p!='0' *p-'0' 3.p1 p2-x4.max(int a , int b ); p = max四、編程題1.【參考代碼】#include

31、<stdio.h>void sort( int *a , int *b , int *c );int main()int m , n , t ;printf( "Please input(m n t):" ) ;scanf( "%d%d%d" , &m , &n , &t ) ;sort( &m , &n , &t ) ;printf( "The result is :%dt%dt%dn" , m , n , t ) ;return 0 ;void sort( int *a

32、, int *b , int *c )int temp ;if( *a > *b ) temp = *a ; *a = *b ; *b = temp ; if( *a > *c ) temp = *a ; *a = *c ; *c = temp ; if( *b > *c ) temp = *b ; *b = *c ; *c = temp ; 2.【參考代碼】程序1:#include <stdio.h>int strcompare( char *str1 , char *str2 );int main( )int m ; char s120 , s220 , *p

33、1 , *p2 ; printf( "Please input(string1):" ) ; scanf( "%s" , s1 ) ;printf( "Please input(string2):" ) ; scanf( "%s" , s2 ) ; m = strcompare ( s1 , s2 ) ; printf( "The result of strcompare is: %dn" , m ) ;return 0 ;int strcompare( char *str1 , char *s

34、tr2 )int i = 0 ; while( ( *( str1 + i ) = *( str2 + i ) ) &&( *( str1 + i ) != '0' ) )i+ ; return( *( str1 + i ) - *( str2 + i ) ) ;程序2:#include <stdio.h>int strcompare( char *str1 , char *str2 );int main( )int m ; char s120 , s220 , *p1 , *p2 ; printf( "Please input(strin

35、g1):" ) ; scanf( "%s" , s1 ) ;printf( "Please input(string2):" ) ; scanf( "%s" , s2 ) ; p1 = s1 ;p2 = s2 ; m = strcompare ( p1 , p2 ) ; printf( "The result of strcompare is: %dn" , m ) ;return 0 ;int strcompare( char *str1 , char *str2 )int i = 0 ; while(

36、 ( *( str1 + i ) = *( str2 + i ) ) &&( *( str1 + i ) != '0' ) )i+ ; return( *( str1 + i ) - *( str2 + i ) ) ;程序3:#include <stdio.h>int strcompare( char str1 , char str2 );int main( )int m ; char s120 , s220 , *p1 , *p2 ; printf( "Please input(string1):" ) ; scanf( &qu

37、ot;%s" , s1 ) ;printf( "Please input(string2):" ) ; scanf( "%s" , s2 ) ; p1 = s1 ;p2 = s2 ; m = strcompare ( p1 , p2 ) ; printf( "The result of strcompare is: %dn" , m ) ;return 0 ;int strcompare( char *str1 , char *str2 )int i = 0 ; while( ( str1i = str2i ) &&

38、amp;( str1i != '0' ) )i+ ; return( str1i- str2i ) ;第10章 結構體與共用體一、選擇題1234567891011CBACCDDDCBD二、填空題1.162 .p->next=head->next head->next=p3.p->next三、程序閱讀題1.51,60,212. 163. 1001,ChangRong,1098.0四、程序完善題1.sizeof( struct ps ) 或 sizeof( bt )2.p=p->next 3.personi.sex五、編程題1.【參考代碼】#defin

39、e N 3#include <stdio.h>struct studentchar num6 ;char name8 ;int score2 ;float ave ; ;void input( struct student stuN ) ;void average( struct student stuN ) ;int max( struct student stuN ) ;int main( ) int i , j ;struct student stuN ;input( stu ) ;average( stu ) ;printf( "NotNametScore1tSc

40、ore2tAveragen" ) ;for( i = 0 ; i < N ; i+ )printf( "%st%st" , stui.num , ) ;for( j = 0 ; j < 2 ; j+ )printf( "%dt" , stui.scorej ) ;printf( "%8.2fn" , stui.ave ) ;i = max( stu ) ;printf( "nThe max is:n" ) ;printf( "NotNametScore1tScor

41、e2tAveragen" ) ;printf( "%st%st" , stui.num , ) ;for( j = 0 ; j < 2 ; j+ )printf( "%dt" , stui.scorej ) ;printf( "%5.2fn" , stui.ave ) ;return 0;void input( struct student stuN)int i , j ;for(i=0;i<N;i+)printf("nPlease input No%d student:n"

42、;,i+1);printf("No:");scanf("%s",stui.num);printf("Name:");scanf("%s",);for(j=0;j<2;j+)printf("score %d:",j+1);scanf("%d",&stui.scorej);void average( struct student stuN)int i , j , sum ;for(i=0;i<N;i+)for(j=0 , stui.ave =

43、 0 ;j<2;j+) stui.ave += stui.scorej;stui.ave = stui.ave / 2 ;int max( struct student stuN)int i , max , index;max = stu0.ave ;index = 0 ;for( i = 1 ; i < N ; i+ )if( max < stui.ave )max = stui.ave ;index = i ;return index;2.【參考代碼】#include<stdio.h>#include<stdlib.h>struct node sh

44、ort int data ;struct node *next ; ;typedef struct node NODE ;struct node *CreatLink( ) ;void PrintLink( NODE *head ) ;int max( NODE *head );int main( )NODE *head ;int max_value ;head = CreatLink( ) ;PrintLink( head ) ;max_value = max( head ) ;printf( "The max is:%dn" , max_value ) ;return

45、0 ;struct node *CreatLink( ) NODE *head , *p , *q ;short int num ;head = ( NODE * )malloc( sizeof( NODE ) ) ;head->next = NULL ;p = head ;printf( "Please input( end of -1)!:n" ) ;scanf( "%d" , &num ) ;while( num != -1 )q = ( NODE *)malloc( sizeof( NODE ) ) ; q->data = n

46、um ;p->next = q ;p = q ;scanf( "%d" , &num ) ; p->next = NULL ;return head ;void PrintLink( NODE *head )NODE *p;p = head->next ;printf( "The data is:n" ) ;while( p != NULL )printf( "%4d" , p->data ) ; p = p->next ;printf( "n" ) ;int max( NOD

47、E *head )NODE *p;short int max = -32768;p = head->next ;while( p != NULL )if( max < p->data )max = p->data ; p = p->next ;return max ;第11章 位運算一、選擇題1234DBCA二、填空題1.0000 11112 .x | ff00 3.4 3三、程序閱讀題1.02. 11 223. 0四、編程題1.【參考代碼】#include<stdio.h>int main()short int data , low ,high ;p

48、rintf( "Please input( short int):" ) ;scanf( "%d" , &data ) ;low = data & 0x00ff ; /* 0x00ff表示低字節全1 */high = data & 0xff00 ; /* 0xff00表示高字節全1 */ printf( "data:0x%x,the value of low byte is:0x%xn" , data , high ) ;printf( "data:0x%x,the value of high byt

49、e is:0x%xn" , data , low ) ;return 0 ;2.【參考代碼】#include <stdio.h>int main( )short int data , result ;printf( "Please intput(short int):" ) ;scanf( "%d" , &data ) ;result = data 0x000f ; /* 0x000f表示低4位全1,高12位全0 */ printf( "The data is 0x%x nThe result is :0x%xn&

50、quot; , data,result ) ;return 0 ;第12章 文件操作一、選擇題123456789101112DB、CCBBABCDBCC131415CDC二、填空題1.二進制 ASCII(文本)2 .FILE *fp # include <stdio.h> 3.n 1 buf的首地址4. 15. 用以獲得文件讀寫位置標記指針的位置,函數返回值為當前文件讀寫位置標記指針相對于文件開頭的字節數 6. 使文件讀寫位置標記指針重新返回文件的開頭 三、程序閱讀題1.1 22. end3. 34. hello,四、程序完善題1.! feof (fp) fgetc (fp) 2.

51、fopen (“num.dat”,”r”) fp,”%d”,&temp z+ 3.( ch=getchar() ) ch , fp 五、編程題1.【參考代碼】#include<stdio.h>#include<stdlib.h>#include<string.h>int main( ) FILE *fp ;char str100 , filename10 ;int i = 0 ;if( ( fp = fopen( "upper.txt" , "w+" ) ) = NULL )printf( "Cann

52、ot open file!n" ) ;exit( 0 ) ;printf( "Please input(string):n" ) ;gets( str ) ;while( stri != '!' )if( stri >= 'a' && stri <= 'z' )stri = stri - 32 ;fputc( stri , fp ) ;i+ ;rewind( fp ) ;fgets( str , strlen( str ) + 1 , fp ) ;printf( "nThe result is :n" ) ;printf( "%sn" , str ) ;fclose( fp ) ;return 0 ;2.【參考代碼】#include<stdio.h>#include<stdlib.h>#define N 5struct studentchar num10 ; char name8 ; int score3 ; float ave ; stuN ;int main( )int i , j , sum ; FILE *fp ;for( i = 0 ; i < N ;

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論