C语言程序设计习题集沈国荣-参考答案_第1页
C语言程序设计习题集沈国荣-参考答案_第2页
C语言程序设计习题集沈国荣-参考答案_第3页
C语言程序设计习题集沈国荣-参考答案_第4页
C语言程序设计习题集沈国荣-参考答案_第5页
已阅读5页,还剩23页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C语言程序设计习题解答沈国荣隋雪莉闵芳目录第1章C语言程序设计概述 2第2章数据类型及其运算3第3章语句与输入输出 4第4章选择结构程序设计6第5章循环结构程序设计9第6章数组.12第7章函数.15第8章 编译预处理 1.8第9章指针19第10章 结构体与共用体 22第11章位运算26第12章文件操作27a第1章C语言程序设计概述、选择题12345678910CBACCBDBDA二、填空题1. 函数、main()函数2. /*、*/3. .C、.OBJ、.EXE4. 顺序结构、选择结构、循环结构三、编程题1 .【参考代码】#include<stdio.h>int main()pri

2、ntf("(学校名称)n");printf( " () n");return 0 ;2 .【参考代码】#include<stdio.h>int main()printf("(学校名称)n () n");return 0 ;第2章数据类型及其运算、选择题12345CCCAB1112131415CADDD678910ADDC、BB1617181920CDC注:第5题B选项为:'''017''t'二、填空题1. 字母、数字、下划线2. 1、4、4、83. -164. 3.55. 0

3、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章语句与输入输由、选择题12345678910BCCCCBBBDA二、填空题1. 123.472. D3. 回车4. 10,2三、程序阅读题1. 2612. 203. 201,104. y=4630y=46305. *3.140000,3.142*6. c:dec=120,oct=170,hex=78,ASCII=x7. x=1 y=2 *sum*=310 squared is : 1008. 2 48 20.

4、2 20.29. x+y+z=4810. 55, ,A四、编程题1 .【参考代码】#include<stdio.h>int main()char ch;printf("请输入一个字符:n");scanf("%c",&ch);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("请输入

5、圆的半径:");scanf("%lf", &r);printf("请输入圆柱高:");scanf("%lf", &h);cl=2*PI*r;cs=PI*r*r;cv=PI*r*r*h;printf("圆的周长为:%.4lfn", cl);printf("圆的面积为:%.4lfn", cs);printf("圆柱的体积为:%.4lfn", cv);return 0 ;3.【参考代码】#include<stdio.h>int main()in

6、t splitInt,one,ten,hundred;printf("输入要处理的整数:");scanf("%d",&splitInt);hundred = splitInt/100;ten = splitInt%100/10;one = splitInt%10;printf("个位:%d 十位:%d 百位:%dn",one,ten,hundred); return 0 ;第4章选择结构程序设计、选择题123456DCCBBD二、程序阅读题1. io2. 2,2,23. 64. 97,b5. c=-16. 88887. 20,

7、08. 2,19. 1,12,22,1-2,210. a=1,b=3三、程序完善题1 . a>b、c>x四、编程题2 .【参考代码】#include<stdio.h>int main()int a, b, c,d,min;printf("输入4个整数:");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("%d'

8、;n",min);return 0 ;3 .【参考代码】#include<stdio.h>int main()int num,a,b,c,d;printf("请输入一个四位整数:");scanf("%d",&num);a=num/1000;b=num%1000/100;c=num%100/10;d=num%10;printf("各位数字之和为:dn",a+b+c+d);return 0 ;4 .【参考代码】#include<stdio.h>int main()int x;printf(&quo

9、t;请输入 x:");scanf("%d",&x);printf("y 的值为:");if(x<0)printf("%dn",x);else if(x<50)printf("%dn",3*x-2);else if(x<100)printf("%dn",4*x+1);elseprintf("%dn",5*x);return 0 ;5 .【参考代码】#include<stdio.h>int main()int dj;float zl

10、,je,yfk;printf("请输入等级(14):");scanf("%d",&dj);if (dj>4|dj<1)printf("无此等级的苹果!n");return 0;printf("请输入重量(公斤):");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

11、=2.5*zl;break;printf("您选择苹果级另ij : %d级n",dj);printf("您购买苹果重量:%.2f公斤n",zl);printf("您应付金额为:.2f元n",je);printf("n");printf("顾客所付金额:");scanf("%f",&yfk);if (yfk<je)printf("Data Error!n");return 0;printf("应找您:%.2f元n",yfk-

12、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. 998988三、程序完善题1.(ch > 'Z' && ch <= 'Z' + 4 ) |(ch > 'z')ch - 262.kk/10continue3.i + t* 10s = s + t4.fabs( t ) >= 1e-6f = -f5.i&

13、lt;10j%3 !=0四、编程题1 .【参考代码】#include<stdio.h>int main()int n , i , j , k ;printf( "Output : n");for(n = 100 ; n < 1000 ; n+ )1 = 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

14、.【参考代码】#include<stdio.h>int main ()int i , m , n , t , p , k ;printf( "Please input:");scanf ( "%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

15、 , k );return 0 ;3 .【参考代码】#include<stdio.h>int main()int i , n , t , sum ;t = 1 ;sum = 0 ;printf( "Please 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&

