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

提交评论