45-学生信息管理系统的编写(C++)

在这里插入图片描述

#include<iostream>
#include<string>
#include<fstream>						//文件的输出输出流操作
#include<iomanip>
#include <algorithm>	
#include<vector>
using namespace std;
const char* filename = "Student.txt"; //学生信息记录的文件名称
class CDate  // 定义日期类
{
private:
    unsigned short int year;   // 年
    unsigned short int month;  // 月
    unsigned short int day;    // 日
public:
    CDate(int y=0,int m=0,int d=0);
    bool operator < (CDate d);  //成员函数,重载<运算符
    friend istream & operator >> (istream &in,CDate &d);    //重载输入运算符
    friend ostream & operator<<(ostream &out,CDate &d);     //重载输出运算符
    friend bool CheckValid(CDate d);    //检查日期合法性
    friend bool LeapYear(int year);
    void SetDate(int y,int m,int d);
};
CDate::CDate(int y,int m,int d):year(y),month(m),day(d) {}
// 设置日期
void CDate::SetDate(int y,int m,int d)
{
    year=y;
    month=m;
    day=d;
}
class Student
{
private:
	string Name;		//姓名
	long Number;		//学号
	string Sex;			//性别
	long Home;			//宿舍号
	long Birthday;		//出生日期;
	int year; 
	Student* next;
public:
	Student(string na = "空", long num = 0, string sex = "男", long home=0,long birthday=0,int  year=20)
	{
		SetName(na);
		SetNumber(num);
		SetSex(sex);
		SetHome(home);
		SetBirthday(birthday);
		Setyear(year);
		next = NULL;
	}
	~Student()					//析构
	{
		Name = ""; Number = 0; Sex = "";Home = 0; Birthday = 0; next = NULL;
	}
	Student& operator =(const Student& T)
	{
		Name = T.Name;
		Number = T.Number;
		Sex = T.Sex;
		Home = T.Home;
		Birthday = T.Birthday;
		year=T.year;
		return *this;
	}
	void SetName(string name)	{Name = name;} //设置名字
	void SetNumber(long num)	{Number = num;} //设置学号
	void SetSex(string sex)		{Sex = sex;	}
	void SetHome(int home)		{Home= home;}
	void SetBirthday(int birthday)   {Birthday = birthday; }
	void Setyear(int birthday){	 year=2020-birthday/10000;}
	friend istream& operator>>(istream& is, Student& T)//输入数据
	{
		string tmp;
		long num,home,birthday;
		cout << "请输入姓名:";		
		is >> tmp;
		T.SetName(tmp);
		cout << "请输入学号:";
		is >> tmp;
		num = atol(tmp.c_str());		//字符型转化为长整形(解决了输入字符时会出现的bug)
		T.SetNumber(num);
		cout << "请输入性别:";
		is >> tmp;
		T.SetSex(tmp);
		cout << "请输入宿舍号:";
		is >> tmp;
		home = atol(tmp.c_str());
		T.SetHome(home);
		cout << "请输入出生日期:";
		is >> tmp;
		birthday = atol(tmp.c_str());
		T.SetBirthday(birthday);
		T.Setyear(birthday); 
		return is;
	}
	friend ostream& operator<<(ostream& os, Student& T)//输出数据
	{
		cout << left << setw(8) << T.Name << left << setw(15) << T.Number << left << setw(8) << T.Sex << left << setw(10) << T.Home << left << setw(10) << T.Birthday <<right<< setw(4)<<T.year<< endl;
		return os;
	}
	friend class  student_List;		//友元类设置
};
class student_List					//学生的信息链表类,主要进行链表的操作,查找输出
{
private:
	Student* head;
public:
	student_List()					//学生链表的初始化,建立头结点,并从文件中读取信息建立链表
	{
		head = new Student;
		head->next = NULL;
		ifstream ifs(filename);
		if (!ifs)					//如果文件不存在
		{
			Filewrite();			//建立文件
		}
	}
	~student_List()
	{
		Student* p = head->next, * q;
		while (p != NULL)
		{
			q = p;
			p = p->next;
			delete q;
		}
		delete head;
	}
	void Fileread()					//读取文件中的信息建立链表
	{
		ifstream ifs(filename);
		if (!ifs)
		{
			cout << "文件打开失败" << endl;
			exit(0);
		}
		Student* q = head;
		while (ifs.peek() != EOF)		//判断是否读取到文件末尾了 采用这个peek函数而不采用ifs.eof()是防止文件为空时还进入循环
		{
			Student* p = new Student;
			ifs >> p->Name
				>> ws
				>> p->Number
				>> ws
				>> p->Sex
				>> ws
				>> p->Home
				>> ws
				>> p->Birthday
				>> ws
				>> p->year
				>> ws;
			q->next = p;
			p->next = NULL;
			q = p;
		}
	}
	void Filewrite()				//通过文件的读写将链表中的内容写到文件中
	{
		Student* p = head->next;
		ofstream ofs(filename);
		if (!ofs)
		{
			cout << "文件打开失败" << endl;
			exit(0);
		}
		while (p != NULL)
		{
			ofs << p->Name << '\t'
				<< p->Number << '\t' << p->Sex << '\t' << p->Home << '\t' << p->Birthday << '\t' << p->year 
				<< endl;
			ofs << endl;
			p = p->next;
		}
		ofs.close();
	}
	void Putin(int n = 1)			//输入,参数的含义是区别与是操作1的输入还是插入....默认是输入函数
	{
		char x = '0';
		int i = 1;				//计数
		int num_0, num_1;//插入位置
		Student* q;
		if (n == 1)				//这是操作输入数据是需要的,如果插入数据则不需要
		{
			cout << "是否清空原来数据 0.是的	1.否,增加信息		3.返回 ";
			cin >> x;
			if (x == '1')//添加进去
			{
				Fileread();				//读取文件形成链表
				Putin(0);				//参数的改变,就是插入函数的调用
				return;
			}
		}
		if (x == '0')
		{
			while (x == '0')
			{
				cout << i++ << endl;
				Student* p = new Student;
				cin >> *p;
				if (n != 3)//节点的插法
				{
					p->next = head->next;
					head->next = p;
				}
				else//节点的任意位置插入
				{
					Display(2);
					cout << "----------------------------------------------------------------" << endl;
					cout << "请选择插入位置:";
					cin >> x;
					num_0 = x - '0';
					q = head;
					num_1 = 0;//寻找位置
					while (q->next != NULL)
					{
						num_1++;
						if (num_0 == num_1)			break;
						q = q->next;
					}
					p->next = q->next;
					q->next = p;
					Display(2);
				}
				cout << "请问是要继续么?(0.是		1.否) "<<endl;
				cin >> x;
				cout << "----------------------------------------------------------------" << endl;
			}
			Filewrite();
		}
	}
	void Display(int x = 1)				//显示输出cout<<left<<setw(40)<<"靠左输出"<<left<<setw(35)<<"靠左输出"<<endl;
	{
		if (x == 1)
			Fileread();
		cout << left << setw(5) << "| 序号 |" << left << setw(8) << "|  姓名 |" << left << setw(10) << "|  学号    |" << left << setw(9) << "| 性别 |" << left << setw(10) << "| 宿舍号 |" << left << setw(8) << "| 出生日期 |" << setw(8) << "| 年龄 |"<< endl;
		Student * p = head->next;
		int i = 1;
		while (p != NULL)
		{
			cout << left << setw(3) << " " << left << setw(3) << i++ << "    " << *p;
			p = p->next;
		}
		cin.get();
		cin.get();
		Filewrite();
	}
	void Insert()				//插入(添加)
	{
		Fileread();				//读取文件形成链表
		Putin(3);				//参数的改变,就是插入函数的调用
	}
	void Delete()				//删除
	{
		Find(2);
		Filewrite();		//写入文件
	}
	void Find(int i = 1)			//查找
	{
		Fileread();//读取文件形成链表
		char x = '0';
		while (x == '0')
		{
			cout << "选择查找方式:	1.姓名	 2.学号	  0.返回";
			cin >> x;
			switch (x)
			{
			case '1':
				Find_01name(i);
				break;
			case '2':
				Find_02number(i);
				break;
			case '0':return;
			default:
				cout << "选择错误!!!";
			}
			cout << "是否继续查找 0.继续 1.放弃  ";
			cin >> x;
		}
		Filewrite();
	}
	void Find_01name(int x = 1)	//查找名字 参数 1.查找 2.删除 3.修改
	{
		int i = 0;
		string na;
		char x_0 = '1';
		Student* p = head->next, * q = head;
		cout << "请输入要查找的姓名:";
		cin >> na;
		cout << left << setw(5) << "| 序号 |" << left << setw(8) << "|  姓名 |" << left << setw(10) << "|  学号    |" << left << setw(9) << "| 性别 |" << left << setw(10) << "| 宿舍号 |" << left << setw(8) << "| 出生日期 |" << setw(8) << "| 年龄 |"<< endl;
		while (p != NULL)
		{
			if (p->Name == na)
			{
				cout << left << setw(3) << " " << left << setw(3) << i++ << "    " << *p;
				if (x == 2)//删除数据
				{
					cout << "是否删除这条数据 0.是 1.否";
					cin >> x_0;
					if (x_0 == '0')
					{
						q->next = p->next;
						delete p;
						p = q;
					}
				}
				if (x == 3)//修改数据
				{
					cout << "是否修该这条数据	0.是		1.否";
					cin >> x_0;
					if (x_0 == '0')
					{
						Change_s(p);
					}
				}
			}
			q = p;
			p = p->next;
		}cout << "-------------------------------------------------------" << endl;
		if (i == 0)
			cout << "用点心叭,没有需要的数据" << endl;
	}
	void Find_02number(int x = 1)//学号查找
	{
		int i = 0;
		long num;
		string tmp;
		char x_0 = '1';
		Student* p = head->next, * q = head;
		cout << "请输入要查找的学号:";
		cin >> tmp;
		num = atol(tmp.c_str());
	cout << left << setw(5) << "| 序号 |" << left << setw(8) << "|  姓名 |" << left << setw(10) << "|  学号    |" << left << setw(9) << "| 性别 |" << left << setw(10) << "| 宿舍号 |" << left << setw(8) << "| 出生日期 |" << setw(8) << "| 年龄 |"<< endl;
		while (p != NULL)
		{
			if (p->Number == num)
			{
				cout << left << setw(3) << " " << left << setw(3) << i++ << "    " << *p;
				if (x == 2)//删除数据
				{
					cout << "是否删除这条数据 0.是 1.否";
					cin >> x_0;
					if (x_0 == '0')
					{
						q->next = p->next;
						delete p;
						p = q;
					}
				}
				if (x == 3)//修改数据
				{
					cout << "是否修该这条数据	0.是		1.否";
					cin >> x_0;
					if (x_0 == '0')
					{
						Change_s(p);
					}
				}
			}
			q = p;
			p = p->next;
		}
		cout << "-------------------------------------------------------" << endl;
		if (i == 0)
			cout << "用点心叭,没有需要的数据" << endl;
	}
	void Change()		//修改             
	{
		Find(3);
		Filewrite();		//写入文件
	}
	void Change_s(Student * p)
	{
		char a = '0';
		string tmp;
		long num,home,birthday;
		while (a == '0')
		{
			cout << "-------------------------------------------------------" << endl;
			cout << "请选择修改选项   1.姓名 2.学号 3.性别 4.宿舍号 5.出生日期";
			cin >> a;
			cout << "-------------------------------------------------------" << endl;
			switch (a)
			{
			case '1':
				cout << "输入名字:";
				cin >> tmp;
				p->SetName(tmp);
				break;
			case '2':
				cout << "输入学号:";
				cin >> tmp;
				num = atol(tmp.c_str());
				p->SetNumber(num);
				break;
			case '3':
				cout << "输入性别:";
				cin >> tmp;
				p->SetSex(tmp);
				break;
			case '4':
				cout << "输入宿舍号:";
				cin >> tmp;
				home = atol(tmp.c_str());
				p->SetHome(home);
				break;
			case '5':
				cout << "请输入出生日期:";
				cin >> tmp;
				birthday = atol(tmp.c_str());
				p->SetBirthday(birthday);
				break;
			default:
				cout << "笨蛋,输错啦" << endl;
			}
			cout << *p;
			cout << "-------------------------------------------------------" << endl;//美化视觉效果
			cout << "是否继续修改其他选项 0.继续 1.不了";
			cin >> a;
		}
	}
	void Write()
	{
		cout<<"文件写入成功" <<endl;
		Filewrite();
	}
};
void Meue()				//主菜单界面
{
	while (1)
	{
		system("cls");
		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 << "                           0.退出登陆          				 	" << endl;		
		cout << "*******************************************************************" << endl;
		student_List A;//建立链表,初始化
		char t;
		cin >> t;
		system("cls");
		switch (t)
		{
		case '1':
			A.Display();
			break;
		case '2':
			A.Insert();
			break;
		case '3':
			A.Find();
			break;
		case '4':
			A.Delete();
			break;
		case '5':
			A.Change();
			break;
		case '6':
			A.Write();
			break;
		case '7':
			A.Putin();
			break;
		case '0': 
			cout<<"系统结束,see you";
			exit(0);
		default: cout << "选择错误";
			break;
		}
	}
}
int main()
{
	while (1)
		 Meue();
	return 0;
}
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

林林林ZEYU

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

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

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

打赏作者

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

抵扣说明:

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

余额充值