⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 设计并实现一个简单的大学人员信息管理系统。对各类人员信息进行管理
💻 CPP
字号:
#include<iostream.h>
#include<fstream.h>
#include"persons.h"
#include"student.h"
#include"teacher.h"
#include"empoyee.h"
#include"chainList.h"

#include<stdio.h>
void fileRead(chainList *list,ifstream &ff);//把文件从硬盘上读入内存
void fileWrite(chainList *list,ofstream &ff);//写入硬盘

int compareStr(char*str1,char*str2);//名字作为关键字,这个函数就用来比较
void addList(chainList *list);//同名的话,覆盖,或者改为新名字加入
void   deleteNode(chainList *node);//根据名字删除
void editList(chainList *list,char *name);//要编辑的人名和新的资料
chainList  *searchList(chainList *list,char*name);//只提供名字查询(其他以后加入)
//返回前一个节点的指针
void orderList(chainList *list);//按名字排序
void orderListByAge(chainList *list);//按年龄降序排列
void orderListBySex(chainList *list);//按照性别排列,女士优先
void printStaticInfo(chainList*list);
void printList(chainList *list);

int  main()
{
	cout<<"\tYou are welcome to the management system!"<<endl;
	cout<<"Do you want to load a file or setup a new list?Press l or s."<<endl;
	char sn;
	cin>>sn;
	char fileName[30];
	chainList  headNode(0,NULL);
	chainList *head=&headNode;
	if(sn=='l')
	{
		ifstream  file;
		cout<<"File's name:"<<endl;
		cin>>fileName;
		file.open(fileName);
	
		if(! file)//为何输入不存在地文件后,机子变得很慢,而且没有结果出来?
		{
			cout<<"File not found!System exit."<<endl;
			return 1;
		}
		//	cout<<"test"<<endl;//竟然通过!file的检查?(文件不存在阿)
		fileRead(head,file);//读入文件到链表
		file.close();
	}
	printStaticInfo(head);
	char ctn;
	
	do
	{
		cout<<"main menu:"<<endl;
		cout<<"\t1.Add a person.  2.Delete a person	3.Edit a person"<<endl;
		cout<<"\t4.Search a person 5.Order the list	6.Print info about the list"<<endl;
		cout<<"\t0.exit"<<endl;
		cin>>ctn;
		switch(ctn)
		{
		case '0':break;
		case '1':
			addList(head);
			break;
		case '2':
			cout<<"Please input the person's name to be deleted:"<<endl;
			char nm[20];
			cin>>nm;
			chainList *pdel;
			pdel=searchList(head,nm);
			char sure;//Make sure whether you want to delete the data
			sure='n';
				if(!pdel->next)
			{
				cout<<"The person you input cann't be found"<<endl;
			}
			
			else{
				cout<<"The persons infor follows:"<<endl;
				pdel->next->pl->print();
				cout<<"Are you sure to delete it?Press y or n"<<endl;

				cin>>sure;
				if(sure=='y')
				{
					deleteNode(pdel);
					cout<<nm<<" have been deleted!"<<endl;
				}
			}
			break;
		case '3':
			cout<<"Please input the person's name:"<<endl;
			char nm1[20];
			cin>>nm1;
			chainList *pp;
			pp=searchList(head,nm1);
			if(pp->next)
			{
				cout<<"The person's infor follows:"<<endl;
				pp->next->pl->print();
				cout<<"Now ,please edit the infors."<<endl;
				editList(head,nm1);

			}
			else
				cout<<"The person can't be found !!"<<endl;
			break;
		case '4':
			cout<<"Please input the person's name:"<<endl;
			char name[30];
			cin>>name;
			chainList *p;
			p=searchList(head,name);
			if(p->next)
			{
			p->next->pl->print();
			}
			else
				cout<<"The person can't be found!"<<endl;

			break;
		case '5':
			cout<<"You  want to order the list by:"<<endl;
			cout<<"1.name  2.age   3.sex(lady first)"<<endl;
				char ch;
			cin>>ch;
			switch(ch)
			{
			case '1':
				cout<<"You choose to order the list by name.The result are as follows:"<<endl;
				orderList(head);
				printStaticInfo(head);
				break;
			case '2':
				cout<<"You choose to order the list by age.The result are as follows:"<<endl;
				orderListByAge(head);
				printStaticInfo(head);
				break;
			case '3':
				cout<<"You choose to order the list by sex.The result are as follows:"<<endl;
				orderListBySex(head);
				printStaticInfo(head);
				break;
			default:
				cout<<"Wrong choice!"<<endl;
			}
			break;
		case '6':
			printStaticInfo(head);
			break;
		default:
			cout<<"Wrong choice!"<<endl;
		}
		
		
	}while(ctn!='0');
	cout<<"Do you want to save the file?No,press n.Else,any other key."<<endl;
	char save;
	cin>>save;
	if(save!='n')
	{
		cout<<"Please input the file's name:"<<endl;
		cin>>fileName;
		ofstream fin;
		fin.open(fileName);//如果文件已经存在,询问是否覆盖。如何知道是否存在
		if(!fin)
		{
			cout<<"Can't open output file.\n";
			return 1;
		}
		fileWrite(head,fin);
		fin.close();
		cout<<"File has been successfully saved!"<<endl;
	}
	cout<<"Byebye!"<<endl;
	return 0;
}

