#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;
#include "List.h"
//char password[10];//用户密码
class Salary:public Data
{public:
char Num[10];//职工号
char Name[20]; //职工姓名
double BaseSalary; //职务工资
double Bonus; //职工奖金
Salary(){ strcpy(Name,"\0");BaseSalary=0.0;Bonus=0.0;strcpy(Num,"\0");}
Salary(char *,double ,double,char*);
void SetSalary(char*,double,double,char*);
int Compare(Data&);
void Show();
};
Salary::Salary(char * name,double base,double bonus,char* num)
{
strcpy(Name,name);
BaseSalary=base;
Bonus=bonus;
strcpy(Num,num);
}
void Salary::SetSalary(char* name,double base,double bonus,char* num)
{
strcpy(Name,name);
BaseSalary=base;
Bonus=bonus;
strcpy(Num,num);
}
/***********************************************************
*功能:比较姓名,查找 *返回:1,0
***********************************************************/
int Salary::Compare(Data& data)
{
Salary&temp=(Salary&)data;
return strcmp(Name,temp.Name);
}
//打印一个结点的数据
void Salary::Show()
{
cout<<setw(10)<<Num<<setw(10)<<Name<<setw(12)<<BaseSalary<<setw(8)<<Bonus<<setw(12)<<(BaseSalary+Bonus)<<endl;
}
/********************************************************
*功能:将记录添加到链表中
*********************************************************/
void AddRecord(List &SalaryList)
{
Node *pnode;
Salary *psal;
char Num[10];
char Name[20];
double BaseSalary,Bonus;
cout<<"请输入职工号(输入q结束):";
// cin.ignore();
cin.getline(Num,10);
while(strcmp(Num,"q"))
{
cout<<"请输入职工姓名:";
cin.ignore();
cin.getline(Name,20);
cout<<"请输入职务工资:";
cin>>BaseSalary;
cout<<"请输入奖金:";
cin>>Bonus;
cin.ignore();
psal=new Salary;//生成新的数据类对象
psal->SetSalary(Name,BaseSalary,Bonus,Num);//数据类对象初始化
pnode=new Node;//生成新结点
pnode->InputData(psal);//结点初始化
SalaryList.AddNode(pnode);//把结点加入链表
cout<<"输入职工号(输入q结束):";
cin.ignore();
cin.getline(Num,10);
}
cout<<endl<<endl;
}
/****************************************************
*功能:显示职工的工资信息
****************************************************/
void DisplayRecord(List &SalaryList)
{
cout<<setw(10)<<"职工号"<<setw(10)<<"姓名"<<setw(12)<<"职务工资"<<setw(12)<<" 职工奖金"<<setw(12)<<"工资总额"<<endl;
SalaryList.ShowList();
cout<<endl<<endl;
}
/***************************************************
*功能:按照姓名查找工资表数据
***************************************************/
void LookUpRecord(List &SalaryList)
{
Node *p;
char Name[20];
cout<<"输入您需要查找的姓名(输入0结束)";
cin.getline(Name,20);
while(strcmp(Name,"0"))
{
Salary salary(Name,0,0,"\0");//生成结点
p=SalaryList.LookUp(salary);//查找数据
if(p)
{
cout<<"在工资表中找到"<<Name<<",内容是:"<<endl;
p->ShowNode();
}
else
cout<<"\n在工资表中查找不到"<<Name<<"."<<endl<<endl;
cout<<"输入你需要查找的姓名(输入0结束)";
cin.getline(Name,20);
}
cout<<endl<<endl;
system("pause");
}
/************************************************************
*功能:删除指定的数据
*************************************************************/
void DeleteRecord(List &SalaryList)
{
Node *p;
char Name[20];
cout<<"输入你需要删除的职工姓名(输入0结束):";
cin.getline(Name,20);
while(strcmp(Name,"0"))
{
Salary salary(Name,0,0,"\0");
p=SalaryList.LookUp(salary);
//删除时先查找
if(p)
{cout<<"在工资表中找到"<<Name<<",内容是:"<<endl;
p->ShowNode();
SalaryList.DeleteNode(p);
cout<<"的资料已删除."<<endl;
delete p;
}
else
cout<<"\n在工资表中查找不到"<<Name<<"."<<endl<<endl;
cout<<"输入您需要删除的职工姓名(输入0结束):";
cin.getline(Name,20);
}
cout<<endl<<endl;
}
//保存数据到文件中
void SaveRecord(List&SalaryList)
{
char name[20];
cout<<"请输入要保存的文件名:";
cin>>name;
ofstream in;
in.open(name,ios::out);
if(!in)
{
cout<<"数据文件打开错误,不能将数据保存到文件中!\n";
return;
}
Node *pnode;
Salary *psal;
pnode=SalaryList.GetListHead(); //取链表头指针
while(pnode)
{
//返回结点指向的数据指针
psal=(Salary*)pnode->GetData();
//写入数据
// in.write((char*)psal,sizeof(Salary));
in<<psal->Num<<psal->Name<<psal->BaseSalary<<' '<<psal->Bonus<<endl;
//取下一结点指针
pnode=SalaryList.GetListNextNode(pnode);
}
in.close();
}
/*******************************************************
*功能:主菜单选项操作
********************************************************/
void Menu(string& choice,List &SalaryList)
{
if(choice=="1")
AddRecord(SalaryList);
else if(choice=="2")
DisplayRecord(SalaryList);
else if(choice=="3")
LookUpRecord(SalaryList);
else if(choice=="4")
DeleteRecord(SalaryList);
else if(choice=="0")
SaveRecord(SalaryList);
else
cout<<"输入错误,请重新选择\n\n";
}
/***********************************************
*功能:读取文件。
***********************************************/
void LoadFile(List &SalaryList)
{
ifstream infile;
infile.open("SALARY.in",ios::in);
if(!infile)
{
cout<<"没有数据文件!\n\n";
return;
}
Node *pnode;
Salary *psal;
cout<<setw(10)<<"职工号"<<setw(10)<<"姓名"<<setw(12)<<"职务工资"<<setw(12)<<" 职工奖金"<<setw(12)<<"工资总额"<<endl;
while(!infile.eof())
{
//定义新的数据对象
psal=new Salary;
infile.read((char*)psal,sizeof(Salary));
pnode=new Node;
//将数据加入
pnode->InputData(psal);
SalaryList.AddNode(pnode);
cout<<setw(10)<<psal->Num<<setw(10)<<psal->Name<<setw(12)<<psal->BaseSalary<<setw(8)<<psal->Bonus<<setw(12)<<(psal->BaseSalary+psal->Bonus)<<endl;
}//删除头结点
SalaryList.DeleteNode(pnode);
infile.close();
}
int main(void)
{
List SalaryList;//定义一个对象
system("cls");
cout<<"\t^^^^^^欢迎进入工资管理系统^^^^^^^^\n";
LoadFile(SalaryList);
string choice;
do
{
cout<<"\t1.添加工资记录\n";
cout<<"\t2.显示工资记录\n";
cout<<"\t3.根据姓名查找工资数据\n";
cout<<"\t4.根据姓名删除工资数据\n";
cout<<"\t0.退出系统\n\n\n";
cout<<"请输入您的选择:";
cin>>choice;
cin.ignore();
Menu(choice,SalaryList);
}while(choice!="0");
cout<<"\n\n\t欢迎再次使用工资管理系统\n\n";
return 0;
}