{
int i,j,total,k,sum; char *p; i=0;total=0; p=str;
while(*(p+i)!='\\0') /*p1字符串是否结束*/ {
k=i; sum=0;
while(*(p+k)==ch) /*k移动到不要删除位置*/ { k++;
sum++; /*删除字符个数*/ }
total+=sum; if (*(p+i)==ch)
{ for(j=i;*(p+j+sum)!='\\0';j++) *(p+j)=*(p+j+sum); *(p+j)='\\0'; } i++; }
return total; }
【习题6-24】
#include \#include \
void reversestr(char * str) ; void main() { char str[50];
printf(\ gets(str);
reversestr(str); printf(\}
void reversestr(char *str) {
char ch,*p1,*p2; p1=str;p2=str; while(*p2!='\\0') { p2++; } p2--;
while(p1 ch=*p1; *p1=*p2; *p2=ch; p1++; p2--; } } 习题七参考答案: 一、单选题 【习题7-1】~ 【习题7-5】: BDACB 【习题7-6】~ 【习题7-10】:BCCDA 【习题7-11】~【习题7-15】:ACACC 【习题7-16】~【习题7-20】:BCCDC 【习题7-21】~【习题7-24】:DABB 二、编写程序题 【习题7-25】参考程序: #include \#include \struct book { char name[30]; int price; }; main() { int i; struct book books[10]; clrscr(); for(i=0;i<10;i++) { printf(\ gets(books[i].name); printf(\ scanf(\ getchar(); } for(i=0;i<10;i++) { printf(\ puts(books[i].name); printf(\ } } 【习题7-26】:参考程序: #include \struct student {int num; char name[8]; int score1; int score2; int score3; int score4; float ave; }; void fun1(struct student stu[]) {int i; for(i=0;i<2;i++) { printf(\ scanf(\ printf(\ scanf(\ printf(\ scanf(\ printf(\ scanf(\ printf(\ scanf(\ printf(\ scanf(\ stu[i].ave=(stu[i].score1+stu[i].score2+stu[i].score3+stu[i].score4)/4.0; } } void fun2(struct student stu[]) {int i; printf(\ for(i=0;i<2;i++) printf(\ ,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].score4,stu[i].ave); } main() {int i; struct student stu1[2]; clrscr(); fun1(stu1); fun2(stu1); } 【习题7-27】参考程序: #include \struct complex { int rp; int ip; }; struct complex input(struct complex plural) { printf(\ scanf(\ printf(\ scanf(\ return plural; } void output(struct complex plural) { printf(\} struct complex add(struct complex plu1,struct complex plu2) { struct complex plu; plu.rp=plu1.rp+plu2.rp; plu.ip=plu1.ip+plu2.ip; return plu; } main() { struct complex plu1,plu2,plu; plu1=input(plu1); plu2=input(plu2); plu=add(plu1,plu2); output(plu); } 【习题7-28】参考程序: struct node *Merge(struct node *h1,struct node *h2) { struct node *p,*q,*r,*s,*h; p=h1;q=h2; if(p->num {h=s=p;r=p;p=p->next;r->next=NULL;} else {h=s=q;r=q;q=q->next;r->next=NULL;} while(p&&q) { if(p->num {r=q;q=q->next;} s->next=r; s=s->next; } if(p) s->next=p; else s->next=q; return h; } 【习题7-29】参考程序: #include \#include \struct chicken { int cock; int hen; int chick; }ch[20]; main() { int i,j,k,n=0; struct chicken *p; for(i=0;i<=100/5;i++) for(j=0;j<=100/3;j++) for(k=0;k<=100*3;k++) if(i+j+k==100&&i*5+j*3+k/3.0==100.00) { ch[n].cock=i; ch[n].hen=j; ch[n].chick=k; n++; } p=ch; for(i=0;i printf(\ p++; } } 习题八参考答案: 一、单选题 【习题8-1】~【习题8-5】:BCCCC 【习题8-6】~【习题8-10】:ABADB 【习题8-11】~【习题8-12】:AC 二、简答题