C语言程序设计教程第九章习题答案_c语言程序设计例题

2020-02-27 其他范文 下载本文

C语言程序设计教程第九章习题答案由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“c语言程序设计例题”。

1、li

300.0 chang 30

200.0 chang2、#include struct students {

char sid[100];

char name[100];

float score[3];}student;void main(){

int i;float j;

printf(“nPlease input sid:

”);

scanf(“%s”,student.sid);

printf(“nPlease input name: ”);

scanf(“%s”,student.name);

printf(“nPlease input 3 score:(like1,1,1)”);/*输入逗号隔开*/

scanf(“%f,%f,%f”,&student.score[0],&student.score[1],&student.score[2]);

printf(“nsid = %s”,student.sid);

printf(“nname = %s”,student.name);

j=(student.score[0]+student.score[1]+student.score[2])/3.0;

printf(“naverage = %.2f”,j);

getch();}

3、#include #include #define F sizeof(student)#define NULL 0 typedef struct scores { int english;int math;int c_language;int all;}TP;typedef struct students { char sid[15];char name[15];TP score;struct students *next;}student;student *input(){ student *head,*p1,*p2;int n=0;char ch;clrscr();head=(student *)malloc(F);head->next=NULL;

do {

n++;

printf(“nnPlease input %d student meage:

nn”,n);

printf(“t%d student sid:

”,n);

p1=(student *)malloc(F);p1->next=NULL;

scanf(“%s”,p1->sid);

printf(“nt%d student name:

”,n);

scanf(“%s”,p1->name);

printf(“nt%d student scores(englesh,math,c_language):

”,n);

scanf(“%d,%d,%d”,&p1->score.english,&p1->score.math,&p1->score.c_language);

p1->score.all=p1->score.english+p1->score.math+p1->score.c_language;

if(n==1)

{ head->next=p1;p2=p1;}

else

{ p2->next=p1;

p2=p1;

}

printf(“nntttContinue or back(pre y/n):

”);

ch=getch();

}while(ch=='y'||ch=='Y');return head;} void average1(student *head){ student *p;int j;clrscr();p=head->next;

while(p)

{ j=p->score.all/3;

printf(“nnname:

%staverage: %d”,p->name,j);

p=p->next;

} printf(“nnnPre eny key return.”);getch();} void average2(student *head){ student *p;int n=0,temp1=0,temp2=0,temp3=0;p=head->next;while(p){ temp1+=p->score.english;

temp2+=p->score.math;

temp3+=p->score.c_language;

p=p->next;n++;} printf(“nnaverage english is : %dnaverage math is : %dnaverage c_language is : %dt”,temp1/n,temp2/n,temp3/n);} student *sort(student *head){ student *head1,*p,*q,*r;int temp1=0,temp2=0,temp3=0,temp4;char s[15],n[15];head1=head;for(p=head1->next;p->next!=NULL;p=p->next){ r=p;

for(q=p->next;q;q=q->next)

if(q->score.all>r->score.all)

r=q;

if(r!=p)

{ strcpy(s,p->sid);strcpy(n,p->name);

temp1=p->score.english;

temp2=p->score.math;

temp3=p->score.c_language;

temp4=p->score.all;

strcpy(p->sid,r->sid);strcpy(p->name,r->name);

p->score.english=r->score.english;

p->score.math=r->score.math;

p->score.c_language=r->score.c_language;

p->score.all=r->score.all;

strcpy(r->sid,s);strcpy(r->name,n);

r->score.english=temp1;

r->score.math=temp2;

r->score.c_language=temp3;

r->score.all=temp4;

} } return head1;} void output(student *head){ student *head2,*p;int i=1;clrscr();head2=sort(head);for(p=head2->next;p!=NULL;p=p->next)

printf(“nnname: %stsid: %stenglish: %dtmath: %dtc_language: %dtaverage: %dtmingci: %d”,p->name,p->sid,p->score.english,p->score.math,p->score.c_language,p->score.all/3,i++);

average2(head);

printf(“nnnttPre eny key back.”);getch();} void main(){ student *head,*p1,*p2;int i=0,j=1;head=input();do {

clrscr();

printf(“nn(1): average1.nn(2): average2.nn(3): sort.nn(4): output.nnn

Please choose:

”);

scanf(“%d”,&i);

switch(i)

{ case 1: average1(head);break;

case 2: clrscr();average2(head);printf(“nnnPre eny key retuen.”);getch();break;

case 3: clrscr();p1=sort(head);for(p2=p1->next;p2!=NULL;p2=p2->next)printf(“nttname: %stmingci:%d”,p2->name,j++);printf(“nnnPre eny key back.”);getch();break;

case 4: output(head);break;

default: printf(“nYour choose is not right.”);break;

} }while(i!=-1);}

