实验11 查找和排序的综合应用 —— 学生信息管理系统

(1)实验目的:

通过该实验,使学生能灵活掌握对查找和排序的综合应用,体会再实际应用中开发过程中其应用场景和作用

(2)代码实现:

//作者:Kuugo
#include<iostream>
#include <algorithm>
#include<cstdlib>
using namespace std;
int size_ = 5;//结构体数组中元素的个数
struct student
{
	int num;//学生编号
	string name;//学生姓名
	string phone;//学生电话
	int English;//英语成绩
	int math;//数学成绩
};
//全局函数(用于sort),对应的是折半查找的部分
bool  cmp_num(const student& a, const student& b)
{
	return a.num < b.num;
}

//功能面板
void menu()
{
	cout << "1.通过学生姓名查找学生信息(顺序查找)" << endl;
	cout << "2.通过学生学号查找学生信息(折半查找)" << endl;
	cout << "3.查看英语成绩排名(冒泡排序)" << endl;
	cout << "4.查看数学成绩排名(快速排序)" << endl;
	cout << "5.增加同学信息" << endl;
	cout << "6.删除同学信息" << endl;
	cout << "7.修改同学信息" << endl;
	cout << "请输入你的选择:";
}
//通过姓名查找(顺序查找)
bool name_search(student* stu, string name, int num)
{
	for (int i = 0; i < size_; i++)
	{
		if (name == stu[i].name)
		{
			num = i;
			return true;
		}
	}
	return false;
}
//通过学号查找(折半查找)
void id_search(student* stu, int id)
{
	int len = 20;
	//创建一个相同的结构体,避免对原结构体的破坏
	student* copy = new student[len];
	for (int i = 0; i < size_; i++)
	{
		copy[i].num = stu[i].num;
		copy[i].name = stu[i].name;
		copy[i].phone = stu[i].phone;
		copy[i].English = stu[i].English;
		copy[i].math = stu[i].math;
	}
	//先对结构体进行快速排序
	sort(copy, copy + size_, cmp_num);
	//开始折半查找
	int low = 0, high = size_ - 1, mid, flag = 0;
	while (low <= high)
	{
		mid = (low + high) / 2;
		if (id > copy[mid].num)
		{
			low = mid + 1;
		}
		else if (id == copy[mid].num)
		{
			flag = 1;
			break;
		}
		else high = mid - 1;
	}
	if (flag == 0)
	{
		cout << "没有该学生" << endl;
	}
	else
	{
		cout << "该学生的编号为:" << copy[mid].num << endl;
		cout << "该学生的姓名为:" << copy[mid].name << endl;
		cout << "该学生的电话为:" << copy[mid].phone << endl;
		cout << "该学生的英语成绩为:" << copy[mid].English << endl;
		cout << "该学生的数学成绩为:" << copy[mid].math << endl;
	}
}
//英语成绩排名(冒泡排序)
void English_sort(student* stu)
{
	int len = 20;
	//创建一个相同的结构体,避免对原结构体的破坏
	student* copy = new student[len];
	for (int i = 0; i < size_; i++)
	{
		copy[i].num = stu[i].num;
		copy[i].name = stu[i].name;
		copy[i].phone = stu[i].phone;
		copy[i].English = stu[i].English;
		copy[i].math = stu[i].math;
	}
	for (int i = 0; i < size_; i++)
	{
		for (int j = 0; j < size_ - i - 1; j++)
		{
			if (copy[j].English > copy[j + 1].English)
			{
				swap(copy[j], copy[j + 1]);
			}
		}
	}
	cout << "英语成绩的排名为:" << endl;
	for (int i = size_ - 1, j = 1; i >= 0; i--, j++)
	{
		cout << "第" << j << "名:" << copy[i].name << " 成绩:" << copy[i].English << endl;
	}
}
//快速排序函数
void quickSort(student* stu, int left, int right)
{
	int i = left, j = right;
	int pivot = stu[(left + right) / 2].math;
	while (i <= j)
	{
		while (stu[i].math < pivot)
		{
			i++;
		}
		while (stu[j].math > pivot)
		{
			j--;
		}
		if (i <= j)
		{
			swap(stu[i], stu[j]);
			i++;
			j--;
		}
	}
	if (left < j)
	{
		quickSort(stu, left, j);
	}
	if (i < right)
	{
		quickSort(stu, i, right);
	}
}
//数学成绩排名(快速排序)
void math_sort(student* stu)
{
	int len = 20;
	//创建一个相同的结构体,避免对原结构体的破坏
	student* copy = new student[len];
	for (int i = 0; i < size_; i++)
	{
		copy[i].num = stu[i].num;
		copy[i].name = stu[i].name;
		copy[i].phone = stu[i].phone;
		copy[i].English = stu[i].English;
		copy[i].math = stu[i].math;
	}
	quickSort(copy, 0, size_ - 1);
	cout << "数学成绩的排名为:" << endl;
	for (int i = size_ - 1, j = 1; i >= 0; i--, j++)
	{
		cout << "第" << j << "名:" << copy[i].name << " 成绩:" << copy[i].math << endl;
	}
}

