知识分享 – 顺序表练习——C++学生信息管理系统

C++顺序表简单实现学生信息管理系统,未加入文件输入输出流,界面较简单,编译通过

#include<stdlib.h>
#include<string>
#include<iostream>
#include<iomanip>
using namespace std;

#define MAXSIZE 100
#define OVERFLOW -2
#define ERROR -1
#define OK 1

typedef int Status;

typedef struct {
	string Name;  // 姓名
	string Num;  // 学号
	string Sex;  // 性别
	int Age;  // 年龄
	string Tele;  // 电话
	string Addr;  // 地址
	float Score;  //3 成绩
}StudentList;

typedef StudentList Elemtype;

typedef struct {
	Elemtype* elem;  
	int length;  // 顺序表长度
}SqList;

// 重载输入运算符
istream& operator >>(istream& in, Elemtype& Stu)
{
	cout << "姓名:";
	in >> Stu.Name;
	cout << "学号:";
	in >> Stu.Num;
	cout << "性别:";
	in >> Stu.Sex;
	cout << "年龄:";
	in >> Stu.Age;
	cout << "电话:";
	in >> Stu.Tele;
	cout << "地址:";
	in >> Stu.Addr;
	cout << "成绩:";
	in >> Stu.Score;
	return in;
}

// 重载输出运算符
ostream& operator <<(ostream& out, Elemtype& Stu)
{
	out << Stu.Name << Stu.Num << Stu.Sex << Stu.Age << Stu.Tele << Stu.Addr << Stu.Score << endl;
	return out;
}


// 初始化顺序表
Status InitList_Sq(SqList& L)
{
	L.elem = new Elemtype[MAXSIZE];
	if (!L.elem) exit(OVERFLOW);
	L.length = 0;
	cout << "初始化成功!" << endl;
	return OK;
}

// 销毁顺序表
void DestroyList(SqList& L)
{
	if (!L.elem)
		delete[] L.elem;
	cout << "销毁成功!" << endl;
}

// 清空顺序表
void ClearList(SqList& L)
{
	L.length = 0;
	cout << "清空成功!" << endl;
}

// 返回顺序表的长度
void GetLength(SqList L)
{
	cout << "顺序表的长度为:" << L.length << endl;
}

// 判断顺序表是否为空
int IsEmpty(SqList L)
{
	if (!L.elem) {
		cout << "顺序表为空!" << endl;
		return 1;
	}
	else {
		cout << "顺序表非空!" << endl;
		return 0;
	}
}

// 创建顺序表
Status CreateList(SqList& L, int length)
{ 
   	// cout << "请输入顺序表的元素:";
	int i;
	if (length < 0) return ERROR;
	if (length >= MAXSIZE) return OVERFLOW;
	for (i = 1; i < length + 1; i++)
	{
		cout << "请输入第" << i << "个学生的信息:" << endl;
		cin >> L.elem[i - 1];
	}
	L.length = length;
	cout << "创建成功!" << endl;
	return OK;
}

// 输出顺序表
void PrintList(SqList L)
{
	int i;
	cout << setw(10) << "姓名" << setw(10) << "学号" << setw(10) << "性别" <<setw(10) 
		<< "年龄" << setw(15) << "电话" << setw(28) << "地址" << setw(15) << "成绩" << endl;
	for (i = 0; i < L.length; i++)
		cout << setw(10) << L.elem[i].Name << setw(10) <<L.elem[i].Num
		<< setw(10) << L.elem[i].Sex << setw(10) << L.elem[i].Age 
		<< setw(15) << L.elem[i].Tele << setw(28) << L.elem[i].Addr << setw(15) << L.elem[i].Score << endl;
	cout << endl;
}

// 按学号排序
void SequenceList(SqList& L)
{
	Elemtype Temp;
	// 冒泡排序
	for (int i = 0; i < L.length - 1; i++)
	{
		for (int j = 0; j < L.length - 1; j++)
			if (L.elem[j].Num > L.elem[j + 1].Num)
			{
				Temp = L.elem[j];
				L.elem[j] = L.elem[j + 1];
				L.elem[j + 1] = Temp;
			}
	}
	cout << "排序成功!" << endl;
}

