charla_charla
Charlotte.Lee
2019-05-31 15:10

想实现恢复信息的功能,但是有时只能恢复最近删除的一个或是不恢复,并且搜索不存在的信息时会返回乱码

employee.h

#include "employee.h"
#include <iostream>
using namespace std;
#include<stdio.h>
#include<string>
#include <iomanip>
#include <stdlib.h>

employee::employee()
{
}


employee::~employee()
{
}

void employee::insert()
{
    cout << "请输入职工姓名" << endl;
    cin>>name;
    cout << "请输入职工编号" << endl;
    cin >> number;
    cout << "请输入职工基本工资" << endl;
    cin >> basic_salary;
    cout << "请输入职工职务工资" << endl;
    cin >> job_salary;
    cout << "请输入职工岗位津贴" << endl;
    cin >> wage_salary;
    cout << "请输入职工五险一金" << endl;
    cin >> Wuxianyijin;
    total_salary = basic_salary + job_salary + wage_salary;



}

ManageSystem::ManageSystem()
{
    LoadInformationToSys();
}

ManageSystem::~ManageSystem()
{
    LoadInformationToFile();
    for (size_t i = 0; i < GetSize(); i++) {
        delete _employee[i];
        _employee[i] = NULL;
    }
}

void ManageSystem::Add(employee * temp)
{
    if (temp == NULL)return;
    employee* temp1 = new employee();
    *temp1 = *temp;
    _employee.push_back(temp1);


    cout << setw(8) << "|"  << "name" << setw(8) << "number" << setw(14) << "basic_salary" << setw(14) << "job_salary" << setw(14) << "wage_salary" << setw(14) << "Wuxianyijin" << setw(14) << "total_salary"  << "|" << endl;
    cout << setw(8) << "|" << temp1->name << setw(8) << temp1->number << setw(14) << temp1->basic_salary << setw(14) << temp1->job_salary << setw(14) << temp1->wage_salary << setw(14) << temp1->Wuxianyijin<< setw(14) << temp1->total_salary<< "|" << endl;


}

size_t ManageSystem::GetSize()
{
    return _employee.size();
}

bool ManageSystem::Delete(int & number)
{
    int ret = -1;
    ret = FindNumber(number);
    if(ret==-1)return false;//若没找到该编号,直接返回
    delete _employee[ret];
    _employee[ret] = NULL;
    DeletedNumber.push_back(ret+1);     //把已删除的职工序号存入DeletedNumber以便恢复




    for (size_t i = ret; i < _employee.size() - 1; i++)
    {
        _employee[i] = _employee[i + 1];
    }
    _employee.pop_back();
    return true;


}

void ManageSystem::Statistics(int* data)
{

    int tobasic_salary=0;
    int tojob_salary=0;
    int towage_salary=0;
    int toWuxianyijin=0;
    int tototal_salary=0;
    int count1=0, count2=0, count3=0;
    float per1, per2, per3;
    size_t i = 0;
    for (i; i < GetSize(); i++) {
        tobasic_salary += _employee[i]->basic_salary;
        tojob_salary += _employee[i]->job_salary;
        towage_salary += _employee[i]->wage_salary;
        toWuxianyijin += _employee[i]->Wuxianyijin;
        tototal_salary += _employee[i]->total_salary;
        if (_employee[i]->total_salary > 7000)count1++;
        else if (_employee[i]->total_salary >= 6000 && _employee[i]->total_salary <= 7000)count2++;
        else if (_employee[i]->total_salary < 6000)count3++;

    }
    per1 = 1.0*count1 / i*100;
    per2 = 1.0* count2 / i * 100;
    per3 = 1.0*count3 / i * 100;
    data[0] = tobasic_salary / i;
    data[1] = tojob_salary / i;
    data[2] = towage_salary / i;
    data[3] = toWuxianyijin / i;
    data[4] = tototal_salary / i;
    data[5] = count1;
    data[7] = count2;
    data[9] = count3;
    data[6] = int(per1);
    data[8] = int(per2);
    data[10] = int(per3);


}

