1 struct Student int num char name20 struct

  • Slides: 89
Download presentation

其它说明: 1) 不要误认为凡是结构体类型都有相同的结构。 例: struct Student { int num; char name[20]; }; struct Student

其它说明: 1) 不要误认为凡是结构体类型都有相同的结构。 例: struct Student { int num; char name[20]; }; struct Student 1 { int num; int age; }; //24 B //8 B

3) 成员的引用相当普通的变量(. 成员运算符) 例: struct S { int num; char name[20]; }s 1, s

3) 成员的引用相当普通的变量(. 成员运算符) 例: struct S { int num; char name[20]; }s 1, s 2; 使用: s 1. num=20; strcpy(s 1. name, "Li. Ming"); s 2. num=21; strcpy(s 2. name, "Zhang. Hua");

4) 成员也可是结构体变量 例:struct Date { int month; int day; int year; }; struct Student

4) 成员也可是结构体变量 例:struct Date { int month; int day; int year; }; struct Student { int num; char name[20]; char sex; int age; Date birthday; char addr[30]; }student 1, student 2;

student 1, student 2 内存分配分别为: 4 B+20 B+1 B+4 B+3*4 B+30 B

student 1, student 2 内存分配分别为: 4 B+20 B+1 B+4 B+3*4 B+30 B