// 删除顺序表中的某个元素,并将其保存在e中
Status DeleteList(SqList& L, Elemtype& e)
{
	int i;
	cout << "要删除元素的位置:";
	cin >> i;
	if (i < 1 || i > L.length) return ERROR;
	e = L.elem[i - 1];
	int j;
	for (j = i; j < L.length; j++)
		L.elem[j - 1] = L.elem[j];
	L.length--;
	cout << "删除成功!" << endl;
	return OK;
}

// 在顺序表中插入元素
Status InsertList(SqList& L)
{
	if (L.length == MAXSIZE) {
		cout << "溢出!" << endl;
		return OVERFLOW;
	}
	int i;
	Elemtype e;
	cout << "请输入您要插入的位置:";
	cin >> i;
	if (i < 1 || i > L.length + 1) {
		cout << "输入错误!" << endl;
		return ERROR;
	}
	cout << "请输入学生信息:" << endl;
	cout << "姓名:";
	cin >> e.Name;
	cout << "学号:";
	cin >> e.Num;
	cout << "性别:";
	cin >> e.Sex;
	cout << "年龄:";
	cin >> e.Age;
	cout << "电话:";
	cin >> e.Tele;
	cout << "地址:";
	cin >> e.Addr;
	cout << "成绩:";
	cin >> e.Score;
	Elemtype* p, * q;
	q = &(L.elem[i - 1]);
	for (p = &(L.elem[L.length - 1]); p >= q; p--)
		* (p + 1) = *p;
	*q = e;
	++L.length;
	cout << "插入成功!" << endl;
	return OK;
}

// 修改顺序表元素
void ChangeList(SqList& L)
{
	string num;
	int i;
	cout << "请输入要修改的学生的学号:";
	cin >> num;
	for (i = 0; i < L.length; i++)
	{
		if (L.elem[i].Num == num)
		{
			cout << "姓名:";
			cin >> L.elem[i].Name;
			cout << "学号:";
			cin >> L.elem[i].Num;
			cout << "性别:";
			cin >> L.elem[i].Sex;
			cout << "年龄:";
			cin >> L.elem[i].Age;
			cout << "电话:";
			cin >> L.elem[i].Tele;
			cout << "地址:";
			cin >> L.elem[i].Addr;
			cout << "成绩:";
			cin >> L.elem[i].Score;
			cout << "修改成功!" << endl;
		}
	}
	if (i == L.length)
		cout << "查无此人!" << endl;
}


// 按学号查找
void SearchList_Num(SqList& L)
{
	string Num;
	int i;
	cout << "请输入要查找的学生的学号:";
	cin >> Num;
	for (i = 0; i < L.length; i++)
	{
		if (L.elem[i].Num == Num)
		{
			cout << setw(10) << "姓名" << setw(10) << "学号" << setw(10) << "性别" << setw(10)
				<< "年龄" << setw(15) << "电话" << setw(28) << "地址" << setw(15) << "成绩" <<  endl;
			cout << setw(10) << L.elem[i].Name << setw(10) << L.elem[i].Num << setw(10)
				<< L.elem[i].Sex << setw(10) << L.elem[i].Age << setw(15)
				<< L.elem[i].Tele << setw(28) << L.elem[i].Addr << setw(15) << L.elem[i].Score << endl;
			break;
		}
	}
	if(i == L.length)
		cout << "查无此人!" << endl;
}

// 按姓名查找
void SearchList_Name(SqList& L)
{
	string Name;
	int i;
	cout << "请输入要查找的学生的姓名:";
	cin >> Name;
	for (i = 0; i < L.length; i++)
	{
		if (L.elem[i].Name == Name)
		{
			cout << setw(10) << "姓名" << setw(10) << "学号" << setw(10) << "性别" << setw(10)
				<< "年龄" << setw(15) << "电话" << setw(28) << "地址" << setw(15) << "成绩" << endl;
			cout << setw(10) << L.elem[i].Name << setw(10) << L.elem[i].Num << setw(10)
				<< L.elem[i].Sex << setw(10) << L.elem[i].Age << setw(15)
				<< L.elem[i].Tele << setw(28) << L.elem[i].Addr << setw(15) << L.elem[i].Score << endl;
			break;
		}
	}
	if(i == L.length)
		cout << "查无此人!" << endl;
}

