(5.5)--5.5Multi-attributedata.pdf

上传人:刘静 文档编号:57972044 上传时间:2022-11-06 格式:PDF 页数:13 大小:609.59KB
返回 下载 相关 举报
(5.5)--5.5Multi-attributedata.pdf_第1页
第1页 / 共13页
(5.5)--5.5Multi-attributedata.pdf_第2页
第2页 / 共13页
点击查看更多>>
资源描述

《(5.5)--5.5Multi-attributedata.pdf》由会员分享,可在线阅读,更多相关《(5.5)--5.5Multi-attributedata.pdf(13页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、Multi-attribute DataMulti-attribute Data5.5Analysis:The basic information of each student includes multiple attributes such as student number,name,admission score,etc.,which involves the storage of multi-attribute data.Multi-attribute Data ProblemExample1Write a program to record a students student

2、number,name and grades of three subjects,and calculate and output the average grade.Multi-attribute Data5.5For the storage of multi-attribute data,C+provides acorrespondingstructure.Userscandefinethestructuretypeaccording to the problem to be solved,and define variables of thestructure to realize th

3、e storage of multi-attribute data.Store Multi-attribute Data Using the C+Structure Multi-attribute Data5.5(1)Definition of Structure TypeThe general syntax of structure type definition is struct;struct is a keyword indicating the beginning of the structure type definition.is the name of the defined

4、structure type.contains the declarations osseveral members.The declaration form of each member is:;A structure type definition must end with a semicolon.Multi-attribute Data5.5Suppose the basic information of a student includes the student number,name and grades of three subjects,a student structure

5、 type can be defined:struct Studentchar num8;char name10;int score3;Multi-attribute Data5.5(2)Definition of Structure VariablesAfter defining the structure type Student,you can use the Student type to define a structure variable:Student s;Multi-attribute Data5.5(3)Initialization of Structure Variabl

6、esThe initialization syntax of the structure variable is=initial value of member1,initial value of member1,initial value of memberN;Multi-attribute Data5.5For example,when defining the Student structure variable s,you can initialize it:Student s=1210101,Zhangsan,78,90,82;Multi-attribute Data5.5(4)Us

7、e Structure VariablesA structure variable contains multiple member.The syntax of accessing a member in the structure variable is.,where.is a member operatorMulti-attribute Data5.5For example,for the structure variable s defined and initialized earlier,you can access one of its members as follows:cou

8、ts.num,s.name,s.score0s.nums.names.score0s.score1s.score2;Multi-attribute Data5.5Tips:In C+,the access to members in structure variables can only be done one by one,not as a whole.For example,the following statement is wrong:couts;Structure variables of the same type can be assigned to each other as

9、 awhole.For example,suppose s1 and s2 are two Student structurevariables,the following assignment statement is correct:s1=s2;Multi-attribute Data5.5#include using namespace std;struct Student char num8;char name10;int score3;Example2Write a program to define a structure type Student and a variable of the Student type to store the data to be processed,and to find the average score of three grades.Multi-attribute Data5.5int main()int avg=0;Student s=1210101,Zhangsan,78,90,82;for(int i=0;i3;i+)avg=avg+s.scorei;avg=avg/3;cout The average grade of s.nameisavgendl;return 0;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 大学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