基础项目: 编写主函数,通过菜单控制及调用相应的子函数完成以下功能: (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; }

相关推荐

最新推荐

recommend-type

子函数同时被中断函数和主函数

本文介绍了单片机中常见的中断函数调用子函数和主函数调用子函数的情况,如果同时调用会出现的问题一节解决方法。
recommend-type

输入圆的半径求周长和面积。程序由三个函数组成:主函数进行输入输出和函数调用,perimeter函数求周长,area函数求面积。

输入圆的半径求周长和面积。程序由三个函数组成:主函数进行输入输出和函数调用,perimeter函数求周长,area函数求面积。
recommend-type

Python如何在main中调用函数内的函数方式

主要介绍了Python如何在main中调用函数内的函数方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

android手机应用源码Imsdroid语音视频通话源码.rar

android手机应用源码Imsdroid语音视频通话源码.rar
recommend-type

营销计划汇报PPT,市场品牌 推广渠道 产品 营销策略tbb.pptx

营销计划汇报PPT,市场品牌 推广渠道 产品 营销策略tbb.pptx
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

可见光定位LED及其供电硬件具体型号,广角镜头和探测器,实验设计具体流程步骤,

1. 可见光定位LED型号:一般可使用5mm或3mm的普通白色LED,也可以选择专门用于定位的LED,例如OSRAM公司的SFH 4715AS或Vishay公司的VLMU3500-385-120。 2. 供电硬件型号:可以使用常见的直流电源供电,也可以选择专门的LED驱动器,例如Meanwell公司的ELG-75-C或ELG-150-C系列。 3. 广角镜头和探测器型号:一般可采用广角透镜和CMOS摄像头或光电二极管探测器,例如Omron公司的B5W-LA或Murata公司的IRS-B210ST01。 4. 实验设计流程步骤: 1)确定实验目的和研究对象,例如车辆或机器人的定位和导航。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。