cpp--data type

cpp允许用户根据自己声明一些类型【数组,结构体类型,共用体类型,枚举类型,类类型】user-defined type

 1 #include <iostream>
 2 using namespace std;
 3 
 4 struct Data
 5 {
 6     int month;
 7     int day;
 8     int year;
 9 };
10 struct Student
11 {
12     int num;
13     char name[20];
14     char sex;
15     Data birthday;
16     float score;
17 }student1,student2={10002,"hello",'b',5,23,9000,90};
18 int main()
19 {
20     student1=student2;
21     cout<<"student1 info about:"<<endl;
22     cout<<"num:"<<student1.num<<endl;
23     cout<<"name:"<<student1.name<<endl;
24     cout<<"sex:"<<student1.sex<<endl;
25     cout<<"month:"<<student1.birthday.month<<endl;
26     cout<<"year:"<<student1.birthday.year<<endl;
27     cout<<"score:"<<student1.score<<endl;
28 }

结构体数组:每个数组元素都是一个结构体类型的数据,他们都分别包括各个成员项

 1 #if 0
 2 3个候选人,最终有一个人当选,10个人参与投票,键盘输入3个候选人名字(10次),输出每个候选人的得票结果
 3 #endif
 4 
 5 #include <iostream>
 6 using namespace std;
 7 
 8 struct Person//声明结构体类型Person
 9 {
10     char name[20];
11     int cout;
12 }leader[3]={"a",0,"b",0,"c",0};//定义Person类型的数组leader
13 int main()
14 { 
15     int i,j;
16     string leader_name;//字符变量
17     for(i=0;i<10;i++)
18     {
19         cin>>leader_name;
20         for(j=0;j<3;j++)
21             if(leader_name==leader[j].name)//将输入的名字与候选人名字作对比
22                 leader[j].cout++;//相同,计数+1
23     }
24     cout<<endl;
25     for(i=0;i<3;i++)
26         cout<<leader[i].name<<":"<<leader[i].cout<<endl;
27     return 0;
28 
29 }

 

posted on 2018-08-28 22:26  fortwater  阅读(260)  评论(0编辑  收藏  举报

导航