16、lt;stdio.h>int main()int i , m ;double sum = 0 , k = 1 ;printf( "Please input : m=");scanf( "%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,

17、24 125 t*M6 mo7 fwo三、程序完善题1 k = ij = iak = maxaj = min2 sum += scoreiscorei<avg3 si = si + aijprintf( "n");4 j = strlen( str )- 1 strj = k5 ( c = getchar( ) ) != '#' numc-'A' += 1四、编程题1.【参考代码】 #include<stdio.h>#define N 5 int main()int aN , i , j , r , temp ;printf

18、( "Please input %d numbersn" , N );for( i = 0 ; i < N ; i+ )scanf( "%d” , &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 ;aprintf( "The array after sort:n");for( i = 0 ; i &l

19、t; N ; i+ )printf( "%5d" , ai);printf( "n");return 0 ;2.【参考代码】#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 <

20、aj)break ;for(k = 8 ; k > j ; k-)ak = ak - 1;aj = x ;for( 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

21、< 5 ; i+ )for ( j = 0 ; j < 5 ; j+)printf( "%4d" , a皿);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 );p

22、rintf( "Please input s2:");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:&q

23、uot;);gets( s1 );for( i = 0 ; s1i != '0' ; i+ );printf( "The length of s1 is %dn" ,i );return 0 ;a第7章函数、选择题123456789101112131415BDDABBCBDDAADDA、程序阅读题1 max is 22 a=1,b=23 1 114a=11,b=12,c=25 66 7897 8 178 0 1 2 0 12三、程序完善题1 float area ( float r )return s2 z = fun( x , y )z = z * x3

24、count =fun( 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");else f( 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 >a#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 = 2 ; i

26、 <= k ; i+ )if( m % i = 0 ) break;if (i >= k + 1 )printf( "%d is a Prime Number.n" , m );elseprintf( "%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 ;int gbs( int m

27、, 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" , t);return 0 ;4.【参考代码

28、】#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 ;void mystrcat( char s1100 , char s230)

29、int i , j ;for( i = 0 ; s1i != '0' ; i+ );for( j = 0 ; s2j != '0' j+ , i+ )s1i = s2j;s1i = '0'a第8章编译预处理、选择题123456ADDABA二、程序阅读题16,1821535第9章指针、选择题123456789101112DBBCDCCCDBCC1314151617CDDAC二、填空题1. 地址,NULL (或 0)2. . char a, *p;, scanf("%c", &a); ,p=&a;3. *m4. f

30、or( k=0; k<10; k+ )5. *(P+i), pi ,*(x+i)6. stri或*( str + i ) , i三、程序阅读题1. gae2. bcdABCD3. 7, 8, 84. 845. 3 14 26. efgh7. w,one8. 7四、程序完善题1. ai 或 *( a + i )2. *p!='0'*p-'0'3. p1p2-x4. max(inta , int b );p = max四、编程题1.【参考代码】#include<stdio.h>void sort( int *a , int *b , int *c

31、);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 , int *b , int *c )int temp ;if( *a > *b ) temp= *a;*

