欢迎来到代码驿站!

C代码

当前位置:首页 > 软件编程 > C代码

C++结构体案例练习分享

时间:2023-02-04 11:15:28|栏目:C代码|点击:

先来说一下结构体的一些例子

包含创建结构体和初始化操作

#include<iostream>
#include<string>
using namespace std;

struct User {
	char name[20];
	char sex[5];
	int age;

};

struct Student {
	string name;
	int age;
	int score;

}stu;//第三种


int main() {

	struct User u1;
	strcpy_s(u1.name,"肥学");
	u1.age = 12;
	strcpy_s(u1.sex,"男");
	
	cout << "name:" << u1.name << "age:" << u1.age << "sex:" << u1.sex << endl;
	struct Student s;
	s.name = "鸣人";
	s.age = 20;
	s.score = 80;
	cout << "name:" <<s.name << " age:" << s.age << " sex:" << s.score << endl;

	struct Student s2={ "pte",20,56 };
	cout << "name:" << s2.name << " age:" << s2.age << " sex:" << s2.score << endl;

	struct User u2 = { "pter","男",20 };
	cout << "name:" << u2.name << " age:" << u2.age << " sex:" << u2.sex << endl;
}

结构体的的一些操作

#include<iostream>
#include<string>

using namespace std;

struct Friend{
	string name;
	int age;
	string sex;
};


struct User {
	string name;
	int age;
	string sex;
	struct Friend f;
};


void addFriend(User* u) {
	Friend f = { "fei",20,"men" };
	u->f = f;

}

void addF2(User& u) {
	Friend f = { "xue",20,"men" };
	u.f = f;

}

int main() {
	//结构体数组
	/*User arru[5];
	arru[0] = { "pter",20,"men" };

	cout << arru[0].name << endl;*/
	//结构体指针
	/*
	User u = { "pter",20,"men" };
	User *p = &u;
	cout << p->name << endl;*/

	//嵌套结构体
	/*
	User u;
	Friend f = { "pter",20,"men" };
	u = { "hello",20,"men",f };
	cout << u.name << "  " << u.f.name << endl;*/

	//***结构体做函数的参数***
	//地址传递
	cout << "地址传递" << endl;
	User u = { "pter",20,"men" };
	cout << "myname:" << u.name << "   fname:" << u.f.name << endl;
	addFriend(&u);
	cout << "myname:" << u.name << "   fname:" << u.f.name << endl;

	//引用传递
	cout << "引用传递" << endl;
	User u2 = { "ppt",20,"men" };
	cout << "myname:" << u2.name << "   fname:" << u2.f.name << endl;
	addF2(u2);
	cout << "myname:" << u2.name << "   fname:" << u2.f.name << endl;



}

结构体的const

#include<iostream>
#include<string>
using namespace std;


struct User {
	const string name;//感觉像Java  private
	const int age;
	const string sex;
};

void printInfo(const User* u) {
	
	cout << u->name<<"  " << u->age <<"  " <<u->sex << endl;
}

int main() {

	User u = { "肥学",20,"男" };
	printInfo(&u);

}

案例练习

给每位老师分配几位学生

#include<iostream>
#include<string>
#include<ctime>
using namespace std;

typedef struct Student {
	string name;
	int age;
	string sex;
}S;

typedef struct Teacher {
	string name;
	int age;
	string sex;
	S sarray[5];
}T;

void assignment(T* tarray,int len) {
	for (int i = 0; i < len; i++) {
		cout << "请输入老师姓名:" ;
		cin >> tarray[i].name;
		cout << "请输入年龄:";
		cin >> tarray[i].age;
		cout << "请输入性别:";
		cin >> tarray[i].sex;
		for (int s = 0; s < 2; s++) {//为了操作方便就输入两位学生信息
			cout << "请输入学生姓名:";
			cin >> tarray[i].sarray[s].name;
			//cout << "请输入学生年龄:";
			//cin >> tarray[i].sarray[s].age;
			tarray[i].sarray[s].age = rand() % 18 + 8;
			cout << "请输入学生性别:";
			cin >> tarray[i].sarray[s].sex;
		}

	}

}

void printInfo(T* tarray,int tnum) {
	cout << "信息展示:\n" << endl;
	for (int i = 0; i < tnum; i++) {
		cout << "老师姓名:"<<tarray[i].name<<endl;
		for (int j = 0; j < 2; j++) {
			cout << "    学生姓名:" << tarray[i].sarray[j].name << endl;
			cout << "    学生姓名:" << tarray[i].sarray[j].age << endl;
			cout << "    学生姓名:" << tarray[i].sarray[j].sex << endl;
		}

	}

}

int main() {
	//随机种子
	srand((unsigned int )time (NULL));

	T tarray[3];
	int tnum = sizeof(tarray) / sizeof(tarray[0]);
	assignment(tarray,tnum);
	printInfo(tarray, tnum);
}

案例练习2

对每个员工的工资进赋值,然后对工资排序输出

#include<iostream>
#include<string>
#include<ctime>
using namespace std;

struct staff {
	string name;
	int salary;

};

void initStaff(staff* sarray,int len) {
	string nameseed = "ABCDE";
	for (int i = 0; i < len; i++) {
		int r = rand() % 10000 + 3000;
		string n = "user_";
		string name = n + nameseed[i];
		staff s = {name,r };
		sarray[i] = s;
	}

}

void printInfo(staff* sarray, int len) {

	for (int i = 0; i < len; i++) {
		cout << "姓名:" << sarray[i].name << "  工资:" << sarray[i].salary << endl;
	}
}

void sortSalary(staff* sarray,int len) {
	for (int i = len-1; i >0; i--) {
		for (int j = len-1; j >= len-i; j--) {
			staff maxsalary;
			if (sarray[j].salary > sarray[j - 1].salary) {
				maxsalary = sarray[j];
				sarray[j] = sarray[j - 1];
				sarray[j - 1] = maxsalary;
			}
		}
	}
}



int main() {
	srand((unsigned int)time(NULL));
	staff sarray[5];
	int snum = (sizeof(sarray)) / sizeof(sarray[0]);
	initStaff(sarray, snum);
	printInfo(sarray, snum);
	sortSalary(sarray, snum);
	cout << "\n\n按工资排序后:\n" << endl;
	printInfo(sarray, snum);

}

上一篇:C++简单集合类的实现方法

栏    目:C代码

下一篇:C++骑士游历问题(马踏棋盘)解析

本文标题:C++结构体案例练习分享

本文地址:http://www.codeinn.net/misctech/225072.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有