c++入门(4)

#include<iostream>
#include<string>
#include<ctime>
using namespace std;
struct student//学生类型
{
	string name;
	int age;
	int score;
};
struct teacher
{
	int id;
	string name;
	int age;
	struct student stu;
};
void printstudent(struct student s)//值传递
{
	cout << "子函数1中  姓名: " << s.name << " 年龄: " << s.age << " 分数: " << s.score << endl;

}
void printstudent1(const struct student* s)//值传递
{
	s->age = 100;//const修饰,不能够在内部修改
	cout << "子函数1中  姓名: " << s->name << " 年龄: " << s->age << " 分数: " << s->score << endl;

}
void printstudent2(struct student* p)
{
	cout << "子函数2中  姓名: " << p->name << " 年龄: " << p->age << " 分数: " << p->score << endl;
}
int main()
{
	struct student s1;
	s1.name = "张三";
	s1.age = 20;
	s1.score = 100;
	cout << "姓名: " << s1.name << " 年龄: " << s1.age << " 分数: " << s1.score << endl;
	struct student s2 = { "李四",19,99 };
	cout << "姓名: " << s2.name << " 年龄: " << s2.age << " 分数: " << s2.score << endl;
	//结构体---自定义数据类型
	//结构体数组
	struct student arry[3] =
	{
		{"张三",18,100},
		{"李四",28,99},
		{"王五",38,66},
	};
	for (int i = 0; i < 3; i++) {
		cout << " 姓名: " << arry[i].name
			<< " 年龄: " << arry[i].age
			<< " 分数: " << arry[i].score<<endl;
	}
	//结构体指针
	struct student s3 = { "赵云",50,99 };//struct可以省略
	struct student* p = &s3;//struct可以省略
	cout << "姓名: " << p->name << " 年龄: " << p->age << " 分数: " << p->score << endl;

	//结构体嵌套结构体
	teacher t = { 10000,"王老师",45,{"小李",20,100} };
	cout << t.id << t.name << t.age << t.stu.name << t.stu.age << t.stu.score << endl;


	//结构体做函数参数----值传递(形参改变不会影响实参)   址传递(形参改变会影响实参)
	
	//结构体中const使用场景
	printstudent(s2);
	printstudent2(&s2);
	system("pause");
	return 0;
}
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
//结构体案例1
struct student
{
	string name;
	int score;
};
struct teacher
{
	string tname;
	struct student sarry[5];
};
void fuzhi(struct teacher tarry[], int sz)
{
	string nameseed = "ABCDE";
	for (int i = 0; i < sz; i++) {
		//给老师开始赋值
		tarry[i].tname = "Teacher_";
		tarry[i].tname += nameseed[i];
		//通过循环给每名老师所带的学生赋值
		for (int j = 0; j < 5; j++) {
			tarry[i].sarry[j].name = "student_";
			tarry[i].sarry[j].name += nameseed[j];
			int random = rand() % 100 + 1;
			tarry[i].sarry[j].score = random;
		}
	}
}
void printinfo(struct teacher tarry[], int sz)
{
	for (int i = 0; i < sz; i++) {
		cout << "老师姓名: " << tarry[i].tname << endl;
		for (int j = 0; j < 5; j++) {
			cout << "\t学生姓名: " << tarry[i].sarry[j].name << " 考试分数: " << tarry[i]. sarry[j].score << endl;
		}
	}
}
int main()
{
	srand((unsigned int)time(NULL));
	struct teacher tarry[3];
	int sz = sizeof(tarry) / sizeof(tarry[0]);
	fuzhi(tarry, sz);
	printinfo(tarry, sz);
	system("pause");
	return 0;
}
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
//结构体案例2---结构体排序
struct hero
{
	string name;
	int age;
	string sex; 
};
void func(struct hero heroarry[],int sz)
{
	for (int i = 0; i < sz - 1; i++) {
		for (int j = 0; j < sz - 1 - i; j++) {
			if (heroarry[j].age > heroarry[j + 1].age) {
				struct hero tmp = heroarry[j];
				heroarry[j] = heroarry[j + 1];
				heroarry[j + 1] = tmp;
			}
		}
	}
	
}
void print(struct hero heroarry[], int sz)
{
	for (int i = 0; i < sz; i++) {
		cout << heroarry[i].name << heroarry[i].age << heroarry[i].sex << endl;
	}
}
int main()
{
	struct hero heroarry[5] =
	{
		{"刘备",45,"男"},
		{"关羽",43,"男"},
		{"张飞",40,"男"},
		{"赵云",38,"男"},
		{"貂蝉",18,"女"},
	};
	int sz = sizeof(heroarry) / sizeof(heroarry[0]);
	func(heroarry, sz);
	print(heroarry, sz);
	system("pause");
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值