1、第九章课后习题答案9.1结构体可以把不同类型的数据组合成一个整体。与数组相比,数组中的元素是按照下标来进行访问的,而结构体是按照用户定义的结构体变量名来访问其成员的。9.2因为在定义结构体时系统并没有为其中的成员分配任何存储空间,而只有定义了结构体变量后,系统才为其中的各成员分配了相应的空间,所以先定义结构体变量,然后才能为结构体变量的成员进行赋值操作,否则,没有任何意义。9.3#include#define N 10typedef struct char numN; /学号 char nameN; /姓名 int age; /年龄 int score; /成绩,若有多门成绩,则可将该变量定义
2、成数组形式ST;void input_fun(ST *); /输入函数void output_fun(ST *); /输出函数void main() char choice=Y; ST stu; while(choice=Y|choice=y) /是否继续输入 input_fun(&stu); getchar(); output_fun(&stu); printf(Continue?); choice=getchar(); void input_fun(ST *p) printf(The num:); scanf(%s,p-num); printf(The name:); scanf(%s,p
3、-name); printf(The age:); scanf(%d,&p-age); printf(The score:); scanf(%d,&p-score);void output_fun(ST *q) printf(The num:); puts(*q).num); printf(The name:); puts(*q).name); printf(The age:%dn,(*q).age); printf(The score:%dn,(*q).score);9.4#include#define N 10struct sdata int year; int month; int da
4、y;typedef struct char numN; /学号 char nameN; /姓名 struct sdata birthday; /生日 int score; /成绩,若有多门成绩,则可将该变量定义成数组形式ST;void input_fun(ST *); /输入函数void output_fun(ST *); /输出函数void main() char choice=Y; ST stu; while(choice=Y|choice=y) /是否继续输入 input_fun(&stu); getchar(); output_fun(&stu); printf(Continue?);
5、 choice=getchar(); void input_fun(ST *p) printf(The num:); scanf(%s,p-num); printf(The name:); scanf(%s,p-name); printf(The birthday:n); printf( the year:); scanf(%d,&p-birthday.year); printf( the month:); scanf(%d,&p-birthday.month); printf( the day:); scanf(%d,&p-birthday.day); printf(The score:);
6、 scanf(%d,&p-score);void output_fun(ST *q) printf(The num:); puts(*q).num); printf(The name:); puts(*q).name); printf(The data:%d.%d.%dn,(*q).data.year,(*q).data.month,(*q).data.day); printf(The score:%dn,(*q).score);9.5#include#define N 5#define M 10typedef struct char numM; char nameM; int price;
7、int amount;GOODS;void initialize(GOODS *);void priarr(GOODS *);void priprice(GOODS *);void main() GOODS goodsN; initialize(goods); priarr(goods); priprice(goods);void initialize(GOODS goods) int i; for(i=0;iN;i+) printf(please input the goods num:); scanf(%s,goodsi.num); printf(please input the good
8、s name:); scanf(%s,goodsi.name); printf(please input the goods price:); scanf(%d,&goodsi.price); printf(please input the goods amount:); scanf(%d,&goodsi.amount); void priarr(GOODS goods) int i; printf(The list is:n); printf( num name price amountn); for(i=0;iN;i+) printf(%4s%8s%8d%8dn,goodsi.num,go
9、odsi.name,goodsi.price,goodsi.amount);void priprice(GOODS goods) int sum=0; int sumi=1; int i,j; for(i=0;iN;i+) sumi=1; sumi=goodsi.amount*goodsi.price; sum=sum+sumi; printf(The all gross is:%dn,sum);9.6#include#define N 10#define M 3typedef struct char numN; /学号 char nameN; /姓名 int age; /年龄 int sco
10、re; /成绩,若有多门成绩,则可将该变量定义成数组形式ST;void input_fun(ST *); /输入函数void output_fun(ST *); /输出函数void order(ST *);int average(ST *);void main() char choice=Y; ST stuM; int i; int ave; for(i=0;inum); printf(The name:); scanf(%s,p-name); printf(The age:); scanf(%d,&p-age); printf(The score:); scanf(%d,&p-score);
11、void output_fun(ST *q) printf(n num name age scoren); for(int i=0;iM;i+) printf(%7s%8s%8d%8dn,qi.num,qi.name,qi.age,qi.score);void order(ST t) ST temp; int i; for(i=0;iM-1;i+) for(int j=i+1;jM;j+) if(ti.scoretj.score) temp=ti; ti=tj; tj=temp; int average(ST t) int i; int sum=0; int ave; for(i=0;iM;i
12、+) sum+=ti.score; ave=sum/M; return ave;9.7#include#include#define N 10struct student char numN; int score; struct student *next;void readlist(struct student *);void main() struct student *head,*q,*m; char choice=Y; if(m=(struct student *)malloc(sizeof(struct student) m-next=NULL; head=m; while(choi
13、ce=Y|choice=y) if(q=(struct student *)malloc(sizeof(struct student) printf(Input the num:); scanf(%s,q-num); printf(Input the score:); scanf(%d,&q-score); q-next=NULL; getchar(); m-next=q; m=m-next; printf(Continue?); choice=getchar(); readlist(head); void readlist(struct student *stu) struct studen
14、t *p; p=stu-next; do printf(the num:); printf(%sn,p-num); printf(the score:); printf(%dn,p-score); p=p-next; while(p!=NULL);9.8#include#include#define N 10struct student char numN; int score; struct student *next;void readlist(struct student *);void main() struct student *head,*q,*m,*k,*n; char choi
15、ce=Y; if(m=(struct student *)malloc(sizeof(struct student) m-next=NULL; head=m; while(choice=Y|choice=y) if(q=(struct student *)malloc(sizeof(struct student) printf(Input the num:); scanf(%s,q-num); printf(Input the score:); scanf(%d,&q-score); q-next=NULL; getchar(); if(head-next=NULL) m-next=q; m=
16、m-next; else if(m-score=q-score) m-next=q; m=m-next; else n=head; k=n-next; while(k-next!=NULL&k-scoreq-score) n=k; k=k-next; q-next=k; n-next=q; printf(Continue?); choice=getchar(); readlist(head); void readlist(struct student *stu) struct student *p; p=stu-next; printf(num scoren); do printf(%5s%5
17、dn,p-num,p-score); p=p-next; while(p!=NULL);9.9#include#includetypedef struct int year; int month; int day;DATA;typedef struct document char num10; DATA data; struct document *next;DOC;void inqueue(DOC *,DOC *,DOC *);void outqueue(DOC *,DOC *);void main() char choice=Y; char add=Y; char del; DOC *fr
18、ont,*rear,*p; front=(DOC *)malloc(sizeof(DOC); front-next=NULL; rear=front; p=rear; while(add=Y|add=y) inqueue(front,rear,p); getchar(); printf(Continue add document?); add=getchar(); p=(DOC *)malloc(sizeof(DOC); getchar(); printf(deal with document?); del=getchar(); while(del=Y|del=y) outqueue(fron
19、t,rear); front=front-next; getchar(); printf(Continue deal with document?); del=getchar(); if(del=N|del=n) break; void inqueue(DOC *front,DOC *rear,DOC *q) printf(enter the num:); scanf(%s,q-num); printf(enter the year:); scanf(%d,&q-data.year); printf(enter the month:); scanf(%d,&q-data.month); pri
20、ntf(enter the day:); scanf(%d,&q-data.day); rear-next=q; rear=q; rear-next=NULL; void outqueue(DOC *front,DOC *rear) DOC *p; if(front=NULL&rear=NULL) printf(Its a empty queue!n); else p=front; printf(The deal with document:n); printf(the num:%5s the date:%4d.%d.%dn,p-num,p-data.year,p-data.month,p-data.day);
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2