例题1:
/**********************************************
**** 8_1对结构体变量的成员进行操作 ****
***********************************************/
# include<iostream>
using namespace std;
struct date
{
int month;
int day;
int year;
};
struct student
{
int num;
char name[20];
struct date birthday;
char addr[30];
};
int main()
{
student stu1;
stu1.num = 1001;
stu1.birthday.month = 8;
stu1.birthday.day = 20;
stu1.birthday.year = 1980;
cout<<stu1.num<<" ";
cout<<stu1.birthday.month<<" ";
cout<<stu1.birthday.day<<" ";
cout<<stu1.birthday.year<<endl;
return 0;
}
例题2:
/**********************************************
**** 8_2两个同类型结构体相互赋值 ****
***********************************************/
# include<iostream>
using namespace std;
int main()
{
struct
{
int num;
int age;
}stu1, stu2;
stu1.num = 1001;
stu1.age = 20;
stu2 = stu1; //对结构体变量进行赋值
cout<<stu2.num<<endl;
cout<<stu2.age<<endl;
return 0;
}
例题3:
/**********************************************************************
**** 8_3有一张包含三名学生的成绩单,将成绩从大到小排序输出 ****
***********************************************************************/
# include<iostream>
using namespace std;
struct student
{
int num;
char name[20];
float score;
}; //定义结构体类型
int main()
{
//定义结构体数组stu,并初始化
student stu[3] = {{1001, "Liu Jin", 75}, {1002, "Li Lan", 82},
{1003, "Ma Kai", 80}};
student temp;
for(int i = 1; i < 3; i++)
for(int j = 0; j <= 2 - i; j++)
if(stu[j].score < stu[j+1].score)
{
temp = stu[j]; stu[j] = stu[j+1]; stu[j+1] = temp;
}
cout<<"Num"<<" Name"<<" Score"<<endl;
for(int k = 0; k < 3; k++)
cout<<stu[k].num<<" "<<stu[k].name<<" "<<stu[k].score<<endl;
return 0;
}
例题4:
/***************************************************
**** 8_4使用结构体数组指针输出数据 ****
****************************************************/
# include<iostream>
using namespace std;
struct student
{
int num;
char name[20];
float score;
};
int main()
{
student stu[3] = {{1001,"Liu Jin",75},{1002,"Li Lan",82},
{1003,"Ma Kai",80}};
student *s = stu; //指针s指向结构体数组的首地址
cout<<"Num"<<"\tName"<<"\t\tScore"<<endl;
for(; s < stu + 3; s++)
cout<<s->num<<"\t"<<s->name<<"\t\t"<<s->score<<endl;
return 0;
}
例题5:
/****************************************
**** 8_5结构体指针做形参 ****
*****************************************/
# include<iostream>
using namespace std;
struct student
{
int num;
char name[20];
float score;
};
void print(student * ps) //形参ps被定义为指向student类型的指针
{
cout<<ps->num<<" "<<ps->name<<" "<<ps->score<<endl;
}
int main()
{
student stu[3] = {{1001,"Liu Jin",75},{1002,"Li Lan",82},
{1003,"Ma Kai",80}};
for(int i = 0; i < 3; i++)
{
print(&stu[i]); //&stu[i]是结构体数组元素stu[i]的地址
}
return 0;
}
例题12:
/*************************************************************
**** 8_12用枚举类型数据来处理和差积的判断 ****
**************************************************************/
# include<iostream>
using namespace std;
int main()
{
enum en{plus,minus,times}op1;
int x, y;
cout<<"请输入两个数";
cin>>x>>y;
op1 = plus;
while(op1 <= times)
{
switch(op1)
{
case plus:cout<<x<<" + "<<y<<" = "<<x + y<<endl;break;
case minus:cout<<x<<" - "<<y<<" = "<<x - y<<endl;break;
case times:cout<<x<<" * "<<y<<" = "<<x * y<<endl;break;
}
int i = (int)op1; //强制类型转换成int型,因为枚举变量不能自增
op1 = en(++i);
}
return 0;
}