2、结构体变量的初始化 定义时指定初始值 例1: struct Student { int num; char name[20]; char sex; int age;

2、结构体变量的初始化 定义时指定初始值 例1: struct Student { int num; char name[20]; char sex; int age; float score; char addr[30]; }student 1={10001, "Zhang Xin", 'M', 19, 90. 5, "Shanghai"};

例2: struct Student { int num; char name[20]; char sex; int age; float score;

例2: struct Student { int num; char name[20]; char sex; int age; float score; char addr[30]; }; Student student 2={10002, "Wang Li", 'F', 20, 98, "Beijing"};

例7. 1 引用结构体变量中的成员。 #include <iostream> using namespace std; struct Date { int month; int

例7. 1 引用结构体变量中的成员。 #include <iostream> using namespace std; struct Date { int month; int day; int year; };

struct Student { int num; char name[20]; char sex; Date birthday; float score; }student

struct Student { int num; char name[20]; char sex; Date birthday; float score; }student 1, student 2= {10002, "Wang Li", 'f', 5, 23, 1982, 89. 5};

int main( ) 运行结果如下: 10002 { Wang Li student 1=student 2; f cout<<student 1.

int main( ) 运行结果如下: 10002 { Wang Li student 1=student 2; f cout<<student 1. num<<endl; cout<<student 1. name<<endl; 5/23/1982 89. 5 cout<<student 1. sex<<endl; cout<<student 1. birthday. month<<'/' <<student 1. birthday. day<<'/' <<student 1. birthday. year<<endl; cout<<student 1. score<<endl; return 0; }

1、定义结构体数组 三种方式: 方式 1: struct Student { int num; char name[20]; char sex; int

1、定义结构体数组 三种方式: 方式 1: struct Student { int num; char name[20]; char sex; int age; float score; char addr[30]; }; Student stu[3];

方式 2: struct Student { int num; char name[20]; char sex; int age; float

方式 2: struct Student { int num; char name[20]; char sex; int age; float score; char addr[30]; }stu[3];

方式 3: struct { int num; char name[20]; char sex; int age; float score;

方式 3: struct { int num; char name[20]; char sex; int age; float score; char addr[30]; }stu[3];

2、结构体数组的初始化 方式: 定义数组={初值表列}; 例: struct Student { int num; char name[20]; char sex; int

2、结构体数组的初始化 方式: 定义数组={初值表列}; 例: struct Student { int num; char name[20]; char sex; int age; float score; char addr[30]; }stu[3]={{10101, "Li Lin", 'M', 18, 87. 5, "103 Beijing Road"}, {10102, "Zhang Fun", 'M', 19, 99, "130 Shanghai Road"}, {10104, "Wang Min", 'F', 20, 78. 5, "1010, Zhongshan Road"}};

 • 定义数组stu时, 也可以不指定元素个数。 即写成以下形式: struct Student { int num; char name[20]; char sex;

• 定义数组stu时, 也可以不指定元素个数。 即写成以下形式: struct Student { int num; char name[20]; char sex; int age; float score; char addr[30]; }stu[ ]={{10101, "Li Lin", 'M', 18, 87. 5, "103 Beijing Road"}, {10102, "Zhang Fun", 'M', 19, 99, "130 Shanghai Road"}, {10104, "Wang Min", 'F', 20, 78. 5, "1010, Zhongshan Road"}};

统计得票: 运行: Zhang↙ for(i=0; i<10; i++) Li↙ { Fun↙ cin>>leader_name; Li↙ for(j=0; j<3; j++)

统计得票: 运行: Zhang↙ for(i=0; i<10; i++) Li↙ { Fun↙ cin>>leader_name; Li↙ for(j=0; j<3; j++) Zhang↙ if(strcmp(leader_name, leader[j]. name) Li↙ ==0) Zhang↙ Li↙ leader[j]. count++; Fun↙ } 输出得票: for(i=0; i<3; i++) { cout<<leader[i]. name<<": " <<leader[i]. count<<endl; } Wang↙ Li: 4 Zhang: 3 Fun: 2

修改程序: (用字符串变量) 全局结构体类型声明: struct Person { string name; int count; }; 定义结构体变量, 但不能初始化: Person

修改程序: (用字符串变量) 全局结构体类型声明: struct Person { string name; int count; }; 定义结构体变量, 但不能初始化: Person leader[3]; for(i=0; i<3; i++) { cin>>leader[i]. name; leader[i]. count=0; } string leader_name;

统计得票: for(i=0; i<10; i++) { cin>>leader_name; for(j=0; j<3; j++) if(leader_name==leader[j]. name) leader[j]. count++; }

统计得票: for(i=0; i<10; i++) { cin>>leader_name; for(j=0; j<3; j++) if(leader_name==leader[j]. name) leader[j]. count++; } 输出得票: for(i=0; i<3; i++) { cout<<leader[i]. name<<": " <<leader[i]. count<<endl; }

2)赋初值 Student student 1; Student *p; p=&student 1; (指向确定) 3)引用成员 student 1. num=98001 (*p).

2)赋初值 Student student 1; Student *p; p=&student 1; (指向确定) 3)引用成员 student 1. num=98001 (*p). num=98001 注意: 括号不能省略。

输出成员值: (三种方式) cout<<stu. num<<" " <<stu. name<<" " <<stu. sex<<" "<<stu. score<<endl; cout<<(*p). num<<"

输出成员值: (三种方式) cout<<stu. num<<" " <<stu. name<<" " <<stu. sex<<" "<<stu. score<<endl; cout<<(*p). num<<" "<<(*p). name<<" " <<(*p). sex<<" "<<(*p). score<<endl; cout<<p->num<<" "<<p->name<<" " <<p->sex<<" "<<p->score<<endl;

例:struct Student { long num; float score; Student *next; }; Student *head; 1)2 人链表

例:struct Student { long num; float score; Student *next; }; Student *head; 1)2 人链表 a) 31001, 89. 5 b) 31003, 90 。 2) 3 人链表 a) 31001, 89. 5 b) 31003, 90 c) 31007, 85

链表结点的结构体类型: struct Student { long num; float score; Student *next; }; 链表的表头: Student *head;

链表结点的结构体类型: struct Student { long num; float score; Student *next; }; 链表的表头: Student *head; Part 1 Part 2

NULL

NULL

