工资管理系统

在这里插入图片描述

#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;

typedef struct _Staff
{
	int no;//工号
	char name[20];//姓名
	float salary;//工资
}Staff;

class Menu//类
{
private:
	int NUM;//记录人数
	Staff sta[100];//最多记录100人
	int chioce;//用户选择
public:
	void Add_Sta_Info();//添加
	void menu();//菜单
	void Run();//循环运行
	void Del_Sta_Info();//删除
	void Dis_Sta_Info();//显示
	void sort();//按工号排序
	void Modify_Sta_Info();//修改
	void Load_File();//读取文件
	void Save_File();//保存文件
	void Sreach_Sta_Info();//范围查找
};

void Menu::Save_File()
{
	ofstream fp("Info.dat",ios::binary);
	for(int i=0;i<NUM;i++)//循环  把每一个员工信息写入文件
	{
		fp.write((char *)&sta[i],sizeof(Staff));
	}	
}

void Menu::Load_File()
{
	ifstream fp("Info.dat",ios::binary);
	NUM=0;
	while(fp.read((char *)&sta[NUM],sizeof(Staff)))//读取文件 每次一个员工信息
	{
		NUM++;//记录人数
	}
}

void Menu::sort()
{
	Staff temp;
	for(int i=0;i<NUM;i++)
	{
		for(int j=i+1;j<NUM;j++)//按工号小到大 排序
		{
			if(sta[i].no>sta[j].no)
			{
				temp=sta[i];
				sta[i]=sta[j];
				sta[j]=temp;
			}
		}
	}
}

void Menu::Modify_Sta_Info()
{
	system("cls");
	int mod_no;
	cout<<"请输入要修改的工号:";
	cin>>mod_no;

	for(int i =0;i<NUM;i++)
	{
		if(sta[i].no == mod_no)
		{
			break;
		}
	}
	if(i==NUM)
	{
		cout<<"无该工号!"<<endl;
		system("pause");
		return ;
	}
	cout<<"请修改姓名:";
	cin>>sta[i].name;
	cout<<"请修改工资:";
	cin>>sta[i].salary;
	cout<<"修改成功!"<<endl;;
	system("pause");
}

void Menu::Dis_Sta_Info()
{
	system("cls");
	if(NUM==0)
	{
		cout<<"无信息!"<<endl;
		system("pause");
		return;
	}
	cout<<setw(10)<<"工号";
	cout<<setw(10)<<"姓名";
	cout<<setw(10)<<"工资"<<endl;
	for(int i=0;i<NUM;i++)
	{
		cout<<setw(10)<<sta[i].no;
		cout<<setw(10)<<sta[i].name;
		cout<<setw(10)<<sta[i].salary<<endl;
	}
	system("pause");
}

void Menu::Sreach_Sta_Info()
{
	system("cls");
	if(NUM==0)
	{
		cout<<"无信息!"<<endl;
		system("pause");
		return;
	}

	float min,max;
	cout<<"请输入要查询的工资最低值:";
	cin>>min;
	cout<<"请输入要查询的工资最大值:";
	cin>>max;
	cout<<endl;
	cout<<setw(10)<<"工号";
	cout<<setw(10)<<"姓名";
	cout<<setw(10)<<"工资"<<endl;
	for(int i=0;i<NUM;i++)
	{
		if(sta[i].salary>=min&&sta[i].salary<=max)
		{
			cout<<setw(10)<<sta[i].no;
			cout<<setw(10)<<sta[i].name;
			cout<<setw(10)<<sta[i].salary<<endl;
		}
	}
	system("pause");
}

void Menu::Run()
{
	Load_File();
	do
	{
		menu();
		switch(chioce)
		{
		case 0:
			Save_File();
			break;
		case 1:
			Add_Sta_Info();
			break;
		case 2:
			Del_Sta_Info();
			break;
		case 3:
			Modify_Sta_Info();
			break;
		case 4:
			Dis_Sta_Info();
			break;
		case 5:
			Sreach_Sta_Info();
			break;
		}
	}while(chioce!=0);
}

void Menu::Del_Sta_Info()
{
	system("cls");
	int del_no;
	cout<<"请输入要删除的工号:";
	cin>>del_no;

	for(int i =0;i<NUM;i++)
	{
		if(sta[i].no == del_no)
		{
			break;
		}
	}
	if(i==NUM)
	{
		cout<<"无该工号!"<<endl;
		system("pause");
		return ;
	}

	for(int j=i;j<NUM;j++)
	{
		sta[j]=sta[j+1];
	}
	NUM--;
	cout<<"删除成功!"<<endl;;
	system("pause");
}

void Menu::menu()
{
	system("cls");
	cout<<"1:增加员工信息"<<endl;
	cout<<"2:删除员工信息"<<endl;
	cout<<"3:修改员工信息"<<endl;
	cout<<"4:显示员工信息"<<endl;
	cout<<"5:查找员工信息"<<endl;
	cout<<"0:退出"<<endl;
	cin>>chioce;
	while(chioce<0||chioce>5)
	{
		cout<<"范围错误,请重新输入:";
		cin>>chioce;
	}
}

void Menu::Add_Sta_Info()
{
	system("cls");
	for(int i =0;i<NUM;i++)
	{
		if(sta[i].no != 100001+i)
		{
			break;
		}
	}
	sta[NUM].no = 100001 + i;
	cout<<"请输入姓名:";
	cin>>sta[NUM].name;
	cout<<"请输入工资:";
	cin>>sta[NUM].salary;
	NUM++;
	sort();
	cout<<"录入成功!"<<endl;
	system("pause");
}


int main()
{
	Menu Run_App;
	Run_App.Run();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/u011256974/article/details/89212152