⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 personnel.cpp

📁 老师安排的一个c++小程序
💻 CPP
字号:
// Personnel.cpp: implementation of the CPersonnel class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Personnel.h"
#include <iostream>
#include <conio.h>
#include <fstream>
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPersonnel::CPersonnel()
{

}

CPersonnel::~CPersonnel()
{

}

void CPersonnel:: DisplayAppInfo()   //界面
{
	cout<<endl;
	cout<<"* * * * * * * * * * * * * * * * * * * * *"<<endl;
	cout<<"*                                       *"<<endl;
	cout<<"*           职工数据查询系统            *"<<endl;
	cout<<"*                                       *"<<endl;
	cout<<"*   制作: 郭辉        8001606082       *"<<endl;
	cout<<"*           计算机网络技术062班         *"<<endl;
	cout<<"*                 2007/12/12            *"<<endl;
	cout<<"* _____________________________________ *"<<endl;
	cout<<"*                                       *"<<endl;
	cout<<"*         请按以下提示进行操作          *"<<endl;
	cout<<"*                                       *"<<endl;
	cout<<"*     1、查询   2、输入  3、显示        *"<<endl;
	cout<<"*                                       *"<<endl;
	cout<<"*                                       *"<<endl;
	cout<<"* * * * * * * * * * * * * * * * * * * * *"<<endl;
 
}

void CPersonnel:: StaffInfoInput()
{
	staffInfo sInfo;
	cout<<"请按以下提示输入数据"<<endl;
	cout<<"员工号:"; cin>>sInfo.num;
	cout<<"姓  名:";   cin>>sInfo.name;
	cout<<"年  龄:";   cin>>sInfo.age;
	cout<<"工  资:";   cin>>sInfo.pay;
	SaveStaffInfoToFile(sInfo,"staff.dat");
	
}

void CPersonnel:: OpenfileDisplay(char *FileNamePath)
{
	fstream iofile(FileNamePath,ios::in|ios::out|ios::binary);
	if(!iofile)
	{ 
		cerr<<"文件打开错误!"<<endl;
		abort();
	}
      staffInfo sInfo;
	  do
	  {
		  iofile.read((char*)&sInfo,sizeof(sInfo));
		  if (iofile.tellg()>0)
		  {
			  cout<<"员工号"<<sInfo.num<<endl;
			  cout<<"姓  名"<<sInfo.name<<endl;
			  cout<<"年  龄"<<sInfo.age<<endl;
			  cout<<"工  资"<<sInfo.pay<<endl;
			  cout<<"---------------"<<endl;
			  cout<<endl;
		  }
	  }
	  while(iofile.tellg()>0);

}

void CPersonnel:: SaveStaffInfoToFile(staffInfo info ,char *FileNamePath)
{
	fstream iofile(FileNamePath,ios::in|ios::out|ios::binary|ios::app);
	if(!iofile)
	{
		cerr<<"文件操作错误!"<<endl;
		exit(1);
	}
	iofile.seekg(0,ios::end);
	iofile.write((char *)&info,sizeof(info));
	iofile.close();

}

void CPersonnel:: QueryStaffNum()
{
	bool find;
	int num;
	fstream iofile("staff.dat",ios::in|ios::out|ios::binary);
	if(!iofile)
	{
		cerr<<"文件不存在或文件错误!"<<endl;
		exit(1);
	}
	cout<<endl;
	cout<<"请输入要查找的员工号,输入0停止查找>";
	cin>>num;
	staffInfo sInfo;
	int m;
	find=false;
	iofile.seekg(0,ios::beg);
	do
	{
		iofile.read((char*) & sInfo, sizeof(sInfo));
		m=iofile.tellg();
		if(num==sInfo.num && m>0)
		{
			cout<<endl;
			cout<<"-----查询结果-----"<<endl;
			cout<<endl;
			cout<<"第"<<m/sizeof(sInfo)<<"条数据是"<<num<<"号"<<endl;
			cout<<endl;
			cout<<"------查询结果-------"<<endl;
			cout<<"员工号:"<<sInfo.num<<endl;
			cout<<"姓  名:"<<sInfo.name<<endl;
			cout<<"年  龄:"<<sInfo.age<<endl;
			cout<<"工  资:"<<sInfo.pay<<endl;
			cout<<"-----------------"<<endl;
			find=true;

		}
	} while(iofile.tellg()>0);
	if(!find)
		cout<<"没有找到员工号为"<<num<<"的数据"<<endl;
	iofile.close();

}

char CPersonnel:: GetLegalInput()    //检查输入合法性
{
	int ch;
	do
	{
		ch=_getch();
	} while(ch!='Y'&&ch!='y'&&
		ch!='N'&&ch!='n'&&
		ch!='Q'&&ch!='q'&&
		ch!='1'&&ch!='2'&&
		ch!='3');
	_putch(ch);
	_putch('\r');
	_putch('\n');
	return ch;




}

void CPersonnel:: DataInput()  
{
	char userResponse;
	bool exitInput=0;

	while(exitInput==0)
	{
		StaffInfoInput();
		cout<<"要继续输入数据吗?(y/n) ";
		userResponse=GetLegalInput();
		if(userResponse=='N'||userResponse=='n')
		
			cout<<"要查看文件内容吗?(y/n) ";
			userResponse=GetLegalInput();
		    if(userResponse=='Y'||userResponse=='y')
			{
		         cout<<endl;
	             cout<<"-----文件内容-----"<<endl;
		         OpenfileDisplay("staff.dat");
				 exitInput=1;
			     cout<<"请按回车键继续!";
			     _getch();
			}
			
		 else
		{
			exitInput=0;
		}  
		
	}

}
	

void CPersonnel:: DataQuery()
{
	char userResponse;
	bool exitQuery=0;

	while(exitQuery==0)
	{
		QueryStaffNum();
		cout<<"要继续查询数据吗?>";
		userResponse=GetLegalInput();
		if(userResponse=='N'||userResponse=='n')
		{
			
			exitQuery=1;
			cout<<"请按回车键继续!";
			_getch();
		}
		else
		{
			exitQuery=0;
		}
	}


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -