基于C/C++的学生信息管理系统

1 系统设计要求

利用数据结构和C++语言知识,在内存中顺序存储一组学生信息(假设问题规模为N),信息如下:学号、学院、专业、姓名、年龄、学分等。

说明:

  • (1)学生信息其余属性可以自拟。
  • (2)学号编码方式自拟

设计相应的数据结构存储所有学生信息,实现如下功能:

  • (1)查询功能:实现按照学号查找学生信息的查询算法时间复杂度小于O(N)
  • (2)添加学生信息
  • (3)删除学生信息
  • (4)修改学生信息
  •   (5)  存储学生信息(可以存储到外部文件,也可存储到后台数据库,数据库自己设计)

设计要求

  • 根据以上功能需求,自己定义合适的数据结构,使查询算法的时间复杂度小于O(N)
  • 提供友好的用户界面,方便用户操作。

2 C/C++代码实现

#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;
#define MAX 1000
struct Student {//设计学生信息结构体
	string s_ID;//学号
	string s_Name;//姓名
	string s_Address;//地址
	string s_PhoneNum;//电话
	string s_Birthday;//出生年月
	string s_Sex ;//性别
	int s_Age = 0;//年龄
};
struct StuInfoSystem {//设计学生信息库结构体
	struct Student studentArray[MAX];//信息库最大容量
	int s_Size = 0;//信息库此时最大索引号,实际人数n=s_Size+1;
};

void readStuFile(StuInfoSystem* abs);//读取文件
void inputInfo(StuInfoSystem* abs, int i);//学生信息输入
void printInfo(StuInfoSystem* abs, int i);//单个学生信息输出
void addStudent(StuInfoSystem* abs);//添加学生函数
int getBinarySearch(int ele, int arr[], int indexArr[], int length);
int getIndex(StuInfoSystem* abs, int element);//获取索引
void deleteStudent(StuInfoSystem* abs);//删除学生信息
void findStudent(StuInfoSystem* abs);//查找学生信息
void modifyStudent(StuInfoSystem* abs);//修改学生信息
void storeStudent(StuInfoSystem* abs);//存储学生信息
void showMenu();//菜单界面

