64,654
社区成员
发帖
与我相关
我的任务
分享
#ifndef employee_H
#define emplouee_H
#include <string>
using namespace std;
struct element
{
int no; //职工序号;
char *name; //职工姓名;
int depno; //职工部门号;
double salary; //职工工资;
element()
{
no = 0;
name = " ";
salary = 0.0;
}
element(int no1, char *name1,double salary1)
{
no = no1;
name =new char[strlen(name) + 1];
strcpy_s(name, strlen(name) + 1,name1);
salary = salary1;
}
};
class link
{
public:
element date;
link *next;
int *pno; //职工序号指针;
int *pdepno; //职工部门号指针;
double *psalary; //职工工资指针;
link(element a, link *nextvalue = NULL)
{
date.no = a.no;
date.depno = a.depno;
date.salary = a.salary;
date.name = new char[strlen(a.name) + 1];
strcpy_s(date.name, strlen(a.name) + 1,a.name);
next = nextvalue;
pno = pdepno = 0;
psalary = 0;
}
link(int no1, int depno1,double salary1, char *name1, link *nextvalue = NULL)
{
date.no =no1;
date.depno =depno1;
date.salary =salary1;
date.name = new char[strlen(name1) + 1];
strcpy_s(date.name, strlen(name1) + 1,name1);
next = nextvalue;
pno = pdepno = 0;
psalary = 0;
}
link(link *nextvalue = NULL, link *pno1 = NULL, link *dep1=NULL,link *salary1=NULL)
{
next = nextvalue;
pno = pdepno = 0;
psalary = 0;
}
};
class employeelinklist:public link
{
public:
link *head;
employeelinklist()
{
head=new link(NULL);
}
~employeelinklist()
{
link *p;
while (head != NULL)
{
p = head;
head = head->next;
delete p;
}
}
void add(); //添加一个职工的信息;
void show(link *head); //输出全部职工信息;
void find(int a); //查找职工号为a的职工信息;
link * deleteemployee(int a); //删除职工号为a的职工信息;
link * nosort(link *h); //通过pno指针将职工记录按no从小到大链接起来;
void noshow(link *head); //沿pno链输出全部职工记录;
link * depnosort(link *h); //通过pdepno指针将职工记录按depno从小到大链接起来;
void depnoshow(link *head); //沿pdepno链输出全部职工记录;
link * salarysort(link *h); //通过psalary指针将职工记录按salary从小到大链接起来;
void salaryshow(link *head); //沿psalary链输出全部职工记录;
void Delete(); //删除职工文件中的全部记录;
void save(); //将单链表中的全部结点存储到职工文件中,然后退出程序运行过程
};
#endif
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
#include "employee.h"
void employeelinklist::add() //添加一个职工的信息;
{
link *p,*r;
cout<<"提示:当要返回主菜单时请输入0。"<<endl;
r = head;
while (1)
{
int no1, depno1, salary1;
static char s[100];
p = new link;
cout<<"请输入职工序号:";
cin >> no1;
if (no1 == 0) break;
p->date.no = no1;
p->pno=&p->date.no;
cout<<"请输入职工的姓名:";
getchar();
gets(s);
p->date.name = new char[strlen(s) + 1];
strcpy_s(p->date.name,strlen(s)+1,s);
cout<<"请输入职工的部门序号:";
cin >> depno1;
p->date.depno = depno1;
p->pdepno=&p->date.depno;
cout<<"请输入职工的工资:";
cin >> salary1;
cout << endl;
p->date.salary = salary1;
p->psalary=&p->date.salary;
r->next = p; //将两个节点连接;
p->next = NULL;
r = p; //使r指针永远放在链表的最后一位;
}
}
void employeelinklist::show(link *head) //输出全部职工信息;
{
link *p;
p = head->next;
if (p == NULL)
cout<<"该文件为空."<<endl;
while (p!= NULL)
{
cout << "该职工的职工序号是:"<<p->date.no<<endl;
cout << "该职工的姓名是:"<<p->date.name<<endl;
cout << "该职工的部门序号是:"<<p->date.depno<<endl;
cout << "该职工的工资薪酬是:"<<p->date.salary<<endl;
p = p->next;
}
}
void employeelinklist::find(int a) //查找职工号为a的职工信息;
{
link *p = head->next;
if (p == NULL)
cout << "该文件为空。"<<endl;
while (p)
{
if (p->date.no == a)
{
cout << "该职工的职工序号是:" << p->date.no << endl;
cout << "该职工的姓名是:" << p->date.name << endl;
cout << "该职工的部门序号是:" << p->date.depno << endl;
cout << "该职工的工资薪酬是:" << p->date.salary << endl;
return;
}
else
p = p->next;
}
cout<<"职工文件中没有该员工信息。"<<endl;
}
link * employeelinklist::deleteemployee(int a) //删除职工号为a的职工信息;
{
link *p,*q=new link();
p = head->next;
while (p)
{
if (p->next->date.no == a)
q = p->next;
else
p = p->next;
}
if (q->next == NULL)
{
p->next = NULL;
delete q;
}
else
{
p->next = q->next;
delete q;
}
return head;
}
link * employeelinklist::nosort(link *h) //通过pno指针将职工记录按no从小到大链接起来;
{
link *p = h;
link *q=new link;
if (p->next== NULL)
cout<<"职工文件为空。"<<endl;
for (p =h->next; p->next != NULL; p = p->next)
{
for (q =p; q->next != NULL; q = q->next)
{
if (q->date.no < p->date.no)
{
int *m,*n;
double *k;
char *name1;
name1 = new char[strlen(p->date.name)+1];
strcpy_s(name1,strlen(p->date.name),p->date.name);
strcpy_s(p->date.name, strlen(p->date.name),q->date.name);
strcpy_s(q->date.name,strlen(p->date.name), name1);
m = p->pno;
n = p->pdepno;
k = p->psalary;
p->pno = q->pno;
p->pdepno=q->pdepno;
p->psalary = q->psalary;
q->pno = m;
q->pdepno = n;
q->psalary = k;
}
}
}
return p;
}
void employeelinklist::noshow(link *head) //沿pno链输出全部职工记录;
{
link *m = head;
if (m->next== NULL)
cout<<"职工文件为空。"<<endl;
while (m)
{
cout << "该职工的职工序号是:" << *m->pno<< endl;
cout << "该职工的姓名是:" << m->date.name<< endl;
cout << "该职工的部门序号是:" << *m->pno<< endl;
cout << "该职工的工资薪酬是:" << *m->pno<< endl;
m = m->next;
}
}
#include <iostream>
#include "employee.h"
using namespace std;
void main()
{
int choice;
employeelinklist G;
do
{
cout << "------------------------------------------------------------------------------------" << endl;
cout << "职工文件的功能:0.退出;1.新建;2,查找;3,删除;4.排序;5.删除全部信息;6.存储退出。" << endl;
cout << "------------------------------------------------------------------------------------" << endl;
cout << "请选择要执行的命令:";
cin >> choice;
switch (choice)
{
case 1:
G.add(); break;
case 2:
link *t = new link;
t = G.nosort(G.head);
cout << "----------------" << endl;
cout << "排序后的结果是:" << endl;
cout << "----------------" << endl;
G.noshow(t);
break;
不用管:
/*case 2:
int num;
cout << "------------------------" << endl;
cout << "请输入要查找的职工序号:";
cout << "------------------------" << endl;
cin >> num;
G.find(num);
break;
case 3:
int num1;
cout << "------------------------" << endl;
cout << "请输出要删除的职工序号:";
cout << "------------------------" << endl;
cin >> num1;
link *p = new link;
p=G.deleteemployee(num1);
cout << "----------------" << endl;
cout << "删除后的结果是:" << endl;
cout << "----------------" << endl;
G.show(p);
break;
case 2:
cout << "-----------------------------------------------------" << endl;
cout << "排序的类型:1.职工序号;2,职工部门序号;3.职工工资。" << endl;
cout << "-----------------------------------------------------" << endl;
cout << "请输入选择:";
int choice1;
cin >> choice1;
switch (choice1)
{
case 1:
link *t = new link;
t=G.nosort(G.head);
cout << "----------------" << endl;
cout << "排序后的结果是:" << endl;
cout << "----------------" << endl;
G.noshow(t);
break;
case 2:
link *p=new link;
p=G.depnosort(G.head);
cout << "排序后的结果是:" << endl;
G.depnoshow(p);
break;
case 3:
link *k = new link;
k=G.salarysort(G.head);
cout << "排序后的结果是:" << endl;
G.salaryshow(k);
break;
}
case 5:
G.Delete();
break;
default:
G.save();
break;*/
}
} while (choice != 0);
}
#include <iostream>
using namespace std;
//7,不用库的智能指针,自己要写一个吧.....
class element;
class auto_p
{
public:
auto_p(element * _p):p(_p),count_ref(new int(1)){}
auto_p(const auto_p& _autop)
{
p=_autop.p;
count_ref=_autop.count_ref;
++(*count_ref);
}
auto_p& operator=(const auto_p& _autop)
{
if(this!=&_autop)
{
p=_autop.p;
count_ref=_autop.count_ref;
++(*count_ref);
}
return *this;
}
element* getP(){return p;}
~auto_p()
{
del();
}
private:
int* count_ref;
element* p;
auto_p();
void del()
{
--(*count_ref);
if(count_ref==0)
{
delete p;
delete count_ref;
}
}
};
class element//2).都是public?类是......
{
public:
element(int no1, string name1,double salary1):no(no1),name(name1),depno(1),salary(salary1),nextElement(0)
{
//6)初始化和赋值是不一样的,构造函数可以直接初始化,就不要让它自动初始再赋值.
//name =new char[strlen(name) + 1];//5),如果有char *.new 之后的,delete呢?
}
auto_p nextElement;//下一个职工,8)用智能指针,对象管理指针.本要方法来读写.这里直接public把.
void showme()
{
cout<<"id:"<<no<<". name:"<<name<<". salary:"<<salary<<endl;
}
private:
element();//3).不需要的构造函数要隐藏.
int no; //职工序号;
string name; //职工姓名;4).用string吧.
int depno; //职工部门号;
double salary; //职工工资;
friend void insertEle(int no1, string name1,double salary1,auto_p& head);
};
void insertEle(int no1, string name1,double salary1,auto_p& head)
{
auto_p ele =auto_p(new element(no1,name1,salary1));
if(head.getP()->nextElement.getP()==0)//只有1条数据.
{
head.getP()->nextElement=ele;
}
else
{
auto_p firstele=head;
auto_p secondele=firstele.getP()->nextElement;
while(secondele.getP()!=0)
{
if(no1>=firstele.getP()->no&& no1<=secondele.getP()->no)
{
firstele.getP()->nextElement=ele;
ele.getP()->nextElement=secondele;
break;
}
else
{
firstele=secondele;
secondele=firstele.getP()->nextElement;
}
}
firstele.getP()->nextElement=ele;
}
}
void add(auto_p head);
int main()
{
int choice=1;//1).要初始化,不初始化,万一里面的值恰好是0呢?
//建立一个链头,是一个哨兵,取一个比任何编号都小的数字-1,作为工号.
auto_p head(new element(-1,"Linkhead",0));
while(choice != 0)
{
cout << "------------------------------------------------------------------------------------" << endl;
cout << "职工文件的功能:0.退出;1.新建;2,查找;3,删除;4.排序;5.删除全部信息;6.存储退出。" << endl;
cout << "------------------------------------------------------------------------------------" << endl;
cout << "请选择要执行的命令:";
cin >> choice;
switch (choice)
{
case 1:
add(head);
break;
case 4:
auto_p tempele=head;
while(1)
{
tempele.getP()->showme();
if(tempele.getP()->nextElement.getP()==0)
{
break;
}
else
{
tempele=tempele.getP()->nextElement;
}
}
break;
}
}
return 0;
}
void add(auto_p head) //添加一个职工的信息;
{
cout<<"提示:当要返回主菜单时请输入0。"<<endl;
while (1)
{
int no1, depno1, salary1;
string name;
//static char s[100];//9)静态变量??为什么
//p = new link;//10)一开始就new?用户按了0.这个内存谁来释放???
cout<<"请输入职工序号:";
cin >> no1;
if (no1 == 0) break;
cout<<"请输入职工的姓名:";
cin>>name;
cout<<"请输入职工的部门序号:";
cin >> depno1;
cout<<"请输入职工的工资:";
cin >> salary1;
cout << endl;
insertEle( no1, name, salary1, head);
}
}
确实要小心写,插入数据那里,代码很简单,不小心,就漏掉边界的考虑。
不过,你是从哪里学的c++啊?老师还是书本啊?
是书本,就丢了吧。
节选了你一部分的代码来改。就发现近10处问题。
整体结构,说实在的,不忍直视。
element类都出来了。加个指针,就是单链模式。
你新出来个类,还有继承。
new的堆内存。没有看到一句delete。
建议。从头看c++。丢掉垃圾书。