第 7 章 结构体、共用体、枚举类型

Click here to load reader

Upload: inoke

Post on 14-Jan-2016

151 views

Category:

Documents


0 download

DESCRIPTION

《C 语言程序设计 》 龙昭华主编. 第 7 章 结构体、共用体、枚举类型. 第一节 结构体 第二节 共用体 第三节 枚举类型 第四节 用 typedef 定义类型 第五节 用指针处理链表. 1 / 72. (第七章 结构体、共用体、枚举类型). 第一节 结构体. 一、 概述. 前面讲过 基本类型 ( 也称简单类型 ) 的变量,如:整型、实型、字符型变量 等。也介绍了 数组 —— 一种构造类型,数组中各元素都是属于同一个类型的。 但是,我们有时还需要将 不同类型的数据 组合成一个有机的整体,便于引 - PowerPoint PPT Presentation

TRANSCRIPT

  • 7

    typedef

    C 1 / 72

  • 2 / 72 ()

    C(structure)

    (num)(name)(sex)(age)(score)(addr)(resume)

  • 3 / 72 1 struct { 1; 2; n; };struct struct struct student { int num; /* */ char name[20]; /* */ char sex; /* */ int age; /* */ float score; /* */ char addr[30]; /* */ char *resume; /* */ };{}(})

  • 24 / 72 (intcharfloat) struct student struct student wang, li, zhang ;

  • 5 / 72 struct student { int num; /* */ char name[20]; /* */ char sex; /* */ int age; /* */ float score; /* */ char addr[30]; /* */ char *resume; /* */ } wang , li , zhang ; struct { 1; 2; n;};

  • 6 / 72 struct { int num; /* */ char name[20]; /* */ char sex; /* */ int age; /* */ float score; /* */ char addr[30]; /* */ char *resume; /* */ } wang , li , zhang ; struct{ 1; 2; n;};

  • 7 / 72 ()sizeofsizeof(wang)sizeof(struct student)struct studentintlongcharflaot struct student_table{ int num; /* */ char name[20]; /* */ char sex; /* */ struct date birthday; /* */ char addr[30]; /* */}chen,zeng,nie;struct date{ int month; /* */ int day; /* */ int year; /* */};

  • 3 8 / 72 struct {;}={};struct student_table{ int num; /* */ char name[20]; /* */ char sex; /* */ struct date birthday; /* */ char addr[30]; /* */}chen={99089, wang jun,M,9,28,1984, 123 Beijing Road};struct date{ int month; /* */ int day; /* */ int year; /* */};

  • 4 9 / 72 ..chen.numchen.num=99089;99089chennumP1897.1 chen.birthday.monthchen.birthday.month=12; 12chenbirthdaymonth chen.birthdaychenbirthdaybirthday

  • 10 / 72 wang.score=li.score; sum=wang.score+li.score; wang.age++;--wang.age;

    scanf(%d,&wang.num); (wang.num) printf(%o,&wang); (wang)

    scanf(%d %s %c %d %f %s %s,&wang); printf(%d,%s,%c,%d,%f,%s,%s\n,wang);

  • 11 / 72 1

    struct { 1 2 n};struct *;struct { 1 2 n} *;

  • 2 12 / 72 (*). ->->7.1 3(P1917.3)struct student_table chen,*p; p=&chen; (pchen) chen.name (*p).name p->name chenppchen*pchen(*p).numchen.num*p.**p.num*(p.num),(*p).nump++sizeof(chen)p->nump->num++--p->num

  • 13 / 72 ()(20) 1 struct student { int num; char name[20]; char sex; int age; float score;}; /* *//* */struct student stu[20];stu20stu[0]stu[1]stu[2]stu[19]struct student

  • 2 14 / 72 struct student { int num; char name[20]; char sex; int age; float score;}stu[20];struct{ int num; char name[20]; char sex; int age; float score;}stu[20]; stu[0]stu[1] struct []={};sturct student stu[3]={{100,wang lin,M,20,98.5}, {101,li gang,M,19,90.0}, {102,liu yan,F,19,96.8}};

  • () 15 / 72 stusturct student stu[ ]={{100,wang lin,M,20,98.5}, {101,li gang,M,19,90.0}, {102,liu yan,F,19,96.8}}; struct student { int num; char name[20]; char sex; int age; float score;}stu[3]={{100,wang lin,M,20,98.5}, {101,li gang,M,19,90.0}, {102,liu yan,F,19,96.8}};struct student { int num; char name[20]; char sex; int age; float score;}stu[ ]={{1,wl,M,20,98.5}, {2,lg,M,19,90.0}, {3,ly,F,19,96.8}};

  • 3 16 / 72 []. struct student stu[3]; stu[0].name,stu[0].sex,stu[0].age,stu[0].score stu[1].name,stu[1].sex,stu[1].age,stu[1].score stu[2].name,stu[2].sex,stu[2].age,stu[2].score struct student hu,wu,stu[3]; hu=stu[0]; wu=stu[2]; stu[2]=stu[1];

  • 17 / 72 struct student stu[3]; scanf(%s %d %f,stu[1].name,&stu[1].age,&stu[1].score);stu[1].name& floatdouble for(i=0;i
  • 7.2 18 / 72#include #include struct person{ char name[20]; int count;}leader[3]={Li,0,Zhang,0,Fu,0};main(){ int i,j; char leader_name[20]; for(i=0;i
  • 7.3 19 / 72 P195-1987.5

    7.4 P1987.6

    7.5C a[i].score[j] void enter(struct student a[],int n) 7.2leader[j].count++;(leader[j].count)++;

  • 20 / 72 struct student { int num; char name[20]; char sex; int age; float score;}stu[3]={{100,wang lin,M,20,98.5}, {101,li gang,M,19,90.0}, {102,liu yan,F,19,96.8}};struct student *p; /* */p=stu; /* */

  • 21 / 72 p->namep->nump->sex(*p).name,(*p).num,(*p).sexp=stu;pstustu[0]p1pstu[1]psizeof(struct student)(++p)->nump1num(101)(p++)->nump->num(100)p1pstu[1]pstruct studentstruct studentstruct student stu[3],*p,peng; p=stu; p=&peng; p=stu[1].num; p=peng.num;ppp=(struct student *)stu[0].name; pstu[0]nameprintf(%s,p);wang linprintf(%s,p+1);stu[1].nameli gangpp+1sizeof(struct student)

  • 7.5 22 / 72 P199-2007.8

    7.6 P1997.7

    7.7for(p=stu;p

  • 7 ----123 / 72C ()

    ***

    7.1 P2167.1

  • 24 / 72 1 stu[1].numstu[2].name[2] 2 CANSIC 3 ()() 4

  • 7.7 25 / 72 stu3main(){ void print(struct student); struct student stu; stu.num=12345; strcpy(stu.name,Li Lin); stu.score[0]=67.5; stu.score[1]=89; stu.score[2]=78.6; print(stu);}12345 Li Lin 67.50000089.000000 78.600000#include #include #define FORMAT %d\t%s\t%f\n%f\t%f\nstruct student{ int num; char name[20]; flaot score[3];};

    void print(struct student stu){ printf(FORMAT,stu.num,stu.name, stu.score[0],stu.score[1],stu.score[2]);}

  • 7.7() () 26 / 72main(){ void print(struct student *); struct student stu; stu.num=12345; strcpy(stu.name,Li Lin); stu.score[0]=67.5; stu.score[1]=89; stu.score[2]=78.6; print(&stu);}12345 Li Lin 67.50000089.000000 78.600000#include #include #define FORMAT %d\t%s\t%f\n%f\t%f\nstruct student{ int num; char name[20]; flaot score[3];};void print(struct student *p){ printf(FORMAT,p->num,p->name, p->score[0],p->score[1],p->score[2]);}scanf(%d%s%f%f%f%f,&stu.num,stu.name,&stu.score[0], &stu.score[1], &stu.score[2]);stu

  • 27 / 72 ()C 1 C(bit field) struct { unsigned 1 unsigned 1 unsigned 1 }

  • 2 28 / 72struct datadata1data2abcd26442struct data{ unsigned a:2; unsigned b:6; unsigned c:4; unsigned d:4;}data1,data2; . data1.a=2; data2.a=data1.a; (data2.a2) data1.b=50; data2.b=data1.c+2; (data2.a14) data1.c=12; data2.c=data1.b-data1.c; (data2.a6)data1.a=9; data1.a2391001201data1.a1

  • 29 / 72 unsignedintunsigned struct data{ unsigned a:2; unsigned b:6; unsigned c:4; unsigned d:4; int e;}data1,data2; (16)unsigned a:17;, 0~16, , 16int (), sizeofsizeof(data1)4

  • (1)30 / 72 struct data { unsigned a:2; unsigned b[2]:6; unsigned c[3]:4; unsigned *d:4; int e; }data1,data2; struct data { unsigned a:2; unsigned b:6; }data1,*p;p=&data1;p->a=2; p->b=50; struct data { unsigned a:2; unsigned b:6; }data3[3];data3[0].a=2; data3[0].b=50; data3[2].a=1; & &data1.a

  • (2)31 / 72 () struct data { unsigned :4; unsigned b:1; unsigned c:1; unsigned :10; }data1,data2; 0 561~4567~16 struct data { unsigned a:4; unsigned :0; unsigned b:3;}data1,data2;ab(12)0

  • (3)32 / 72 struct data { unsigned a:2; unsigned b:5; unsigned c:6; unsigned d:4; }data1,data2; printf(%d,%d,%d,%d\n,data1.a,data1.b,data1.c,data1.d); %u%o%x data1.a+5*data1.b+10/data1.c sizeof(data1)3abcd

  • 33 / 72 1 union { 1 1 1 }structunion

  • 2 34 / 72 (intcharfloat) union data { int i; char ch; float f; }

    union data a,b,*p,c[20]; union data { int i; char ch; float f; } a,b,*p,c[20] union { int i; char ch; float f; } a,b,*p,c[20]

    4

  • 35 / 72 . -> (*).union data{ int i; char ch; float f;} a,b,*p,c[20] ;p=&a; a.i p->i (*p).i c[2].i a.ch p->ch (*p).ch c[2].ch

    a=5;b=a;

  • 36 / 72 1 2 a.i=1; a.ch=c; a.f=3.14; a.ia.cha.f 3&a&a.i&a.ch&a.f 4union data{ int i; char ch; float f;} a={2,c,3.14};a=5;m=a;

  • () 37 / 72 5 6struct{ int age; char *addr; union { int i; char *ch; }x; }y[10]; y[1]xi, y[1].x.i;

    y[2]xch *y[2].x.ch; y[2].x.*ch; 7b=a;

  • 7.8 38 / 72 P212-2137.14 P213-2147.15 7.9 N-S Li501Wang

  • 7.9() 39 / 72struct{ int num; char name[10]; char sex; char job; union { int class; char post[10]; }ct;}ps[2];main(){int n,i; for(i=0;i
  • 40 / 72 ANSIC C 1 enum { 1[=1], 2[=2], ... n[=n][,] };

  • 41 / 72 enum weekdays {sun,mon,tue,wed,thu,fri,sat}; enum colortype {red,yellow,blue,white,black,green}; enum sex {male=1,famale=2}; enum string {x1,x2,x3,x4,x5,x6,};

    enumenum weekdays C x3=2; () (enum weekday)

  • 242 / 72 enum weekdays { sun, mon, tue, wed, thu, fri, sat }

    enum weekdays workday; enum weekdays { sun, mon, tue, wed, thu, fri, sat }workday enum { sun, mon, tue, wed, thu, fri, sat }

  • 43 / 72 1 (=)012...1 enum colortype {red,yellow,blue,white,black,green}color;red,yellow,blue,white,black,green012345 enum string {x1,x2=0,x3,x4=50,x5,x6,x7}x;x1=0,x2=0,x3=1,x4=50,x5=51,x6=52 ,x7=53 enum string {x1=5,x2,x3,x4=-2,x5,x6,x7}x; x1=5,x2=6,x3=7,x4=-2,x5=-1,x6=0 ,x7=1 enum string {x1=5,x2,x3,x4=-2}x=x3; x7

    1 xx1x2x7

  • 244 / 72 enum colortype {red,yellow,blue,white,black,green}color;color=white; printf(c=%d\n,color);c=3 enum weekdays {sun,mon,tue,wed,thu,fri,sat}workday=wed;if(workday==mon) if(workday>tue) if(sun>sat) workday=2;workday=(enum weekdays)2;2workdayworkday=tue; workday=(enum weekdays)(10-6); 4workdayworkday=thu;

  • 7.1045 / 72ijk ijk5 N-S(npriloop3) 533N-S

  • 7.10()46 / 72main(){ enum color {red,yellow,blue,white,black}; enum color i,j,k,pri; int n=0,loop; for(i=red;i
  • typedef47 / 72 C(intlongcharfloatdouble)typedef typedef int INTEGER; typedef float REAL;INTEGERintREALfloatint i , j ; INTEGER i , j ; float a , b ; REAL a , b ;FORTRANINTEGERREALintfloattypedef struct date { int month; int day; int year; }DATE;struct date{ int month; int day; int year;}birthday,*p;DATE birthday;DATE *p;

  • 48 / 72 1 int i; 2 iCOUNTint COUNT; 3typedef typedef int COUNT; 4 COUNT i , j , k ; int i , j , k ;

    int n[100]; typedef int NUM[100];NUM n , a , b ; int n[100] , a[100] , b[100] ; char *p; typedef char *STRING;STRING p , s[10] ; char *p , *s[10] ; int (*p)(); typedef int (*POINTER)();POINTER p1 , p2 ; int (*p1)() , (*p2)() ; typedef

  • 49 / 72 1typedef 2typedefintlongcharfloatdoubletypedef 3typedef 4typedef#definetypedef int COUNT; #define COUNT intCOUNTint 5()typedef#include 6typedefint24intlongtypedef typedef

  • 7 ----250 / 72C ()

    ***

    7.2 P2177.1

    7.3 P2187.2

    7.4 P218-2197.2

  • 51 / 72 headNULL

  • 52 / 72 7.11 3li7_11.c #define NULL 0struct student{ long num; /**/ float score; /**/ struct student * next;};main(){ struct student a,b,c,*head,*p; a.num=99101; a.score=67; b.num=99102; b.score=86; c.num=99104; c.score=74; head=&a; a.next=&b; b.next=&c; c.netx=NULL; p=head; do { printf(%ld %5.1f\n,p->num,p->score); p=p->next; }while(p!=NULL);}abc

  • 53 / 72 Cmalloc()calloc()free()malloc.hstdlib.h 1malloc void * malloc(unsigned size)size(size)char *pc; pc=(char *)malloc(100); 100int *pi; pi=(int *)malloc(20*sizeof(int)); 20 struct student { long num; float score; struct student *next; };struct student *stu;int len;len=sizeof(struct student);stu=(struct student *)malloc(len);

  • 2calloc54 / 72 void * calloc(unsigned n,unsigned size)nsizeNULL struct student *ps; ps=(struct student *)calloc(2,sizeof(struct student));student2 3free void free(void *p)ppmalloccallocfreefree(pc); free(pi); free(stu); free(ps); CmalloccallocANSICmalloccalloc void *

  • 55 / 72 7.12 create()7.9 head----new----tail----count----

  • 7.12(1)----56 / 72

  • 7.12(2)57 / 72

  • 7.12(3)----58 / 72

  • 7.12(4)59 / 72

  • 7.12(5)60 / 72

  • 7.12(6)61 / 72

  • 7.12(7)----create62 / 72 #define NULL 0#define NODE struct student#define LEN sizeof(NODE)#include NODE{ long num; float score; NODE *next;};NODE *create(void){ int count=0; float sc; NODE *head=NULL; NODE *new,*tail; tail=(NODE *)malloc(LEN); for(;;) { new=(NODE *)malloc(LEN); scanf("%ld,%f",&new->num,&sc); new->score=sc; if(new->num==0) { free(new); break; } count++; if(count==1) head=new; else tail->next=new; tail=new; } tail->next=NULL; return(head);}

  • 63 / 72 NULL7.13 printlist()7.12P204 void printlist(NODE *head){ NODE *p; p=head; if(head!=NULL) do { printf("%ld , %5.1f\n",p->num, p->score); p=p->next; }while(p!=NULL);}

  • 64 / 72 1 2

  • 365 / 72 4(3)

  • 7.14 insert()P205-2067.1066 / 72 i1iii=0iNULL7.12 NODE *insert(head, new, i)NODE *head, *new; int i{ NODE *p; if(head==NULL) { head=new; new->next=NULL; }else { if(i==0) { new->next=head; head=new; }else { for(p=head;p!=NULL&&i>1;p=p->next,i--); if(p==NULL) /*,2,5*/ printf("Error!\n"); else { new->next=p->next; p->next=new; } } } return(head);}

  • 67 / 72 cf 1 2

  • 368 / 72 7.15 dellist()7.12 P206-2077.11

    head head=c->next; f->next=c->next;

  • 7.15()69 / 72 NODE *dellist(NODE *head,long num){ NODE *f,*c; if(head==NULL) { printf("\nList is empty.\n"); return(head); } if(head->num==num) /**/ { c=head; head=c->next; free(c); }else { f=head; c=f->next; while(c!=NULL&&c->num!=num) { f=c; c=f->next; } if(c!=NULL) /**/ { f->next=c->next; free(c); }else printf("%ld not been fount.\n"); } return(head);}(7.12)(7.13)(7.14)(7.15)li7_12.cmain

  • 7.16 70 / 72 #li7_16.c#include #include #include #define NODE struct stationNODE{ char name[8]; NODE *next;};NODE *creat_sta(NODE *h);void print_sta(NODE *h);int num=0; /**/main(){ NODE *head; clrscr(); head=NULL; printf("Please input station name: \n"); head=creat_sta(head); printf("-------------------\n"); printf("Number of station = %d\n",num); print_sta(head); getch(); /**/}

  • 7.16()71 / 72 NODE *creat_sta(NODE *h){ NODE *p1,*p2; p1=p2=(NODE *)malloc(sizeof(NODE)); if(p2!=NULL) { scanf("%s",&p2->name); p2->next=NULL; /*p2*/ } while(p2->name[0]!='#') { num++; if(h==NULL)h=p2; elsep1->next=p2; p1=p2; /*p1*/ p2=(NODE *)malloc(sizeof(NODE)); if(p2!=NULL) { scanf("%s",&p2->name); p2->next=NULL; } } return(h);}void print_sta(NODE *h){ NODE *temp; temp=h; while(temp!=NULL) { printf("%-8s",temp->name); temp=temp->next; }}

  • 7 ----372 / 72C ()

    ***

    7.5 P216-2177.1

    7.6 P217-2187.2

    7.7 P219-2207.2

    7.8 P220-2227.2