bool ManageSystem::Getinformation(int & num, employee ** ttemp)
{
    if (!ttemp) {

        return false; }
    int index = FindNumber(num);
    if (index != -1) {     //查找成功
        **ttemp = *(_employee[index]);//赋值
    return true;}
    else {
        return false; 
        cout << "查找失败" << endl;
    }

}



void ManageSystem::GetNumber(int * a)
{
    size_t i = 0;
    for (i; i < GetSize(); i++) {
        a[i] = _employee[i]->number;
    }

}

void ManageSystem::Changename(int  &num,string & newone)
{
    _employee[FindNumber(num)]->name = newone;
    cout << "修改成功" << endl;
    return;
}

void ManageSystem::Changenumber(int & choice,int & num, int & newone)
{
    if (choice == 2)_employee[FindNumber(num)]->basic_salary = newone;
    if (choice == 3)_employee[FindNumber(num)]->job_salary = newone;
    if (choice == 4)_employee[FindNumber(num)]->wage_salary = newone;
    if (choice == 5)_employee[FindNumber(num)]->Wuxianyijin = newone;
    _employee[FindNumber(num)]->total_salary = _employee[FindNumber(num)]->basic_salary + _employee[FindNumber(num)]->job_salary + _employee[FindNumber(num)]->wage_salary;
    cout << "修改成功" << endl;
    return;
}



size_t ManageSystem::FindNumber(int & num)
{
    for (size_t i = 0; i < _employee.size(); i++) {
        if (num == _employee[i]->number) {
            return i;
        }
    }
    return -1;//找不到返回-1
}

string i_Readpart(char*buf, int& index) {     //index记录所筛选到的位置,每次只读取一串字符串
    string stri = "";
    char strTemp[64] = "";
    int i = 0;
    while (buf[index] != ' '&&buf[index] != '\n'&&buf[index] != '\0')
    {
        strTemp[i++] = buf[index]; //从文件中读取的一行字符串中筛选出有效信息
        index++;
    }
    if (buf[index] == ' ')index++;//遇到空格跳过
    stri = strTemp;
    return stri;


}

void i_writepart(vector<employee*>& employ, FILE* fout, int indx)
{
    string str = "";
    char number[20] = { 0 };
    char basic_salary[20] = { 0 };
    char job_salary[20] = { 0 };
    char wage_salary[20] = { 0 };
    char Wuxianyijin[20] = { 0 };
    char total_salary[20] = { 0 };
    _itoa(employ[indx]->number, number, 10);//把整数按十进制转换为字符串
    _itoa(employ[indx]->basic_salary, basic_salary,10);
    _itoa(employ[indx]->job_salary, job_salary,10);
    _itoa(employ[indx]->wage_salary, wage_salary,10);
    _itoa(employ[indx]->Wuxianyijin, Wuxianyijin,10);
    _itoa(employ[indx]->total_salary, total_salary,10);
    str += employ[indx]->name;
    str += " ";
    str += number;
    str += " ";
    str += basic_salary;
    str += " ";
    str += job_salary;
    str += " ";
    str += wage_salary;
    str += " ";
    str += Wuxianyijin;
    str += " ";
    str += total_salary;
    str += "\n";                      //以上是把一个职工的信息转换为标准形式

    fputs(str.c_str(), fout);           //把标准形式的信息放入文件中
}


void ReadFile(vector<employee*>&_employee, char*buf, int index) {
    employee* temp = NULL;
        temp=new employee();
    string strtemp = "";
    strtemp = i_Readpart(buf, index);
    if (!strtemp.empty())//非空则成立
        temp->name = strtemp;
    strtemp = i_Readpart(buf, index);
    if (!strtemp.empty())
    {
        int num = atoi(strtemp.c_str()); temp->number = num;//转换字符串为整型数,赋值
    }
    strtemp = i_Readpart(buf, index);
    if (!strtemp.empty())
    {
        int basic_sa = atoi(strtemp.c_str()); temp->basic_salary = basic_sa;
    }
    strtemp = i_Readpart(buf, index);
    if (!strtemp.empty())
    {
        int job_sa = atoi(strtemp.c_str()); temp->job_salary = job_sa;
    }
    strtemp = i_Readpart(buf, index);
    if (!strtemp.empty()) 
    {
        int wage_sa = atoi(strtemp.c_str()); temp->wage_salary = wage_sa;
    }
    strtemp = i_Readpart(buf, index);
    if (!strtemp.empty())
    {
        int Wuxianyi = atoi(strtemp.c_str()); temp->Wuxianyijin = Wuxianyi;
    }
    strtemp = i_Readpart(buf, index);
    if (!strtemp.empty())
    {
        int total_sa = atoi(strtemp.c_str()); temp->total_salary = total_sa;
    }
    _employee.push_back(temp);

}


