职工管理系统完整

tech2022-08-29  118

#include #include using namespace std; #include

//------------------------------------------------------------------- //菜单 void caidan() { cout << “******************************************************” << endl; cout << “***************欢迎使用职工管理系统” << endl; cout << “0.退出管理程序” << endl; cout << “1.增加职工信息” << endl; cout << “2.显示职工信息” << endl; cout << “3.删除离职职工” << endl; cout << “4.修改职工信息” << endl; cout << “5.查找职工信息” << endl; cout << “6.按照编号排序” << endl; cout << “7.清空所有文档” << endl; cout << endl; } //-------------------------------------------------------------------

class zhigong { public: //有编号 姓名 职位等级 int bianhao; string name; int lv;

}; //------------------------------------------------------------------- //------------------------------------------------------------------- //输入职工信息 zhigong z[999]; int num = 0;//公用方便其他函数调用 //------------------------------------------------------------------- void zengjia() {

int a2; cout << "1.员工" << endl; cout << "2.经理" << endl; cout << "3.老板" << endl; cout << "请选择" << endl; cin>>a2; if (a2 == 1) { cout << "输入职工编号:"; cin >> z[num].bianhao; cout << endl; cout << "输入职工姓名:"; cin >> z[num].name; cout << endl; z[num].lv = 1; } if (a2 == 2) { cout << "输入职工编号:"; cin >> z[num].bianhao; cout << endl; cout << "输入职工姓名:"; cin >> z[num].name; cout << endl; z[num].lv = 2; } if (a2 == 3) { cout << "输入职工编号:"; cin >> z[num].bianhao; cout << endl; cout << "输入职工姓名:"; cin >> z[num].name; cout << endl; z[num].lv = 3; } num++;

} //------------------------------------------------------------------- void baocun()//保存职工信息 {

fstream ofs; ofs.open("zhigong.txt", ios::out); for (int o = 0; o < 999; o++) { if(z[o].lv>=1&&z[o].lv<=3) ofs << z[o].bianhao << " " << z[o].name << " " << z[o].lv << endl; }

}

//------------------------------------------------------------------- //初始化 void chushihua() {

fstream f; f.open("zhigong.txt", ios::in);//读文件 //1.文件不存在 if (!f.is_open()) { cout << "文件不存在" << endl; f.close(); return; } //2.文件存在但为空 char ch; f >> ch; if (f.eof()) { //文件为空 cout << "文件为空" << endl; f.close(); } //3.文件存在,且有数据 fstream s; s.open("zhigong.txt", ios::in); int bianhao; string name; int lv; while (s >> bianhao && s >> name && s >> lv) { z[num].bianhao = bianhao; z[num].name = name; z[num].lv = lv; num++; }

} //------------------------------------------------------------------- void xianshi()//显示职工信息 { fstream f; f.open(“zhigong.txt”, ios::in); int bianhao; string name; int lv; while (f >> bianhao && f >> name &&f>>lv) { cout << “编号:” << bianhao << " 姓名:" << name << " 等级:" << lv << endl; } f.close(); } //------------------------------------------------------------------- void shanchu()//删除 { cout << “请输入要删除离职人员的(编号)或(姓名)” << endl; int a;//选择按钮

int bianhaoxz;//供自己输入的去对比文件输入相同来用的变量 string namexz; int bianhao; string name; int lv; cout << "请输入编号" << endl; cin >> bianhaoxz;//选择按钮 int num1 = 0; int num2 = 0;//num2测他现在有多少行我需要知道他现在一空有多少个存放数据的数组 fstream f; f.open("zhigong.txt", ios::in);//只读 while (f >> bianhao && f >> name&&f>>lv) //读这个文件里的内容 有的读就符合条件 { //因为上面我写下来 文件夹的第一行肯定是数组[0] num1++; //num1代表第num1条 如果要是想拿数组就是num1-1 if (bianhaoxz == bianhao) { break;//退出循环 } } f.close(); //开始删掉 删掉后面的数组全部要+1顶上来位置不是会造成一个空的行 for (num1; num1 < 999; num1++) { z[num1-1] = z[num1]; //假如是数组3要被删了 不造成空口话 直接用数组[4]把他覆盖掉 然后4去了3的位子 //5就去接替4的位子后面也是这样 直到他的lv不等于1|2|3 这三个数其中一个就是没有了 //还有一种情况如果是数组存放数据 最后一个数组 那他就无法+1 他无法被一个空的东西赋值 if (num1 == num) { z[num1].lv = 0; //这样他不就不可已进入重写就可以变成删除了 } if (z[num1-1].lv != 1 &&z[num1-1].lv != 2&&z[num1-1].lv != 3) //当lv不是1.2.3这三个数其中之一 { break; } } //这个时候再重新录入文件把之前的文件给覆盖掉 fstream ofs; ofs.open("zhigong.txt", ios::out); for (int o = 0; o < 999; o++) { if (z[o].lv >= 1 && z[o].lv <= 3) { ofs << z[o].bianhao << " " << z[o].name << " " << z[o].lv << endl; } }

} //-------------------------------------------------------------------