4、#include #include #define NULL 0 #define F sizeof(worker)typedef struct work { char sid[15];char name[15];int money;struct work *next;}worker;int min=0,max=0;char a[15],b[15];worker *input(){ worker *head,*p,*q;int n=0;char ch;head=(worker *)malloc(F);head->next=0;do { n++;

p=(worker *)malloc(F);p->next=0;

printf(“nntPlease input %d worker meage :

”,n);

printf(“n%d worker sid:

”,n);scanf(“%s”,p->sid);

printf(“n%d worker name:

”,n);scanf(“%s”,p->name);

printf(“n%d worker money:

”,n);scanf(“%d”,&p->money);

if(n==1)

{

head->next=p;q=p;

max=p->money;strcpy(a,p->name);

min=p->money;strcpy(b,p->name);

}

else

{

q->next=p;

if(p->money>max){max=p->money;strcpy(a,p->name);}

if(p->moneymoney;strcpy(b,p->name);}

q=p;

}

printf(“ntty/n”);ch=getch();}while(ch=='y'||ch=='Y');return head;} void output(){

clrscr();printf(“nThe max money is: %dttname is: %snn”,max,a);printf(“nThe min money is: %dttname is: %s”,min,b);} void main(){

input();output();getch();}

5、6、#include“stdio.h” #define F sizeof(stu)#define NULL 0 typedef struct student { int sid;int average;struct student *next;}stu;stu *head;stu *create(){ stu *p1,*p2;int n=0;char ch;head=(stu *)malloc(F);head->next=NULL;

do {

n++;

printf(“nnPlease input %d student meage:

nn”,n);

printf(“t%d student sid:

”,n);

p1=(stu *)malloc(F);p1->next=NULL;

scanf(“%d”,&p1->sid);

printf(“nt%d student average:

”,n);

scanf(“%d”,&p1->average);

if(n==1)

{ head->next=p1;p2=p1;}

else

{ p2->next=p1;

p2=p1;

}

printf(“nntttContinue or back(pre y/n):

ch=getch();

}while(ch=='y'||ch=='Y');return head;} stu *select(stu *head,int x){ stu *s;s=head->next;while(s){

if(s->sid==x)

break;

s=s->next;} return s;}

stu *insert(stu *head,int x,int y){ stu *p,*r,*q;clrscr();p=head->next;r=(stu *)malloc(sizeof(stu));r->sid=x;r->average=y;if(p==NULL)/*如果插入空表*/

{

p=r;

r->next=NULL;

”);

printf(“ninsert succe!”);

}

else

{ while(x>p->sid)/*找到插入的位置,按学号大小。(找到位置或者到了表尾都会跳出循环)*/

{

if(p->next==NULL)break;p=p->next;

}

if(x

sid)

/*插到中间位置*/

{

r->sid=p->sid;

r->average=p->average;

p->sid=x;

p->average=y;

r->next=p->next;

p->next=r;

printf(“ninsert succe!”);

}

else if(x==p->sid)/*学号不能相同*/

printf(“nError--->your input this same sid.”);

else

/*插到末尾*/

{

p->next=r;

r->next=NULL;

printf(“ninsert succe!”);

}

}

