首页 养生问答 疾病百科 养生资讯 女性养生 男性养生
您的当前位置:首页正文

C语言程序设计教程课后习题参考答案

2024-03-28 来源:华佗健康网
《C语言程序设计教程》

课后习题参考答案

习题1

1.(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(“%lf\\n”, 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)10101(5)-998(6)1)202)83)704.(1)

#include main( ){

14)0

05)0

6)0

double r, h ,v;r = 2.5;h = 3.5;

v = 3.14*r*r*h;

printf(“v=%lf\\n”, v);}

(2)

#include main( ){

char ch;

ch = getchar( );

printf(“%c\\n”, ch + 32);}

(3)

#include main( ){

printf(“ *\\n”);printf(“ ***\\n”);printf(“ *****\\n”);printf(“*******\\n”);}

(4)

#include main( ){

double x;

scanf(“%lf”, &x);

printf(“%d , %lf\\n”, (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(“%lf\\n”, 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)0

3.

(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(“%d\\n”, (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=%c\\n”, x, y);

tmp = x;x = y;y = tmp;printf(“After swap: x=%c, y=%c\\n”, x, y);}

(3)

#include main( ){

char ch;

ch = getchar( );

printf(“%c\\n”, 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=5

y=10y=5

3.(1)y=’A’ && ch<=’Z’

ch>=’a’&&ch<=’z’ch = ch-32(3)x>2&&x<=10x>-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 ( x>y )

{ 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 %d\\n”, x, y ,z);}(2)

#include main( ){

int score;

scanf(“%d”, &score);

if ( score < 0 || 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”);else

printf(“不及格\\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(“%d\\n”, 1);//位数

printf(“%d\\n”, g); //各位上的数值}

else if ( n < 100 ) //两位数{

printf(“%d\\n”, 2);//位数printf(“%d %d\\n”, g,s);

}

else if ( n < 1000 ) //三位数{

printf(“%d\\n”, 3);//位数

printf(“%d %d %d\\n”, g, s, b);}

else if ( n < 10000 ) //四位数{

printf(“%d\\n”, 4);//位数

printf(“%d %d %d %d\\n”, 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”);else

printf(“能被7整除\\n”);

}else

printf(“不能被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\

printf(\"请输入您乘车的总路程:\");scanf(\"%lf\if( carType == 1)//夏利{

if( distance < 3 )

totalMoney = 7.0;else

totalMoney = 7 + xiali * (distance – 3);

}

else if( carType == 2 ) //富康{

if( distance < 3 )

totalMoney = 8.0;else

totalMoney = 8 + fukang * (distance – 3);

}

else if( carType == 3 ) //富康{

if( distance < 3 )

totalMoney = 9.0;else

totalMoney = 9 + sangtana * (distance – 3);

}

printf(\"(四舍五入)您的车费为:%.0lf\\n\

}(6)

#include main( ){

double a, b, c;

scanf(“%lf%lf%lf”, &a, &b, &c);if( a+b>c && b+c>a && c+a>b ){

if( a==b && b==c )

printf(“等边三角形\\n”);else if( a==b || b== c || c==a )

printf(“等腰三角形\\n”);

else

printf(“一般三角形\\n”);

}else

printf(“不能构成三角形\\n”);

}

第5章

1.(1)C(2)C(3)K=36(4)C(5)B2.

(1) 3次

(2) x>=1 && x<=10 || x>=200&&x<210(3) e == 0(4) 6次(5) 10

3.

(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<=’Z’ || ch>=’a’&&ch<=’z’)

alpha++;

else if( ch>=’0’ && ch<=’9’)

digit++;

else if( ‘ ‘ == ch )

space++;else

other++;

}

printf(“%d %d %d %d\\n”, alpha, digit, space, other );}(2)

#include main( ){

int m20, m10;

