第七章 自定义数据类型分析.docx
《第七章 自定义数据类型分析.docx》由会员分享,可在线阅读,更多相关《第七章 自定义数据类型分析.docx(22页珍藏版)》请在冰豆网上搜索。
第七章自定义数据类型分析
第七章用户自定义数据类型
7.1结构体类型
7.1.1结构体的概述
一个学生的学号、姓名、性别、年龄、成绩、家庭住址
numnamesexagescoreaddr
10010LiFunM1887.5BeiJing
声明一个新的结构体的类型:
structStudent
{intnum;
charname[20];
charsex;
intage;
floatscore;
charaddr[30];
};
7.1.2结构体类型变量的定义方法及其初始化
1.定义结构体变量的方法
(1)先声明结构体的类型再定义变量名
Studentstudent1,student2;
(2)声明类型的同时定义变量
structStudent
{intnum;
charname[20];
charsex;
intage;
floatscore;
charaddr[30];
}std1,std2;
(3)直接定义结构体类型变量
struct
{intnum;
charname[20];
charsex;
intage;
floatscore;
charaddr[30];
}std1,std2;
(4)成员也可以是一个结构体变量
structDate
{intmonth;
intday;
intyear;
};
structStudent
{intnum;
charname[20];
charsex;
intage;
Datebirthday;
floatscore;
charaddr[30];
};
2.结构体变量的初始化
structStudent
{intnum;
charname[20];
charsex;
intage;
floatscore;
charaddr[30];
}student1={10001,"ZhangXin",'M',19,90.5,"shanghai"};
Studentstudent2={10002,"WangLi",'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<<&student1;
cout<<&student1.age;
例7.1引用结构体变量中的成员P199
#include
usingnamespacestd;
structDate
{intmonth;
intday;
intyear;
};
structStudent
{intnum;
charname[20];
charsex;
Datebirthday;
floatscore;
charaddr[30];
}student1,student2={10002,"WangLi",'F',5,23,1982,89.5};
voidmain()
{
student1=student2;
cout<cout<cout<cout<<<cout<}
10002
WangLi
F
5/23/1982
89.5
7.1.4结构体数组
1.定义结构体数组
structStudent
{intnum;
charname[20];
charsex;
intage;
floatscore;
charaddr[30];
}stu[3];
Studentx[8];
2.结构体数组的初始化
Studenty[2]={{10101,"LiLin",'M',18,87.5,"103BeijingRoad"}
{10102,"ZhangFun",'M'19,99,"130ShanghaiRoad"}};
3.结构体数组应用举例
例7.2对候选人得票统计程序.P202
#include
usingnamespacestd;
structPerson
{
charname[20];
intcount;
};
voidmain()
{Personleader[3]={"Li",0,"Zhang",0,"Fun",0};
inti,j;
charleader_name[20];
for(i=0;i<10;i++)
{
cin>>leader_name;
for(j=0;j<3;j++)
if(strcmp(leader_name,leader[j].name)==0)
{leader[j].count++;break;}
}
for(i=0;i<3;i++)
cout<'<}
Zhang
Li
Fun
Li
Zhang
Li
Zhang
Li
Fun
Wang
Li:
4
Zhang:
3
Fun:
2
7.1.5指向结构体变量的指针
1.通过指向结构体变量的指针引用结构体变量中的成员
例7.3指向结构体变量的指针的应用P204
#include
#include
usingnamespacestd;
voidmain()
{
structStudent
{
intnum;
stringname;
charsex;
floatscore;
};
Studentstu;
Student*p=&stu;
stu.num=10301;
stu.name="WangFun";
stu.sex='F';
stu.score=89.5;
cout<<cout<<(*p).num<<""<<(*p).name<<""<<(*p).sex<<""
<<(*p).score<cout<num<<""<name<<""<sex<<""
<score<}
10301WangFunF89.5
10301WangFunF89.5
10301WangFunF89.5
"->"是指向运算符,即指向结构体变量运算符。
请分析以下几种运算:
p->n
p->n++
++p->n
#include
#include
usingnamespacestd;
voidmain()
{
structStudent
{
intnum;
stringname;
charsex;
floatscore;
};
Studentstu;
Student*p=&stu;
stu.num=10301;
cout<num<cout<num++<cout<<++p->num<cout<}
10301
10301
10303
10303
2.用结构体变量和指向结构体变量的指针构成链表
structStudent
{intnum;
floatscore;
Student*next;
};
利用指向自己的指针构成链表。
例7.4P206
#defineNULL0
#include
usingnamespacestd;
structStudent
{intnum;
floatscore;
Student*next;
};
voidmain()
{
Studenta,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)
{cout<num<<""<score<next;}
}
3100189.5
3100390
3100785
7.1.6结构体类型数据作为函数参数
例7.5
(1)用结构体变量作函数参数。
#include
#include
usingnamespacestd;
structStudent
{intnum;
charname[20];
floatscore[3];
};
voidmain()
{voidprint(Student);
Studentstu;
stu.num=12345;strcpy(stu.name,"LiFeng");
stu.score[0]=67.5;
stu.score[1]=89;
stu.score[2]=78.5;
print(stu);
}
voidprint(Studentstu)
{
cout<<<''<}
12345LiFeng67.58978.5
(2)用指向结构体变量的指针作实参数。
#include
#include
usingnamespacestd;
structStudent
{intnum;
charname[20];
floatscore[3];
};
voidmain()
{voidprint(Student*);
Studentstu={12345,"LiFeng",67.5,89,78.5};
print(&stu);
}
voidprint(Student*p)
{
cout<num<<''<name<<''<score[0]
<<''<score[1]<<''<score[2]<}
12345LiFeng67.58978.5
(3)用结构体变量的引用做函数参数。
#include
#include
usingnamespacestd;
structStudent
{intnum;
stringname;
floatscore[3];
};
voidmain()
{voidprint(constStudent&);
Studentstu;
stu.num=12345;stu.name="LiFeng";
stu.score[0]=67.5;
stu.score[1]=89;
stu.score[2]=78.5;
print(stu);
}
voidprint(constStudent&stu)
{
cout<<<''<}
12345LiFeng67.58978.5
7.1.7用new和delete运算符进行动态分配和撤销内存空间
int*p=newint;deletep;
int*p=newint(100);deletep;
char*pc=newchar[10];delete[]pc;//删除数组
int(*pp)[4]=newint[5][4];delete[]pp;//删除数组
例7.6开辟空间存放一个结构体变量。
#include
usingnamespacestd;
structStudent
{intnum;
stringname;
charsex;
};
voidmain()
{
Student*p;p=newStudent;
p->num=10123;p->name="WangFun";
p->sex='M';
cout<name<<''<num<<''<sex<deletep;
}
WangFun10123M
new和delete运算符与含有指向自己的指针的结构体,就可以实现动态链表,在数据结构课中要详细介绍。
7.2共用体类型
7.2.1共用体的概念
几个不同的变量共占同一段内存的结构,称为共用体(有的书称之为联合)。
7.2.2对共用体变量的访问方式
【例】演示联合的例子。
这里exam是个联合,为说明数据成员val和h开始于同一地址,考虑下面的程序:
#include
#include
unionexam
{shortval;
charh[2];
};
voidmain()
{
examvar;
var.h[0]='A';
cout<<"Thevalueofvar.val:
"<cout<<"Thevalueofvar.h[0]:
"<var.val=66;
var.h[1]='A';
cout<cout<//65*256+66=16706
}
Thevalueofvar.val:
65
Thevalueofvar.h[0]:
A
16706
B
A
7.2.3共用体类型数据的特点
(1)每一瞬时只有一个成员起作用,其它成员不起作用。
(2)共用体变量的地址和它的各个成员的地址都是同一地址。
不能对共用体变量赋值;不能企图引用变量名来得到一个值;不能在定义共用体变量时对它进行初始化;不能用共用体变量作为函数参数。
7.3枚举类型
如果一个变量只有几中可能的值,可以定义为枚举类型
enumcolor{RED,BLUE,GREEN};
关键字enum标志枚举类型定义开始,分号标志其结束。
在C++中允许不写enum;
enum后的标识符color称为枚举类型名。
枚举元素(枚举常量):
花括号内用逗号隔开的标识符是为这个类型定义的常量,
枚举元素的缺省赋值:
编译器为这些枚举常量赋予不同的值,第一个常量RED值为0,以后的常量的值是它前面常量的值增1,所以,BLUE为1, GREEN为2。
声明枚举变量:
枚举标记可以用类型名说明具有该类型的变量,例如:
colorCorVariable;
上面的语句说明了变量CorVariable,可以将说明类型color时所列举的枚举常量中的任何一个置给变量CorVariable,例如:
CorVariable=GREEN;
cout<#include
voidmain()
{
enumcolor{RED,BLUE,GREEN};
colorCorVariable;
CorVariable=GREEN;
cout<cout<//CorVariable=1;//这样不行
CorVariable=(color)1;
}
2
4
枚举常量的自定义赋值:
说明可在枚举常量名之后使用等号将一个(char或int类型的)常量置给一个枚举常量名,以改变编译的缺省赋值,例如:
enumcolor{RED=-1,BLUE,GREEN=6,YELLOW};
这里RED代表值-1,而BLUE为它前面的枚举常量的值加1,所以BLUE代表值0。
同样,GREEN的值为6,而YELLOW的值为7。
为枚举常量指定重复的值也是合法的,例如:
enumstate{FALSE,TRUE,FAIL=0,BAD=0};
在这个说明中FALSE,FAIL和BAD的值都为0。
枚举常量的类型:
枚举常量的类型隐含是unsignedchar或int类型,但到底是何种类型取决于枚举常量的值。
如果所有的值都可用unsignedchar表示,则它就是每个枚举常量的类型。
例7.7口袋中有红、黄、蓝、白、黑5种颜色的球若干个。
每次从口袋中任意取出3个球,问得到3种不同颜色球的可能取法,输出每种排列的情况。
#include
#include
usingnamespacestd;
enumcolor{red,yellow,blue,white,black};
voidmain()
{voidprint(color);
colorpri;
inti,j,k,n=0;
for(i=red;i<=black;i++)
for(j=red;j<=black;j++)
if(i!
=j)
{for(k=red;k<=black;k++)
if((k!
=i)&&(k!
=j))
{
cout<pri=color(i);print(pri);
pri=color(j);print(pri);
pri=color(k);print(pri);
cout<}
}
cout<<"total:
"<}
voidprint(colorco)
{
cout<switch(co)
{
casered:
cout<<"red";break;
caseyellow:
cout<<"yellow";break;
caseblue:
cout<<"blue";break;
casewhite:
cout<<"white";break;
caseblack:
cout<<"black";break;
default:
break;
}
}
38whiteredblue
39whiteredblack
40whiteyellowred
41whiteyellowblue
42whiteyellowblack
43whitebluered
44whiteblueyellow
45whiteblueblack
46whiteblackred
47whiteblackyellow
48whiteblackblue
49blackredyellow
50blackredblue
51blackredwhite
52blackyellowred
53blackyellowblue
54blackyellowwhite
55blackbluered
56blackblueyellow
57blackbluewhite
58blackwhitered
59blackwhiteyellow
60blackwhiteblue
total:
60
7.4用typedef声明类型
用typedef声明一个新的类型名来代替已有的类型名。
typedefintINTEGER;
typedeffloatREAL;
则,以下两行等价:
inti,j;floata,b;
INTEGERi,j;REALa,b;
typedefintNUM[100];NUMn;//定义n为包含100整型元素数组。
typedefchar*STRING;//STRING为字符指针类型。
STRINGp,s[10];//p为字符指针变量,s为字符指针数组.
typedefint(*POINTER)();//POINTER为指向函数指针类型。
函数返回整型值;函数没有参数。
POINTERp1;//p1为指向函数的指针变量。
使用typedef有利于程序的通用与移植。
1实验目的:
(1)掌握结构体的变量的定义和使用。
(2)掌握结构体类型数组的概念和应用。
2实验内容:
第七章习题1习题3习题5
3实验结论:
(3)
例7.7namenumsexjobgrade/position
Li1011fs3
Wang2085mtprof
#include
#include
#include
usingnamespacestd;
struct
{intnum;
charname[10];
charsex;
charjob;
unionP
{intgrade;
char