数据结构学生信息表+附加题

#include <iostream>
#include<algorithm>
#include<string>

using namespace std;
const int Maxsize = 100;

typedef struct Student {
	string num;
	string name;
	char gender;
	double score;
}Student;
class SeqList
{
public:
	SeqList() {};
	void CreatList(Student& st, int i, int n)//建立顺序表1
	{
		if (length < Maxsize) {
			stu[i] = st;

		}
		else throw"上溢";
		length = n;
	}



	void insert(int pos, Student& s) {//插入学生信息2

		if (pos < 0 || pos>length) {
			cout << "学生信息插入位置不合理" << endl;
		}
		else if (length >= Maxsize) {
			throw"学生信息表满了";
		}
		else {
			cout << "输入学生信息:" << endl;
			cout << "学号:"; cin >> s.num; cout << endl;
			cout << "姓名:"; cin >> s.name; cout << endl;
			cout << "性别:"; cin >> s.gender; cout << endl;
			cout << "成绩:"; cin >> s.score; cout << endl;
			for (int i = length; i > pos; --i) {
				stu[i] = stu[i - 1];
			}
			stu[pos] = s;
			length++;
			cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
			cout << "            学生信息线性表" << endl;
			cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
			printList();
		}
	}

	void findList(int a33) {//查询学生信息3
		if (a33 == 1) {
			cout << "请输入要查找的学号:"; string xh; cin >> xh; cout << endl;
			int pd = 0;
			for (int i = 0; i < length; i++) {
				if (stu[i].num == xh) {
					cout << "您要查询的学生为:" << endl;
					cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
					cout << stu[i].num << "       " << stu[i].name << "       " << stu[i].gender << "       " << stu[i].score << endl;
					pd++;
				}
			}
			if (pd == 0) cout << "找不到此学生!" << endl;
		}
		else {
			cout << "请输入要查找的姓名:"; string xm; cin >> xm; cout << endl;
			int pd = 0;
			for (int i = 0; i < length; i++) {
				if (stu[i].name == xm) {
					cout << "您要查询的学生为:" << endl;
					cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
					cout << stu[i].num << "       " << stu[i].name << "       " << stu[i].gender << "       " << stu[i].score << endl;
					pd++;
				}
			}
			if (pd == 0) cout << "找不到此学生!" << endl;
		}
	}

	void delNode(int a44) {//删除单个学生信息4
		if (a44 == 1) {
			cout << "请输入要查找的学号:"; string xh; cin >> xh; cout << endl;
			int pd = 0;
			for (int i = 0; i < length; i++) {
				if (stu[i].num == xh) {
					length--;
					for (int j = i; j < length; j++) {
						stu[j] = stu[j + 1];
					}
					cout << "该学生的信息已被删除!" << endl;
					pd++;
				}
			}
			if (pd == 0)cout << "没有找到要删除的记录!" << endl;
		}
		else {
			cout << "请输入要查找的姓名:"; string xm; cin >> xm; cout << endl;
			int pd = 0;
			for (int i = 0; i < length; i++) {
				if (stu[i].name == xm) {
					length--;
					for (int j = i; j < length; j++) {
						stu[j] = stu[j + 1];
					}
					cout << "该学生的信息已被删除!" << endl;
					pd++;
				}
			}
			if (pd == 0)cout << "没有找到要删除的记录!" << endl;
		}
	}

	void printList() {//输出学生信息5
		cout << " 学号       姓名        性别       成绩" << endl;
		for (int i = 0; i < length; i++) {
			cout << stu[i].num << "       " << stu[i].name << "       " << stu[i].gender << "       " << stu[i].score << endl;
		}
	}

	void Remove(int j, int n) {//批量删除学生信息6
		int pd = 0;
		int m = j + n;
		for (int i = j; i < m; i++) {
			if (i + n-1 <= length) {
				stu[i - 1] = stu[i + n - 1];
				pd++;
				length--;
			}
			else {
				pd++; length--;
			}
		}

		if (pd == n)cout << "这n个学生的信息已被删除!" << endl;
		else cout << "没有找到要删除的记录!" << endl;
	}

