21xrx.com
2024-09-20 01:06:50 Friday
登录
文章检索 我的文章 写文章
C++学生信息管理系统课程设计:查询模块代码
2023-07-01 06:07:17 深夜i     --     --
C++ 学生信息管理系统 课程设计 查询模块 代码

在C++的学生信息管理系统中,查询模块是非常重要的一个模块之一。在这个模块中,有查询功能的代码,可以让用户通过各种不同的方式查找、筛选、排序和显示已存储的学生信息。

以下是查询模块的相关代码示例:

1.查询全部学生信息:

void showAllStudents(Student *stu, int num) {

  for (int i = 0; i < num; i++) {

    cout << "\n姓名: " << stu[i].name << endl;

    cout << "学号: " << stu[i].id << endl;

    cout << "年龄: " << stu[i].age << endl;

    cout << "性别: " << stu[i].sex << endl;

    cout << "成绩: " << stu[i].score << endl;

    cout << "---------------------\n";

  }

}

此代码通过遍历学生信息数组,并输出每个学生的详细信息,实现了查询全部学生信息的功能。

2.按学号查询:

void searchStudentById(Student *stu, int num, string id) {

  for (int i = 0; i < num; i++) {

    if (stu[i].id == id) {

      cout << "\n姓名: " << stu[i].name << endl;

      cout << "学号: " << stu[i].id << endl;

      cout << "年龄: " << stu[i].age << endl;

      cout << "性别: " << stu[i].sex << endl;

      cout << "成绩: " << stu[i].score << endl;

      return;

    }

  }

  cout << "\n不存在该学号对应的学生信息!\n";

}

此代码通过遍历学生信息数组,并对比学生学号,输出对应学生的详细信息,实现了按学号查询的功能。

3.按成绩排序:

void sortByScore(Student *stu, int num) {

  for (int i = 0; i < num - 1; i++) {

    for (int j = 0; j < num - 1 - i; j++) {

      if (stu[j].score > stu[j + 1].score) {

        Student temp = stu[j];

        stu[j] = stu[j + 1];

        stu[j + 1] = temp;

      }

    }

  }

  cout << "\n按成绩排序后的学生信息:\n";

  showAllStudents(stu, num);

}

此代码通过冒泡排序法,将学生信息数组按成绩从小到大排列,并输出排序后的学生信息,实现了按成绩排序的功能。

以上是查询模块的代码示例,其中覆盖了常见的查询、筛选和排序等常见功能。小伙伴们可以在此基础上进行修改和扩展,实现更加丰富多样的查询功能。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复
    相似文章