void fileRead(chainList *list,ifstream &ff)
{
	chainList*p=list;
	int tp;
		char nm[20];
		int ag;
		char sx;
		int slr;
		int grd;
		char schl[20];
		char sb[20];
		int teaYear;
		int aa=1;
		persons*psn;//插入人员指针
		ff>>tp;
		while(tp)
		{	ff>>ag;
			ff>>nm;
			ff>>sx;
			switch(tp)
		{
		
		case 1:
		ff>>grd;
		ff>>schl;
			psn=new student(ag,nm,sx,grd,schl);
			break;
		case 2:
			ff>>sb;
			ff>>teaYear;
			psn=new teacher(ag,nm,sx,sb,teaYear);
			break;
		case 3:
			ff>>slr;
			psn=new empoyee(ag,nm,sx,slr);
			break;
		}

			chainList*s=new chainList(tp,psn);
			p->next=s;
			p=s;
			ff>>tp;
		}
}
void fileWrite(chainList *list,ofstream &ff)
{
	chainList*p=list->next;
	while(p)
	{
		ff<<p->typeOfPn<<" "<<p->pl->age<<" "<<p->pl->name<<" "<<p->pl->sex<<" ";
		switch(p->typeOfPn)
		{
		case 1:
			ff<<((student*)p->pl)->grade<<" "<<((student*)p->pl)->school<<endl;
			break;
		case 2:
			ff<<((teacher*)p->pl)->subject<<" "<<((teacher*)p->pl)->yearsOfT<<endl;
			break;
		case 3:
			ff<<((empoyee*)p->pl)->salary<<endl;
			break;
		}
	p=p->next;
	}
	int aa=0;
	ff<<aa;
}
int compareStr(char*str1,char*str2)//比较字符串
{
	char*p1=str1,*p2=str2;
		
	int i=0;
	while((p1[i]==p2[i])&&p1[i]&&p2[i])
		i++;
	if(p1[i]==p2[i])
	{
		return 0;
	}
	else
	{	if(p1[i]>p2[i])
			return 1;
		else
			return -1;
	}	
}