// 按电话查找
void SearchList_Tele(SqList& L)
{
	string Tele;
	int i;
	cout << "请输入要查找的学生的电话:";
	cin >> Tele;
	for (i = 0; i < L.length; i++)
	{
		if (L.elem[i].Tele == Tele)
		{
			cout << setw(10) << "姓名" << setw(10) << "学号" << setw(10) << "性别" << setw(10)
				<< "年龄" << setw(15) << "电话" << setw(28) << "地址" << setw(10) << "成绩" << endl;
			cout << setw(10) << L.elem[i].Name << setw(10) << L.elem[i].Num << setw(10)
				<< L.elem[i].Sex << setw(10) << L.elem[i].Age << setw(15)
				<< L.elem[i].Tele << setw(28) << L.elem[i].Addr << setw(10) << L.elem[i].Score << endl;
			break;
		}
	}
	if(i == L.length)
		cout << "查无此人!" << endl;
}

int main()
{
	SqList L;
	int p = 0, i = 0;
	// int n, i;
	int Length;
	Elemtype e;	
	cout << "请输入顺序表的长度:";
	cin >> Length;
	/*  
	// test
	// InitList_Sq(L);
	// cout << "请输入顺序表的长度:";
	// cin >> length;
	// CreateList(L, Length);

	// 输出测试
	// PrintList(L);

	// 排序测试
	// SequenceList(L);
	// PrintList(L);

	// 删除测试
	// cin >> i;
	// DeleteList(L, e);
	// cout << e.Name << e.Num << e.Sex << e.Age << e.Tele << e.Addr << e.Score << endl;
	// PrintList(L);

	// 插入测试
	// InsertList(L);
	// PrintList(L);
	// cout << "顺序表的长度为:" << GetLength(L) << endl;
	
	// 修改测试
	// ChangeList(L);
	// PrintList(L);

	// 按学号查找测试
	// SearchList_Num(L);

	// 按姓名查找测试
	// SearchList_Name(L);

	// 按电话查找
	// SearchList_Tele(L);
	*/

	system("cls");
	while (1)
	{
		cout << endl << "1. 初始化顺序表" << endl;
		cout << "2. 创建顺序表" << endl;
		cout << "3. 输出顺序表元素" << endl;
		cout << "4. 顺序表元素排序" << endl;
		cout << "5. 向顺序表中插入元素" << endl;
		cout << "6. 删除顺序表元素" << endl;
		cout << "7. 修改顺序表元素" << endl;
		cout << "8. 按学号查找顺序表元素" << endl;
		cout << "9. 按姓名查找顺序表元素" << endl;
		cout << "10. 按电话查找顺序表元素" << endl;
		cout << "11. 清空顺序表" << endl;
		cout << "12. 销毁顺序表" << endl;
		cout << "0. 退出" << endl;
		cout << endl << "请输入您的选择:";
		cin >> p;
		system("cls");
		switch (p)
		{
		case 0: exit(-2);
		case 1: InitList_Sq(L); break;	
		case 2: CreateList(L, Length);break;
		case 3: PrintList(L); break; 
		case 4: SequenceList(L); break;
		case 5: InsertList(L); break;
		case 6: DeleteList(L, e); break;
		case 7: ChangeList(L); break;
		case 8: SearchList_Num(L); break;
		case 9: SearchList_Name(L); break;
		case 10: SearchList_Tele(L); break;
		case 11: ClearList(L); break;
		case 12: DestroyList(L); break;
		default: cout << "输入错误!" << endl;
		}
		cout << endl << "输入1返回主界面 \n输入0退出" << endl;
		cin >> i;
		switch (i)
		{
		case 1: system("cls"); break;
		case 0: exit(-2); break;
		}
	}
	cout << endl;
	return 0;
}

正文完