getclassname可以获取T是什么类型
#include
#include
#include
#include<stdlib.h>
#include
#include<Windows.h>
using namespace std;
class Employee
{
protected:
int id;
string name;
string sex;
string degree;
public:
void setid(int id)
{
this->id = id;
}
void setname(string name)
{
this->name = name;
}
void setsex(string sex)
{
this->sex = sex;
}
void setdegree(string degree)
{
this->degree = degree;
}
int getid()
{
return id;
}
string getname()
{
return name;
}
string getsex()
{
return sex;
}
string getdegree()
{
return degree;
}
friend ofstream& operator<<(ofstream& of, Employee& p);
friend ostream& operator<<(ostream& os, Employee& p);
friend ifstream& operator>>(ifstream& ifs, Employee& p);
friend istream& operator>>(istream& cin, Employee& P);
friend bool operator==(Employee& p, Employee& q);
bool operator >(Employee& p)
{
if (this->getid() > p.getid())
{
return 1;
}
else
{
return 0;
}
}
};
int sum = 0;
template
class Node
{
private:
T data;
Node* next;
public:
const char* GetClassName()
{
return typeid(this).name();
}
T& getdata()
{
return data;
}
void setnext(Node next)
{
this->next = next;
}
Node* getnext()
{
return next;
}
};
bool operator==(Employee& p, Employee& q)
{
if (p.getid() == q.getid() && p.getname() == q.getname() && p.getsex() == q.getsex() && p.getdegree() == q.getdegree())
{
return 1;
}
return 0;
}
template
class List
{
private:
Node* head;
public:
List()
{
head = new Node;
head->setnext(NULL);
}
Node* gethead()
{
return head;
}
void add(Node* head);
void show(Node* head);
void Delete(Node* head, int m);
void change(Node* head, int g);
void sort(Node* head);
void quit();
void getlist(Node* head);
void writefile(Node* head);
};
ofstream& operator<<(ofstream& of, Employee& p)
{
of << p.getid() << " " << p.getname() << " " << p.getsex() << " " << p.getdegree() << endl;
return of;
}
ostream& operator<<(ostream& os, Employee& p)
{
cout << “工号:” << p.getid() << “\t姓名:” << p.getname() << “\t性别:” << p.getsex() << “\t职位:” << p.getdegree() << endl;
return os;
}
ifstream& operator>>(ifstream& ifs, Employee& p)
{
int id;
string name;
string sex;
string degree;
ifs >> id >> name >> sex >> degree;
p.setid(id);
p.setname(name);
p.setsex(sex);
p.setdegree(degree);
return ifs;
}
istream& operator>>(istream& cin, Employee& P)
{
cout << “工号:”;
int id;
cin >> id;
P.setid(id);
cout << “姓名:”;
string name;
cin >> name;
P.setname(name);
cout << “性别:”;
string sex;
while (1)
{
cin >> sex;
if (sex != “男” && sex != “女”)
{
cout << “输入错误,请输入男或女:” << endl;
}
else
{
break;
}
}
P.setsex(sex);
cout << “职位:”;
string degree;
while (1)
{
cin >> degree;
if (degree != “销售人员” && degree != “普通职工” && degree != “经理”)
{
cout << “请输入普通职工或销售人员或经理!” << endl;
}
else
{
break;
}
}
P.setdegree(degree);
return cin;
}
template
void List::getlist(Node* head)
{
Node* p = head;
Node* q = new Node;
q->setnext(NULL);
string classname;
ifstream fp;
classname = p->GetClassName();
classname.erase(remove(classname.begin(), classname.end(), ‘<’), classname.end());
classname.erase(remove(classname.begin(), classname.end(), ‘>’), classname.end());
fp.open(classname, ios::in);
sum = 0;
while (fp >> q->getdata())
{
sum++;
p->setnext(q);
p = p->getnext();
q->setnext(NULL);
q = new Node;
}
fp.close();
}
template
void List::writefile(Node* head)
{
string classname;
Node* p = NULL;
classname = p->GetClassName();
classname.erase(remove(classname.begin(), classname.end(), ‘<’), classname.end());
classname.erase(remove(classname.begin(), classname.end(), ‘>’), classname.end());
ofstream of;
of.open(classname, ios::out);
p = head;
while (p->getnext() != NULL)
{
p = p->getnext();
of << p->getdata()<<" ";
}
of.close();
}
//增加函数
template
void List::add(Node* head)
{
Node* q = head;
Node* p = new Node;
while (q->getnext() != NULL)
{
q = q->getnext();
}
cin >> p->getdata();
Node<T>* f = head->getnext();
while (f != NULL)
{
if (f->getdata() == p->getdata())
{
cout << "数据重复请重新输入" << endl;
f = head->getnext();
cin >> p->getdata();
continue;
}
f = f->getnext();
}
q->setnext(p);
q = q->getnext();
q->setnext(NULL);
List<T>l;
l.writefile(head);
cout << "录入成功!" << endl;
}
template
void List::show(Node* head)
{
Node* p = head;
List* l = NULL;
string classname;
classname = p->GetClassName();
classname.erase(remove(classname.begin(), classname.end(), ‘<’), classname.end());
classname.erase(remove(classname.begin(), classname.end(), ‘>’), classname.end());
if (head->getnext() == NULL)
{
return;
}
int flag = 1;
if (classname == “class Nodeclass Employee”)
{
flag = 0;
}
if (flag == 0)
{
l->sort(head);
}
while (p->getnext() != NULL)
{
p = p->getnext();
cout << p->getdata();
}
}
template
void List::Delete(Node* head, int m)
{
Node* p = head;
while (p->getnext() != NULL)
{
if (m > sum)
{
cout << “节点不存在!” << endl;
return;
}
if (m <= sum)
{
Node* q = p->getnext();
for (int i = 1; i < m; i++)
{
p = p->getnext();
q = q->getnext();
}
p->setnext(q->getnext());
delete q;
q = NULL;
cout << “删除成功!” << endl;
List l;
l.writefile(head);
return;
}
}
}
template
void List::change(Node* head, int g)
{
Node* p = head;
while (p->getnext() != NULL)
{
if (g > sum)
{
cout << “节点不存在!” << endl;
return;
}
if (g <= sum)
{
for (int i = 1; i <= g; i++)
{
p = p->getnext();
}
cin >> p->getdata();
List l;
l.writefile(head);
cout << “改动成功!” << endl;
return;
}
}
}
template
void List::sort(Node* head)
{
Node* f, * p, * x, * y;
f = NULL;
while (f != head->getnext()->getnext())//外层判断少一次循环
{
for (p = head; p->getnext()->getnext() != f; p = p->getnext())
{
if (p->getnext()->getdata() > p->getnext()->getnext()->getdata())
{
x = p->getnext();//x和y首先指向这两个要交换的节点
y = p->getnext()->getnext();
p->setnext(y);//交换三部曲
x->setnext(y->getnext());
y->setnext(x);
}
}
f = p->getnext();//把 f 往前移
}
}
template
void List::quit()
{
int a = 3;
cout << " 秒后关闭程序";
while (–a >= 1)
{
printf("\r%d", a);
printf("\b");
Sleep(1000);
}
cout << “感谢您的使用!再见\n”;
exit(0);
}
void Menu();
void Menu()
{
int a;
Listlist;
while (1)
{
list.getlist(list.gethead());
cout << “\n”;
cout << " 欢迎使用公司职工信息管理软件\n" << endl;
cout << " ----------------------------" << endl;
cout << " -------1.录入职工信息-------" << endl;
cout << " -------2.修改职工信息-------" << endl;
cout << " -------3.删除职工信息-------" << endl;
cout << " -------4.浏览职工信息-------" << endl;
cout << " ---------5.退出系统---------" << endl;
cout << " ----------------------------" << endl;
cout << “\n\n请选择:”;
while (1)
{
cin >> a;
if (a < 1 || a > 5)
{
cout << “输入错误请重新输入” << endl;
}
else
{
break;
}
}
switch (a)
{
case 1:
{
int number = 0;
cout << “请输入要增加的个数:”;
cin >> number;
while (number–)
{
list.add(list.gethead());
}
system(“pause”);
system(“cls”);
break;
}
case 2:
cout << “请输入要改动的节点:”;
int g;
cin >> g;
list.change(list.gethead(), g);
system(“pause”);
system(“cls”);
break;
case 3:
cout << “请输入要删除的节点:”;
int h;
cin >> h;
list.Delete(list.gethead(), h);
system(“pause”);
system(“cls”);
break;
case 4:
list.show(list.gethead());
system(“pause”);
system(“cls”);
break;
case 5:
list.quit();
break;
default:
printf(“输入无效!”);
system(“pause”);
system(“cls”);
break;
}
}
}
int main()
{
Menu();
return 0;
}