#include
#include
#include
#include
#include
using namespace std;
class Employee
{
public:
string num,name;
int hour,time,money,pos;
struct Employee* next;
};
typedef class Employee Node;
typedef Node* Link;
Link Add(Link Head);
Link Modify(Link Head);
void Del(Link Head);
Link search_front(Link Head);//查询满足工号的
void Search(Link Head);
void Sort(Link Head);
void Save(Link Head);
void Show(Link Head);
void Public_Node(Link pNode);
Node *Locateofnum(Link l,char findmess[]);
Node *Locateofnum(Link l,string num){
Node *r;
r=l;
r=l-> next;
while(r){
if(r-> num == num)
return r;
r=r-> next;
}
return 0;
}
Link search_front(Link Head){
Link Front;
string num;
Front=Head;
cout<<"工号";
cin>>num;
cout<<endl<<"----------------查询结果------------------"<<endl;
while(Front->next){
cout<<Front->next->num<<endl;
if(Front->next->num == num)
return Front;//返回的查询到的节点的前节点
Front->next=Front->next->next;
}
return Front;
}
void Public_Node(Link pNode)
{//在标准输出设备上输出。
cout<<setw(10)<<left<<pNode->num
<<setw(10)<<left<<pNode->name
<<setw(10)<<left<<pNode->hour
<<setw(10)<<left<<pNode->time
<<setw(10)<<left<<pNode->money<<endl;
}
Link Modify(Link Head){
Node *p;
int flag;
char ch;
string num,name;
int hour,time,money;
if(!Head->next){
cout<<"没有工人记录"<<endl;
getchar();
// break;
}
while(1){
cout<<"修改工人信息"<<endl;
//search_front(Head);
cout<<"请输入工人工号"<<endl;
cin>>num;
p=Locateofnum(Head,num); //查询到该节点
if(p){
cout<<"请输入修改后的名字"<<endl;
cin>>name;
cout<<"请输入修改后的每小时工资"<<endl;
cin>>hour;
cout<<"请输入修改后的工时"<<endl;
cin>>time;
cout<<endl;
p->name=name;
p->hour=hour;
p->time=time;
p->money=time * hour * 30;
cout<<"是否继续操作(y/n)?"<<endl;
cin>>ch;
if(ch == 'y' || ch == 'Y')
continue;
else break;
}
else
cout<<"查找不到该工人"<<endl;
}
}
Link Add(Link Head){
Node next1;
string num,name;
int hour,time,money;
char ch;
Link Newnode;
do{
Newnode=(Link)new Node;
//Newnode=(Link)new Node;
if(!Newnode){
cout<<"内存申请失败!!"<<endl;
return 0;
}
cout<<"请输入工人工号:";
cin>>num;
cout<<"请输入工人名字:";
cin>>name;
cout<<"请输入工人每小时工资";
cin>>hour;
cout<<"请输入工人每天的工时";
cin>>time;
//cout>>"请输入每天的工资";
cout<<endl;
Newnode->num=num;
Newnode->name=name;
Newnode->hour=hour;
Newnode->time=time;
Newnode->money=time * hour * 30;
//指针域
Newnode->next=Head->next;
Head->next=Newnode;
cout<<"数据添加成功!是否继续添加?(Y/N)"<<endl;
cin>>ch;
}
while(ch =='y' || ch =='Y');
return Head;
}
void Del(Link Head){
Link input,input1;
input1=search_front(Head);
printf("________________________%d\n",input1->hour);
input=input1->next;
if(input){
input1->next=input->next;
delete input;//删除此节点。
cout<<"删除成功"<<endl;
}
else
cout<<"没找到此职工的记录,无法删除。"<<endl;
}
void Save(Link Head){
// cout<<"454";getchar();cout<<"454";getchar();cout<<"454";getchar();
FILE *fp;
Node *p;
int count=0;
fp=fopen("worker.txt","wb+"); //以只写方式
p=Head-> next;
while(p){
if(fwrite(p,sizeof(Node),1,fp) == 1){
p=p->next;
count++;
}
else
break;
}
if(count>0){
//getchar();
//printf("\n\n\n\n\n = = = = >save file complete ,total saved record number is : %d \n",count);
cout<<"\n\n\n\n\n = = = = >保存文件成功 "<<endl;
//getchar();
// saveflag=0;
}
else {
//system("cls");
//printf("the current link is empty,no worker record is saved!\n");
cout<<"链表是空的,没有成员\n"<<endl;
//getchar();
}
fclose(fp);
}
void Search(Link Head)
{ string num,name;
int i;
Node *p;
cout<<"按1姓名查询,按2工号查询[1~2]"<<endl;
cin>>i;
p=Head->next;
if(i == 1){
cout<<"请输入已存在的名字"<<endl;
cin>>name;
while(p)
{
if(p->name==name){
break;
}
p=p->next;
}
}
else if(i == 2){
cout<<"输入已存在的工号"<<endl;
cin>>num;
while(p)
{
if(p->num==num){
break;
}
p=p->next;
}
}
if(p){
Public_Node(p);
}
else cout<<"员工不存在"<<endl;
}
void Sort(Link Head){
struct Employee temp;
int fig,i;
char ch;
Node *p1,*p2,*p; // p1,p2指向要排序的节点,p指向两个节点中分数较高的一个
if(Head->next == NULL){
cout<<"没有工人信息";
getchar();
return ;
}
while(1){
cout<<"请输入[1~3](1:按总工资2:按每小时工资3:按工时:)";
cin>>fig;
p1=Head->next;
while(p1){
p2=p1->next;
while(p2){
if((fig==1 && p2->money >= p1->money)||(fig == 2 && p2->hour >= p1->hour)||(fig == 3 && p2->time >= p1->time)){
swap(p1->num,p2->num);
swap(p1->name,p2->name);
swap(p1->hour,p2->hour);
swap(p1->time,p2->time);
swap(p1->money,p2->money);
}
p2=p2->next;
}
Public_Node(p1);
p1=p1->next;
}
printf("=====>排序成功\n");
printf("是否继续查询(y/n)?");
scanf("%c",&ch);getchar();
if(ch == 'y' || ch == 'Y')
continue;
else
{printf("按回车键返回菜单!");getchar();break;}
}
}
void Show(Link Head){
Link pri;
pri=Head->next;
cout<<"==================所有职工信息=================="<<endl;
while(pri){
Public_Node(pri);
pri=pri->next;
}
}
int main()
{
int flag=1;
Link Head,now,front;
FILE *fp;
int select;
Node *p,*r; //定义记录指针变量
//Head=Creat(Link Head);
//Node *First;
Head=(Link)new Node;
if(!Head){
cout<<"申请内存失败!"<<endl;
return 0;
}
Head->next=NULL;
r=Head;
fp=fopen("worker.txt","ab+");
if(fp == NULL){
cout<<"= = = = >不能打开文件 "<<endl;
exit(0);
}
while(1){
cout<<"*****************************************************"<<endl;
cout<<"*====================菜单选顶=======================*"<<endl;
cout<<"*===================================================*"<<endl;
cout<<"* 1.注册职工 2.修改信息 3.删除信息 4.信息查询 *"<<endl;
cout<<"* 5.保存文件 6.工资排行 7.信息显示 0.退出系统 *"<<endl;
cout<<"*****************************************************"<<endl;
cout<<endl<<"请选择相应操作菜单项:";
cin>>select;
switch(select){
case 1:Add(Head);break;
case 2:Modify(Head);break;
case 3:Del(Head);break;
case 4:Search(Head);break;
case 5:Save(Head);break;
case 6:Sort(Head);break;
case 7:Show(Head);break;
case 0:return 0;
}
// system("cls");
}
return 0;
}
这个有bug 在读取文件内容的时候