改错

来源:互联网 发布:光纤网络转换器 编辑:程序博客网 时间:2024/05/06 03:03

#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

const char *filename = "paid.txt";
class EmployeeData
{
public:
 EmployeeData():id(0),name(),salary(0.0),allowance(0.0),bonus(0.0),deduction(0.0){}
 void Input();
 void Print();
 void Save() const;
 void Load(ifstream& fin);

 inline int Id() const{return id;}
 inline string Name() const{return name;}
 inline float Salary() const{return salary;}
 inline float Allowance() const{return allowance;}
 inline float Bonus() const{return bonus;}
 inline float Deduction() const{return deduction;}
 inline float NetPay() const
 {return (salary+allowance+bonus-deduction);}

private:
 int id;   // 职工号
 string name; // 姓名
 float salary; // 基本工资
 float allowance; // 补贴
 float bonus; // 奖金
 float deduction;// 扣除金额
};

void EmployeeData::Input()
{
 cout<<"输入职工号:";
 cin >> id;
 cout<<"输入姓名:";
 cin >> name;
 cout<<"输入基本工资:";
 cin >> salary;
 cout<<"输入补贴金额:";
 cin >> allowance;
 cout<<"输入奖励金额:";
 cin >> bonus;
 cout<<"输入扣除金额:";
 cin >> deduction;
}

void EmployeeData::Print()
{
 cout<<"职工号: /t"<< id<<endl;
 cout<<"姓名:   /t"<< name<<endl;
 cout<<"基本工资:/t"<< salary<<endl;
 cout<<"补贴金额:/t"<< allowance<<endl;
 cout<<"奖励金额:/t"<< bonus<<endl;
 cout<<"扣除金额:/t"<< deduction<<endl;
 cout<<"实发金额:/t"<< NetPay()<<endl;
}

void EmployeeData::Save() const
{
 ofstream fout;
 fout.open(filename, ios::app);
 fout << id <<'/t' << name<<'/t' << salary<<'/t' <<allowance<<'/t'
  << bonus<<'/t' << deduction<<'/t'<< NetPay()<<'/n';
 fout.close();
}
void EmployeeData::Load(ifstream& fin)
{
 //ifstream fin(filename);
 int netpay(0);
 fin >> id >> name >> salary >>allowance >> bonus >> deduction >> netpay;
 fin.close();
}

class DataManager
{
public:
 DataManager(){entries.clear();}
 ~DataManager(){entries.clear();}
 //菜单
 void DisplayMenu();

private:
 // 录入
 void Input();
 //查询
 void Search();
 //统计
 void Stat();
 //默认构造函数
 EmployeeData Find(int id) const;
 //添加
 void Append(EmployeeData entry);
 //保存
 void Save() const;
 //读取
 void Load();

private:
 vector<EmployeeData> entries;
};

void DataManager::Input()
{
 cout << "[职工信息录入]/n请输入员工信息"<<endl;
 EmployeeData entry;
 entry.Input();
 Append(entry);
 cout << endl;
}

void DataManager::Search()
{
 cout << "[信息查询]/n请输入员工的职工号:"<<endl;
 int id(0);
 cin >> id;
 EmployeeData found=Find(id);
 if(found.Id() == 0 && found.Name()=="")
 {
  cout << "无此员工号:"  << id<< endl;
 } else {
  found.Print();
 }
 cout << endl;
}

void DataManager::Stat()
{
 float s(0.0);
 float a(0.0);
 float b(0.0);
 float d(0.0);
 float p(0.0);
 unsigned int count(entries.size());
 for(unsigned int i(0);i<count;i++)
 {
  s+=entries.at(i).Salary();
  a+=entries.at(i).Allowance();
  b+=entries.at(i).Bonus();
  d+=entries.at(i).Deduction();
  p+=entries.at(i).NetPay();
 }
 cout << "    /t/t总金额/t平均金额"<<endl;
 cout << "----------------------------------"<<endl;
 cout << "基本工资/t"<<s<<"/t"<<s/count<<endl;
 cout << "补贴金额/t"<<a<<"/t"<<a/count<<endl;
 cout << "奖励金额/t"<<b<<"/t"<<b/count<<endl;
 cout << "扣除金额/t"<<d<<"/t"<<d/count<<endl;
 cout << "实发工资/t"<<p<<"/t"<<p/count<<endl;
 cout << endl;
}

EmployeeData DataManager::Find(int id) const
{
 EmployeeData res;
 for(unsigned int i(0);i<entries.size();i++)
 {
  if(entries.at(i).Id() == id)
  {
   res = entries.at(i);
   break; // for
  }
 }
 return res;
}
 
void DataManager::Append(EmployeeData entry)
{
 entries.insert(entries.end(), entry);
}

void DataManager::Save() const
{
 ofstream fout;
 fout.open(filename, ios::app );
 fout.clear(); // 重新保存
 fout << entries.size() <<'/t'<< endl; //保存总数
 fout.close();
 for(unsigned int i(0);i<entries.size();i++)
 {
  entries.at(i).Save();
 }
}

void DataManager::Load()
{
 ifstream fin(filename);
 unsigned int count(0);
 fin >> count;
 
 entries.clear();
 for(unsigned int i(0);i<count;i++)
 {
  EmployeeData entry;
  entry.Load(fin);
  Append(entry);
 }
 fin.close();
}

void DataManager::DisplayMenu()
{
 int opt(0);
 Load();
 do
 {
  cout << "1.职工信息录入"<<endl;
  cout << "2.信息查询"<<endl;
  cout << "3.工资统计"<<endl;
  cout << "4.退出"<<endl;
  cin >> opt;
  switch(opt)
  {
  case 1:
   Input();
   Save();
   break;
  case 2:
   Search();
   break;
  case 3:
   Stat();
   break;
  }
 } while (opt!=4);
}

int main()
{
    DataManager dm;
 
 dm.DisplayMenu();
    return 0;
}

原创粉丝点击