#include<cstdio>
#include <cstdio>
#include <windows.h> //一些基本的库函数的头文件
#include <cstring>
#include <iostream> //字符处理头文件
#define N 100
using namespace std;
struct employee //雇员结构体
{
int num; //职工号
char name[20];
char sex;
int age;
char xueli[30];
int jwage; //工资 (基本工资,职务工资,奖金)工资总和
int zwage; //职务工资
int jjwage; // 奖金
int twage; //工资总和
char addr[30];
char tel[20]; }em[100];
//申明
/*定义一个结构体*/
void menu(); //主菜单
void input(); //输入
void save(int); //保存
void display(); //显示
void del(); //删除
void add(); //添加
void search(); //查找
void search_num(); //按职工号
void search_xueli(); //按学历
void search_tel(); //按电话
void modify(); /*定义各函数*/ //修改
void sort(); //排序
int main() {
menu();
int n,flag;
char a;
do{
cout << "请选择你需要操作的步骤(1--7):\n";
cin >> n;
if(n>=1&&n<=7){
flag=1; //标记
break;
}else{
flag=0;
cout << "您输入有误,请重新选择!";
}
}while(flag==0);
while(flag==1){
switch(n)
{
case 1:system("cls");cout << " ◆◆◆输入职工信息◆◆◆\n";cout << endl;input();break;
case 2:system("cls");cout << " ◆◆◆浏览职工信息◆◆◆\n";cout << endl;display();break;
case 3:system("cls");cout << " ◆◆◆按职工号查询职工信息◆◆◆\n";cout << endl;search();break;
case 4:system("cls");cout << " ◆◆◆删除职工信息◆◆◆\n";cout<< endl;del();break;
case 5:system("cls");cout << " ◆◆◆添加职工信息◆◆◆\n";cout << endl;add();break;
case 6:system("cls");cout <<" ◆◆◆修改职工信息◆◆◆\n";cout << endl;modify();break;
case 7:exit(0);break; default :break; } // exit结束
getchar();
cout << endl;
cout << "是否继续进行(y or n):\n";
cin >> a;
if(a=='y') { flag=1; system("cls"); /*清屏*/
menu(); /*调用菜单函数*/
cout << "请再次选择你需要操作的步骤(1--7):\n";
cin >> n;
cout<< endl;
}else
exit(0);
}
}
void menu()
/*菜单函数*/
{
cout << " ************欢迎进入职工信息管理系统**********\n";
cout <<" 1.录入职工信息";
cout <<" 2.浏览职工信息\n";
cout <<" 3.查询职工信息";
cout <<" 4.删除职工信息\n";
cout <<" 5.添加职工信息";
cout <<" 6.修改职工信息\n";
cout <<" 7.退出\n";
cout <<" ********************谢谢使用******************\n";
cout<< endl;
cout<< endl;
}
void input()
/*录入函数*/
{
int i,m;
cout << "请输入需要创建信息的职工人数(1--100):\n";
cin >> m;
for (i=0;i<m;i++)
{
em[i].twage =0;
cout << "请输入职工号: ";
cin >> em[i].num;
cout << "请输入姓名: ";
cin >> em[i].name;
getchar();
cout << "请输入性别(f--女 m--男): ";
cin >> em[i].sex;
cout <<"请输入年龄: ";
cin >> em[i].age;
cout <<"请输入学历: ";
cin >>em[i].xueli;
cout <<"请输入基本工资: ";
cin >>em[i].jwage;
cout <<"请输入职务工资: ";
cin >> em[i].zwage;
cout <<"请输入奖金: ";
cin >> em[i].jjwage;
cout <<"请输入住址: ";
cin >> em[i].addr;
cout <<"请输入电话: ";
cin >> em[i].tel;
em[i].twage= em[i].jwage+em[i].zwage+em[i].jjwage; //总工资
cout <<'\n';
}
cout <<"\n创建完毕!\n";
save(m);
}
void save(int m) /*保存文件函数*/
{
int i;
FILE*fp;
if ((fp=fopen("employee_list","wb"))==NULL) { //文件的保存----项目的根目录
cout << "cannot open file\n"; //这个是按照相对路径来打开的,如果文件不存在就会自动创建一个文件,文件名字叫meployee_list
exit(0);
}
for (i=0;i<m;i++) /*将内存中职工的信息输出到磁盘文件中去*/
if (fwrite(&em[i],sizeof(struct employee),1,fp)!=1) //fwrite
cout <<"file write error\n";
fclose(fp);
}
int load() /*导入函数*/
{
FILE*fp; //文件指针
int i=0;
if((fp=fopen("employee_list","rb"))==NULL) //文件打开
{
cout << "cannot open file\n";
exit(0);
}
else
{
do
{
fread(&em[i],sizeof(struct employee),1,fp);
i++;
}while(feof(fp)==0);
}
fclose(fp); //关闭指针
return(i-1); //返回这个雇员人数
}
void display() /*浏览函数*/
{
int i;
int m=load();
sort();
cout <<" 职工号\t姓名\t\t性别\t\t年龄 \n";
for(i=0;i<m;i++) /*m为输入部分的职工人数*/
cout << em[i].num << em[i].name << em[i].sex << em[i].age;
cout <<"\n 学历\t基本工资\t职务工资\t奖金\t总工资\t住址\t\t电话 \n";
for(i=0;i<m;i++) /*m为输入部分的职工人数*/
cout << em[i].xueli << em[i].jwage << em[i].zwage << em[i].jjwage << em[i].twage << em[i].addr << em[i].tel;
}
void del() /*删除函数*/
{
int m=load();
int i,j,n,t,flag;
char name[20];
cout <<"\n 原来的职工信息:\n";
display();
cout <<endl;
cout <<"请输入要删除的职工的姓名:\n";
cin >> name;
for(flag=1,i=0;flag&&i<m;i++) //先找:按照职工姓名找!找到相同的删除,怎么删除,移动的方法。
{ //移动法
if(strcmp(em[i].name,name)==0)
{
cout <<"\n已找到此人,原始记录为:\n";
cout <<" 职工号\t姓名\t\t性别\t\t年龄 \n";
cout <<em[i].num << em[i].name << em[i].sex << em[i].age;
cout <<"\n 学历\t\t基本工资\t\t职务工资\t\t奖金\t\t总工资\t\t住址\t\t电话 \n";
cout << em[i].xueli << em[i].jwage << em[i].zwage << em[i].jjwage << em[i].twage << em[i].addr << em[i].tel;
cout <<"\n确实要删除此人信息请按1,不删除请按0\n";
cin >> n;
if(n==1) 移动的方法覆盖
{
for(j=i;j<m-1;j++)
{
strcpy(em[j].name,em[j+1].name);
em[j].num=em[j+1].num;
em[j].sex=em[j+1].sex;
em[j].age=em[j+1].age;
strcpy(em[j].xueli,em[j+1].xueli);
em[j].jwage=em[j+1].jwage;
em[j].zwage=em[j+1].zwage;
em[j].jjwage=em[j+1].jjwage;
em[j].twage=em[j+1].twage;
strcpy(em[j].addr,em[j+1].addr);
strcpy(em[j].tel,em[j+1].tel);
}
flag=0;
} ///结束
}
}
if(!flag)
m=m-1;
else
cout << "\n对不起,查无此人!\n";
cout << "\n 浏览删除后的所有职工信息:\n";
save(m);
/*调用保存函数*/
display();
/*调用浏览函数*/
cout << "\n继续删除请按1,不再删除请按0\n";
cin >> t;
switch(t)
{
case 1:del();break;
case 0:break;
default :break;
}
}
void add()/*添加函数*/
{
FILE*fp;
int n;
int count=0;
int i;
int m=load();
cout << "\n 原来的职工信息:\n";
display();
cout << endl;
fp=fopen("emploee_list","a");
cout << "请输入想增加的职工数:\n";
cin >> n;
for (i=m;i<(m+n);i++) {
cout << "\n 请输入新增加职工的信息:\n";
cout << "请输入职工号: ";
cin >> em[i].num;
cout << endl;
cout << "请输入姓名: ";
cin >> em[i].name;
getchar();
cout << "请输入性别(f--女 m--男): ";
cin >> em[i].sex;
cout << "请输入年龄: ";
cin >> em[i].age;
cout << "请输入学历: ";
cin >> em[i].xueli;
cout <<"请输入基本工资: ";
cin >> em[i].jwage;
cout << "请输入职务工资: ";
cin >> em[i].zwage;
cout <<"请输入奖金: ";
cin >> em[i].jjwage;
cout <<"请输入住址: ";
cin >>em[i].addr;
cout <<"请输入电话: ";
cin >>em[i].tel;
cout <<endl;
em[i].twage= em[i].jwage+em[i].zwage+em[i].jjwage;
count=count+1;
cout <<"已增加的人数:\n";
cout <<count;
}
cout <<"\n添加完毕!\n";
m=m+count;
cout <<"\n浏览增加后的所有职工信息:\n";
cout <<endl;
save(m);
sort();
display();
fclose(fp);
}
void search()/*查询函数*/
{
int t,flag;
do {
cout <<"\n按职工号查询请按1; 按学历查询请按2; 按电话号码查询请按3; 进入主函数请按4\n"; cin >> t;
if(t>=1&&t<=4)
{
flag=1;
break;
}
else
{
flag=0;
cout <<"您输入有误,请重新选择!";
}
}while(flag==0);
while(flag==1) {
switch(t){
case 1:cout <<"按职工号查询\n";
search_num();
break;
case 2:cout <<"按学历查询\n";
search_xueli();
break;
case 3:cout <<"按电话号码查询\n";
search_tel();
break;
case 4:main();
break;
default:break;
}
}
}
void search_num() {
int num;
int i,t;
int m=load();
cout <<"请输入要查找的职工号:\n";
cin >> num;
for(i=0;i<m;i++) //遍历 :一个一个找
if(num==em[i].num) {
cout <<"\n已找到此人,其记录为:\n";
cout <<" 职工号\t姓名\t\t性别\t\t年龄 \n";
cout <<em[i].num << em[i].name << em[i].sex << em[i].age;
cout <<"\n 学历\t\t基本工资\t\t职务工资\t\t奖金\t\t总工资\t\t住址\t\t电话 \n";
cout <<em[i].xueli << em[i].jwage << em[i].zwage << em[i].jjwage << em[i].twage << em[i].addr << em[i].tel;
break;
}
if(i==m)
cout <<"\n对不起,查无此人\n";
cout <<endl;
cout <<"返回查询函数请按1,继续查询职工号请按2\n";
cin >> t;
switch(t){
case 1:search();break;
case 2: break;
default:break;
}
}
void search_xueli() {
char xueli[30];
int i,t;
int m=load();
cout <<"请输入要查找的学历:\n";
cin >>xueli;
for(i=0;i<m;i++)
if(strcmp(em[i].xueli,xueli)==0) {
cout <<"\n已找到,其记录为:\n";
cout <<" 职工号\t姓名\t\t性别\t\t年龄 \n";
printf("\n %d\t\t%s\t%c\t\t%d\t\n",em[i].num,em[i].name,em[i].sex,em[i].age);
cout <<"\n 学历\t\t基本工资\t\t职务工资\t\t奖金\t\t总工资\t\t住址\t\t电话 \n";
cout <<em[i].xueli << em[i].jwage << em[i].zwage << em[i].jjwage << em[i].twage << em[i].addr << em[i].tel;
}
if(i==m)
cout <<"\n对不起,查无此人\n";
cout <<endl;
cout <<"返回查询函数请按1,继续查询学历请按2\n";
cin >> t;
switch(t)
{
case 1:search();break;
case 2:break;
default :break;
}
}
void search_tel() {
char tel[20];
int i, t;
int m=load();
cout <<"请输入要查找的电话号码:\n";
cin >>tel;
for(i=0;i<m;i++)
if(strcmp(tel,em[i].tel)==0) {
cout <<"\n已找到此人,其记录为:\n";
cout <<" 职工号\t姓名\t\t性别\t\t年龄 \n";
cout <<em[i].num << em[i].name << em[i].sex << em[i].age;
cout <<"\n 学历\t\t基本工资\t\t职务工资\t\t奖金\t\t总工资\t\t住址\t\t电话 \n";
cout << em[i].xueli << em[i].jwage << em[i].zwage << em[i].jjwage << em[i].twage << em[i].addr << em[i].tel;
break;
}
if(i==m)
cout <<"\n对不起,查无此人\n";
cout <<endl;
cout <<"返回查询函数请按1,继续查询电话号码请按2\n";
cin >> t;
switch(t) {
case 1:search();break;
case 2:break;
default :break;
}
}
void modify() /*修改函数*/ {
int num;
char name[20];
char sex;
int age;
char xueli[30];
int jwage;
int zwage;
int jjwage;
char addr[30];
char tel[20];
int b,c,i,n,t,flag;
int m=load();
cout <<"\n 原来的职工信息:\n";
display();
cout <<endl;
cout <<"请输入要修改的职工的姓名:\n";
cin >>name; //先找这个姓名,找到了相同的,就输入覆盖掉。
for(flag=1,i=0;flag&&i<m;i++) {
if(strcmp(em[i].name,name)==0) {
cout <<"\n已找到此人,原始记录为:\n";
cout <<" 职工号\t姓名\t性别\t\t年龄 \n";
printf("\n %d\t\t%s\t%c\t\t%d\t\n",em[i].num,em[i].name,em[i].sex,em[i].age);
cout <<"\n 学历\t基本工资\t职务工资\t奖金\t总工资\t\t住址\t\t电话 \n";
cout <<em[i].xueli << em[i].jwage << em[i].zwage << em[i].jjwage << em[i].twage << em[i].addr << em[i].tel;
cout <<"\n确实要修改此人信息请按1 ; 不修改请按0\n";
cin >> n;
if(n==1) {
cout <<"\n需要进行修改的选项\n 1.职工号 2.姓名 3.性别 4.年龄 5.学历 6.基本工资 7.职务工资 8.奖金 9.住址 10.电话\n";
cout <<"请输入你想修改的那一项序号:\n";
cin >> c;
if(c>10||c<1)
cout <<"\n选择错误,请重新选择!\n";
}
flag=0;
}
}
if(flag==1)
cout <<"\n对不起,查无此人!\n";
do {
switch(c) /*因为当找到第i个职工时,for语句后i自加了1,所以下面的应该把改后的信息赋值给第i-1个人*/
{
case 1:cout <<"职工号改为: ";
cin >> num;
em[i-1].num=num;
break;
case 2:cout <<"姓名改为: ";
cin >>name;
strcpy(em[i-1].name,name);
break;
case 3:cout <<"性别改为: ";
getchar();
cin >> sex;
em[i-1].sex=sex;
break;
case 4:cout <<"年龄改为: ";
cin >> age;
em[i-1].age=age;
break;
case 5:cout <<"学历改为: ";
cin >>xueli;
strcpy(em[i-1].xueli,xueli);
break;
case 6:cout <<"基本工资改为: ";
cin >> jwage;
em[i-1].jwage=jwage;
break;
case 7:cout <<"职务工资改为: ";
cin >> zwage;
em[i-1].zwage=zwage;
break;
case 8:cout <<"奖金改为: ";
cin >> jjwage;
em[i-1].jjwage=jjwage;
break;
case 9:cout <<"住址改为: ";
cin >> addr;
strcpy(em[i-1].addr,addr);
break;
case 10:cout << "电话改为: ";
cin >> tel;
strcpy(em[i-1].tel,tel);
break;
}
em[i-1].twage= em[i-1].jwage+em[i-1].zwage+em[i-1].jjwage;
cout << endl;
cout << "\n是否确定所修改的信息?\n 是 请按1 ; 不,重新修改 请按2: \n";
cin >> b;
}
while(b==2);
cout << "\n浏览修改后的所有职工信息:\n";
cout << endl;
save(m);
display();
cout << "\n继续修改请按1,不再修改请按0\n";
cin >> t;
switch(t) {
case 1:modify();break; case 0:break; default :break;
}
}
//冒泡法排序
void sort(){
int m= load();
int num;
char name[20];
char sex;
int age;
char xueli[30];
int jwage;
int zwage;
int jjwage;
int twage;
char addr[30];
char tel[20];
for(int i=0;i< m;i++){
for(int j =0;j<m -i-1;j++){
if(em[j].twage>em[j+1].twage){ //交换
strcpy(name,em[j].name);
strcpy(em[j].name,em[j+1].name);
strcpy(em[j+1].name,name);
num=em[j].num;
em[j].num=em[j+1].num;
em[j+1].num=num;
age =em[j].age;
em[j].age=em[j+1].age;
em[j+1].age=age;
strcpy(xueli,em[j].xueli);
strcpy(em[j].xueli,em[j+1].xueli);
strcpy(em[j+1].xueli,xueli);
jwage=em[j].jwage;
em[j].jwage=em[j+1].jwage;
em[j+1].jwage=jwage;
zwage=em[j].zwage;
em[j].zwage=em[j+1].zwage;
em[j+1].zwage=zwage;
jjwage=em[j].jjwage;
em[j].jjwage=em[j+1].jjwage;
em[j+1].jjwage=jjwage;
twage=em[j].twage;
em[j].twage=em[j+1].twage;
em[j+1].twage=twage;
strcpy(addr,em[j].addr);
strcpy(em[j].addr,em[j+1].addr);
strcpy(em[j+1].addr,addr);
strcpy(tel,em[j].tel);
strcpy(em[j].tel,em[j+1].tel);
strcpy(em[j+1].tel,tel);
}
}
}
}
管理系统c++
最新推荐文章于 2023-02-24 18:58:42 发布