编写程序,初始化顺序表L(学生信息从文件读入)
时间: 2023-05-29 20:04:28 浏览: 63
这里提供一个Python的示例代码,可以从文件读取学生信息并初始化顺序表L:
```python
class Student:
def __init__(self, id, name, score):
self.id = id
self.name = name
self.score = score
def init_list(filename):
L = []
with open(filename, 'r') as f:
for line in f:
id, name, score = line.strip().split(',')
L.append(Student(id, name, float(score)))
return L
# 读取文件中的学生信息并初始化顺序表L
L = init_list('students.txt')
```
其中,`Student`类表示一个学生对象,包含学号、姓名和成绩三个属性。`init_list`函数从指定的文件中读取每行学生信息,创建一个`Student`对象并添加到顺序表L中。最后,函数返回初始化后的顺序表L。可以根据实际情况修改`Student`类的属性和`init_list`函数的实现。
相关问题
基础项目: 编写主函数,通过菜单控制及调用相应的子函数完成以下功能: (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>将顺序表的元素按“班级”信息有序输出,同一班级的按姓名有序输出。
主函数代码如下:
#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;
}
顺序表的定义,初始化,插入,删除,并编写一个读入一个字符串,把它顺序存入一个顺序表并逆序打印出
顺序表是一种线性数据结构,它用一组连续的存储单元来存储数据元素,其特点是可以随机访问任何一个元素。
定义:顺序表可以用数组来实现,定义一个固定大小的数组作为顺序表的存储空间,并在数组中依次存放顺序表中的元素。
初始化:顺序表的初始化就是将其元素个数设置为0,即表为空。
插入:顺序表中的插入操作需要将插入位置后的元素全部向后移动一位,然后将要插入的元素放到指定位置。
删除:顺序表的删除操作需要将删除位置后的元素全部向前移动一位,然后将最后一个元素删除。
以下是一个读入一个字符串,把它顺序存入一个顺序表并逆序打印出的示例代码:
```C++
#include <iostream>
#include <string>
using namespace std;
const int MAXSIZE = 100; // 顺序表的最大长度
typedef struct {
char data[MAXSIZE]; // 顺序表的存储空间
int length; // 顺序表的当前长度
} SeqList;
// 初始化顺序表
void InitList(SeqList &L) {
L.length = 0;
}
// 插入元素
bool ListInsert(SeqList &L, int pos, char elem) {
if (pos < 1 || pos > L.length + 1 || L.length >= MAXSIZE) {
return false;
}
for (int i = L.length; i >= pos; i--) {
L.data[i] = L.data[i-1];
}
L.data[pos-1] = elem;
L.length++;
return true;
}
// 删除元素
bool ListDelete(SeqList &L, int pos) {
if (pos < 1 || pos > L.length) {
return false;
}
for (int i = pos; i < L.length; i++) {
L.data[i-1] = L.data[i];
}
L.length--;
return true;
}
int main() {
SeqList L;
InitList(L);
string str;
cout << "请输入字符串:";
getline(cin, str);
for (int i = 0; i < str.length(); i++) {
ListInsert(L, i+1, str[i]);
}
cout << "逆序输出:";
for (int i = L.length; i >= 1; i--) {
cout << L.data[i-1];
}
cout << endl;
return 0;
}
```
该程序首先定义了顺序表的结构体,并初始化了一个空的顺序表。接着通过 `getline()` 函数读取用户输入的字符串,并将其逐个插入到顺序表中。最后,通过循环逆序输出顺序表中的元素。