return head;} stu *get(stu *head,int n)/*得到位置为n的结点的指针*/ { stu *p;int i;p=head->next;if(n==0)return head;else

{

for(i=1;i

p=p->next;

return p;} } stu *delete(stu *head,int sid){

stu *p,*q;int temp=0,i=0;p=head->next;if(!p)

{

printf(“nlist is empty.pre eny key back.”);getch();return head;}/*表空*/ else { while(p)

/*查找学号为sid的结点的指针*/

{i++;/*标记学号为sid的结点的位置*/

if(p->sid==sid)

{temp=1;break;} /*temp=1标记找到了*/

p=p->next;}

if(temp==1)/*如果有学号为sid的结点*/

{ q=get(head,i-1);/*得到sid的前一个结点的指针*/

q->next=p->next;

free(p);

printf(“nndelete suce!!”);

return head;

}

else

/*没有找到*/

{ printf(“nnNO this data.n”);

return head;

} } } void print(stu *head){ stu *p;p=head->next;if(!p){printf(“nlist is empty.pre eny key back.”);getch();} while(p){

printf(“n%d :t%d ”,p->sid,p->average);

p=p->next;} } main(){ stu *p1,*p2;char ch1;int n,i=0,j=0;head=create();do {clrscr();printf(“n1.insert.”);printf(“n2.select.”);printf(“n3.delect.”);printf(“n4.print list.”);printf(“n5.EXIT

”);printf(“n

............choice(1-5).............”);ch1=getch();switch(ch1){

case '1':

{ clrscr();

printf(“nplease input insert sid.and average(like 1,1):”);

scanf(“%d,%d”,&i,&j);

head=insert(head,i,j);

printf(“nnnPre eny key back.”);getch();

break;

}

case '2':

{ clrscr();

printf(“ninput you want to selete sid:

”);

scanf(“%d”,&n);

p1=select(head,n);

{

if(p1)printf(“nsid:%dtaverage:%d”,p1->sid,p1->average);

else

printf(“nNo this data.”);

}

printf(“nnnPre eny key back.”);getch();

break;

}

case '3':

{ clrscr();printf(“nPlease input you want delete sid: ”);

scanf(“%d”,&n);

head=delete(head,n);

printf(“nnnPre eny key back.”);getch();

break;

}

case '4':

{ clrscr();

printf(“All information :”);

print(head);

printf(“nnnPre eny key back.”);getch();

break;

}

case '5': return;

default: printf(“nnYour enter is not right.pre eny key back.”);getch();}

}while(n);}

7、#include #define F sizeof(L)typedef struct list {

char data;

struct list *next;}L;L *set_list(){

L *head,*p1,*p2;

char c;

int n=0;

head=(L *)malloc(F);head->next=0;

/*建立链表*/

p1=p2=head;

printf(“nPlease input char(pre * finish):”);

scanf(“%c”,&c);

while(c!='*')

{

n++;

if(n==1)

p1->data=c;

else

{

p1=(L *)malloc(F);

p1->data=c;

p2->next = p1;

p2 = p1;

p1->next = 0;

}

scanf(“%c”,&c);

}

p1=head;

while(p1)

{

printf(“%c ”,p1->data);p1=p1->next;

}

printf(“nnn”);

return head;} void change_list(L *head1)

/*算法:p2指向最后一个元素,p1指向第一个元素。交换他们的值,p1,p2同时往中间靠拢。*/ {

L *p1,*p2,*p3;

int i,j,k,n=1;

char temp;

p1=head1;p2=head1;p3=head1;

while(p3->next)

{ p3=p3->next;n++;

}/*求链长*/

for(i=n;i>(n/2);i--)/*外循环使p1后移,p2前移。*/

{

p2=head1;for(j=1;j

p2=p2->next;/*p2指向最后一个元素*/ temp=p1->data;p1->data=p2->data;p2->data=temp;/*交换他们的值*/ p1=p1->next;/*p1向后移*/

}

while(head1)

{ printf(“%c ”,head1->data);head1=head1->next;} } void main(){ L *head;head=set_list();change_list(head);getch();}

《C语言程序设计教程第九章习题答案.docx》
将本文的Word文档下载,方便收藏和打印
推荐度:
C语言程序设计教程第九章习题答案
点击下载文档
相关专题 c语言程序设计例题 第九章 习题 答案 c语言程序设计例题 第九章 习题 答案
[其他范文]相关推荐
    [其他范文]热门文章
      下载全文