Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by minhkhanh ( 4 years ago )
#pragma once
#include<iostream>
#include<string>

#include"Nam.h"
#include<string.h>
#pragma warning(disable:4996)
using namespace std;


class NhanVien
{
protected:
	string ID;
	string Name;
	string Gender;
	string address;
	Nam Age;
	float luong;
public:
	//kiem tra cac dieu kien
	bool checkID(string id);
	void chuanHoaName(string name);
	int checkGender(string gd);
	bool checkAge(Nam age);


	//nhap thong tin nhan vien
	void inPutId();
	void inPutName();
	void inPutGenDer();
	void inPutBirthDay();
	void inPutAddress();

	//constructors,destructors
    NhanVien();
	~NhanVien();

	//getter
	void setId(string id);
	void setName(string name);
	void setGender(string gende);
	void setAdress(string adr);
	void setAge(Nam age);

	//setter
	string getId();
	string getName();
	string getGender();
	string getAdress();
	Nam getAge();

	int tinhsotuoiNV();

	virtual long long tinhLuong() = 0;
	//virtual string toString();

    virtual void nhapThongTinNV();
    virtual void xuatThongTinNV();
	virtual string toString();
	

};

//
#include "NhanVien.h"
#include"Nam.h"

NhanVien::NhanVien()
{

}

NhanVien::~NhanVien()
{

}

//setter
void NhanVien::setId(string id)
{
	ID = id;
}

void NhanVien::setName(string n)
{
	Name = n;
}

void NhanVien::setGender(string gd)
{
	Gender = gd;

}


void NhanVien::setAdress(string adr)
{
	address = adr;
}

void NhanVien::setAge(Nam age)
{
	Age = age;
}


//getter
string NhanVien::getId()
{
	return ID;
}

string NhanVien::getName()
{
	return Name;
}

string NhanVien::getGender()
{
	return Gender;
}



string NhanVien::getAdress()
{
	return address;
}

Nam NhanVien::getAge()
{
	return Age;
}

//tinh so tuoi