//修改职工信息 void xiugai() { int num2 = 0; int bianhao; string name; int lv; int bianhaoxz; cout << “请输入要修改员工的编号” << endl; cin >> bianhaoxz; fstream f; f.open(“zhigong.txt”, ios::in);//读

while (f >> bianhao && f >> name && f >> lv) //获取了编号的行数 他的[数组] =行数-1 == num2-1 { num2++; if (bianhaoxz == bianhao) { break; } } string a; f.close(); cout << "请输入新的编号:"; cin >> z[num2 - 1].bianhao; cout << endl; cout << "请输入新的姓名:"; cin >> z[num2 - 1].name; cout << endl; cout << "请输入他的新职位:"; cin >> a; if (a == "员工") { z[num2 - 1].lv = 1; } else if (a == "经理") { z[num2 - 1].lv = 2; } else if (a == "老板") { z[num2 - 1].lv = 3; } //这个时候再重新录入文件把之前的文件给覆盖掉 fstream ofs; ofs.open("zhigong.txt", ios::out); for (int o = 0; o < 999; o++) { if (z[o].lv >= 1 && z[o].lv <= 3) { ofs << z[o].bianhao << " " << z[o].name << " " << z[o].lv << endl; } }

} //-------------------------------------------------------------------

//查找员工 void chazhao() { int a; cout << “请输入员工编号” << endl; cin >> a;

int bianhao; string name; int lv; fstream f; f.open("zhigong.txt", ios::in); while (f >> bianhao && f >> name && f >> lv) { if (a==bianhao) { cout << "编号:" << bianhao; cout << " 姓名:" << name << " 等级:" << lv << endl; } } f.close();

}

//------------------------------------------------------------------------------------------------- //按照编号排序 void paixu() { cout << num << endl; for (int a = 0; a < num - 1; a++) { for (int b = 0; b < num-a-1; b++) { if (z[b].bianhao > z[b + 1].bianhao) { class zhigong temp; temp = z[b]; z[b] = z[b + 1]; z[b + 1] = temp; } } }

//这个时候再重新录入文件把之前的文件给覆盖掉 fstream ofs; ofs.open("zhigong.txt", ios::out); for (int o = 0; o < 999; o++) { if (z[o].lv >= 1 && z[o].lv <= 3) { ofs << z[o].bianhao << " " << z[o].name << " " << z[o].lv << endl; } } ofs.close();

} //--------------------------------------------------------

//清空文档 void qingkong() { //这个时候再重新录入文件把之前的文件给覆盖掉

for (int a = 0; a < 999; a++) { z[a].lv = 0; //直接把全部lv等于0 if (a == num - 1) { break; } } //重新写入覆盖 因为lv在1-3才能传入数据 等于0直接等于覆盖个空的 fstream ofs; ofs.open("zhigong.txt", ios::out); for (int o = 0; o < 999; o++) { if (z[o].lv >= 1 && z[o].lv <= 3) { ofs << z[o].bianhao << " " << z[o].name << " " << z[o].lv << endl; } } ofs.close();

}

//------------------------------------------------------------------- int main() {

//初始化 chushihua();

//----------------------------------------------------------- int A;//选择 while(1) { //菜单 caidan(); cin >> A;//选择 //------------------------------------------------------------ //0.退出管理程序 if (A == 0) { cout << “欢迎下次使用” << endl; system(“pause”); exit(0);//退出函数 } //--------------------------------------------------------------- //1.增加职工信息 if (A == 1) { zengjia(); //输入职工信息 cout << “添加成功” << endl; baocun();//保存职工信息

};

//-------------------------------------------------------------------------- if (A==2)//2.显示职工信息 { xianshi(); } //-------------------------------------------------------------------------- if (A == 3)//删除离职职工 { shanchu(); }

//-------------------------------------------------------------------------- //4.修改职工信息 if (A == 4) { xiugai(); }

//5.查找职工信息 if (A == 5) { chazhao(); } //6.按照编号排序 if (A == 6) { paixu(); } //7.清空文档 if (A == 7) { qingkong(); } } system("pause"); return 0;

}

//num=最大行数

最新回复(0)