首頁 > 軟體

C++結構體案例練習分享

2022-04-20 19:01:08

先來說一下結構體的一些例子

包含建立結構體和初始化操作

#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 << "nn按工資排序後:n" << endl;
	printInfo(sarray, snum);

}

到此這篇關於C++結構體案例練習分享的文章就介紹到這了,更多相關C++結構體內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com