int NhanVien::tinhsotuoiNV()
{
	int cdate, cmonth, cyear;
	time_t t = time(0);
	struct tm* Now = localtime(&t);
	cdate = Now->tm_year;
	cmonth = Now->tm_mon + 1;
	cyear = Now->tm_year + 1900;

	//
	int d, m, y;
	d = Age.getter_Date();
	m = Age.getter_Month();
	y = Age.getter_Year();


	int mangngay[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
	if (cyear % 4 == 0 && cyear % 100 != 0 || cyear % 400 == 0) {
		mangngay[1] = 29;
	}

	if (d > cdate) {
		cdate = cdate + mangngay[m - 1];
		cmonth = cmonth - 1;
	}
	if (m > cmonth) {
		cyear = cyear - 1;
		cmonth = cmonth + 12;
	}
	int date_Caculated = cdate - d;
	int month_Caculated = cmonth - m;
	int year_Caculated = cyear - y;
	return year_Caculated;
}


//kiem tra cac thuoc tinh
bool NhanVien::checkID(string id)
{
	ID = id;
	if (ID.size()!= 5) return false;
	for (int i = 0; i < ID.size(); i++) {
		if (ID[i] < '0' || ID[i]>'9') return false;
	}
	return true;
}

//
void NhanVien::chuanHoaName(string n) {
	Name = n;
	char* temp = new char[n.size() + 1];
	strcpy(temp, n.c_str());
	char* p1 = strtok(temp, " ");
	while (p1 != NULL) {
		p1[0] = toupper(p1[0]);
		cout << p1 << " ";
		p1 = strtok(NULL, " ");
	}
}

int NhanVien::checkGender(string gd)
{
	Gender = gd;
	if (gd == "Nam" || gd == "nam") {  
		return 1;

	}
	else if (gd == "Nu" || gd == "nu")
	{
		return 0;
	}
	return 10;

}

bool NhanVien::checkAge(Nam age)
{
	int n = tinhsotuoiNV();
	if (checkGender(Gender) == 1 && n > 18 && n < 60) return true;
	else if (checkGender(Gender) == 0 && n > 18 && n < 55) return true;
	return false;
}


//nhao thong tin
void NhanVien::nhapThongTinNV()
{
	rewind(stdin);
	// nhap vao ID
	do {
		cout << "\nNhap vao ID: ";
		getline(cin, ID);
		if (!checkID(ID)) {
			cout << "\nMa ID chi gom 5 so";
		}
	} while (!checkID(ID));

	//Nhap vao Ho Ten
     cout << "\nNhap vao Ho Ten: ";
	 getline(cin, Name);

	 do {
		 cout << "\nNhap vao gioi tinh(Nam or Nu): ";
		 getline(cin, Gender);
		 if (checkGender(Gender)==10) cout << "\nGioi tinh chua xac dinh";

	 } while (checkGender(Gender) == 10);

	 //Nhap vao ngay sinh:
	 int n = tinhsotuoiNV();
	 do
	 {
		 cin >> Age;	
		 if (!checkAge(Age)) cout << "\nLoi Nhap Tuoi: Nam chi tu 18-60, Nu chi tu 18-55";
	 } while (!checkAge(Age));

	 rewind(stdin);
	 cout << "\nNhap vao dia chi: ";
	 getline(cin, address);
}

//xuat thong tin

void NhanVien::xuatThongTinNV()
{
	cout << "\nID: " << ID;
	cout << "\nHo Ten: ";
	chuanHoaName(Name);
	cout << "\nGioi Tinh:" << Gender;
	cout<< Age;
	int n = tinhsotuoiNV();
	cout << "\nTuoi nhan vien: "<<n;
	cout << "\nDia chi: " << address;
}


string NhanVien::toString()
{
	return string(ID) + "|" + string(Name) + "|" + string(Gender) + "|" + "|" + string(address);
}

//
#pragma once
#include"NhanVien.h"
class NVCongNhat:public NhanVien
{
private:
	int songay;
public:
	void nhapThongTinNV();
	void xuatThongTinNV();
	long long tinhLuong();
	NVCongNhat(void);
	~NVCongNhat(void);

};
#include "NVCongNhat.h"

void NVCongNhat::nhapThongTinNV()
{
	NhanVien::nhapThongTinNV();
	do {
		cout << "\nNhap vao so ngay cong: ";
		cin >> songay;
		if (songay < 0) cout << "\nSo ngay k hop le";
	} while (songay < 0);
}

long long NVCongNhat::tinhLuong()
{
	return (size_t)songay * 20000;
}

void NVCongNhat::xuatThongTinNV()
{
	NhanVien::xuatThongTinNV();
	cout << "\nSo ngay cong: " << songay;

	cout << "\nLuong: " << NVCongNhat::tinhLuong()<<" VND";
}



NVCongNhat::NVCongNhat(void)
{
}

NVCongNhat::~NVCongNhat(void)
{
}
//
#pragma once
#include"NhanVien.h"

class NVSanXuat:public NhanVien
{
private:
	int sosanpham;
public:
	void nhapThongTinNV();
	void xuatThongTinNV();
	long long tinhLuong();
	NVSanXuat(void);
	~NVSanXuat(void);

};

#include "NVSanXuat.h"

void NVSanXuat::nhapThongTinNV()
{
	NhanVien::nhapThongTinNV();
	do {
		cout << "\nNhap vao so san pham ";
		cin >> sosanpham;
		if (sosanpham < 0) cout << "\nSo san pham k hop le";
	} while (sosanpham < 0);
}

long long NVSanXuat::tinhLuong()
{
	return (size_t)300000 * sosanpham;
}

void NVSanXuat::xuatThongTinNV()
{
	NhanVien::xuatThongTinNV();
	cout << "\nSo ngay cong: " << sosanpham;
	cout << "\nLuong: " << NVSanXuat::tinhLuong()<<" VND";
}


NVSanXuat::NVSanXuat(void)
{
}

NVSanXuat::~NVSanXuat(void)
{
}
//
#pragma once
#include"Nam.h"

#include"NhanVien.h"
#include"NVCongNhat.h"
#include"NVSanXuat.h"

#include<stdio.h>
#include<vector>
#include<fstream>


class CongTy {
protected:
	/*
	NhanVien** arrNV;
	int size;
	int capacity;
	*/
	vector<NhanVien*> NVLIST;
public:
	//
	CongTy(void);

	~CongTy(void);
	/*
	int getSize() const;
	int getCapacity() const;
	void resize();
	void reserve(int newalloc);
	void push(NhanVien*NV);
	void print();
	*/
	//cau 1:
	void nhapDSNhanVien();
	void xuatDSNhanVien();
	//
	int SLNV();
	int SLNVSX();
	int SLNVCN();
	//
	long long tinhLuongNV();
	long long tinhLuongNVSX();
	long long tinhluongNVCN();

	void tinhLuongTB();
	void nvMaxLuong();

	void sapxepDSNV();

	//
	void writeToFileArrayNhanVien(string fileName);
	void readFromFileArrayNhanVien(string fileName);
};
//
#include "CongTy.h"


/*
int CongTy::getSize() const
{
	return size;
}

int CongTy::getCapacity() const
{
	return capacity;
}

void CongTy::resize()
{
	capacity *= 2;
	NhanVien** temp = arrNV;
	arrNV = new NhanVien * [capacity];
	for (int i = 0; i < size; i++) {
		arrNV[i] = temp[i];
	}
	while (temp) {
		delete[] temp;
	}
}

void CongTy::push(NhanVien* NV)
{
	if (size == capacity) resize();
	arrNV[size++] = NV;

}




CongTy::CongTy(void)
{
	arrNV = new NhanVien * [1];
	this->size = 0;
	this->capacity = 0;

}

CongTy::~CongTy(void)
{
	while (arrNV) {
		for (int i = 0; i < size; i++) {
			delete arrNV[i];
		}
	}
	delete[]arrNV;
}
*/

CongTy::CongTy(void)
{
}

CongTy::~CongTy(void)
{
}



void CongTy::nhapDSNhanVien()
{
	NhanVien* nv = NULL;
	int luachon;
	do {
		cout << "\n-------------------MENU-----------------------";
		cout << "\n----------Nhap danh sach nhan vien----------";
		cout << "\n1.Nhan Vien San Xuat";
		cout << "\n2.Nhan Vien Cong Nhat";
		cout << "\n0Thoat";
		cout << "\n----------------------------------------------";

		do {
			cout << "\nNhap Lua Chon: ";
			cin >> luachon;
			if (luachon <0 || luachon >2) cout << "\nLua chon k hop le";
		} while (luachon <0 || luachon >2);


		if (luachon == 1) { nv = new NVSanXuat; }
		else if (luachon == 2) { nv = new NVCongNhat; }
	    if(luachon!=0){
			nv->nhapThongTinNV();
			NVLIST.push_back(nv);
		}

	} while (luachon != 0);
}

void CongTy::xuatDSNhanVien()
{
	cout << "\n------Danh Sach Nhan Vien Cong Ty-------";
	for (int i = 0; i < NVLIST.size(); i++) {
		cout << "\nThong Tin Nhan Vien Thu " << i + 1;
		NVLIST[i]->xuatThongTinNV();
		cout << endl;
	}
}
//
int CongTy::SLNVSX()
{
	
	vector<NVSanXuat>NVSXLIST;
	int slnvsx = NVSXLIST.size();
	return slnvsx;
}

int CongTy::SLNVCN()
{
	vector<NVCongNhat>NVCNLIST;
	int slnvcn = NVCNLIST.size();
	return slnvcn;
}


//
long long CongTy::tinhLuongNV()
{
	long long luong = 0;
	for (int i = 0; i < NVLIST.size(); i++) {
		luong += NVLIST[i]->tinhLuong();
	}
	return luong;
}

long long CongTy::tinhLuongNVSX()
{
	long long luong= 0;
	vector<NVSanXuat>NVSXLIST;
	int slnvsx = NVSXLIST.size();
	for (int i = 0; i < slnvsx; i++) {
		luong += NVSXLIST[i].tinhLuong();
	}
	return luong;
}

//
long long CongTy::tinhluongNVCN()
{
	long long luong = 0;
	vector<NVCongNhat>NVCNLIST;
	int slnvcn = NVCNLIST.size();
	for (int i = 0; i < slnvcn; i++) {
		luong += NVCNLIST[i].tinhLuong();
	}
	return luong;
}

void CongTy::nvMaxLuong()
{
	NhanVien* nv = NVLIST[0];
	long long max = NVLIST[0]->tinhLuong();
	for (int i = 0; i < NVLIST.size(); i++) {
		if (max < NVLIST[i]->tinhLuong()){
			nv = NVLIST[i];
		}
	}
	cout << "\nNhan vien co luong lon nhat:";
	nv->xuatThongTinNV();
}




//
//
void CongTy::tinhLuongTB()
{
	int slnv = NVLIST.size();
	long long luongtb = tinhLuongNV() / slnv;
	for (int i = 0; i < slnv; i++) {
		if (NVLIST[i]->tinhLuong() > luongtb) {
			NVLIST[i]->xuatThongTinNV();
			cout << endl;
		}
	}
	
}


void CongTy::sapxepDSNV() {
	for (int i = 0; i <NVLIST.size(); i++)
	{
		for (int j = i + 1; j < NVLIST.size(); j++)
		{
			if (NVLIST[i]->tinhLuong() > NVLIST[j]->tinhLuong())
			{
				swap(NVLIST[i], NVLIST[j]);
			}
		}
	}
}


//doc ghi file nhi phan
void CongTy::writeToFileArrayNhanVien(string fileName)
{
	fstream output;
	output.open(fileName, ios::binary | ios::out | ios_base::trunc);

	for (int i = 0; i < NVLIST.size(); i++)
	{
		char temp[100];
		if (dynamic_cast<NVSanXuat*>(&*NVLIST[i])) strcpy(temp, ("1|" + NVLIST[i]->toString()).c_str());
		else strcpy(temp, ("2|" + NVLIST[i]->toString()).c_str());
		output.write((char*)&temp, sizeof(temp));
	}
	output.close();
}



void CongTy::readFromFileArrayNhanVien(string fileName)
{
	fstream input;
	input.open(fileName, ios::binary | ios::in);

	char temp[100];
	while (input.read((char*)&temp, sizeof(temp)))
	{
		string s(temp);
		string type = s.substr(0, s.find("|"));
		s.erase(0, s.find("|") + 1);

		NhanVien* nv;
		if (type == "1")
		{
			nv = new NVSanXuat;
		}
		else nv = new NVCongNhat;

		nv->xuatThongTinNV();
		cout << "==================================\n";
	}
	input.close();
}
//
#include"CongTy.h"
#define fileName_listNhanVien "nhanvien.bin"
int main() {
	CongTy CT;
	CT.nhapDSNhanVien();
	system("cls");
	CT.sapxepDSNV();
	CT.xuatDSNhanVien();
	CT.writeToFileArrayNhanVien(fileName_listNhanVien);
	cout << "\n----------------------------------------";
	cout << "CAU 3:\n";
	CT.readFromFileArrayNhanVien(fileName_listNhanVien);
	cout << "===========================================\n";
	CT.nvMaxLuong();
	return 0;
}

 

Revise this Paste

Parent: 122071
Your Name: Code Language: