第七章 自定义数据类型_第7章自定义数据类型
第七章 自定义数据类型由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“第7章自定义数据类型”。
第七章 用户自定义数据类型 7.1 结构体类型 7.1.1 结构体的概述
一个学生的学号、姓名、性别、年龄、成绩、家庭住址
num name sex age score addr
10010 Li Fun M 18 87.5 BeiJing 声明一个新的结构体的类型: struct Student { int num;char name[20];char sex;int age;float score;char addr[30];};
7.1.2 结构体类型变量的定义方法及其初始化 1.定义结构体变量的方法
(1)先声明结构体的类型再定义变量名 Student student1,student2;(2)声明类型的同时定义变量 struct Student { int num;char name[20];char sex;int age;float score;char addr[30];}std1,std2;(3)直接定义结构体类型变量 struct { int num;char name[20];char sex;int age;float score;char addr[30];}std1,std2;(4)成员也可以是一个结构体变量 struct Date { int month;int day;int year;};struct Student { int num;char name[20];char sex;int age;Date birthday;float score;char addr[30];};2.结构体变量的初始化 struct Student { int num;char name[20];char sex;int age;float score;char addr[30];}student1={10001,“Zhang Xin”,'M',19,90.5,“shanghai”};Student student2={10002,“Wang Li”,'F',20,98,“Beijing”};7.1.3 引用结构体变量
(1)可以将一个结构体变量的值赋给另一个具有相同结构的结构体变量。
student1=student2;(2)可以引用一个结构体变量中的一个成员的值。
student1.num=10010;“.”是成员运算符,它的优先级最高。(3)对于结构体嵌套,要逐级引用。
student1.birthday.month=11;(引用student1中birthday中的month成员)。
(4)不能将一个结构体变量作为一个整体进行输入和输出。(5)对于结构体变量的成员可以像普通变量一样进行各种运算。(6)可以引用结构体变量成员的地址,也可以引用结构体变量的地址。
cout using namespace std;struct Date { int month;int day;int year;};struct Student { int num;char name[20];char sex;Date birthday;float score;char addr[30];}student1,student2={10002,“Wang Li”,'F',5,23,1982,89.5};void main(){ student1=student2;cout
7.1.4 结构体数组 1.定义结构体数组 struct Student { int num;char name[20];char sex;int age;float score;char addr[30];}stu[3];Student x[8];2.结构体数组的初始化
Student y[2]={{10101,“Li Lin”,'M',18,87.5,“103 Beijing Road”} ,{10102,“Zhang Fun”,'M'19,99,“130 Shanghai Road”}};3.结构体数组应用举例
例7.2 对候选人得票统计程序.P202 #include using namespace std;struct Person {
char name[20];int count;};void main(){ Person leader[3]={“Li”,0,“Zhang”,0,“Fun”,0};int i,j;char leader_name[20];for(i=0;i
cin>>leader_name;
for(j=0;j
if(strcmp(leader_name,leader[j].name)==0)
{ leader[j].count++;break;} } for(i=0;i
cout #include using namespace std;void main(){
struct Student {
int num;
string name;
char sex;
float score;};Student stu;Student *p=&stu;stu.num=10301;stu.name=“Wang Fun”;stu.sex='F';stu.score=89.5;cout
numnamesex
score
“->”是指向运算符,即指向结构体变量运算符。请分析以下几种运算: p->n p->n++ ++p->n #include #include using namespace std;void main(){
struct Student {
int num;
string name;
char sex;
float score;};Student stu;Student *p=&stu;stu.num=10301;coutnumnum++num using namespace std;struct Student { int num;float score;Student *next;};void 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;while(p!=NULL){ coutnumscorenext;} } 31001 89.5 31003 90 31007 85
7.1.6 结构体类型数据作为函数参数 例7.5(1)用结构体变量作函数参数。#include #include using namespace std;struct Student { int num;char name[20];float score[3];};void main(){ void print(Student);Student stu;stu.num=12345;strcpy(stu.name,“Li Feng”);stu.score[0]=67.5;stu.score[1]=89;stu.score[2]=78.5;print(stu);} void print(Student stu){ cout
#include using namespace std;struct Student { int num;char name[20];float score[3];};void main(){ void print(Student*);Student stu={12345,“Li Feng”,67.5,89,78.5};print(&stu);} void print(Student *p){ coutnumnamescore[0]
score[1]score[2] #include using namespace std;struct Student { int num;string name;float score[3];};void main(){ void print(const Student &);Student stu;stu.num=12345;stu.name=“Li Feng”;stu.score[0]=67.5;stu.score[1]=89;stu.score[2]=78.5;print(stu);} void print(const Student &stu){ cout
7.1.7 用new和delete运算符进行动态分配和撤销内存空间int *p=new int;delete p;int *p=new int(100);delete p;char *pc=new char[10];delete []pc;//删除数组
int(*pp)[4]=new int[5][4];delete[]pp;//删除数组 例7.6 开辟空间存放一个结构体变量。#include using namespace std;struct Student { int num;
string name;char sex;};void main(){
Student *p;p=new Student;p->num=10123;p->name=“Wang Fun”;p->sex='M';coutnamenumsex
7.2 共用体类型 7.2.1 共用体的概念
几个不同的变量共占同一段内存的结构,称为共用体(有的书称之为联合)。
7.2.2 对共用体变量的访问方式
【例】演示联合的例子。
这里exam是个联合,为说明数据成员val和h开始于同一地址,考虑下面的程序: #include #include union exam { short val;char h[2];};void main(){ exam var;var.h[0]= 'A';cout
(1)每一瞬时只有一个成员起作用,其它成员不起作用。(2)共用体变量的地址和它的各个成员的地址都是同一地址。不能对共用体变量赋值;不能企图引用变量名来得到一个值;不能在定义共用体变量时对它进行初始化;不能用共用体变量作为函数参数。
7.3 枚举类型
如果一个变量只有几中可能的值,可以定义为枚举类型 enum color{RED,BLUE,GREEN};关键字enum标志枚举类型定义开始,分号标志其结束。在C++中允许不写enum;enum后的标识符color称为枚举类型名。枚举元素(枚举常量):花括号内用逗号隔开的标识符是为这个类型定义的常量,枚举元素的缺省赋值:编译器为这些枚举常量赋予不同的值,第一个常量RED值为0,以后的常量的值是它前面常量的值增1,所以,BLUE为1, GREEN为2。
声明枚举变量:枚举标记可以用类型名说明具有该类型的变量,例如:
color CorVariable;上面的语句说明了变量CorVariable,可以将说明类型color时所列举的枚举常量中的任何一个置给变量CorVariable , 例如:
CorVariable=GREEN;cout
#include void main(){ enum color{RED,BLUE,GREEN};color CorVariable;CorVariable=GREEN;cout
CorVariable=(color)1;} 2 4 枚举常量的自定义赋值:说明可在枚举常量名之后使用等号将一个(char或int类型的)常量置给一个枚举常量名,以改变编译的缺省赋值,例如:
enum color {RED=-1,BLUE,GREEN=6,YELLOW};这里RED代表值-1,而BLUE为它前 面的枚举常量的值加1,所以BLUE代表值0。同样,GREEN的值为6,而YELLOW的值为7。为枚举常量指定重复的值也是合法的,例如:
enum state {FALSE,TRUE,FAIL=0,BAD=0};在这个说明中FALSE,FAIL和BAD的值都为0。
枚举常量的类型:枚举常量的类型隐含是unsigned char 或int类型,但到底是何种类型取决于枚举常量的值。如果所有的值都可用unsigned char表示,则它就是每个枚举常量的类型。例7.7 口袋中有红、黄、蓝、白、黑5种颜色的球若干个。每次从口袋中任意取出3个球,问得到3种不同颜色球的可能取法,输出每种排列的情况。
#include #include using namespace std;enum color{red,yellow,blue,white,black};void main(){ void print(color);color pri;int i,j,k,n=0;for(i=red;i
for(j=red;j
if(i!=j)
{ for(k=red;k
if((k!=i)&&(k!=j))
{
cout
pri=color(i);print(pri);
pri=color(j);print(pri);
pri=color(k);print(pri);
cout
}
} cout
47 white black yellow 48 white black blue 49 black red yellow 50 black red blue 51 black red white 52 black yellow red 53 black yellow blue 54 black yellow white 55 black blue red 56 black blue yellow 57 black blue white 58 black white red 59 black white yellow 60 black white blue total:60 7.4 用typedef声明类型
用typedef声明一个新的类型名来代替已有的类型名。typedef int INTEGER;typedef float REAL;则,以下两行等价: int i,j;float a,b;INTEGER i,j;REAL a,b;
typedef int NUM[100];NUM n;//定义n为包含100整型元素数组。typedef char *STRING;//STRING为字符指针类型。
STRING p,s[10];//p为字符指针变量,s为字符指针数组.typedef int(*POINTER)();//POINTER为指向函数指针类型。函数返回整型值;函数没有参数。
POINTER p1;//p1为指向函数的指针变量。
使用typedef有利于程序的通用与移植。实验目的:
(1)掌握结构体的变量的定义和使用。(2)掌握结构体类型数组的概念和应用。2 实验内容:
第七章 习题1 习题3 习题5 3 实验结论:
(3)
例7.7 name num sex job grade/position Li 1011 f s 3 Wang 2085 m t prof #include #include #include using namespace std;struct { int num;char name[10];char sex;char job;union P { int grade;
char position[10];}category;}person[2];void main(){ int i;for(i=0;i
cin>>person[i].num>>person[i].name
>>person[i].sex>>person[i].job;
if(person[i].job=='s')
cin>>person[i].category.grade;
else
cin>>person[i].category.position;} cout
cout
if(person[i].job=='s')
cout
else
cout
cout