for(m20=1; m20<5; m20++){

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

if( 20*m20+10*m10 == 100 )

printf(“%d, %d\\n”, m20, m10 );

}}(3)

#include main( ){

int x, y, z;

for(x=0; x<10; x++)

for(y=0; y<10; y++)

for(z=0; z<10; z++)

if( x*100+y*10+z + y*100+z*10+z == 532 )

printf(“%d %d %d\\n”, x, y, z);

}(4)

#include main( ){

int row, spaceCount,starCount;int n;

scanf(\"%d\

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; rowfor( 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\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%d\\n\}

第6章1.

(1)无返回值(2)double2

(3)无限循环(死循环)(4)result = 7202.(1)

#include void print1( int n );

main( ){

int n;

scanf(\"%d\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\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\printf(\"请输入现在日期:\");

scanf(\"%d%d%d\age= yearOld(by,bm,bd,nowy,nowm,nowd);printf(\"age=%d\\n\}

int yearOld(int byear, int bmonth, int bday, int nyear, int nmonth, int nday){

int age;

age = nyear - byear;if( nmonthage--;

else if( nmonth == bmonth && nday>bday)

age--;return age;}

(4)

#include int sum( int n );main( )

{

int n,s;

scanf(\"%d\s = sum( n );

printf(\"s=%d\\n\}

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\

printf(\"%.0lf\\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\printf(\"%d\\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\printf(\"%d\\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\

printf(\"%lf\\n\}

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 a[3][2]={{1,2}, {3,4}, {5,6} };(5)69(6)abcG2.(1)

#include

void reverse( int a[ ], int n );int main( ){

int array[10]={0};int i;

printf(“请输入10个整数:”);for( i=0; i<10; i++)

scanf(“%d”, &array[i]);

reverse( array, 10); //调用函数逆序存储数组中的数据printf(“逆序后的元素为:\\n”);for( i=0; i<10; i++)

printf(“%5d”, array[i]);printf(“\\n”);return 0;}

void reverse( int a[ ], int n ){

int i;int tmp;

for( i=0;itmp = a[i];a[i] = a[n-i-1];}}

(2)

#include #include

void reverseStr( char str[ ] );main( ){

char s[100];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 = str[i];

a[n-i-1] = tmp;

str[i] = str[j];str[j] = t;i++;j--;}}

(3)

#include

int copyTo(int s1[], int n, int s2[ ]);main( ){

int s1[10], s2[10];int i,count;

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

scanf(\"%d\count = copyTo(s1, 10, s2);for(i=0; iprintf(\"%d \printf(\"\\n\");}

int copyTo(int s1[], int n, int s2[ ]){

int i, j=0;

for(i=0; iif( s1[i] % 2 )

s2[j++] = s1[i];

}

return j;}

(4)

#include

void copyToStr(char str1[ ], char str2[ ] );main( ){

char s1[100], s2[100];gets(s1);

copyToStr( s1, s2 );puts(s2);}

void copyToStr(char str1[ ], char str2[ ] )

{

int i=0,j=0;

while( str1[i] != '\\0' ){

if( str1[i]>='a'&&str1[i]<='z' ){

str2[j] = str1[i];j++;}i++;}

str2[j] = '\\0';return j;}

(5)

#include

void deleteAll( char str[ ], char ch);main( ){

char s[100], ch;gets( s );

ch = getchar( );deleteAll( s, ch );puts( s );}

void deleteAll( char str[ ], char ch){

int i, j;i = 0; j = 0;

while( str[i] ){

if( str[i] != ch ){

str[j++] = str[i];}i++;}

str[j] = '\\0';}

(6)

#include

void replaceAll(char str[ ], int ch1, char ch2);main( ){

char s[100], 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( str[i] ){

if( str[i] == ch1 )

str[i] = ch2;i++;}}

(7)

#include

int transformToBin( int dnum, int bin[ ] ) ;

int main( )

{

int array[32]={0}; //保存转换后的二进制数int num; //待转换的整数

int cc;//最后得到的二进制总共多少位printf(“请输入一个整数:”);scanf(“%d”, &num);

cc = transformToBin( num, array ); //调用转换函数

cc--; //往回退一个元素下标,使cc指向最后一个元素for( ; cc>=0;cc-- ) //输出转换后的二进制数

printf(“%d”, array[cc]);printf(“\\n”);return 0;}

int transformToBin( int dnum, int bin[ ] ){

int count = 0;

while ( dnum ) //当dnum还未转换完毕 {

bin[count++] = dnum % 2;//余数保留到数组对应元素中dnum /= 2;//数本身除2}

return count;

}

(8)

#include

int transformToHex( int dnum, char hex[ ] ) ;int main( ){

char array[32]; //保存转换后的进制数int num; //待转换的整数

int cc;//最后得到的进制总共多少位printf(\"请输入一个整数:\");scanf(\"%d\

cc = transformToHex( num, array ); //调用转换函数

cc--; //往回退一个元素下标,使cc指向最后一个元素for( ; cc>=0;cc-- ) //输出转换后的进制数

printf(\"%c\array[cc]);printf(\"\\n\");return 0;}

int transformToHex( int dnum, char hex[ ] ){

int count = 0;int t;

while ( dnum ) //当dnum还未转换完毕 {

t = dnum % 16;if( t < 10 )

hex[count] = t+'0';//余数保留到数组对应元素中else

hex[count] = t-10+'A';count++;

dnum /= 16;//数本身除16}

return count;}

(9)

#include #include #include

#define M 5 //行#define N 6 //列

void generate( int a[ ][N], int row, int col );void display( int a[][N], int row, int col);

void getMaxEveryRow(int a[][N], int row, int col, int y[]);main( ){

int arr[M][N], y[M]={0};int i;

generate(arr, M, N);display(arr, M, N);

getMaxEveryRow(arr,M,N,y);//输出最大值for(i=0; iprintf(\"%d \printf(\"\\n\");}

void generate( int a[ ][N], int row, int col ){

int i,j;

srand( time(NULL) );for(i=0; ifor(j=0; ja[i][j] = rand( )%101;

}

void display( int a[][N], int row, int col){

int i,j;

for(i=0; ifor(j=0; jprintf(\"%4d\printf(\"\\n\");}}

void getMaxEveryRow(int a[][N], int row, int col,int y[]){

int i,j;

for(i=0; i{

y[i] = a[i][0];for(j=1; jif( y[i] < a[i][j] )

y[i] = a[i][j];}}

(10)

#include #include #include

#define M 5 //行#define N 6 //列

void generate( int a[ ][N], int row, int col );void display( int a[][N], int row, int col);

void getMinEveryCol(int a[][N], int row, int col);main( ){

int arr[M][N];int i;

generate(arr, M, N);display(arr, M, N);

getMinEveryCol(arr,M,N);//输出最小值for(i=0; iprintf(\"%d \printf(\"\\n\");}

void generate( int a[ ][N], int row, int col ){

int i,j;

srand( time(NULL) );for(i=1; ifor(j=0; ja[i][j] = rand( )%101;

}

void display( int a[][N], int row, int col){

int i,j;

for(i=1; ifor(j=0; jprintf(\"%4d\printf(\"\\n\");}

}

void getMinEveryCol(int a[][N], int row, int col){

int i,j;

for(i=0; ia[0][i] = a[1][i];for(j=2; jif( a[0][i] > a[j][i] )

a[0][i] = a[j][i];

}}

第8章1.

(1)局部(2)void(3)auto(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(“ %lf\\n”, c);}

double xc( double x, double y ){

return sqrt( x*x+y*y );}(2)

staticexternregister

#include

long seconds(int hour, int minute, int second);main( ){

int h,m,s;long sec;

printf(\"输入时间:\");

scanf(\"%d%d%d\sec = seconds(h,m,s);

printf(\"离12点最近的秒数:%ld\\n\}

long seconds(int hour, int minute, int second){

long s;

if( hour < 6 ){

s = second+minute*60+hour*60*60;}else{

s = 60-second+(60-minute-1)*60+(12-hour-1)*60*60;}

return s;}

(3)

#include int fun( int n );main( ){

int n;

scanf(\"%d\

if( fun( n ) ) //是质数,则输出该数;不是的话,不作任何处理

printf(\"%d\\n\

}

int fun( int n ){

int i;

for(i=2; iif( n%i == 0 )

break;

if( ireturn 0;else

return 1;

}

(4)

#include int fun( int n );main( ){

int n;

int count=0;

for(n=2;n<1000;n++)

if( fun( n ) ) {

printf(\"%4d\count++;

if( count % 10 == 0 )

printf(\"\\n\");

}

printf(\"\\n\");

}

int fun( int n ){

int i;

for(i=2; iif( n%i == 0 )

break;

if( ireturn 0;else

return 1;

}

(5)

#include int func( int n );main( ){

int n;

scanf(\"%d\

printf(\"%d\\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\if( x < y ){

t=x; x=y; y=t;}

printf(\"%d\\n\}

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)0

2.(1)

#include #include

void reverse( int *p, int n);main( ){

int i;

int a[10]={1,2,3,4,5,6,7,8,9,10}; reverse(a,10);

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

printf(\"%d \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 s[100];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 a[10]={1,2,3,4,5,6,7,8,9, 10};int b[10], count,i;

count=copyTo(a,10,b);for(i=0; iprintf(\"%d \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 s1[100], s2[100];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 s[100], 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 s[100], 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 array[32]; //保存转换后的进制数int num; //待转换的整数

int cc;//最后得到的进制总共多少位printf(\"请输入一个整数:\");scanf(\"%d\

cc=transformToBin( num, array ); //调用转换函数cc--; //使cc指向最后一个元素

for( ; cc>=0;cc-- ) //输出转换后的进制数

printf(\"%d\array[cc]);printf(\"\\n\");return 0;}

int transformToBin( int dnum, int *bin ){

int *pb;int t;pb = bin;

while ( dnum ) //当dnum还未转换完毕 {

t = dnum % 2;*pb = t;pb++;

dnum /= 2;//数本身除2}

return pb-bin;}

(8)

#include #include

void transformToHex( int dnum, char *hex ) ;int main( ){

char array[32]; //保存转换后的进制数int num; //待转换的整数

int cc;//最后得到的进制总共多少位printf(\"请输入一个整数:\");scanf(\"%d\

transformToHex( num, array ); //调用转换函数cc=strlen(array)-1; //使cc指向最后一个元素for( ; cc>=0;cc-- ) //输出转换后的进制数

printf(\"%c\array[cc]);printf(\"\\n\");return 0;}

void transformToHex( int dnum, char *hex ){

char *ph;int t;ph = hex;

while ( dnum ) //当dnum还未转换完毕 {

t = dnum % 16;if( t < 10 )

*ph = t+'0';else

*ph = t-10+'A';ph++;

dnum /= 16;//数本身除16}

*ph = '\\0';}

(9)

#include

void strToLow(char *str);main( )

{

char s[100];gets( s );

strToLow( s );puts( s );

}

void strToLow( char *str ){

while( *str ){

if( *str>='A'&&*str<='Z' )

*str = *str + 32;str++;}}

第10章1.

(1)12(2)(++p)->num(5)B

(6)B

2.(1)

struct myDate{

int year, month, day;};

(2)

struct empInfo{

char empNo[15];char empName[10];char xb; //性别int age;

struct myDate empDay;double salary;};

(3)

#include

typedef struct STUDENT {

char name[20];float height;

3)D7)10,A

4) p2 != NULL

(((int score;}STUD;main( ){

STUD a;

printf(“姓名:”);gets( a.name);printf(“身高:”);

scanf(“%f”, &a.height);printf(“成绩:”);

scanf(“%d”, &a.score);

//输出信息(仅为测试用)

printf(“%s %.2f %d \\n”, a.name, a.height, a.score);}

第11章1.

(1)打开文件:fopen

关闭文件:fclose使用:

FILE *fp;

fp = fopen(“myfile”, “r”);…

fclose( fp );(2)”w”

(3)文本文件:内容均以字符的形式存储,占的字节数可能会大些

二进制文件:内容以二进制编码的形式存储,占的字节数可能会稍小。(4)第一个参数:读取得到的信息存储在内存的哪个地方

第二个参数:读取的字节数 第三个参数:读取的块数

第四个参数:文件指针,表示从哪个文件中读取数据(5)第一个参数:待写入文件的信息存储在内存的哪个地方

第二个参数:写入的字节数 第三个参数:写入的块数

第四个参数:文件指针,表示要写入到哪个文件中

(6)为了安全,gets少用,因为其没有指定输入字符的大小,限制输入缓冲区得大小,如果输入的字符大于定义的数组长度,会发生内存越界,堆栈溢出。后果非常怕怕fgets会指定大小,如果超出数组大小,会自动根据定义数组的长度截断。

2.(1)

#include #include main( ){

FILE *fpr, *fpw;char ch;

fpr = fopen(\"a.txt\fpw = fopen(\"b.txt\if( NULL==fpr ){

printf(\"文件打开不成功\\n\");return 0;}

if( NULL == fpw ){

printf(\"文件打开不成功\\n\");return 0;}

while( !feof( fpr ) ){

ch = fgetc( fpr );

fprintf(fpw, \"%c\}

fclose( fpr );fclose( fpw );}

(2)

#include main( ){

FILE *fp1, *fp2;

double pi = 3.1415926;

fp1 = fopen( \"a.txt\以文本的形式写入fp2 = fopen( \"b.txt\以二进制的形式写入if( NULL==fp1 ){

printf(\"文件打开不成功\\n\");return 0;}

if( NULL == fp2 ){

printf(\"文件打开不成功\\n\");return 0;}

fprintf(fp1, \"%lf\

fwrite(&pi,sizeof(pi),1,fp2);fclose( fp1 );fclose( fp2 );}

打开之后的效果:

(3)

#include #include main( ){

FILE *fpr;

fpr = fopen( \"a.txt\

if( NULL==fpr ){

printf(\"文件打开不成功\\n\");return 0;}

while( !feof( fpr ) ){

putchar( fgetc( fpr ) );}

fclose( fpr );}

(4)

#include

typedef struct STUDENT {

char no[15];char name[10];int score;}STUD;#define N 3main( ){

STUD s[N];int i;

FILE *fp;

int count; //读取文件时才使用for(i=0; iprintf(\"请输入第%d个学生信息:\\n\" , i+1);printf(\" 学号:\");gets( s[i].no);

printf(\" 姓名:\");gets( s[i].name);

printf(\" 成绩:\");scanf(\"%d\

fflush(stdin);//清除键盘缓冲区}

//打开文件,并写入fp = fopen( \"a.txt\if( NULL == fp){

printf(\"文件打开不成功,不能写入\\n\");return 0;}

for(i=0; ifwrite( &s[i], sizeof(s[i]), 1, fp );fclose(fp);

//以下代码从文件中读取出信息,并显示在屏幕上fp = fopen(\"a.txt\

count = 0;

while( !feof( fp ) ){

if( fread(&s[count], sizeof(s[count]), 1, fp) )

count++;

}

fclose(fp);

//输出

for(i=0; iprintf(\"%-15s\printf(\"%-10s\printf(\"%-4d\\n\}}

因篇幅问题不能全部显示,请点此查看更多更全内容