32、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 , *p1 , *p2 ;printf( "Please input(string1):");scanf( "%s"

33、; , 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 *str2 )int i = 0 ;while( ( *( strl + i ) = *( str2 + i ) ) &&( *( strl + i ) !=

34、'0' ) i+ ;return( *( strl + 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(stringl):");scanf( "%s" , si );printf( "Please input(string2):");scanf( &qu

35、ot;%s" , s2 );pi = si ;p2 = s2 ;m = strcompare ( pl , p2 );printf( "The result of strcompare is: %dn" , m );return 0 ;int strcompare( char *str1 , char *str2 )int i = 0 ;while( ( *( strl + i ) = *( str2 + i ) ) &&( *( strl + i ) != '0' ) i+ ;return( *( strl + i ) - *(

36、str2 + i );程序3:#include <stdio.h>int strcompare( char str1 , char str2口);int main()int m ;char s120 , s220 , *p1 , *p2 ;printf( "Please input(stringl):");scanf( "%s" , si );printf( "Please input(string2):");scanf( "%s" , s2 );pi = si ;p2 = s2 ;m = strcom

37、pare ( pi , p2 );printf( "The result of strcompare is: %dn" , m );return 0 ;int strcompare( char *stri , char *str2 )int i = 0 ;while( ( strii = str2i ) &&( strii != '0' )i+ ;return( strii- str2i );第10章结构体与共用体、选择题1234567891011CBACCDDDCBD二、填空题1. 112. . p->next=head->ne

38、xthead->next=p3. p->next三、程序阅读题1. 51,60,212. 163. 1001,ChangRong,1098.0四、程序完善题1. sizeof( struct ps ) 或 sizeof( bt )2. p=p->next3. personi.sex五、编程题1 .【参考代码】#define N 3#include <stdio.h>struct studentchar num6;char name8;int score2;float ave ;void input( struct student stuN);void averag

39、e( struct student stuN);int max( struct student stuN);int main()int i , j ;struct student stuN;input( stu );average( stu );printf( "NotNametScore1tScore2tAverage'n");for( i = 0 ; i < N ; i+ )printf( "%st%st" , stui.num , ); for( j = 0 ;j < 2 ; j+ )printf( &quo

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

41、f( "%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",i+1);printf("No:");scanf("%s",stui.num);printf("Name:");scanf("%s",);for(j=0;j<2;j+)printf(&quo

42、t;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 = 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 =

43、 1 ; i < N ; i+ )if( max < stui.ave )max = stui.ave ;index = i ;return index;2 .【参考代码】#include<stdio.h>#include<stdlib.h>struct nodeshort int data ;struct node *next ;;typedef struct node NODE ;struct node *CreatLink();void PrintLink( NODE *head );int max( NODE *head );int main()NO

44、DE *head ;int max_value ;head = CreatLink();PrintLink( head );max_value = max( head );printf( "The max is:%dn" , max_value );return 0 ;struct node *CreatLink()NODE *head , *p , *q ;short int num ;head = ( NODE * )malloc( sizeof( NODE ); head->next = NULL ;p = head ;printf( "Please

45、input( end of-1)!:n");scanf( "%d" , &num );while( num != -1 )q = ( NODE *)malloc( sizeof( NODE ); q->data = num ;p->next = q ;P = q ;scanf( "%d" , &num );p->next = NULL ;return head ;void PrintLink( NODE *head )NODE *p;p = head->next ;printf( "The da

46、ta is:n");while( p != NULL )printf( "%4d" , p->data );p = p->next ;printf( "n");int max( NODE *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. 0

47、000 11112. . x | ff003. 43三、程序阅读题1. 02. 11 223. 0四、编程题1 .【参考代码】#include<stdio.h>int main()short int data , low ,high ;printf( "Please input( short int):");scanf( "%d" , &data );low = data & 0x00ff ; /* 0x00ff 表示低字节全 1 */high = data & 0xff00 ; /* 0xff00表示高字节全 1 *

48、/printf( "data:0x%x,the value of low byte is:0x%xn" , data , high ); printf( "data:0x%x,the value of high byte is:0x%xn" , data , low );return 0 ;2.【参考代码】#include <stdio.h>int main()short int data , result ;printf( "Please intput(short int):");scanf( "%d"

49、; , &data );result = data A 0x000f ;/* 0x000f 表示低 4 位全 1,高 12 位全 0 */ printf( "The data is 0x%x nThe result is :0x%xn" , data,result );return 0 ;aa第12章文件操作、选择题123456789101112DB、CCBBABCDBCC131415CDC二、填空题1 . 二进制ASCII(文本)2 . FILE *fp# include <stdio.h>3 . n -1buf的首地址4 . 15 .用以获得文件读写

50、位置标记指针的位置,函数返回值为当前文件读写位置标记指针相对于文件开头的字节数6 .使文件读写位置标记指针重新返回文件的开头 三、程序阅读题1. 1 22. end3. 34. hello,fgetc (fp) fp, "%d',&tempch , fpz+四、程序完善题1. ! feof (fp)2. fopen ( num.dat ","r")3. ( ch=getchar()五、编程题1 .【参考代码】#include<stdio.h>#include<stdlib.h>#include<string.

51、h>int main()FILE *fp ;char str100 , filename10;int i = 0 ;if( ( fp = fopen( "upper.txt" , "w+" ) ) = NULL ) printf( "Cannot open file!n");exit( 0 );printf( "Please input(string):n");gets( str );while( stri != '!') if( stri >= 'a' &&am

52、p; 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 5 struct studentchar

53、 num10;char name8;int score3; float ave ; stuN; int main()int i , j , sum ;FILE *fp ;for( i = 0 ; i < N ; i+ ) printf( "nPlease input student information: n" , i + 1 ); printf( "No:");scanf( "%s" , stui.num );printf( "Name:");scanf( "%s" , stui.na

54、me );sum = 0 ;for( j = 0 ; j < 3 ; j+ ) printf( "Score%d:" , j + 1 );scanf( "%d" , &stui.scorej); sum += stui.scorej;stui.ave = sum / 3.0 ;if( ( fp = fopen( "stud.dat" , "w" ) ) = NULL ) printf( "cannot open stud for write!n");exit( 0 );for( i = 0 ; i < N ; i+ ) fwrite( &stui , sizeof( struct student ) ,1 , fp );fclose( fp );if( ( fp = fopen( "stud.dat" , "r" ) ) = NULL ) printf( "cannot open stud for read!n");exit( 0 );printf( "nNotNametScore1tScore2tAverage'n");for( i = 0 ;

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论