C语言程序设计填空题及答案复习用 联系客服

发布时间 : 星期六 文章C语言程序设计填空题及答案复习用更新完毕开始阅读eb6a4822da38376baf1faefc

printf(\; }

r( int m )

{ printf(\② ); m = ③ ; if( ④ ) ⑤ ; }

【3.31】输入n值,输出高度为n的等边三角形。例如当n=4时的图形如下: * *** ***** *******

#include void prt( char c, int n ) { if( n>0 )

{ printf( \; ① ; } }

main() { int i, n;

scanf(\;

for( i=1; i<=n; i++ ) { ② ; ③ ;

printf(\; } }

【3.32】下面的函数实现N层嵌套平方根的计算。 double y(double x, int n) { if( n==0 ) return(0);

else return ( sqrt(x+( ① )) );

}

【3.33】函数revstr(s)将字符串s置逆,如输入的实参s为字符串\, 则返回时 s 为字符串\。递归程序如下: revstr( char *s ) { char *p=s, c; while(*p) p++; ① ; if(s

revstr(s+1); ③ ; } }

如下是由非递归实现的revstr(s)函数: revstr (s) char *s;

{ char *p=s, c; while( *p ) p++; ④ ;

while( s

【3.34】下面函数用递归调用的方法,将str中存放的长度为n的字符串反转过来,例如原来是\,反序为\。

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个整数,程序按降序完成从大到小的排序。

#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(\; for( i=0; i<10;i++)

scanf(\; sort( ⑤ ); printf(\; for( i=0; i<10; i++) printf(\; }

【3.36】下面函数的功能是将一个整数存放到一个数组中。存放时按逆序存放。例如:483存放成\。 #include

void convert(char *a, int n) { int i;

if((i=n/10) !=0 ) convert( ① , i ); *a = ② ; }

char str[10]= \;

main()

{ int number;

scanf(\; convert( str, number ); puts(str); }

【3.37】下面程序的功能是实现数组元素中值的逆转。 #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(\; printf(\; }

invert(int *s,int num) { int *t,k; t=s+num; while( ① ) { k=*s; *s=*t; *t=k; ② ; ③ ; } }

【3.38】下面程序通过指向整型的指针将数组a[3][4] 的内容按3行×4列的格式输出,请给printf( )填入适当的参数,使之通过指针p将数组元素按要求输出。 #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(\① ); } }

【3.39】下面程序的功能是:从键盘上输入一行字符,存入一个字符数组中,然后输出该字符串。 #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】下面函数的功能是将字符变量的值插入已经按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、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;is[j] ) { ⑨ } }

main( )

{ char s1[100],s2[100],s3[200]; printf(\; scanf(\,s1);

printf(\; scanf(\,s2); strsort(s1); strsort(s2); ⑩ = '\\0';

strmerge(s1,s2,s3);

printf(\,s3); }

【3.42】已知某数列前两项为2和3,其后继项根据前面最后两项的乘积,按下列规则生成:

① 若乘积为一位数,则该乘积即为数列的后继项; ② 若乘积为二位数,则该乘积的十位上的数字和个位上的数字依次作为数列的两个后继项。

下面的程序输出该数列的前N项及它们的和,其中,函数sum(n,pa) 返回数列的前N项和,并将生成的前N项存入首指针为pa的数组中,程序中规定输入的N值必须大于2,且不超过给定的常数值MAXNUM。

例如:若输入N的值为10,则程序输出如下内容: sum(10)=44

2 3 6 1 8 8 6 4 2 4 #include \

#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; total += *pa; } } }

④ ; }

main()

{ int n, *p, *q, num[MAXNUM]; do

{ printf(\; scanf(\; }while( ⑤ );

printf(\; for( p=num, q = ⑥ ; p

【3.43】下面程序的功能是输入学生的姓名和成绩,然后输出。

#include struct stuinf

{ char name[20]; /* 学生姓名 */ int score; /* 学生成绩 */ } stu, *p; main ( ) { p=&stu;

printf(\; gets( ① );

printf(\; scanf(\, ② );

printf(\, ③ , ④ ); }

【3.44】下面程序的功能是按学生的姓名查询其成绩排名和平均成绩。查询时可连续进行,直到输入0时才结束。

?? #include #include

#define NUM 4 ? struct student ? { int rank; char *name; float score; ????????};

???????? ① stu[ ]={ 3,\,89.3, ???????? 4,\,78.2, ???????? 1,\,95.1, ???????? 2,\,90.6 };

????????main()

????????{ char str[10]; ???????? int i; ???????? do

{ printf(\; ???????? scanf(\,str);

???????? for( i=0;i

???????? { printf(\,stu[i].name); ???????? printf(\,stu[i].rank);

???????? printf(\,stu[i].score);???????? ③ ; ???????? }

???????? if( i>=NUM ) printf(\; ???????? }while( strcmp(str,\; ????????}

【3.45】下面程序的功能是从终端上输入5个人的年龄、性别和姓名,然后输出。 #include \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( \; scanf(\p->sex); ② ; } }

data_out( struct man *p, int n )

{ struct man *q = __③__; for( ;p

printf(\;%u;%s\\n\p->name, p->age, p->sex); }

【3.46】输入N个整数,储存输入的数及对应的序号,

并将输入的数按从小到大的顺序进行排列。要求:当两

个整数相等时,整数的排列顺序由输入的先后次序决定。

例如:输入的第3个整数为5,第7个整数也为5,则

将先输入的整数5排在后输入的整数5的前面。程序如

下:

#include \

#define N 10

struct

{ int no;

int num;

} array[N];

main( )

{ int i,j,num; for( i=0;i

{ printf(\,i); scanf(\,&num);

for( ① ;j>=0&&array[j].num ② num; ③ ) array[j+1]=array[j]; array[ ④ ].num=num; array[ ⑤ ].no=i; }

for( i=0;i

printf(\,%d\\n\,i,array[i].num,array[i].no); }

【3.47】以下程序的功能是:读入一行字符(如:a、...y、z),按输入时的逆序建立一个链接式的结点序列,即先输入的位于链表尾(如下图),然后再按输入的相反顺序输出,并释放全部结点。 #include

main( ) { struct node { char info; struct node *link; } *top,*p; char c; top=NULL; while((c= getchar( )) ① ) { p=(struct node *)malloc(sizeof(struct node)); p->info=c; p->link=top; top=p; }

while( top ) { ② ;

top=top->link; putchar(p->info); free(p); } }

【3.48】下面函数将指针p2所指向的线性链表,串接到p1所指向的链表的末端。假定p1所指向的链表非空。#define NULL 0 struct link { float a;

struct link *next; }; concatenate ( p1,p2 ) struct list *p1,*p2;

{ if( p1->next==NULL ) p1->next=p2; else

concatenate( ① ,p2);

}

【3.49】下面程序的功能是从键盘输入一个字符串,然后反序输出输入的字符串。 #include struct node { char data; struct node *link; }*head; main() { char ch; struct node *p; head = NULL; while(( ch=getchar())!='\\n' )