【3.31】輸入n值,輸出高度為n的等邊三角形。例如當(dāng)n=4時的圖形如下:
*
***
*****
*******
#include
void prt( char c, int n )
{ if( n>0 )
{ printf( "%c", c );
、 ;
}
}
main()
{ int i, n;
scanf("%d", &n);
for( i=1; i<=n; i++ )
{ ② ;
③ ;
printf("\n");
}
}
【3.32】下面的函數(shù)實現(xiàn)N層嵌套平方根的計算。
double y(double x, int n)
{ if( n==0 )
return(0);
else return ( sqrt(x+( ① )) );
}
【3.33】函數(shù)revstr(s)將字符串s置逆,如輸入的實參s為字符串"abcde", 則返回時 s 為字符串"edcba"。遞歸程序如下:
revstr( char *s )
{ char *p=s, c;
while(*p) p++;
、 ;
if(s
{ c=*s;
*s=*p;
、 ;
revstr(s+1);
、 ;
}
}
如下是由非遞歸實現(xiàn)的revstr(s)函數(shù):
revstr (s)
char *s;
{ char *p=s, c;
while( *p ) p++;
、 ;
while( s
{ c=*s;
⑤ = *p;
*p-- = c;
}
}
【3.34】下面函數(shù)用遞歸調(diào)用的方法,將str中存放的長度為n的字符串反轉(zhuǎn)過來,例如原來是"ABCDE",反序為"EDCBA"。
void invent(char *str,int n)
{ char t;
t=*str; *str=*(str+n-1); *(str+n-1)=t; if( n>2 ) invent ( ① ,n-2);
else ② ;
}
【3.35】從鍵盤上輸入10個整數(shù),程序按降序完成從大到小的排序。
#include
int array[10];
sort( int *p, int *q )
{ int *max, *s;
if( ① )
return;
max=p; for( s=p+1; s<=q; s++)
if( *s > *max )
、 ; swap( ③ );
sort( ④ ); }
swap( int *x, int *y ) { int temp;
temp=*x;
*x=*y;
*y=temp;
}
main()
{ int i; printf("Enter data :\n"); for( i=0; i<10; i++)
scanf("%d", &array[i]); sort( ⑤ );
printf("Output:");
for( i=0; i<10; i++)
printf("%d ", array[i]);
}
【3.36】下面函數(shù)的功能是將一個整數(shù)存放到一個數(shù)組中。存放時按逆序存放。例如:483存放成"384"。
#include
void convert(char *a, int n)
{ int i;
if((i=n/10) !=0 )
convert( ① , i );
*a = ② ;
}
char str[10]= " ";
main()
{ int number;
scanf("%d", &number);
convert( str, number );
puts(str);
}
【3.37】下面程序的功能是實現(xiàn)數(shù)組元素中值的逆轉(zhuǎn)。
#include
main()
{ int i,n=10,a[10]={1,2,3,4,5,6,7,8,9,10};
invert(a,n-1);
for(i=0;i<10;i++)
printf("%4d",a[i]);
printf("\n");
}
invert(int *s,int num)
{ int *t,k;
t=s+num;
while( ① )
{ k=*s;
*s=*t;
*t=k;
、 ;
③ ;
}
}
【3.38】下面程序通過指向整型的指針將數(shù)組a[3][4] 的內(nèi)容按3行×4列的格式輸出,請給printf( )填入適當(dāng)?shù)膮?shù),使之通過指針p將數(shù)組元素按要求輸出。
#include
int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}}, *p=a;
main()
{ int i,j;
for(i=0;i<3;i++ )
{ for(j=0;j<4;j++ )
printf("%4d ", ① );
}
}
【3.39】下面程序的功能是:從鍵盤上輸入一行字符,存入一個字符數(shù)組中,然后輸出該字符串。
#include
main ( )
{ char str[81], *sptr;
int i;
for(i=0;i<80;i++ )
{ str[i]=getchar( );
if(str[i]== '\n') break;
}
str[i]= ① ;
sptr=str;
while( *sptr )
putchar( *sptr ② );
}
【3.40】下面函數(shù)的功能是將字符變量的值插入已經(jīng)按ASCII碼值從小到大排好序的字符串中。
void fun(char *w,char x,int *n)
{ int i,p=0;
while(x>w[p]) ① ;
for(i=*n;i>=p;i--) ② ;
w[p]=x;
++*n;
}
【3.41】下面程序的功能是從鍵盤上輸入兩個字符串,對兩個字符串分別排序;然后將它們合并,合并后的字符串按ASCII碼值從小到大排序,并刪去相同的字符。
#include
strmerge(a,b,c) /* 將已排好序的字符串a(chǎn)、b合并到c */
char *a,*b,*c;
{ char t,*w;
w=c;
while( *a!= '\0' ① *b!='\0' )
{ t= ② ?*a++:*b<*a ? *b++ : ( ③ ); /* 將*a、*b的小者存入t */
if( *w ④ '\0' ) *w=t;
else if( t ⑤ *w) *++w=t; /* 將與*w不相同的t存入w */
}
while( *a != '\0' ) /* 以下將a或b中剩下的字符存入w */
if( *a != *w ) *++w=*a++;
else a++;
while( *b != '\0')
if( *b != *w ) *++w=*b++;
else b++;
*++w = ⑥ ;
}
strsort( char *s ) /* 將字符串s中的字符排序 */
{ int i,j,n;
char t,*w;
、 ;
for( n=0;*w != '\0'; ⑧ )
w++;
for( i=0;i for( j=i+1;j if( s[i]>s[j] ) { ⑨ } } main( ) { char s1[100],s2[100],s3[200]; printf("\nPlease Input First String:"); scanf("%s",s1); printf("\nPlease Input Second String:"); scanf("%s",s2); strsort(s1); strsort(s2); 、 = '\0'; strmerge(s1,s2,s3); printf("\nResult:%s",s3); } 【3.42】已知某數(shù)列前兩項為2和3,其后繼項根據(jù)前面最后兩項的乘積,按下列規(guī)則生成: 、 若乘積為一位數(shù),則該乘積即為數(shù)列的后繼項; 、 若乘積為二位數(shù),則該乘積的十位上的數(shù)字和個位上的數(shù)字依次作為數(shù)列的兩個后繼項。 下面的程序輸出該數(shù)列的前N項及它們的和,其中,函數(shù)sum(n,pa) 返回數(shù)列的前N項和,并將生成的前N項存入首指針為pa的數(shù)組中,程序中規(guī)定輸入的N值必須大于2,且不超過給定的常數(shù)值MAXNUM。 例如:若輸入N的值為10,則程序輸出如下內(nèi)容: sum(10)=44 2 3 6 1 8 8 6 4 2 4 #include "stdio.h" #define MAXNUM 100 int sum(n, pa) int n, *pa; { int count, total, temp; *pa = 2; ① =3; total=5; count=2; while( count++ { temp = *(pa-1) * *pa; if( temp<10 ) { total += temp; *(++pa) = temp; } else { ② = temp/10; total += *pa; if( count { count ++; pa++; 、 = temp%10; total += *pa; } } } 、 ; } main() { int n, *p, *q, num[MAXNUM]; do { printf("Input N=? (2 scanf("%d", &n); }while( ⑤ ); printf("\nsum(%d)=%d\n", n, sum(n, num)); for( p=num, q = ⑥ ; p printf("%4d", *p); printf("\n"); } 【3.43】下面程序的功能是輸入學(xué)生的姓名和成績,然后輸出。 #include struct stuinf { char name[20]; /* 學(xué)生姓名 */ int score; /* 學(xué)生成績 */ } stu, *p; main ( ) { p=&stu; printf("Enter name:"); gets( ① ); printf("Enter score: "); scanf("%d", ② ); printf("Output: %s, %d\n", ③ , ④ ); } 【3.44】下面程序的功能是按學(xué)生的姓名查詢其成績排名和平均成績。查詢時可連續(xù)進行,直到輸入0時才結(jié)束。 #include #include #define NUM 4 struct student { int rank; char *name; float score; }; ① stu[ ]={ 3,"liming",89.3, 4,"zhanghua",78.2, 1,"anli",95.1, 2,"wangqi",90.6 }; main() { char str[10]; int i; do { printf("Enter a name"); scanf("%s",str); for( i=0;i if( ② ) { printf("Name :%8s\n",stu[i].name); printf("Rank :%3d\n",stu[i].rank); printf("Average :%5.1f\n",stu[i].score); ③ ; } if( i>=NUM ) printf("Not found\n"); }while( strcmp(str,"0")!=0 ); } 【3.45】下面程序的功能是從終端上輸入5個人的年齡、性別和姓名,然后輸出。 #include "stdio.h" struct man { char name[20]; unsigned age; char sex[7]; }; main ( ) { struct man person[5]; data_in(person,5); data_out(person,5); } data_in(struct man *p, int n ) { struct man *q = ① ; for( ;p { printf( "age:sex:name" ); scanf("%u%s", &p->age, p->sex); 、 ; } } data_out( struct man *p, int n ) { struct man *q = __③__; for( ;p printf("%s;%u;%s\n", p->name, p->age, p->sex); }