【3.21】下面程序的功能是实现数组元素中值的逆转。 #include
{ 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; ② ; ③ ; } }
答案:① s 【3.22】下面函数的功能是将字符变量的值插入已经按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; } 答案:① p++ ② w[i+1]=w[i] } 【3.23】下面程序的功能是输入学生的姓名和成绩,然后输出。 #include { char name[20]; /* 学生姓名 */ int score; /* 学生成绩 */ } stu, *p; 11 main ( ) { p=&stu; printf(\; gets( ① ); printf(\; scanf(\, ② ); printf(\, ③ , ④ ); } 答案:① stu.name ② &stu.score ③ p->name ④ p->score 【3.24】下面程序的功能是按学生的姓名查询其成绩排名和平均成绩。查询时可连续进行,直到输入0时才结束。 ?? #include ???????? ① 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,\; ????????} 答案:① struct student ② strcmp(stu[i].name,str)==0 ③ break 注释:程序的主体是一个二重循环,内层for循环完成查找学生的工作。①处是进行结构数 12 组说明并初始化,按照结构变量说明的格式规定,应该填写:strcut student。②处为if语句的逻辑条件,应当是当查找到指定的学生后输出学生的情况,因此应当填写:strcmp(stu[i].name,str)==0。③处应当将控制退出内层的for循环,只能选择break语句。 【3.25】下面函数将指针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); } 答案:① p1->next 【326】下面函数的功能是创建一个带有头结点的链表,将头结点返回给主调函数。链表用于储存学生的学号和成绩。新产生的结点总是位于链表的尾部。 struct student { long num; int score; struct student *next; }; struct student *creat() { struct student *head=NULL,*tail; long num; int a; tail= ① malloc(LEN); do { scanf(\; if(num!=0) { if(head==NULL) head=tail; else ② ; tail->num=num; tail->score=a; tail->next=(struct student *)malloc(LEN); } else tail->next=NULL; }while(num!=0); return( ③ ); 13 } 答案:① (struct student *) ② tail=tail->next ③ head 注释:①malloc函数的作用是在内存开辟指定字节数的存储空间,并将此存储空间的地址返回赋给尾指针tail,但是此地址为void型,应将其强制转换为所要求的结构指针类型。 ②新开辟的结点的内存地址存于tail所指向的已建立的链表的尾结点的结构成员next,新结点连入链表以后,尾指针tail应指向新的结点。 【3.27】下面程序的功能是统计文件中的字符的个数。 #include { long num=0; ① *fp; if((fp=fopen(\{ printf(\; exit(0); } while( ② ) { fgetc(fp); num++; } printf(\; fclose(fp); } 答案:① FILE ② !feof(fp) 注释:FILE 是文件结构类型名。feof()是测试文件结束标志的函数。 14