bool ManageSystem::Save()
{
    if (DeletedNumber.empty())return false;
    else
    {
        size_t i = 0;
        pFile = fopen("D:\\employee.txt", "r");//打开文件
        assert(pFile);
        char buf[128];
        int index = 0;

        vector<int>::iterator r_iter;
        for (r_iter = DeletedNumber.begin(); r_iter != DeletedNumber.end(); ++r_iter) {
            int curline = 0;

            while (!feof(pFile))
            {
                index = 0;
                memset(buf, 0, sizeof(buf));
                fgets(buf, sizeof(buf), pFile);
                curline++;


                if (curline == *r_iter) {      //只有所读到行数等于已删除行才会被读取进vector中
                    ReadFile(_employee, buf, index);

                    break;
                }

            }
        }
        if (fclose(pFile) != 0) {
            cout << "关闭失败" << endl;
            return false;
        }
    }


    return true;
}

void ManageSystem::LoadInformationToSys()
{
    pFile = fopen("D:\\employee.txt", "r");//打开文件
    assert(pFile);
    char buf[128];
    int index = 0;

    while (!feof(pFile)) {
        index = 0;
        memset(buf, 0, sizeof(buf));
        fgets(buf, sizeof(buf), pFile);//从文件中读取字符串,每次只读取一行直至文件末尾
        if(!feof(pFile))
        ReadFile(_employee, buf, index);//每次读取一个职工的信息

    }
    if (fclose(pFile) != 0) {
        cout << "关闭失败" << endl;
        return;
    }
}

void ManageSystem::LoadInformationToFile()
{
    FILE* fout = fopen("D:\\employee.txt", "w");
    size_t i = 0;
    while (i < GetSize())
    {
        i_writepart(_employee, fout, i);
        i++;
    }
    fclose(fout);
}

main

#include<stdio.h>
#include<vector>
#include <iostream>
#include "employee.h"
#include<string>
using namespace std;
#include <iomanip>
#include <stdlib.h>




void Menu();


void Save(ManageSystem &s) {
    cout << "请问是否恢复已删除职工信息? 1.是;2.否" << endl;
    int ch;
    cin >> ch;
    if (ch == 2)return;
    else {
        if(s.Save())cout<<"恢复成功";
        else cout << "恢复失败";

    }

}




void Addemployee(ManageSystem & s) {
    employee *temp=new employee;
    int add_number;
    cout << "请输入添加数量" << endl;
    cin >> add_number;
    if (cin.fail())cout << "输入的不是数字!" << endl;
    else {
        int i = 0;

        for (i; i < add_number; i++) {
            cout << "请输入第" << i + 1 << "个职工信息" << endl;
            temp->insert();
            s.Add(temp);

        }
    }

    delete temp;
    temp = NULL;


}



void Delete(ManageSystem &s) {
    getchar();
    int num;
    cout << "请输入要删除的职工编号" << endl;
    cin >> num;
    if (s.Delete(num) == true) {
        cout << "删除成功。" << endl;

    }
    else cout << "你所要删除的职工编号不存在。" << endl;
}

void Printed(bool ret,employee* temp) {
    if(temp==NULL ) {
        cout << "查无此人。" << endl;//查找结果
        return;
    }
    if(ret==true)cout << setw(8) << "|" << temp->name << setw(8) << temp->number << setw(14) << temp->basic_salary << setw(14) << temp->job_salary << setw(14) << temp->wage_salary << setw(14) << temp->Wuxianyijin << setw(14) << temp->total_salary << "|" << endl;


}


void Print_i(ManageSystem& s, int& num) {
    employee* temp=new employee();
    bool ret = s.Getinformation(num, &temp);
    if (ret == true) {

        Printed(ret,temp);
        return;
    }
    else {
        cout << "输入错误" << endl;
        return;
    }
    delete temp;
    temp = NULL;
}