int main( ) { Student a, b, c, *head, *p; a. num=31001; a. score=89.

int main( ) { Student a, b, c, *head, *p; a. num=31001; a. score=89. 5; b. num=31003; b. score=90; c. num=31007; c. score=85; head=&a; a. next=&b; b. next=&c; c. next=NULL;

p=head; do { cout<<p->num<<" "<<p->score<<endl; p=p->next; } while(p!=NULL); return 0; }

p=head; do { cout<<p->num<<" "<<p->score<<endl; p=p->next; } while(p!=NULL); return 0; }

小结 2、链表 1)链表结点类型声明 struct A 1 { int a; char b; A 1 *next;

小结 2、链表 1)链表结点类型声明 struct A 1 { int a; char b; A 1 *next; }; A 1 *head; 2)声明链表结点变量 A 1 a, b; 3)a, b构成链 head=&a; a. next=&b; b. next=NULL; 4)链输出(结点循环) A 1 *p; p=head; while(p!=NULL) { cout<<p->a<<p->b<<endl; p=p->next; }

小结 4、new和delete( malloc(); free(); ) 1) int *p; p=new int; cin>>*p; cout<<*p; delete p;

小结 4、new和delete( malloc(); free(); ) 1) int *p; p=new int; cin>>*p; cout<<*p; delete p; 2) float *p; p=new float(1. 2); cout<<*p; delete p; 3) char *p; p=new char[10]; delete [ ]p;

全局结构体类型和变量声明: 其中一成员为共用体类型和变量的声明; struct { int num; char name[10]; char sex; char job; union P

全局结构体类型和变量声明: 其中一成员为共用体类型和变量的声明; struct { int num; char name[10]; char sex; char job; union P { int grade; char position[10]; }category; }person[2];

也可以这样声明: (好的声明) 先声明共同体类型, 再全局声明结构体类型和变量。 union P { int grade; char position[10]; }; struct Data

也可以这样声明: (好的声明) 先声明共同体类型, 再全局声明结构体类型和变量。 union P { int grade; char position[10]; }; struct Data { int num; char name[10]; char sex; char job; P category; }; Data person[2];

if(person[i]. job=='s') cin>>person[i]. category. grade; else if (person[i]. job=='t') cin>>person[i]. category. position; ————— if

if(person[i]. job=='s') cin>>person[i]. category. grade; else if (person[i]. job=='t') cin>>person[i]. category. position; ————— if (person[i]. job== 's') cout<<person[i]. num <<setw(6) <<person[i]. name<<" " <<person[i]. sex <<" "<<person[i]. job <<setw(10)<<person[i]. category. grade<<endl; else cout<<person[i]. num <<setw(6)<<person[i]. name<<" " <<person[i]. sex<<" " <<person[i]. job <<setw(10)<<person[i]. category. position<<endl;

7. 4 用typedef声明类型 用typedef声明一个新的类型名来代替已有的类型名。 例1: typedef int INTEGER; typedef float REAL; 这样, 以下两行等价: ①

7. 4 用typedef声明类型 用typedef声明一个新的类型名来代替已有的类型名。 例1: typedef int INTEGER; typedef float REAL; 这样, 以下两行等价: ① int i, j; float a, b; ② INTEGER i, j; REAL a, b;

例2: typedef int COUNT; COUNT i, j; 例3: typedef struct { int month; int

例2: typedef int COUNT; COUNT i, j; 例3: typedef struct { int month; int day; int year; }DATE; //DATE类型名, 非变量名 可用DATE定义变量: DATE birthday; //结构体变量 DATE *p; //结构体指针变量

小结 2、枚举类型,枚举常量 enum weekday{sun, mon, tue, wed, thu, fri, sat}; 3、枚举变量 weekday workday, week_end;

小结 2、枚举类型,枚举常量 enum weekday{sun, mon, tue, wed, thu, fri, sat}; 3、枚举变量 weekday workday, week_end; 4、 typedef struct { int month; int day; int year; }DATE; DATE a 1, b 1; //DATE类型名, 非变量名