void addList(chainList *list)//添加节点
{
	char tp;
	int type;
	do{
		cout<<"The person whose info you want to input is:"<<endl;
		cout<<"1.a student    2.a teacher	3.a common employee"<<endl;
		cout<<"0.Return to main menu."<<endl;
		cin>>tp;//为什么一输入字符就死循环??
		char nm[20];
		int ag;
		char sx;
		int slr;
		int grd;
		char schl[20];
		char sb[20];
		int teaYear;
		int aa=1;
		persons*psn;//插入人员指针
		switch(tp)
		{
		case '0':aa=0;
			type=0;
			break;
		case '1':
			type=1;
			cout<<"name:"<<endl;
			cin>>nm;
			cout<<"age:"<<endl;
			cin>>ag;
			cout<<"male or female?m or f?"<<endl;
			cin>>sx;
			cout<<"The student's grade"<<endl;
			cin>>grd;
			cout<<"From which school:"<<endl;
			cin>>schl;
			psn=new student(ag,nm,sx,grd,schl);
			break;
		case '2':type=2;
			cout<<"name:"<<endl;
			cin>>nm;
			cout<<"age:"<<endl;
			cin>>ag;
			cout<<"male or female?m or f?"<<endl;
			cin>>sx;
			cout<<"subject field:"<<endl;
			cin>>sb;
			cout<<"Years as a teacher:"<<endl;
			cin>>teaYear;
			psn=new teacher(ag,nm,sx,sb,teaYear);
			break;
		case '3':type=3;
			cout<<"name:"<<endl;
			cin>>nm;
			cout<<"age:"<<endl;
			cin>>ag;
			cout<<"male or female?m or f?"<<endl;
			cin>>sx;
			cout<<"The employee's salary:"<<endl;
			cin>>slr;
			psn=new empoyee(ag,nm,sx,slr);
			break;
		default:type=6;
			aa=0;
			cout<<"Wrong choice!"<<endl;
		}
		chainList*p=list;//新节点在p所指的节点之前插入,
		if(aa)
		{
			
			int com;
			if(p->next)
			{
				com=compareStr(nm,p->next->pl->name);
				cout<<com<<endl;
			}
			while(p->next&&com>0)//比p节点小或者p为空退出
			{
			p=p->next;
			if(p->next)
			com=compareStr(nm,p->next->pl->name);
			}
			chainList *s=new chainList(type,psn);
			s->next=p->next;
			p->next=s;
		}	
	}while(type);
	
}
void deleteNode(chainList *node)
{
		chainList*ptemp=node->next;
		node->next=ptemp->next;
		delete ptemp;

}
void editList(chainList *list,char *name)//删除,添加就是编辑。缺点:不想删除的也被删除了。
{
	chainList *pde;
	pde=searchList(list,name);

	char tp;
	int type;
	do{
		cout<<"The person whose info you want to input is:"<<endl;
		cout<<"1.a student    2.a teacher	3.a common employee"<<endl;
		cout<<"0.Return to main menu."<<endl;
		cin>>tp;//为什么一输入字符就死循环??
		char nm[20];
		int ag;
		char sx;
		int slr;
		int grd;
		char schl[20];
		char sb[20];
		int teaYear;
		int aa=1;
		persons*psn;//插入人员指针
		if(tp==1||tp==2||tp==3)
				deleteNode(pde);
		switch(tp)
		{
		case '0':aa=0;
			type=0;
			break;
		case '1':
			type=1;
			cout<<"name:"<<endl;
			cin>>nm;
			cout<<"age:"<<endl;
			cin>>ag;
			cout<<"male or female?m or f?"<<endl;
			cin>>sx;
			cout<<"The student's grade"<<endl;
			cin>>grd;
			cout<<"From which school:"<<endl;
			cin>>schl;
			psn=new student(ag,nm,sx,grd,schl);
			break;
		case '2':type=2;
			cout<<"name:"<<endl;
			cin>>nm;
			cout<<"age:"<<endl;
			cin>>ag;
			cout<<"male or female?m or f?"<<endl;
			cin>>sx;
			cout<<"subject field:"<<endl;
			cin>>sb;
			cout<<"Years as a teacher:"<<endl;
			cin>>teaYear;
			psn=new teacher(ag,nm,sx,sb,teaYear);
			break;
		case '3':type=3;
			cout<<"name:"<<endl;
			cin>>nm;
			cout<<"age:"<<endl;
			cin>>ag;
			cout<<"male or female?m or f?"<<endl;
			cin>>sx;
			cout<<"The employee's salary:"<<endl;
			cin>>slr;
			psn=new empoyee(ag,nm,sx,slr);
			break;
		default:type=6;
			aa=0;
			cout<<"Wrong choice!"<<endl;
		}
		chainList*p=list;//新节点在p所指的节点之前插入,
		if(aa)
		{
			
			int com;
			if(p->next)
			{
				com=compareStr(nm,p->next->pl->name);
				cout<<com<<endl;
			}
			while(p->next&&com>0)//比p节点小或者p为空退出
			{
			p=p->next;
			if(p->next)
			com=compareStr(nm,p->next->pl->name);
			}
			chainList *s=new chainList(type,psn);
			s->next=p->next;
			p->next=s;
		}	
	}while(type);
	
}
chainList *searchList(chainList *list,char*name)//查找函数
{
	chainList *p=list;
	int com;
	if(p->next)
		com=compareStr(name,p->next->pl->name);
	
	while(p->next&&com)//com为0值(即名字相同)或者p->next为空退出
	{		p=p->next;
	if(p->next)
	com=compareStr(name,p->next->pl->name);
	}
	return p;

}
void orderList(chainList *list)
{
	chainList *p,*q;
	p=list;
	q=p->next;
	while(q->next&&p)
	{
		while((p->next!=q->next)&&p->next)//如果相等,则什么也不做
		{
			int i=compareStr(q->next->pl->name,p->next->pl->name);
			i=-i;
			if(i>0)//字母顺序小的插在前面
			{
				chainList*s;
				s=q->next;
				q->next=s->next;
				
				s->next=p->next;
				p->next=s;
				p=s;
				break;
			}
			if(i==0)//字母顺序相同的插到后面
			{
				chainList*ss;
				ss=q->next;
				q->next=ss->next;
				
				p=p->next;//让p永远指向插入位置前一个位置。
				
				ss->next=p->next;
				p->next=ss;
				p=ss;
				break;
			}
			p=p->next;
		
		}
		p=list;//让p重新指向头节点。
		if(q->next)
		q=q->next;//q前进一步。
	}
}

	

