《(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;