C++结构体案例练习分享
先来说一下结构体的一些例子
包含创建结构体和初始化操作
#include#include 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:" < 结构体的的一些操作
#include#include 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#include 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 <<" " < sex << endl; } int main() { User u = { "肥学",20,"男" }; printInfo(&u); } 案例练习
给每位老师分配几位学生
#include#include #include 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 << "老师姓名:"< 案例练习2
对每个员工的工资进赋值,然后对工资排序输出
#include#include #include 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#正则表达式大全
X 关闭
X 关闭
- 15G资费不大降!三大运营商谁提供的5G网速最快?中国信通院给出答案
- 2联想拯救者Y70发布最新预告:售价2970元起 迄今最便宜的骁龙8+旗舰
- 3亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 4现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 5如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 6AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 7转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 8充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 9好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 10名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?