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

内容发布更新时间 : 2024/12/23 2:54:30星期一 下面是文章的全部内容请认真阅读。

return 1; }

(4)

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

int n;

int count=0;

for(n=2;n<1000;n++) if( fun( n ) ) { printf(\ count++; if( count % 10 == 0 ) printf(\ } printf(\}

int fun( int n ) {

int i;

for(i=2; i

(5)

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

int n;

scanf(\

printf(\}

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(\ if( x < y ) { t=x; x=y; y=t; }

printf(\}

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(\ printf(\}

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; i

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