基础项目: 编写主函数,通过菜单控制及调用相应的子函数完成以下功能: (1)初始化顺序表L(学生信息从文件读入)。 (2)输出顺序表L(要求一名学生的信息占一行)。 (3)输出顺序表L的长度。 (4)输出顺序表L的第i个学生的信息,i的值可从键盘输入。 (5)输出学生x的位置,x的关键字值(学号或姓名)可从键盘输入。 (6)在第i个元素位置上插入学生x的信息,i和x的信息可从键盘输入。 (7)删除L的第i个元素,i的值可从键盘输入。 (8)删除L中学生x的信息,x的关键字值(学号或姓名)可从键盘输入。 3.拓展项目: <1>将顺序表的元素按“班级”信息分别存储到一个顺序表中。 <2>将顺序表的元素按“班级”信息有序输出,同一班级的按姓名有序输出。
时间: 2023-06-01 11:02:30 浏览: 79
主函数代码如下:
#include <iostream>
#include <fstream>
#include <string>
#include "Student.h" //学生类的头文件
#include "SeqList.h" //顺序表类的头文件
using namespace std;
//从文件读入学生信息,并初始化顺序表
void initList(SeqList<Student> &L)
{
ifstream infile("student.txt"); //打开文件
if (!infile)
{
cerr << "Fail to open file: student.txt" << endl;
exit(1);
}
Student stu;
while (infile >> stu) //重载运算符>>
L.insert(L.length() + 1, stu); //在末尾插入学生信息
infile.close(); //关闭文件
}
//输出顺序表
void printList(SeqList<Student> &L)
{
for (int i = 1; i <= L.length(); i++)
cout << L.get(i) << endl; //重载运算符<<
}
//输出顺序表长度
void printLength(SeqList<Student> &L)
{
cout << "Length of the list is " << L.length() << endl;
}
//输出第i个学生的信息
void printStudent(SeqList<Student> &L, int i)
{
if (i < 1 || i > L.length())
{
cerr << "Invalid position!" << endl;
exit(1);
}
cout << L.get(i) << endl;
}
//输出学生x的位置
void printPosition(SeqList<Student> &L, string x)
{
int pos = L.search(x); //查找位置
if (pos == 0)
cout << "Student not found!" << endl;
else
cout << "Position of student " << x << " is " << pos << endl;
}
//在第i个元素位置上插入学生x的信息
void insertStudent(SeqList<Student> &L, int i, Student x)
{
if (i < 1 || i > L.length() + 1)
{
cerr << "Invalid position!" << endl;
exit(1);
}
L.insert(i, x);
cout << "Insert student " << x.getName() << " successfully!" << endl;
}
//删除第i个元素
void deleteStudent(SeqList<Student> &L, int i)
{
if (i < 1 || i > L.length())
{
cerr << "Invalid position!" << endl;
exit(1);
}
Student stu = L.get(i);
L.remove(i);
cout << "Delete student " << stu.getName() << " successfully!" << endl;
}
//删除学生x的信息
void deleteStudent(SeqList<Student> &L, string x)
{
int pos = L.search(x); //查找位置
if (pos == 0)
{
cout << "Student not found!" << endl;
return;
}
Student stu = L.get(pos);
L.remove(pos);
cout << "Delete student " << stu.getName() << " successfully!" << endl;
}
int main()
{
SeqList<Student> L; //定义一个学生顺序表
initList(L); //从文件读入学生信息,并初始化顺序表
int choice;
do
{
cout << endl << "Menu:" << endl;
cout << "1. Print the list" << endl;
cout << "2. Print the length of the list" << endl;
cout << "3. Print the information of the i-th student" << endl;
cout << "4. Print the position of the student x" << endl;
cout << "5. Insert a student at the i-th position" << endl;
cout << "6. Delete the i-th student" << endl;
cout << "7. Delete the student x" << endl;
cout << "0. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 0:
break;
case 1:
printList(L);
break;
case 2:
printLength(L);
break;
case 3:
int i;
cout << "Enter the position of the student: ";
cin >> i;
printStudent(L, i);
break;
case 4:
string x;
cout << "Enter the keyword of the student: ";
cin >> x;
printPosition(L, x);
break;
case 5:
Student stu;
cout << "Enter the information of the student:" << endl;
cin >> stu; //重载运算符>>
cout << "Enter the position to insert: ";
cin >> i;
insertStudent(L, i, stu);
break;
case 6:
cout << "Enter the position to delete: ";
cin >> i;
deleteStudent(L, i);
break;
case 7:
cout << "Enter the keyword of the student: ";
cin >> x;
deleteStudent(L, x);
break;
default:
cerr << "Invalid choice!" << endl;
break;
}
} while (choice != 0);
return 0;
}