本系统能实现的功能及优点有:
1.动态创建对象(链表实现,使用new向系统申请内存)
2.输入与输出对象的信息:包括单项输入与输出,与全部输入与输出
3.定位插入及删除对象资料(链表实现)
4.动态查找与身份数据匹配的人,并输出他的信息
5.动态查找工资最高的人
6.重要数据类型使用private,实现数据的保密
7. 输出某一对象的总人数(构造时人数++,析构时–)
8.使用了文件的输入输出
9.用归并排序对链表进行排序
10.较为人性化
总代码长度1757行
总结一下自己犯的错,及debug时的痛苦
1.find函数的循环是
while(temp->next&&temp->Tnum!=data)
注意的是逻辑运算符是&&不是||,其中一个不符合就停止,而且是temp->next!=NULL;
,不是temp!=NULL;
,否则编译器会强制停止,这么做的话特判一下
if(temp->Tnum!=data) return NULL;
即可
2.creat函数的参数要加引用&,否则无法改变尾指针的地址,然后insert函数调用creat函数的时候尾指针就仍是NULL,然后编译器又会强制停止
3.delete函数参数也要加引用,否则当删除的是第一个对象时,无法改变首指针的地址,又会强制停止
void delete1(Teacher* &head)
而且当删除为第一个对象(head==temp)时,不能在
while(temp->Tnum!=data&&temp!=NULL)
pre=temp,temp=temp->next;
后特判,而是要先判断temp是否为NULL,不为空才head=temp->next;否则先判断完后,head与temp就不相等了然后删除的就不是第一个对象了
4.同样的sort函数参数head也要加&
void Tsort(Teacher* &h)
5.findrich函数找到一个类中最有钱的人,也是
while(head->next)
{
if(Maxn<head->SMwage)///原来在后面没注意加了一个';'导致判断没用了
{
Maxn=head->SMwage;
temp=head;
}
head=head->next;
}
6.注意加&的head要让temp=head,temp=temp->next,否则head的地址会改变
7.find函数返回的地址temp,if(temp) cout<<"查找成功";
而不是if(!temp) cout<<"查找成功";
类似的逻辑符号上的错误还犯了一些
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
const int TEACHER =2;
const int STUDENT =1;
const int S_MANAGER=4;
const int MANAGER=3;
class S_Manager;
class Person
{
string name,gender,birtheday;
string others[10];
int cnt;///补充其他数据的数量
public:
static int sum;///总对象数量
Person():cnt(0){
sum++;}
virtual ~Person(){
sum--;}///必须提供定义 否则找不到析构函数入口
void printprompt(int case1)
{
if(case1==1) cout<<"请按如下顺序输入";
if(case1==2) cout<<"请根据数字选择输出哪一项";
if(case1==3) cout<<"请根据数字选择选择替换的";
cout<<"基本信息1:名字,2:性别,3:出生年月";
if(case1==2) cout<<",4:全部";
if(case1==2||case1==3)cout<<"\n请输入数字";
cout<<'\n';
}
virtual void setwage( int)=0;///给工资的信息输入留一个接口
void baseread()///读入基本数据
{
printprompt(1);///提示语
cin>>name;
cin>>gender;
cin>>birtheday;
cout<<'\n';
}
void baseprint()///输出基本数据
{
int n;
printprompt(2);
cin>>n;
switch(n)
{
case 1:
if(!name.size())
cout<<"没有此项数据\n";
else cout<<name<<' ';
break;
case 2:
if(!gender.size())
cout<<"没有此项数据\n";
else cout<<gender<<' ';
break;
case 3:
if(!birtheday.size())
cout<<"没有此项数据\n";
else cout<<birtheday<<' ';
break;
case 4:
if(!name.size())
cout<<"没有姓名数据 ";
else cout<<name<<' ';
if(!gender.size())
cout<<"没有性别数据\n";
else cout<<gender<<' ';
if(!birtheday.size())
cout<<"没有生日数据\n";
else cout<<birtheday<<' ';
break;
default:
cout<<"没有此输出项\n";
}
cout<<'\n';cout<<'\n';
}
void Preplacedata()///数据替换
{
int n;
string rdata;
printprompt(3);
cin>>n;
switch(n)
{
case 1:
cout<<"请输入替换后的姓名\n";
cin>>rdata;
name=rdata; break;
case 2:
cout<<"请输入性别\n";
cin>>rdata;
gender=rdata; break;
case 3:
cout<<"请输入生日\n";
cin>>rdata;
birtheday=rdata; break;
default:
cout<<"没有此项数据\n";
}
cout<<'\n';
}
string getname(){
return name;}
};
int Person:: sum=0;
class Manager: virtual public Person
{
string departmant;int Mwage;string Mnum;
public:
friend class S_Manager;
static int Msum;
Manager *next;
Manager():next(NULL){
Msum++;}
~Manager(){
Msum--;};
int getMwage(){
return Mwage;}
friend void Msort(Manager* &head);
friend Manager* Mfind(Manager* head);///查找函数
friend void delete1(Manager* &head);///删除函数
void printManprompt(int case1)
{
if(case1==1) cout<<"请按如下顺序输入";
if(case1==2) cout<<"请根据数字选择输出哪一项";
if(case1==3) cout<<"请根据数字选择替换哪一项";
cout<<"管理员信息:1.管理员编号,2.所属部门,3.管理员工资";
if(case1==2)cout<<",4.全部";
if(case1==2||case1==3)cout<<"\n请输入数字";
cout<<"\n";
}
void Msetdata()///输入基础信息
{
baseread();
printManprompt(1);
cin>>Mnum>>departmant>>Mwage;
}
void setwage(int wage){
Mwage=wage;}
void Mprint()///输出基本数据
{
int n;
this->baseprint();
printManprompt(2);
cin>>n;
switch(n)
{
case 1:
if(!Mnum.size())
cout<<"没有此项数据\n";
else cout<<Mnum<<' ';
break;
case 2:
if(!departmant.size())
cout<<"没有此项数据\n";
else cout<<departmant<<' ';
break;
case 3:
if(!Mwage)
cout<<"没有此项数据\n";
else cout<<Mwage<<' ';
break;
case 4:
if(!Mnum.size())
cout<<"没有管理员编号数据\n";
else cout<<Mnum<<' ';
if(!departmant.size())
cout<<"没有所属部门数据\n";
else cout<<departmant<<' ';
if(!Mwage)
cout<<"没有管理员工资数据\n";
else cout<<Mwage<<' ';
break;
default:
cout<<"没有此输出项\n";
}
cout<<'\n';
cout<<'\n';
}
void Mreplacedata()///数据替换
{
Preplacedata();
int n;
printManprompt(3);
cin>>n;
string rdata;int Rdata;
switch(n)
{
case 1 :
cout<<"请输入替换后的管理员编号\n";
cin>>rdata;
Mnum=rdata;
cout<<"替换成功\n";
break;
case 2:
cout<<"请输入替换后的所属部门\n";
cin>>rdata;
departmant=rdata;
cout<<"替换成功\n";
break;
case 3:
cout<<"请输入替换后的工资\n";
cin>>Rdata;
Mwage=Rdata;
cout<<"替换成功\n";
break;
default:
cout<<"没有此项数据\n";
}
printf("\n");
cout<<'\n';
}
};
int Manager:: Msum=0;
string Mfindpromote(int &case1)
{
cout<<"请输入查询的方式(输入数字):1.管理员编号,2.姓名\n";
int n;
cin>>n;
while(n!=1&&n!=2){
cout<<"没有此项查找方式,请重新输入查找方式\n";cin>>n;}
string data;
if(n==1)
{
cout<<"请输入管理员编号\n";
cin>>data;
case1=n;
}
else if(n==2)
{
cout<<"请输入姓名\n";
cin>>data;
case1=n;
}
cout<<'\n';
return data;
}
Manager* Mfind(Manager* head)
{
if(head==NULL) {
cout<<"还未创建对象\n";return NULL;}
cout<<"请输入查询的方式(输入数字):1.管理员编号,2.姓名\n";
int n; cin>>n;
while(n!=1&&n!=2){
cout<<"没有此项查找方式,请重新输入查找方式\n";cin>>n;}
string data;
if(n==1)
{
cout<<"请输入管理员编号\n";
cin>>data;
Manager* temp=head;
while(temp->Mnum!=data&&temp->next!=NULL)
temp=temp->next;
if(temp->Mnum!=data) return NULL;
return temp;
}
else if(n==2)
{
cout<<"请输入姓名\n";
cin>>data;
Manager* temp=head;
while(temp->getname()!=data&&temp->next!=NULL)
temp=temp->next;
if(temp->getname()!=data) return NULL;
return temp;
}
else return NULL;
}
Manager* creatManager(Manager* &back1)///head要加引用!!!!!!!!!!!!!,否则地址不会改变
{
//int i=1;
cout<<"请输入录入信息的管理员个数\n";
int n;cin>>n;
Manager *head=NULL;
Manager *ob1=NULL;
Manager *ob2=NULL;
ob1=new Manager();
head=ob1;
for(int i=0;i<n;i++)
{
cout<<"请输入第"<<i+1<<"个管理员的信息\n";
ob1->Msetdata();///注意调用方式,是*(ob1).Tsetdata();或者ob1->Tsetdata();
if(i+1<n)
{
ob2=ob1;
ob1=new Manager;
ob2->next=ob1;
}
}
back1=ob1;///用于插入时返回尾指针的位置
//cout<<"!!"<<ob1<<endl;
back1->next=NULL;
ob1->next=NULL;
ob2=NULL;
ob1=NULL;
cout<<'\n';
return head;
}
void Minsert(Manager* &head)///head要加引用!!!!!!!!!!!!!,否则地址不会改变
{
Manager* back1=NULL;
if(!head)
{
cout<<"还未创建对象,现在开始创建\n";
head=creatManager(back1);
cout<<"创建完成,是否继续插入信息:1.是,2.否\n";
int n; cin>>n;
if(n==2) return;
}
cout<<"请选择信息的插入方式,1.直接插入信息,2.插入到某一管理员的信息位置后\n";
int n;cin>>n;
if(n==2)
{
Manager *temp=NULL;
temp=Mfind(head);
//cout<<temp;
if(temp)
{
cout<<"查找成功,开始插入管理员信息\n";
Manager *temphead=creatManager(back1);///创建一个新的链表
//cout<<back1->next<<endl;
///将新链表连接到原来的链表上
back1->next=temp->next;
temp->next=temphead;
//cout<<temp<<' '<<head->next;
//cout<<temp->next;
}
else {
cout<<"查找失败,自动选择直接插入信息模式\n";n=1;}
}
if(n==1)
{
Manager *temp=head;
//cout<<head->next;