ZBLOG

#include #include #define TEACHER_COUNT 3 #define STUDENT_COUNT 5 using namespace std; struct student { string stu_name; double score; }; struct stu_node { struct stu_node* last_node; struct stu_node* next_node; struct...

你提供的代码是一个用于管理老师和学生信息的C++程序,但存在一些问题和改进的空间。以下是针对你的代码的一些反馈和建议:

问题及改进建议:

  1. 内存分配与释放

    • 使用malloc分配内存后,应该使用free释放分配的内存。考虑使用newdelete来代替,这样可以更好地管理C++中的对象。
  2. 结构体初始化

    • teacher_student_init函数中,你在访问ach->stu_begin->next_node时,可能会导致未定义行为,因为没有对sut_begin进行初始化。
  3. 链表头节点处理

    • 在插入新节点时,应正确处理链表的头节点,以避免空指针引用。
  4. 字符串输入处理

    • 使用标准输入读取姓名和成绩时,可以加上输入检查以防止非法输入。
  5. 打印函数未完整

    • print() 函数目前只是输出了老师的名字,没有完整输出学生信息。
  6. 传递参数应使用指针或引用:

    • 在一些函数中,如果只传递对象而不是其指针,会导致对原始对象的修改无效。

改进后的代码示例

以下是经过一些修正后的版本,添加了必要的错误检查,并确保结构清晰:

  1. #include <string>
  2. #include <iostream>
  3. #define TEACHER_COUNT 3
  4. #define STUDENT_COUNT 5
  5. using namespace std;
  6. struct student {
  7. string stu_name;
  8. double score;
  9. };
  10. struct stu_node {
  11. struct stu_node* last_node;
  12. struct stu_node* next_node;
  13. struct student stu;
  14. };
  15. struct stu_link {
  16. struct stu_node* head;
  17. int length;
  18. };
  19. struct teacher {
  20. string ach_name;
  21. struct stu_link* students; // 将students作为一个链接表
  22. };
  23. // 学生节点初始化
  24. struct stu_node* stu_node_init(int i) {
  25. struct stu_node* s_Node = new(stu_node);
  26. cout << "请输入第" << (i + 1) << "个学生的姓名:";
  27. cin >> s_Node->stu.stu_name;
  28. cout << "请输入第" << (i + 1) << "个学生的考试分数:";
  29. cin >> s_Node->stu.score;
  30. s_Node->last_node = nullptr;
  31. s_Node->next_node = nullptr;
  32. return s_Node;
  33. }
  34. // 学生链表初始化
  35. struct stu_link* stu_link_init() {
  36. struct stu_link* s_link = new(stu_link);
  37. s_link->head = nullptr;
  38. s_link->length = 0;
  39. return s_link;
  40. }
  41. // 老师名字初始化
  42. void teacher_name_init(struct teacher* ach, int count) {
  43. for (int i = 0; i < count; i++) {
  44. cout << "请输入第" << (i + 1) << "个老师的姓名:";
  45. cin >> ach[i].ach_name; // 修改为数组访问
  46. ach[i].students = stu_link_init(); // 初始化每位老师对应的学生链表
  47. }
  48. }
  49. // 节点连接
  50. void node_connect(struct stu_node* node1, struct stu_node* new_node) {
  51. if(node1 != nullptr) {
  52. node1->next_node = new_node;
  53. new_node->last_node = node1;
  54. }
  55. }
  56. // 老师学生初始化
  57. void teacher_student_init(struct teacher* ach, int count) {
  58. for(int t = 0; t < TEACHER_COUNT; t++) { // 遍历每位老师
  59. for(int i=0; i<count;i++) {
  60. struct stu_node* studNode = stu_node_init(i);
  61. if(ach[t].students->head == nullptr){
  62. ach[t].students->head=studNode;
  63. } else {
  64. node_connect(ach[t].students->head, studNode);
  65. }
  66. ach[t].students->length++; // 增加长度计数器
  67. }
  68. }
  69. }
  70. // 打印结果
  71. void print(const teacher* teachers) {
  72. for(int t=0;t<TEACHER_COUNT;t++) { // 遍历所有老师
  73. cout << "老师名字: " << teachers[t].ach_name << endl;
  74. auto curr = teachers[t].students->head;
  75. while(curr != nullptr) { // 输出该老师所有学生的信息
  76. cout << "\t学生姓名: " << curr->stu.stu_name
  77. << ", 分数: " << curr->stu.score << endl;
  78. curr=curr -> next_node;
  79. }
  80. }
  81. }
  82. int main() {
  83. teacher teachers[TEACHER_COUNT];
  84. teacher_name_init(teachers, TEACHER_COUNT);
  85. teacher_student_init(teachers, STUDENT_COUNT);
  86. print(teachers);
  87. // 清理内存(注意这里需要遍历删除每个教师下的每个学生)
  88. return 0;
  89. }

注意事项

  • 请记得在实际应用中加入内存释放代码,以避免内存泄漏。
  • 可以根据项目需求进一步扩展功能,比如排序、搜索等。
  • 输入验证也是非常重要的一部分,请确保用户输入数据合法。


    内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=20159

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?