资源描述
C语言程序设计教程课后习题参考答案习题11. (1)编译、链接 .exe(2)函数 主函数(或main函数)(3)编辑编译链接2. (1)-(5):DDBBC(6)-(10):ABBBC3. (1)答:C语言简洁、紧凑,使用方便、灵活;C语言是高级语言,同时具备了低级语言的特征;C语言是结构化程序设计语言,具有结构化的程序控制语句;C语言有各种各样的数据类型;C语言可移植性好;生成目标代码质量高,程序执行效率高。(2)编辑、编译、链接、执行(3)一个C程序由一或多个函数组成,一函数若干条语句构成,每条语句的末尾必须以分号结束。(4)标识符,关键字,运算符,分隔符,常量,注释符等4. 从键盘输入一个双精度小数,打印出它的余弦值。#include #include main( )double x;scanf(“%lf”, &x);printf(“%lfn”, cos(x) );第2章1.(1)BDE、ACFG(2)D(3)C(4)C2(1)错(2)错(3)错(4)对(5)错3. (1)a=3,b=-27(2)a=11,b=6,c=6(3)3(4)1010110(5)-998(6)1)202)83)704)05)06)04.(1)#include main( )double r, h ,v;r = 2.5;h = 3.5;v = 3.14*r*r*h;printf(“v=%lfn”, v);(2)#include main( )char ch;ch = getchar( );printf(“%cn”, ch + 32);(3)#include main( )printf(“ *n”);printf(“ *n”);printf(“ *n”);printf(“*n”);(4)#include main( )double x;scanf(“%lf”, &x);printf(“%d , %lfn”, (int)x, x (int)x );(5)#include main( )double a=3, b=5;double result = (-2 * a + ( 4*a b )/( 2*a + b ) )/( (a - 4*b)/(a + b) );printf(“%lfn”, result);习题31.(1)D(2)AD(3)C(4)B(5)A(6)-(10):BDACB2.(1)3.141593,3.1416,3.142(2)c=K(3)| 123.46|,|123 |(4)x= 1.23,y= 50.00(5)03.(1)scanf(%f”, c); 改为:scanf(“%f”, &c);f = (9/5)*c+32;改为:f = (9.0/5)*c + 32;printf(“摄氏温度%f度相当于华氏温度%f度”, &c, &f); 改为:printf(“摄氏温度%f度相当于华氏温度%f度”, c, f);(2)补充定义:int h;h = 500/60改为: h = 500 / 60;m = 500% 60 改为: m = 500%60;printf(“500分钟是%d小时%d分钟,”&h, &m); 改为:printf(“500分钟是%d小时%d分钟” , h, m);4.(1)#includemain( )char x,y;scanf(“%c%c”, &x, &y);printf(“%dn”, (x-0) + (y-0) );(2)#include main( )char x, y;char tmp;printf(“Input two characters:”);scanf(“%c%c”, &x, &y);printf(“Before swap: x=%c, y=%cn”, x, y);tmp = x;x = y;y = tmp;printf(“After swap: x=%c, y=%cn”, x, y);(3)#include main( )char ch;ch = getchar( );printf(“%cn”, ch - 32);第4章1.(1)-(5):CAACA2.(1)BBB(2)AAABBBCCC(3)end(4)d=20(5)s=2,t=3(6)first third(7)y=0y=5y=10y=53.(1)yzxzx=A & ch=a&ch2&x-1&x=2(4)t=x;x=y;y=t;4.(1)#include main( )int x, y , z, t;scanf(“%d%d%d”, &x, &y, &z);if ( xy ) t=x; x=y; y=t;if( x z )t = x; x = z; z= t; if( y z )t = y; y= z; z = t;printf(“%d %d %dn”, x, y ,z);(2)#include main( )int score;scanf(“%d”, &score);if ( score 100 )printf(“成绩不合理n”);else if( score=90 )printf(“优秀n”);else if( score=80 )printf(“良好n”);else if( score = 70 )printf(“中等n”);else if( score = 60 )printf(“及格n”);elseprintf(“不及格n”);(3)#include main( )int n;int g,s,b,q;/各位上的数值scanf(“%d”, &n);g = n%10;/个位s = n/10%10;/十位b = n/100%10;/百位q = n/1000%10;/千位if( n 10 ) /一位数printf(“%dn”, 1);/位数printf(“%dn”, g); /各位上的数值else if ( n 100 ) /两位数printf(“%dn”, 2);/位数printf(“%d %dn”, g,s);else if ( n 1000 ) /三位数printf(“%dn”, 3);/位数printf(“%d %d %dn”, g, s, b);else if ( n 10000 ) /四位数printf(“%dn”, 4);/位数printf(“%d %d %d %dn”, g, s, b, q);(4)#include main( )int n;scanf(“%d”, &n);if( n % 3=0 & n%5=0 & n%7=0 )printf(“能同时被3、5、7整除n”);else if( n%3=0 & n%5=0)printf(“能被3和5整除n”);else if( n%3=0 & n%7=0 )printf(“能被3和7整除n”);else if( n%5=0 & n%7=0 )printf(“能被5和7整除n”);else if( n%3=0 | n%5=0 | n%7=0 )if( n%3=0 )printf(“能被3整除n”);else if( n%5=0 )printf(“能被5整除n”);elseprintf(“能被7整除n”);elseprintf(“不能被3、5、7中任一个数整除n”);(5)#include main( )int carType;/车型。1代表夏利;2代表富康;3代表桑塔纳double xiali = 2.1;/每公里价格double fukang = 2.4;double sangtana = 2.7;double distance; /距离double totalMoney;/总的收费printf(请输入您乘坐的车型:1代表夏利;2代表富康;3代表桑塔纳:);scanf(%d, &carType);printf(请输入您乘车的总路程:);scanf(%lf, &distance);if( carType = 1)/夏利if( distance 3 )totalMoney = 7.0;elsetotalMoney = 7 + xiali * (distance 3);else if( carType = 2 ) /富康if( distance 3 )totalMoney = 8.0;elsetotalMoney = 8 + fukang * (distance 3);else if( carType = 3 ) /富康if( distance 3 )totalMoney = 9.0;elsetotalMoney = 9 + sangtana * (distance 3);printf((四舍五入)您的车费为:%.0lfn, totalMoney );(6)#include main( )double a, b, c;scanf(“%lf%lf%lf”, &a, &b, &c);if( a+bc & b+ca & c+ab )if( a=b & b=c )printf(“等边三角形n”);else if( a=b | b= c | c=a )printf(“等腰三角形n”);elseprintf(“一般三角形n”);elseprintf(“不能构成三角形n”);第5章1.(1)C(2)C(3)K=36(4)C(5)B2.(1) 3次(2) x=1 & x=200&x210(3) e = 0(4) 6次(5) 103.(1) 20,10(2) 16,0(3) 7BAB4BAB1BC(4) ABABABC(5) * * *4.(1) a != b(2) n / 10(3) scanf(“%d”, &a);5. (3) 行int fac = 1, sum = 0;6.(1)#include main( )char ch;int alpha=0, space=0, digit=0, other=0;while( (ch=getchar( ) ) != n )if( ch=A&ch=a&ch=0 & ch=9)digit+;else if( = ch )space+;elseother+;printf(“%d %d %d %dn”, alpha, digit, space, other );(2)#include main( )int m20, m10;for(m20=1; m205; m20+)for(m10 = 1; m1010; m10+)if( 20*m20+10*m10 = 100 )printf(“%d, %dn”, m20, m10 );(3)#include main( )int x, y, z;for(x=0; x10; x+)for(y=0; y10; y+)for(z=0; z10; z+)if( x*100+y*10+z + y*100+z*10+z = 532 )printf(“%d %d %dn”, x, y, z);(4)#include main( )int row, spaceCount,starCount;int n;scanf(%d, &n);for( row = 1; row = n; +row)for( spaceCount = 1;spaceCount = n - row ;+ spaceCount)printf( ); /打印出某行上星号前的空格for( starCount = 1;starCount = 2* row - 1;+starCount )printf(*); /打印出某行上的所有星号printf(n); /换行/打印下半部分for(row=1; rown; row+)for( spaceCount = 1;spaceCount = row ;+ spaceCount)printf( ); /打印出某行上星号前的空格for( starCount = 1;starCount = 2*(n- row) - 1;+starCount )printf(*); /打印出某行上的所有星号printf(n); /换行(5)#include main( )int n;int g,s,b,q;int t;scanf(%d, &n);g = n % 10;s = n / 10 % 10;b = n /100 % 10;q = n/ 1000%10;g = (g+5)%10;s = (s+5)%10;b = (b+5)%10;q = (q+5)%10;/第1位和第4位交换t = g; g = q; q = t;/第2位和第3位交换t = s; s = b; b = t;printf(%d%d%d%dn, q,b,s,g);第6章1. (1)无返回值(2)double2(3)无限循环(死循环)(4)result = 7202.(1)#include void print1( int n );main( )int n;scanf(%d, &n);print1( n );void print1( int n )int row, col;for(row = 1; row=n; row+)for(col=1; col=row; col+)printf(#);printf(n);(2)#include void print2( int n );main( )int n;scanf(%d, &n);print2( n );void print2( int n )int row, col;for(row = 1; row=n; row+)for(col=1; col=2*row-1; col+)printf();printf(n);(3)#include int yearOld(int byear, int bmonth, int bday, int nyear, int nmonth, int nday);main( )int nowy,nowm,nowd;int by, bm, bd;int age;printf(请输入生日:);scanf(%d%d%d, &by, &bm, &bd);printf(请输入现在日期:);scanf(%d%d%d, &nowy, &nowm, &nowd);age= yearOld(by,bm,bd,nowy,nowm,nowd);printf(age=%dn, age);int yearOld(int byear, int bmonth, int bday, int nyear, int nmonth, int nday)int age;age = nyear - byear;if( nmonthbday)age-;return age;(4)#include int sum( int n );main( )int n,s;scanf(%d, &n);s = sum( n );printf(s=%dn, s);int sum( int n )int s=0;while ( n )s += n % 10;n /= 10;return s;(5)#include double sumfac( int n );main( )int n;scanf(%d, &n);printf(%.0lfn, sumfac( n ) );double sumfac( int n )double f=1.0, s = 0.0;int i;for(i=1;i=n; i+)f *= i;s += f;return f;(6)#include int gcd(int m , int n);main( )int m, n;scanf(%d%d, &m, &n);printf(%dn, gcd(m ,n) );int gcd(int m, int n)int t,r;if( m n ) t = m; m= n; n = t; r = m % n;while( r )m = n; n = r;r = m % n;return n;(7)#include int gcd(int m , int n);int lcm(int m, int n);main( )int m, n;scanf(%d%d, &m, &n);printf(%dn, lcm(m ,n) );int gcd(int m, int n)int t,r;if( m n ) t = m; m= n; n = t; r = m % n;while( r )m = n; n = r;r = m % n;return n;int lcm(int m, int n)return m*n/gcd(m,n);(8)#include double mypower(double x, int y);main( )double x; int y;scanf(%lf%d, &x, &y);printf(%lfn, mypower(x,y) );double mypower(double x, int y)int i;double f=1.0;for(i=1; i=y; i+)f *= x;return f;第7章1.(1)6(2)5(3)不能(4)int a32=1,2, 3,4, 5,6 ;(5)69(6)abcG2.(1)#include void reverse( int a , int n );int main( )int array10=0;int i;printf(“请输入10个整数:”);for( i=0; i10; i+)scanf(“%d”, &arrayi);reverse( array, 10); /调用函数逆序存储数组中的数据printf(“逆序后的元素为:n”);for( i=0; i10; i+)printf(“%5d”, arrayi);printf(“n”);return 0;void reverse( int a , int n )int i;int tmp;for( i=0;in/2;+i)tmp = ai;ai = an-i-1;an-i-1 = tmp;(2)#include #include void reverseStr( char str );main( )char s100;gets( s );reverseStr( s );puts( s );void reverseStr( char str )int i,j;char t;i=0;j=strlen(str)-1;while( i j )t = stri;stri = strj;strj = t;i+;j-;(3)#include int copyTo(int s1, int n, int s2 );main( )int s110, s210;int i,count;for(i=0; i10; i+)scanf(%d, &s1i);count = copyTo(s1, 10, s2);for(i=0; icount; i+)printf(%d , s2i );printf(n);int copyTo(int s1, int n, int s2 )int i, j=0;for(i=0; in; i+)if( s1i % 2 )s2j+ = s1i;return j;(4)#include void copyToStr(char str1 , char str2 );main( )char s1100, s2100;gets(s1);copyToStr( s1, s2 );puts(s2);void copyToStr(char str1 , char str2 )int i=0,j=0;while( str1i != 0 )if( str1i=a&str1i=z )str2j = str1i;j+;i+;str2j = 0;return j;(5)#include void deleteAll( char str , char ch);main( )char s100, ch;gets( s );ch = getchar( );deleteAll( s, ch );puts( s );void deleteAll( char str , char ch)int i, j;i = 0; j = 0;while( stri )if( stri != ch )strj+ = stri;i+;strj = 0;(6)#include void replaceAll(char str , int ch1, char ch2);main( )char s100, c1, c2;gets( s );c1 = getchar( );c2 = getchar( );replaceAll( s, c1, c2 );puts( s );void replaceAll(char str , int ch1, char ch2)int i;i = 0;while( stri )if( stri = ch1 )stri = ch2;i+;(7)#include int transformToBin( int dnum, int bin ) ;int main( )int array32=0; /保存转换后的二进制数int num; /待转换的整数int cc;/最后得到的二进制总共多少位printf(“请输入一个整数:”);scanf(“%d”, &num);cc = transformToBin( num, array ); /调用转换函数cc-; /往回退一个元素下标,使cc指向最后一个元素for( ; cc=0;cc- ) /输出转换后的二进制数printf(“%d”, arraycc);printf(“n”);return 0;int transformToBin( int dnum, int bin )int count = 0;while ( dnum ) /当dnum还未转换完毕 bincount+ = dnum % 2;/余数保留到数组对应元素中dnum /= 2;/数本身除2return count;(8)#include int transformToHex( int dnum, char hex ) ;int main( )char array32; /保存转换后的进制数int num; /待转换的整数int cc;/最后得到的进制总共多少位printf(请输入一个整数:);scanf(%d, &num);cc = transformToHex( num, array ); /调用转换函数cc-; /往回退一个元素下标,使cc指向最后一个元素for( ; cc=0;cc- ) /输出转换后的进制数printf(%c, arraycc);printf(n);return 0;int transformToHex( int dnum, char hex )int count = 0;int t;while ( dnum ) /当dnum还未转换完毕 t = dnum % 16;if( t 10 )hexcount = t+0;/余数保留到数组对应元素中else hexcount = t-10+A;count+;dnum /= 16;/数本身除16return count;(9)#include #include #include #define M 5 /行#define N 6 /列void generate( int a N, int row, int col );void display( int aN, int row, int col);void getMaxEveryRow(int aN, int row, int col, int y);main( )int arrMN, yM=0;int i;generate(arr, M, N);display(arr, M, N);getMaxEveryRow(arr,M,N,y);/输出最大值for(i=0; iM; i+)printf(%d , yi);printf(n);void generate( int a N, int row, int col )int i,j;srand( time(NULL) );for(i=0; iM; i+)for(j=0; jN; j+)aij = rand( )%101;void display( int aN, int row, int col)int i,j;for(i=0; iM; i+)for(j=0; jN; j+)printf(%4d, aij);printf(n);void getMaxEveryRow(int aN, int row, int col,int y)int i,j;for(i=0; iM; i+)yi = ai0;for(j=1; jN; j+)if( yi aij )yi = aij;(10)#include #include #include #define M 5 /行#define N 6 /列void generate( int a N, int row, int col );void display( int aN, int row, int col);void getMinEveryCol(int aN, int row, int col);main( )int arrMN;int i;generate(arr, M, N);display(arr, M, N);getMinEveryCol(arr,M,N);/输出最小值for(i=0; iN; i+)printf(%d , arr0i);printf(n);void generate( int a N, int row, int col )int i,j;srand( time(NULL) );for(i=1; iM; i+)for(j=0; jN; j+)aij = rand( )%101;void display( int aN, int row, int col)int i,j;for(i=1; iM; i+)for(j=0; jN; j+)printf(%4d, aij);printf(n);void getMinEveryCol(int aN, int row, int col)int i,j;for(i=0; iN; i+)a0i = a1i;for(j=2; j aji )a0i = aji;第8章1. (1)局部(2)void(3)autostaticexternregister(4)auto(5)return(6)递归(7)求 1!+2!+3!+4!+5!(8)注意全局变量和局部变量的区别2.(1)#include #include double xc( double x, double y );main( )double a, b;double c;scanf(“%lf%lf”, &a, &b);c = xc( a, b );printf(“ %lfn”, c);double xc( double x, double y )return sqrt( x*x+y*y );(2)#include long seconds(int hour, int minute, int second);main( )int h,m,s;long sec;printf(输入时间:);scanf(%d%d%d, &h, &m, &s);sec = seconds(h,m,s);printf(离12点最近的秒数:%ldn, sec);long seconds(int hour, int minute, int second)long s;if( hour 6 )s = second+minute*60+hour*60*60;elses = 60-second+(60-minute-1)*60+(12-hour-1)*60*60;return s;(3)#include int fun( int n );main( )int n;scanf(%d, &n);if( fun( n ) ) /是质数,则输出该数;不是的话,不作任何处理printf(%dn, n );int fun( int n )int i;for(i=2; in; i+)if( n%i = 0 )break;if( in )return 0;elsereturn 1;(4)#include int fun( int n );main( )int n;int count=0;for(n=2;n1000;n+)if( fun( n ) ) printf(%4d, n );count+;if( count % 10 = 0 )printf(n);printf(n);int fun( int n )int i;for(i=2; in; i+)if( n%i = 0 )break;if( in )return 0;elsereturn 1;(5)#include int func( int n );main( )int n;scanf(%d, &n);printf(%dn, func(n) );int func( int n )if ( 1 = n )return 3;return 2*func(n-1)-1;(6)#include int gcd(int m, int n);main( )int x,y,t;scanf(%d%d, &x, &y);if( x y )t=x; x=y; y=t;printf(%dn, gcd(x,y) );int gcd(int m, int n)if (0=n)return m;return gcd(n, m%n);第9章1.(1)xyzNKT(2)bcdefgh(3)4,4(4)qponmzyx(5)abcCD(6)02(1)#include #include void reverse( int *p, int n);main( )int i;int a10=1,2,3,4,5,6,7,8,9,10; reverse(a,10);for(i=0; i10; i+)printf(%d , ai);printf(n);void reverse( int *p, int n)int *q;int t;q = p + n - 1;while( p q )t = *p; *p = *q;*q = t;p+;q-;(2)#include #include void reverseStr( char *str );main( )char s100;gets(s);reverseStr(s);puts(s);void reverseStr( char *str )char *pEnd,t;pEnd = str + strlen(str) - 1;while( str pEnd )t = *str;*str = *pEnd;*pEnd = t;str+;pEnd-;(3)#include int copyTo(int *s1, int n, int *s2);main( )int a10=1,2,3,4,5,6,7,8,9, 10;int b10, count,i;count=copyTo(a,10,b);for(i=0; icount; i+)printf(%d , bi );printf(n);int copyTo(int *s1, int n, int *s2)int count=0;int *ps1, *ps2;ps2 = s2;for(ps1 = s1; ps1 s1 + n; ps1+)if( *ps1 % 2 )/奇数*ps2+ = *ps1;return ps2 - s2;(4)#include void copyToStr(char *str1, char *str2);main( )char s1100, s2100;gets( s1 );copyToStr(s1, s2);puts( s2 );void copyToStr(char *str1, char *str2)while( *str1 )if( *str1 = a & *str1 = z )*str2+=*str1;str1+;*str2 = 0;(5)#include void deleteAll(char *str, char ch);main( )char s100, c;gets(s);c = getchar( );deleteAll(s, c);puts(s);void deleteAll(char *str, char ch)char *p;p = str;while( *str )if( *str != ch )*p+ = *str;str+;*p = 0;(6)#include void replaceAll( char *str, char ch1, char ch2);main( )char s100, c1, c2;printf(输入字符串:);gets(s);printf(输入查找字符:);c1 = getchar( );fflush(stdin); /清除键盘缓冲区printf(输入替换字符:);c2 = getchar( );replaceAll(s,c1, c2);puts(s);void replaceAll( char *str, char ch1, char ch2)while( *str )if( *str = ch1 )*str = ch2;str+;(7)#include int transformToBin( int dnum, int *bin ) ;int main( )int array32; /保存转换后的进制数int num; /待转换的整数int cc;/最后得到的进制总共多少位printf(请输入一个整数:);scanf(%d, &num);cc=transformToBin( num, array ); /调用转换函数cc-; /使cc指向最后一个元素for( ; cc=0;cc- ) /输
展开阅读全文