void orderListByAge(chainList *list)
{
	chainList*p,*q;
	p=list;
	q=p->next;
	while(q->next&&p)
	{
		while(p->next!=q->next)//如果相等,则什么也不做
		{
			int i=q->next->pl->age-p->next->pl->age;
			if(i>0)//年龄大的插到用于比较的节点前面
			{
				chainList*s;
				s=q->next;
				q->next=s->next;

				s->next=p->next;
				p->next=s;
				p=s;
				break;
			}
			if(i==0)//年龄相同的插到后面
			{
				chainList*ss;
				ss=q->next;
				q->next=ss->next;

				p=p->next;//让p永远指向插入位置前一个位置。

				ss->next=p->next;
				p->next=ss;
				p=ss;
				break;
			}
			p=p->next;
		}
		p=list;//让p重新指向头节点。
		if(q->next)
		q=q->next;//q前进一步。
	}
}
void orderListBySex(chainList *list)//按性别排序,插入排序
{
	chainList*p,*q;
	p=q=list;

	while(q->next)
	{
		if(q->next->pl->sex=='f')
		{
			chainList*s;
			s=q->next;
			q->next=s->next;//删除,然后插到前面
			
			s->next=p->next;
			p->next=s;
			p=s;
		}
		q=q->next;
	}
}

void printStaticInfo(chainList*list)
{
	int count,countS,countT,countE,countMale,countFemale;
	count=countS=countT=countE=countMale=countFemale=0;
	chainList*p=list->next;
	while(p)
	{
		p->pl->print();
		count++;
		if(p->pl->sex=='m')
			countMale++;
		else
			countFemale++;
		switch(p->typeOfPn)
		{
		case 1:
			countS++;
			break;
		case 2:
			countT++;
			break;
		case 3:
			countE++;
			break;
		}
		p=p->next;
		
	}
	cout<<"********************************************"<<endl;
	cout<<"Here are the infors in the systym:"<<endl;
	cout<<"Persons' count in all:"<<count<<endl;
	cout<<"Males' count in all:"<<countMale<<endl;
	cout<<"Females' count in all:"<<countFemale<<endl;
	cout<<"Students' count in all:"<<countS<<endl;
	cout<<"Teachers's count in all:"<<countT<<endl;
	cout<<"Common employees' count in all:"<<countE<<endl;
	cout<<"********************************************"<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -