C语言程序设计第9章.ppt
- 格式:ppt
- 大小:102.01 KB
- 文档页数:17
第9章结构【练习9-1】定义一个能够表示复数的结构类型,一个复数包括实数与虚数两个局部。
解答:struct complex{float real;float imaginary;};【练习9-2】人的出生日期由年、月、日组成,请在例 9-1 中的通讯录结构中增加一个成员:出生日期,用嵌套定义的方式重新定义该结构类型。
解答:struct date{int year;int month;int day;};struct student{int num;char name[10];struct date birthday;int computer,english,math;double average;};【练习9-3】例 9-1 中,如果要计算的是三门课程的课程平均成绩,应该如何改写程序?解答:#include<stdio.h>struct student{int num;char name[10];int computer,english,math;double average;};int main(void){int i, n;double math_sum,english_sum,computer_sum;struct student s1;printf("Input n:");scanf("%d", &n);printf("Input the student's number, name and course scores:\n"); math_sum=english_sum=computer_sum=0;for(i=1;i<=n;i++){printf("No.%d:",i);scanf("%d%s%d%d%d",&s1.num,,&s1.math,&s1.english,&s1 puter);math_sum+=s1.math;english_sum+=s1.english;computer_sum+=s1 puter;}printf("math_ave:%.2lf\nenglish_ave:%.2lf\ncomputer_ave:%.2lf\n", math_sum/n,english_sum/n,computer_sum/n);return 0;}【练习9-4】定义一个包含 5 名学生信息的结构数组,并对该结构数组的所有元素进行初始化。
1.(1)struct struct student stu(2). ->(3)4(4)a(5)malloc() free()(6)s->next=p->next p->next=sp->next=p->next->next(7)typedef(8)枚举2.(1)scanf("%s",*cPtr->suit);错误。
更正为scanf("%s",cPtr->suit);(2)printf("%s\n", hearts.face); 错误。
更正为printf("%s\n", hearts[0].face); (3)赋初值错误。
应该赋予一个字符型的初值。
(4)枚举类型的定义错误。
更正为enum a {one=9,two=-1,three};3.(1)A)(2)D)(3)D)(4)D)(5)D)(6)B)4.(1)①sizeof(stu)(2)②struct object *next;(3)③old=q->age; ④q->name,q->age(4)⑤struct link *head ⑥p->data ⑦p->next5.(1)51,60,21(2)1,1(3)04510(4)2,3(5)求链表中所有节点数据域之和。
6.#include <stdio.h>struct complex /*定义结构体类型复数*/{double x; /*实部*/double y; /*虚部*/};struct complex complex_add( struct complex a,struct complex b )/*求复数的加法*/ {struct complex c;c.x = a.x+b.x;c.y = a.y+b.y;return c;}struct complex complex_sub( struct complex a,struct complex b ) /*求复数的减法*/ {struct complex c;c.x = a.x-b.x;c.y = a.y-b.y;return c;}void complex_print( struct complex c ) /*输出复数*/{if( c.x )printf( "%lf",c.x );if( c.y>0 )printf( "+%lfi",c.y );else if( c.y<0 )printf( "%lfi",c.y );}void complex_scan( struct complex *p ){scanf( "%lf%lfi",&p->x,&p->y );}int main(void){struct complex a, b, c;complex_scan( &a );complex_scan( &b );c = complex_add( a,b );complex_print( a );printf( " + " );complex_print( b );printf( " = " );complex_print( c );printf("\n");c = complex_sub( a,b );complex_print( a );printf( " - " );complex_print( b );printf( " = " );complex_print( c );printf("\n");return 0;}7..struct dt{ int year;int month;int day;}date;main(){ int I,day_num;printf(“please input year,yue and day\n”);scanf(“%d,%d,%d”,&date.year, &date.month, &date.day);printf(“\n %d 月%d 日is %d 第%d天”,date.month, date.day, date.year,days(date.year,date.month,date.day));}days(year,month,day)int year,month,day;{int day_sum,I;static int_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};day_sum=0;for (i=1;i<month;i++){day_sum+=day_tab[i];}day_sum+=day;if{{year%4==0 &&year%100!=0||year%4==0}&&month>=3}day_sum+=1;return(day_sum);}8.typedef char datatype;typedef struct node{ datatype data;struct node *next;}linklist;insert(linklist *head,datatype a, datatype key) { linklist *s,*p,*q;s=(linklist *)malloc(sizeof(linklist));s->data=key;q=head; p=head->next;if(p==NULL){s->next=p;q->next=s;return;}while((p->data!=a)&&(p->next!=NULL)) {q=p; p=p->next;}if(p->data==a){s->next=p;q->next=s;}else{s->next=NULL;p->next=s;}}9.typedef int datatype;typedef struct node{ datatype data;struct node *next;}linklist;invert(linklist *head){ linklist *p,*q;p=head->next;if(p!=NULL){ head->next=NULL;do{q=p->next; p->next=head->next;head->next=p; p=q;}while(p!=NULL);}}。