void Printwhole(ManageSystem& s) {
    size_t i = 0;
    int*a = new int[s.GetSize()];
    s.GetNumber(a);
    cout << setw(8) << "|" << "name" << setw(8) << "number" << setw(14) << "basic_salary" << setw(14) << "job_salary" << setw(14) << "wage_salary" << setw(14) << "Wuxianyijin" << setw(14) << "total_salary" << "|" << endl;
    while (i < s.GetSize()) {
        Print_i(s, a[i]);
        i++;
    }
    delete a;
    a = NULL;
}

void Satistics(ManageSystem& s) {
    int data[11];
    s.Statistics(data);
    Printwhole(s);
    cout << endl << "Average basic_salary :" << data[0] << endl << "Average job_salary :" << data[1] << endl << "Average wage_salary :" << data[2] << endl << "Average Wuxianyijin :" << data[3] << endl << "Average total_salary :" << data[4] << endl;
    cout << endl <<"Total salary more than 7000 :" << setw(8) << data[5] << setw(8) << data[6] <<"%"<< endl;
    cout << "Total salary from 6000 to 7000 :" << setw(8) << data[7] << setw(8) << data[8] << "%" << endl;
    cout<< "Total salary less than 6000 :" << setw(8) <<data[9] << setw(8) <<data[10]<< "%" << endl;



}

void Change(ManageSystem&s) {
    cout << "请输入要修改的员工的编号: " << endl;
    int num;
    cin >> num;
    Print_i(s, num);
    employee* temp=new employee();
    string name0;
    bool ret = s.Getinformation(num, &temp);
    if (temp == NULL||ret==false) {
        return;
    }
    else {
        int choice;
        cout << endl;
        cout << "请选择要修改的内容: " << endl;
        cout << "1.姓名  2.基本工资  3.职务工资  4.岗位津贴  5.五险一金" << endl;
        cin >> choice;
        switch (choice) {
        case 1:cout << "请输入你要修改的姓名 :";
            cin >> name0;
            s.Changename(num, name0); break;
        case 2:
        case 3:
        case 4:
        case 5:cout << "请输入你要修改的数据 :";
            int newone;
            cin >> newone;
            s.Changenumber(choice, num, newone); break;
        default:cout << "输入错误,修改失败!" << endl; break;
        }


    }
    delete temp;
    temp = NULL;

}

void Search(ManageSystem& s) {
    cout << "请输入要查找的员工的编号:" << endl;
    int numb;
    cin >> numb;
    Print_i(s, numb);

}


int main() {
    ManageSystem s;

    while (1)
    {
        string key="8207181706";
        string in;
        cout << "               " << "请输入密码:" << endl;
        cin >> in;

        if (in == key) {
            cout << "输入正确!" << endl;
                break;
        }
        else { cout << "输入错误!" << endl; }
        getchar();
    }


    while (1) {
        int n;
        Menu();
        cin >> n;
        if (n > 6 || n < 1) break;
        switch (n)
        {
        case 1:Addemployee(s); break;
        case 2:Search(s); break;
        case 3:Change(s); break;
        case 4:Delete(s); break;
        case 5:Save(s); break;//恢复数据
        case 6:Satistics(s); break;
        default:
            break;
        }

    }
    cin.get();
    return 0;
}

void Menu() {

    cout << endl;
    cout << endl;
    cout << "-------------------------------------" << endl;
    cout << "           欢迎来到职工管理系统     " << endl;
    cout << "-------------------------------------" << endl;
    cout << "                  菜单                      " << endl;
    cout << "                1.添加                  " << endl;
    cout << "                2.查询                  " << endl;
    cout << "                3.修改                  " << endl;
    cout << "                4.删除                  " << endl;
    cout << "                5.恢复                 " << endl;
    cout << "                6.统计                  " << endl;   
    cout << "-------------------------------------" << endl;
    cout << "   注意:退出系统后将无法恢复,自动保存!       " << endl;
    cout << endl;
    cout << "         输入1~6以外的数字将退出          " << endl;
    cout << endl;
    cout << "               请选择功能                " << endl;
    cout << endl;






}







  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

为你推荐