Click here to load reader

第 10 章 结构体及用户定义类型

Embed Size (px)

DESCRIPTION

第 10 章 结构体及用户定义类型. 合肥师范学院计算机科学与技术系. 主要知识点. 10.1 自定义类型标识符 10.2 结构体的定义与引用 10.3 链表. 例如 : typedef int INTEGER ; typedef float REAL;. 又如 : typedef int ARRAY[100]; ARRAY a, b, c;. 所以有 : int i; float x;. 等价与. 等价于. int a[100],b[100],c[100];. INTEGER i ; REAL x;. - PowerPoint PPT Presentation

Citation preview

  • 10

  • 10.1 10.2 10.3

  • 10.1 typedef

  • 1; : int a[10];2 : int ARRAY [10];3typedef; : typedef int ARRAY[10];4 : ARRAY a,b,c;

  • 10.2 , C ,(,, C

  • 10.2.1 struct { 1 1 2 2 n n}; struct student { int numchar name[20]char sexint agefloat score }struct date { int month, day, year }

  • struct student { int numchar nume[20]char sex int age struct date birthdayfloat score }struct student { int numchar name[10]char sex int age struct date { int year; int month; int day; } birthdayfloat score }struct student int , float

  • 10.2.2 1 struct student { int num char nume[20] char sex int age float score } struct student std, *pstd, pers[3]2 struct student { int num char name[20] char sex int age float score} std*pstd, pers[3]

  • 10.2.3 . (*). -> struct student long num char name[20] char sex int age float score}std,pers[5],*sp=&stdstd.num=10101 sp->num=10101std.sex=M (*sp).sex=Mstd.score=75+8 (*sp).score=75+8std.age++ sp->age++

  • 10.1

    #include stdio.hmain( ){ struct student{ 1ong num char name[20] char sex float score} s1={20001Li LiM85} struct student s2 float sumave scanf(%ld %s %c%f&s2.nums2.name&s2.sex&s2.score) sum=s1.score+s2.score ave=sum/2 printf(NO\tNAME\tSEX\tSCORE\n) printf(%ld\t%s\t%c\t%5.1f\ns1.nums1.names1.sexs1.score) printf(%ld\t%s\t%c\t%5.1f\ns2.nums2.names2.sexs2.score) printf(sum=%5.1f\tave=%5.1f\nsumave)}

  • 10.2 NC

    #include stdio.h#define N 30main( ){ struct student { int num float score} stud[N] float ave=0max int i k=0 printf(input: num name score) for(i=0 i

  • 10.3

    #include string.hmain( ){ struct student { 1ong num char name[20] float score } struct student stu1 *sp sp=&stu1 stu1.num20001 strcpystu1.nameLi Li stu1.score=85 printf(%ldt%ldt%ldnstu1num(*sp).num sp->num) printf(%st%st%snstu1name(*sp).name sp->name) printf(%5.2f\t%5.2f\t%5.2f\n stu1.score(*sp).score sp->score)}

  • 10.3 1 1head223NULL

  • struct slist { int data;/**/ struct slist *next;/**/ };typedef struct slist SLIST; malloc(size);size calloc(n,size);nsize free(*ptr);ptr

  • 2. 10.4 creat( )5 (1) (2) pqheadheadpq(3) (4) 4

  • #define N 5#define LEN sizeof(STUD)struct student{int num float score struct student *next}typedef struct student STUD /* 1 */STUD *creat( ){STUD *head*p*q/* 2 */ int i p=( STUD *)malloc(LEN) scanf(%d%f&p->num&p->score) p->next=NULL head=p q=p /* 3 */ for(i=1inext=NULL scanf(%d%f&p->num&p->score) q->next=p q=p } /* 4 */ return(head)}

  • 3. 10.5 print( )

    void print(STUD *head){ STUD *p p=head while(p!=NULL) { printf(%d%f\np->nump->score) p=p->next /**/ }}

  • 4. 10.6 delete( ) num

  • STUD *delete(STUD *headint num){STUD *p*q if(head==NULL) {printf(list nullreturn(0)} p=head while(num!=p->num && p->next!=NULL)* p* { q=pp=p->next} ** if(num==p->num) ** { if(p==head) head=p->next* * else q->next=p->next* * printf(delete %d\np->num) free(p) } else ** printf(%ld not been found\nnum) return(head)}

  • 5. 10.7 insert( )num xy

  • STUD *insert(STUD *headSTUD *stud){ STUD *p*q*p0 p=head /* p */ p0=stud *p0* if(head==NULL ** { head=p0p0->next=NULL} else { while((p0->num>p->num) && (p->next!=NULL)) { q=ppp->next} if(p0->numnum) { if(head==p)headp0** else q->next=p0 *q* p0->next=p } else { p->next=p0p0->next=NULL}** } returnhead}

  • 10.12

    STUD *creat( )void print(STUD *head)STUD *delete(STUD *headint num)STUD *insert(STUD *headSTUD *stud)main( ){ STUD *head *stu int del_num printf(input records \n) head=creat( ) * * print(head) ** printf(input the deleted number:) scanf(%d&del_num)** while(del_num!=0) { head=delete(headdel_num)* * print(head) printf(input the deleted number:) scanf(%d&del_num)** } printf(input the inserted record\n) stu=(STUD *)malloc(LEN) scanf(%d%f&stu->num&stu->score)** while(stu->num!=0) { head=insert(headstu)** printhead printf(input the inserted record\n) stu=( STUD *)malloc(LEN) scanf(%d%f&stu->num&stu->score)** }}