//读取文件中已有的学生信息
void readStuFile(StuInfoSystem* abs) {//读取文件中所有学生信息
	int i = 0;
	string line;
	//1.创建字符流
	ifstream ifs;
	//2.打开文件
	ifs.open("StuInformation.txt", ios::in);
	//3.读文件
	if (ifs.is_open()) {
		while (1) {
			getline(ifs, line);//获取每一行学生信息
			if (ifs.eof()) { //判断文件是否结束
				break;
			}
			if (i > 0) {
				istringstream istr;
				string id, address, phone, name, birthday,sex;
				int age;
				istr.str(line);//转化成字符串  然后进行切割
				istr >> id;
				istr >> name;
				istr >> address;
				istr >> phone;
				istr >> birthday;
				istr >> sex;
				istr >> age;
				abs->s_Size = i - 1;
				abs->studentArray[abs->s_Size].s_ID = id;
				abs->studentArray[abs->s_Size].s_Name = name;
				abs->studentArray[abs->s_Size].s_Address = address;
				abs->studentArray[abs->s_Size].s_PhoneNum = phone;
				abs->studentArray[abs->s_Size].s_Birthday = birthday;
				abs->studentArray[abs->s_Size].s_Sex = sex;
				abs->studentArray[abs->s_Size].s_Age = age;
			}
			if (abs->s_Size == MAX) {
				cout << "学生信息管理系统已满!" << endl;
				break;
			}
			i++;
		}
	}
	else {
		cout << "学生信息文件打开失败" << endl;
	}
	//4.关闭文件
	ifs.close();
}
//学生信息输入函数
void inputInfo(StuInfoSystem* abs, int i) {//学生信息输入函数
	string ID;
	cout << "请输入学生ID:" << endl;
	cout << "提示:学号长度为8,前四个数字为2022,例如:20220001 " << endl;
	while (1) {
		cin >> ID;
		int n = atoi(ID.c_str()) - 20220000;//将字符串转化成整数 
		//对学号后四位进行判断
		if (ID.length() == 8 && n >= 0 && n < 1000) {
			abs->studentArray[i].s_ID = ID;
			break;
		}
		else {
			cout << "输入有误,请重新输入:" << endl;
		}
	}

	cout << "请输入姓名: " << endl;
	string name;
	cin >> name;
	abs->studentArray[i].s_Name = name;

	cout << "请输入地址: " << endl;
	string address;
	cin >> address;
	abs->studentArray[i].s_Address = address;

	string phone;
	cout << "请输入电话号码: (提示:长度为11位 )" << endl;
	while (1) {
		cin >> phone;
		//对学号后四位进行判断
		if (phone.length() == 11) {
			abs->studentArray[i].s_PhoneNum = phone;
			break;
		}
		else {
			cout << "输入有误,请重新输入:" << endl;
		}
	}

	string birthday;
	cout << "请输入年月:(提示:学号长度为6,范例格式:202201) " << endl;
	while (1) {
		cin >> birthday;
		//对学号后四位进行判断
		if (birthday.length() == 6 ) {
			abs->studentArray[i].s_Birthday = birthday;
			break;
		}
		else {
			cout << "输入有误,请重新输入:" << endl;
		}
	}

	cout << "请输入性别:男 或 女 " << endl;
	string sex ;
	while (1) {
		cin >> sex;
		if (sex == "男" | sex == "女") {
			abs->studentArray[i].s_Sex = sex;
			break;
		}
		cout << "输入有误,请重新输入:" << endl;
	}

	cout << "请输入年龄: 范围(0~130)" << endl;
	int age = 0;
	while (1) {
		cin >> age;
		if (age > 0 && age < 130) {
			abs->studentArray[i].s_Age = age;
			break;
		}
		cout << "输入有误,请重新输入:" << endl;
	}

}
//学生信息输出函数
void printInfo(StuInfoSystem* abs, int i) {//学生信息输出函数
	printf_s("学号:%-10s 姓名:%-10s 地址:%-10s 电话:%-10s 性别:%-10s 年龄:%2d\n",
		(abs->studentArray[i].s_ID).c_str(), (abs->studentArray[i].s_Name).c_str(),
		(abs->studentArray[i].s_Address).c_str(),(abs->studentArray[i].s_PhoneNum).c_str(), 
		(abs->studentArray[i].s_Birthday).c_str(), abs->studentArray[i].s_Age);
}
//1、添加学生信息
void addStudent(StuInfoSystem* abs) {
	if (abs->s_Size == MAX) {	//判断学生信息容量是否满了,如果满了就不再添加
		cout << "学生信息管理系统已满,无法添加!" << endl;
		return;
	}
	else {
		abs->s_Size++;		//更新学生人数
		inputInfo(abs, abs->s_Size);
		cout << "添加成功!(记得使用功能6将变更后的学生信息保存学生信息到文件中)" << endl;
		system("pause");//请按任意键继续
		system("cls");//清屏
	}
}
//2、输出文件中所有学生信息
void showStudent(StuInfoSystem* abs) {
	readStuFile(abs);//读取学生信息后再显示
	for (int i = 0; i <= abs->s_Size; i++) {
		printInfo(abs, i);
	}
	system("pause");
	system("cls");
}
//二分法查找
int getBinarySearch(int ele, int arr[], int indexArr[], int length) {
	int min = 0;
	int max = length - 1;
	int mid = 0;
	while (min <= max) {
		mid = (min + max) >> 1;
		if (ele > arr[mid]) {
			min = mid + 1;
		}
		if (ele < arr[mid]) {
			max = mid - 1;
		}
		if (ele == arr[mid]) {
			return indexArr[mid];
		}
	}
	return -1;
}
//获取索引
int getIndex(StuInfoSystem* abs, int element) {
	int n = MAX;
	int arr[MAX] = { 0 };
	int indexArr[MAX];
	for (int i = 0; i <= abs->s_Size; i++) {
		arr[i] = atoi((abs->studentArray[i].s_ID).c_str()) - 20220000;
	}
	// 下标变化数组
	for (int i = 0; i < n; i++) {
		indexArr[i] = i;
	}
	// 排序前后的下标对应关系
	for (int i = 0; i < n - 1; i++) {//选择排序法
		for (int j = i + 1; j < n; j++) {
			if (arr[i] > arr[j]) {
				int temp = arr[i];
				arr[i] = arr[j];
				arr[j] = temp;
				int indexTemp = indexArr[i];
				indexArr[i] = indexArr[j];
				indexArr[j] = indexTemp;
			}
		}
	}
	int index = getBinarySearch(element, arr, indexArr, n);
	return index;
}
//3、删除某学生信息
void deleteStudent(StuInfoSystem* abs) {
	cout << "请输入您要删除的学生ID:" << endl;
	int id;
	cin >> id;
	int ret = getIndex(abs, id - 20220000);//获取要删除的学生信息索引
	if (ret == -1) {
		cout << "查无此人" << endl;
	}
	else {
		for (int i = ret; i < abs->s_Size; i++) {
			abs->studentArray[i] = abs->studentArray[i + 1];
		}
		abs->s_Size--;
		cout << "删除成功!(记得使用功能6将变更后的学生信息保存学生信息到文件中)" << endl;
	}
	system("pause");
	system("cls");
}
//4.查找学生信息
void findStudent(StuInfoSystem* abs) {
	cout << "请输入您要查找的学生ID:" << endl;
	int id;
	cin >> id;
	int ret = getIndex(abs, id - 20220000);//获取要找到的学生信息索引
	if (ret == -1)
		cout << "查无此人";
	else
		printInfo(abs, ret);
	system("pause");
	system("cls");
}
//5.修改某学生信息
void modifyStudent(StuInfoSystem* abs) {
	cout << "请输入您要修改的学生ID:" << endl;
	int id;
	cin >> id;
	int ret = getIndex(abs, id - 20220000);//获取要修改学生信息索引
	if (ret != -1) {
		inputInfo(abs, ret);
		cout << "修改成功!(记得使用功能6将变更后的学生信息保存学生信息到文件中)" << endl;
	}
	else {
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}
//6.存储学生信息
void storeStudent(StuInfoSystem* abs) {
	int i = 0;
	//1.创建流对象
	ofstream ofs;
	//2.打开文件
	ofs.open("StuInformation.txt", ios::out | ios::trunc);
	//每次将文件清空再重新写入
	//3.写文件
	if (ofs.is_open()) {
		ofs << " 学号\t\t姓名\t\t地址\t\t电话\t\t\t出生年月\t性别\t年龄\t" << endl;
		for (i = 0; i <= abs->s_Size; i++) {
			ofs << abs->studentArray[i].s_ID << "\t";
			ofs << abs->studentArray[i].s_Name << "\t\t";
			ofs << abs->studentArray[i].s_Address << "\t";
			ofs << abs->studentArray[i].s_PhoneNum << "\t"; 
			ofs << abs->studentArray[i].s_Birthday << "\t";
			ofs << abs->studentArray[i].s_Sex << "\t";
			ofs << abs->studentArray[i].s_Age << "\t"<<endl;
		}
		//4.关闭文件
		ofs.close();
		cout << "保存成功" << endl;
	}
	else {
		cout << "学生信息文件打开失败而保存失败" << endl;
	}
	system("pause");
	system("cls");
}
//菜单界面
void showMenu() {
	cout << "**********************************************************" << endl;
	cout << "=              欢迎使用学生信息管理系统!                =" << endl;
	cout << "=              该系统功能如下:                          =" << endl;
	cout << "=                  1-添加学生信息                        =" << endl;
	cout << "=                  2-显示文件中所有学生信息              =" << endl;
	cout << "=                  3-删除某学生信息                      =" << endl;
	cout << "=                  4-根据ID查找学生信息                  =" << endl;
	cout << "=                  5-修改某学生信息                      =" << endl;
	cout << "=                  6-存储学生信息                        =" << endl;
	cout << "=                  0-退出学生信息管理系统                =" << endl;
	cout << "=                  请开始您的使用!                      =" << endl;
	cout << "**********************************************************" << endl;
}
//主函数
int main() {
	//学生信息管理系统初始化
	StuInfoSystem abs;
	abs.s_Size = 0;
	readStuFile(&abs);//将文件中已经存在的学生信息读取出来,同时用于测试
	int select = 0;
	while (1) {
		showMenu();
		cout << "请选择功能:" << endl;
		cin >> select;
		switch (select) {
		case 1://1、添加学生信息  
			addStudent(&abs);//利用地址传递,可以修改实参
			break;
		case 2: {// 2、显示所有学生信息
			int i = 0;
			cout << "请先使用功能6将学生信息保存到文件中 是否继续使用?" << endl;
			cout << "1--已保存继续使用" << endl;
			cout << "0--不使用" << endl;
			cout << "请输入选择:" << endl;
			cin >> i;
			if (i == 1) {
				cout << "文件中学生信息如下:" << endl;
				showStudent(&abs);
				break;
			}
			else {
				break;
			}
		}
		case 3:// 3、删除某学生信息
			deleteStudent(&abs);
			break;
		case 4://4、根据ID查找学生信息
			findStudent(&abs);
			break;
		case 5://5、修改某学生信息
			modifyStudent(&abs);
			break;
		case 6://6、存储学生信息  
			storeStudent(&abs);
			break;
		case 0://0、退出学生信息管理系统
			cout << "欢迎下次使用!" << endl;
			system("pause");
			return 0;
			break;
		default:
			system("cls");
			cout<<"输入有误!请重新输入:" << endl;
			break;
		}
	}
	system("pause");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

神马都会亿点点的毛毛张

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

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

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

打赏作者

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

抵扣说明:

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

余额充值