c++第八章例题

例题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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值