	void Add(int n, int pos, Student& s) {//批量添加学生信息7

		if (pos < 0 || pos>length+1) {
			cout << "学生信息插入位置不合理" << endl;
		}
		else if (length >= Maxsize) {
			throw"学生信息表满了";
		}
		else {
			int m = length;
			int x = length - pos + 1;
			int b = pos;
			while (x--) {
				stu[b + n - 1] = stu[b-1];
				b++;
			}
			for (int i = pos - 1; i < pos + n - 1; i++) {
				cout << "输入学生信息:" << endl;
				cout << "学号:"; cin >> s.num; cout << endl;
				cout << "姓名:"; cin >> s.name; cout << endl;
				cout << "性别:"; cin >> s.gender; cout << endl;
				cout << "成绩:"; cin >> s.score; cout << endl;
				stu[i] = s;
				length++;
			}
			cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
			cout << "            学生信息线性表" << endl;
			cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
			printList();
		}
	}

	void Sort() {//男女排序
		int left = 0;
		int right = length - 1;
		while (left < right) {
			while (left < right && stu[left].gender == 'M') {//找左边第一个女生
				left++;
			}
			while (left < right && stu[right].gender == 'W') {//找右边第一个男生
				right++;
			}
			if (left < right) {
				stuu[0] = stu[right];
				stu[right] = stu[left];
				stu[left] = stuu[0];
				left++;
				right--;
			}
		}
		
	}
private:
	Student stu[Maxsize];
	Student stuu[1];
	int length;
};
void MenuList()
{
	cout << "               学生信息管理系统" << endl;
	cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
	cout << "            1.学生信息线性表的建立" << endl;
	cout << "            2.插 入 学 生 信 息" << endl;
	cout << "            3.查 询 学 生 信 息" << endl;
	cout << "            4.删 除 学 生 信 息" << endl;
	cout << "            5.输 出 所 有 学生信息" << endl;
	cout << "            6.批量删除 学生信息" << endl;
	cout << "            7.批量添加 学生信息" << endl;
	cout << "            8.将 男 女 前 后 分 组" << endl;
	cout << "            0.退出学生管理系统" << endl;
	cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
	cout << "请选择功能序号,按数字键0退出:";
}
int main()
{
	SeqList SL;
	Student s;
	int  pos, n;
	while (true)
	{
		MenuList();
		while (cin >> n)
		{
			if (n == 1)//建立学生信息表
			{
				cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
				cout << "            学生信息线性表的建立" << endl;
				cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
				cout << "有几位学生?请输入:" << endl;
				int n; cin >> n;
				cout << "以下请输入这" << n << "个学生的信息:" << endl;
				for (int i = 0; i < n; i++) {
					cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
					cout << "第" << i + 1 << "个学生的信息为:" << endl;
					cout << "学号:"; cin >> s.num; cout << endl;
					cout << "姓名:"; cin >> s.name; cout << endl;
					cout << "性别:"; cin >> s.gender; cout << endl;
					cout << "成绩:"; cin >> s.score; cout << endl;
					SL.CreatList(s, i, n);
				}
				break;
			}
			if (n == 2)//学生信息表插入
			{
				cout << "请输入要插入的学生数据的位置:"; cin >> pos; cout << endl;
				SL.insert(pos - 1, s);

				break;
			}
			if (n == 3)//学生信息查询功能
			{
				cout << "请输入要查找的方式:" << endl;
				cout << "1.按学号查询" << endl;
				cout << "2.按姓名查询" << endl;
				cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
				cout << "请选择1或2:"; int a3; cin >> a3; cout << endl;
				SL.findList(a3);
				break;
			}
			if (n == 4)//学生信息的删除
			{
				cout << "请先查找要删除的学生信息" << endl;
				cout << "1.按学号查询" << endl;
				cout << "2.按姓名查询" << endl;
				cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
				cout << "请选择1或2:"; int a4; cin >> a4; cout << endl;
				SL.delNode(a4);
				break;
			}
			if (n == 5)//输出学生信息表
			{
				cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
				cout << "            输出所有学生信息" << endl;
				cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
				SL.printList();
				break;
			}
			if (n == 6)
			{
				cout << "请输入从第几个学生开始删除:"; int i; cin >> i; cout << endl;
				cout << "请输入批量删除几个学生:"; int n; cin >> n; cout << endl;
				cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
				SL.Remove(i, n);
				break;
			}
			if (n == 7)
			{
				cout << "请输入要插入的学生数据的位置:"; int ps; cin >> ps; cout << endl;
				cout << "请输入要插入几个学生:"; int k; cin >> k; cout << endl;
				SL.Add(k, ps, s);
				break;
			}
			if (n == 8) {
				SL.Sort(); 
				cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
				cout << "            输出所有学生信息" << endl;
				cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
				SL.printList();
				break;
			}
			if (n == 0)
			{
				cout << "退出学生信息管理系统,欢迎下次使用!";
				return 0;
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拔刀能留住落樱吗、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值