//增加同学信息
void addStudent(student* stu)
{
	if (size_ > 20)
	{
		cout << "学生信息已满,不能再添加了!" << endl;
	}
	else
	{
		cout << "请输入学生的编号:";
		cin >> stu[size_].num;
		cout << "请输入学生的姓名:";
		cin >> stu[size_].name;
		cout << "请输入学生的电话:";
		cin >> stu[size_].phone;
		cout << "请输入学生的英语成绩:";
		cin >> stu[size_].English;
		cout << "请输入学生的数学成绩:";
		cin >> stu[size_].math;
		size_++;
	}
}
//删除同学信息
void deletestudent(student* stu)
{
	cout << "下面是所有同学的信息:" << endl;
	for (int i = 0; i < size_; i++)
	{
		cout << i << " 编号:" << stu[i].num << " 姓名:" << stu[i].name << " 电话:" << stu[i].phone << " 英语成绩:" << stu[i].English << " 数学成绩:" << stu[i].math << endl;
	}
	int n;//删除第几个学生
	cout << "你要删除第几个学生?" << endl;
	cin >> n;
	for (int i = n - 1; i < size_ - 1; i++)
	{
		stu[i] = stu[i + 1];
	}
	size_--;
}
void alterstudent(student* stu)
{
	cout << "下面是所有同学的信息:" << endl;
	for (int i = 0; i < size_; i++)
	{
		cout << i << " 编号:" << stu[i].num << " 姓名:" << stu[i].name << " 电话:" << stu[i].phone << " 英语成绩:" << stu[i].English << " 数学成绩:" << stu[i].math << endl;
	}
	int n;//要修改第几个同学
	cout << "你要修改第几个同学的信息?" << endl;
	cin >> n;
	cout << "请输入学生的编号:";
	cin >> stu[n - 1].num;
	cout << "请输入学生的姓名:";
	cin >> stu[n - 1].name;
	cout << "请输入学生的电话:";
	cin >> stu[n - 1].phone;
	cout << "请输入学生的英语成绩:";
	cin >> stu[n - 1].English;
	cout << "请输入学生的数学成绩:";
	cin >> stu[n - 1].math;
}
int main()
{
	student stu[20] = { {1,"Argo","123456",90,90},
		{2,"Kuugo","123456",91,91},
		{3,"Sousuke","123456",92,92},
		{4,"Gou","123456",93,93},
		{5,"Yuujirou","123456",94,94}
	};//该结构体最多只有20个元素
	while (true)
	{
		menu();
		int choice;
		cin >> choice;
		switch (choice)
		{
		case 1://通过学生姓名查找学生信息
		{
			string name;
			int num = 0;
			cout << "请输入你要查找学生的姓名:";
			cin >> name;
			if (name_search(stu, name, num))
			{
				cout << "你要查找的学生信息如下:" << endl;
				cout << num << " 编号:" << stu[num].num << " 姓名:" << stu[num].name << " 电话:" << stu[num].phone << " 英语成绩:" << stu[num].English << " 数学成绩:" << stu[num].math << endl;
			}
			else
			{
				cout << "没有这个学生" << endl;
			}
		}
		break;
		case 2://通过学生学号查找学生信息
		{
			int id;
			cout << "请输入学生的学号:";
			cin >> id;
			id_search(stu, id);
		}
		break;
		case 3://查看英语成绩排名(冒泡排序)
			English_sort(stu);
			break;
		case 4://查看数学成绩排名(快速排序)
			math_sort(stu);
			break;
		case 5://增加同学信息
			addStudent(stu);
			break;
		case 6://删除同学信息
			deletestudent(stu);
			break;
		case 7://修改同学信息
			alterstudent(stu);
			break;
		default:
			break;
		}
		//按任意键继续
		system("pause");
		//清屏
		system("cls");
	}
	return 0;
}

代码不足之处:结构体的大小写死,长度是由全局变量控制的。

题目来源:henu 计算机与信息工程学院 数据科学与大数据技术 数据结构实验

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值