VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 813|回复: 0

[交流] c++结构体实例(实战练手)加深理解

[复制链接]
13_avatar_middle
最佳答案
1 
在线会员 发表于 2022-1-7 13:30:23 | 显示全部楼层 |阅读模式
//定义老师结构体
  1. struct Teacher
  2. {
  3.         string tName; //老师姓名
  4.         student sArray[5];//创建一个老师带5名学生
  5. };
复制代码


//定义学生结构体
  1. struct Student
  2. {
  3.         string sName;//学生姓名
  4.         int score;//学生分数
  5. };
复制代码


再main里面
  1. //创建3名老师的数组
  2.         struct Teacher tArray[3];
复制代码
  1. //给老师赋值,并给老师带的学生赋值
  2.         int len = sizeof(tArray) / sizeof(tArray[0]);
  3.         alloCation(tArray, len);
复制代码

  1. void alloCation(struct Teacher tArray[], int len)
  2. {
  3.         string monicker = "ABCDE";
  4.         for (int i = 0; i < len; i++)
  5.         {
  6.                 tArray[i].tName = "Teacher_";
  7.                 tArray[i].tName += monicker[i];

  8.                 for (int j = 0; j < 5; j++)
  9.                 {
  10.                         tArray[i].sArray[j].sName = "Student_";
  11.                         tArray[i].sArray[j].sName += monicker[j];

  12.                        tArray[i].sArray[j].score = 60;//学生分数统一暂定60.等下修改随机数
  13.                 }
  14.         }
  15. }
复制代码

  1. //打印所有老师带学生的信息
  2.         printInfo(tArray, len);
复制代码


打印所有老师带学生的信息
  1. void printInfo(struct Teacher tArray[], int len)
  2. {
  3.         for (int i = 0; i < len; i++)
  4.         {
  5.                 cout << "老师姓名:" << tArray[i].tName << endl;
  6.                 for (int j = 0; j < 5; j++)
  7.                 {
  8.                         cout << "\t学生姓名:" << tArray[i].sArray[j].sName << " 分数:" << tArray[i].sArray[j].score << endl;
  9.                 }
  10.         }
  11. }
复制代码


修改分数 分配随机数
srand((unsigned int)time(NULL));

完整代码:
  1. #include <iostream>
  2. #include <string>
  3. #include <ctime>
  4. using namespace std;


  5. //定义学生结构体
  6. struct Student
  7. {
  8.         string sName;//学生姓名
  9.         int score;//学生分数
  10. };
  11. //定义老师结构体
  12. struct Teacher
  13. {
  14.         string tName; //老师姓名
  15.         Student sArray[5];//创建一个老师带5名学生
  16. };

  17. void alloCation(struct Teacher tArray[], int len)
  18. {
  19.         string monicker = "ABCDE";
  20.         for (int i = 0; i < len; i++)
  21.         {
  22.                 tArray[i].tName = "Teacher_";
  23.                 tArray[i].tName += monicker[i];

  24.                 for (int j = 0; j < 5; j++)
  25.                 {
  26.                         tArray[i].sArray[j].sName = "Student_";
  27.                         tArray[i].sArray[j].sName += monicker[j];
  28.                         int random = rand() % 61 + 40;
  29.                         tArray[i].sArray[j].score = random;
  30.                 }
  31.         }
  32. }

  33. void printInfo(struct Teacher tArray[], int len)
  34. {
  35.         for (int i = 0; i < len; i++)
  36.         {
  37.                 cout << "老师姓名:" << tArray[i].tName << endl;
  38.                 for (int j = 0; j < 5; j++)
  39.                 {
  40.                         cout << "\t学生姓名:" << tArray[i].sArray[j].sName << " 分数:" << tArray[i].sArray[j].score << endl;
  41.                 }
  42.         }
  43. }
  44. int main()
  45. {
  46.         //创建随机数种子
  47.         srand((unsigned int)time(NULL));
  48.         //创建3名老师的数组
  49.         struct Teacher tArray[3];
  50.         //给老师赋值,并给老师带的学生赋值
  51.         int len = sizeof(tArray) / sizeof(tArray[0]);
  52.         alloCation(tArray, len);
  53.         //打印所有老师带学生的信息
  54.         printInfo(tArray, len);
  55.         system("pause");
  56.         return 0;
  57. }
复制代码





上一篇:SQL 中的modify使用问题
下一篇:c++结构体实战练习(冒泡排序)
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-6-4 22:30

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表