建立包含学生信息(学号,姓名,成绩)的线性表,使其具有如下功能
(1)根据指定学生个数,逐个输入学生信息
(2)逐个显示学生表中所有学生的相关信息
(3)根据姓名进行查找,返回此学生的学号,成绩
(4)根据指定的位置可返回相的的学生信息(学号,姓名,成绩)
(5)给定一个学生信息,插入到表中指定的位置。
(6)删除指定位置的学生记录
(7)统计表学生个数
(8)已知2个班的成绩 将学生成按成绩从低到离序,并输出结果
可以使用数组或链表来实现线性表,线性表中每一个元素就是学生结构体。你这内容有点多啊。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 学生信息类
class Student {
public:
Student(int num, string name, int score) : m_num(num), m_name(name), m_score(score) {}
int num() const { return m_num; }
string name() const { return m_name; }
int score() const { return m_score; }
private:
int m_num;
string m_name;
int m_score;
};
// 学生表类
class StudentTable {
public:
// 添加新的学生信息到表中
void addStudent(const Student& s) {
m_students.push_back(s);
}
// 根据学号查找学生信息
Student findStudent(int num) const {
for (auto& s : m_students) {
if (s.num() == num) {
return s;
}
}
throw runtime_error("Student not found.");
}
// 根据姓名查找学生信息
vector<Student> findStudentsByName(string name) const {
vector<Student> students;
for (auto& s : m_students) {
if (s.name() == name) {
students.push_back(s);
}
}
return students;
}
// 根据表中的位置查找学生信息
Student findStudentByIndex(int index) const {
if (index < 0 || index >= m_students.size()) {
throw runtime_error("Index out of range.");
}
return m_students[index];
}
// 向表中指定的位置插入学生信息
void insertStudent(int index, const Student& s) {
if (index < 0 || index > m_students.size()) {
throw runtime_error("Index out of range.");
}
m_students.insert(m_students.begin() + index, s);
}
// 删除表中指定位置的学生信息
void removeStudent(int index) {
if (index < 0 || index >= m_students.size()) {
throw runtime_error("Index out of range.");
}
m_students.erase(m_students.begin() + index);
}
// 返回表中学生的个数
int count() const {
return m_students.size();
}
// 将学生表按成绩进行排序
void sortByScore() {
sort(m_students.begin(), m_students.end(),
[](const Student& s1, const Student& s2) {
return s1.score() < s2.score();
}
);
}
// 输出学生表中的所有学生信息
void printAllStudents() const {
for (auto& s : m_students) {
cout << "num: " << s.num()
<< ", name: " << s.name()
<< ", score: " << s.score() << endl;
}
}
private:
vector<Student> m_students;
};
int main() {
StudentTable table;
// 输入 n 个学生信息
int n;
cout << "Input the number of students: ";
cin >> n;
for (int i = 0; i < n; i++) {
int num, score;
string name;
cout << "Input the information of student " << i + 1 << ":" << endl;
cout << "num: ";
cin >> num;
cout << "name: ";
cin >> name;
cout << "score: ";
cin >> score;
table.addStudent(Student(num, name, score));
}
// 输出学生表中所有学生信息
cout << "All students:" << endl;
table.printAllStudents();
cout << endl;
// 根据姓名查找学生信息
string name;
cout << "Input the name of the student to find: ";
cin >> name;
auto students = table.findStudentsByName(name);
if (students.empty()) {
cout << "Cannot find the student." << endl;
} else {
for (auto& s : students) {
cout << "num: " << s.num()
<< ", score: " << s.score() << endl;
}
}
cout << endl;
// 根据位置查找学生信息
int index;
cout << "Input the index of the student to find: ";
cin >> index;
try {
auto s = table.findStudentByIndex(index);
cout << "num: " << s.num()
<< ", name: " << s.name()
<< ", score: " << s.score() << endl;
} catch (const exception& e) {
cout << e.what() << endl;
}
cout << endl;
// 插入学生信息
int insertIndex, insertNum, insertScore;
string insertName;
cout << "Input the position to insert student: ";
cin >> insertIndex;
cout << "Input the information of the student to insert: " << endl;
cout << "num: ";
cin >> insertNum;
cout << "name: ";
cin >> insertName;
cout << "score: ";
cin >> insertScore;
try {
table.insertStudent(insertIndex, Student(insertNum, insertName, insertScore));
cout << "Insert success." << endl;
} catch (const exception& e) {
cout << e.what() << endl;
}
cout << endl;
// 删除学生信息
int removeIndex;
cout << "Input the position to remove student: ";
cin >> removeIndex;
try {
table.removeStudent(removeIndex);
cout << "Remove success." << endl;
} catch (const exception& e) {
cout << e.what() << endl;
}
cout << endl;
// 统计表中学生个数
cout << "The number of all students: " << table.count() << endl;
cout << endl;
// 按成绩排序并输出学生信息
table.sortByScore();
cout << "All students sorted by score:" << endl;
table.printAllStudents();
cout << endl;
return 0;
}
代码如下:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 学生类
class Student {
public:
int id; // 学号
string name; // 姓名
int score; // 成绩
Student(int i, string n, int s) : id(i), name(n), score(s) {}
};
// 学生表类
class StudentList {
private:
vector<Student> list; // 学生表
public:
// 添加学生信息
void add(int id, string name, int score) {
Student s(id, name, score);
list.push_back(s);
}
// 显示学生表中所有学生的相关信息
void show() {
cout << "学号\t姓名\t成绩" << endl;
for (auto s : list) {
cout << s.id << "\t" << s.name << "\t" << s.score << endl;
}
}
// 根据姓名进行查找,返回此学生的学号,成绩
Student findByName(string name) {
for (auto s : list) {
if (s.name == name) {
return s;
}
}
return Student(-1, "", -1);
}
// 根据指定的位置可返回相应的学生信息(学号,姓名,成绩)
Student findByIndex(int index) {
if (index < list.size()) {
return list[index];
}
else {
return Student(-1, "", -1);
}
}
// 给定一个学生信息,插入到表中指定的位置。
void insert(int index, int id, string name, int score) {
Student s(id, name, score);
if (index < list.size()) {
list.insert(list.begin() + index, s);
}
else {
list.push_back(s);
}
}
// 删除指定位置的学生记录
void remove(int index) {
if (index < list.size()) {
list.erase(list.begin() + index);
}
}
// 统计表中学生个数
int count() {
return list.size();
}
// 按成绩从低到高排序
void sort() {
std::sort(list.begin(), list.end(), [](const Student& s1, const Student& s2) {
return s1.score < s2.score;
});
}
};
int main() {
StudentList sl;
// 添加学生信息
int n;
cout << "请输入学生个数:";
cin >> n;
for (int i = 0; i < n; i++) {
int id, score;
string name;
cout << "请输入学号:";
cin >> id;
cout << "请输入姓名:";
cin >> name;
cout << "请输入成绩:";
cin >> score;
sl.add(id, name, score);
}
// 显示学生表中所有学生的相关信息
sl.show();
// 根据姓名进行查找,返回此学生的学号,成绩
string name;
cout << "请输入要查找的学生姓名:";
cin >> name;
Student s = sl.findByName(name);
if (s.id == -1) {
cout << "未找到该学生" << endl;
}
else {
cout << "学号:" << s.id << "\t姓名:" << s.name << "\t成绩:" << s.score << endl;
}
// 根据指定的位置可返回相应的学生信息(学号,姓名,成绩)
int index;
cout << "请输入要查找的学生位置:";
cin >> index;
s = sl.findByIndex(index);
if (s.id == -1) {
cout << "未找到该学生" << endl;
}
else {
cout << "学号:" << s.id << "\t姓名:" << s.name << "\t成绩:" << s.score << endl;
}
// 给定一个学生信息,插入到表中指定的位置。
int id, score;
string newname;
cout << "请输入要插入的学生位置:";
cin >> index;
cout << "请输入学号:";
cin >> id;
cout << "请输入姓名:";
cin >> newname;
cout << "请输入成绩:";
cin >> score;
sl.insert(index, id, newname, score);
sl.show();
// 删除指定位置的学生记录
cout << "请输入要删除的学生位置:";
cin >> index;
sl.remove(index);
sl.show();
// 统计表中学生个数
cout << "学生个数:" << sl.count() << endl;
// 按成绩从低到高排序
sl.sort();
sl.show();
return 0;
}
这里有很多参考资料,但是没有明确的问题描述,因此我不清楚你需要解决哪个问题。如果你需要使用C++实现数据结构,可以使用参考资料中提供的代码作为参考。具体实现会受到具体要求的影响,因此需要更详细的问题描述才能给出具体的解决方案。