xin gửi tới code C về bài tập quản lí nhân viên, bảng lương của nhân viên, thông tin nhân viên.....
dựa vào bài này, các bạn có thể xây dựng thêm những hàm quản lí khác
#include
#include
#include
#include
#include
#include
struct Date{
int ngay;
int thang;
int nam;
};
struct NhanVien{
char manv[10];
char holot[15];
char ten[10];
Date ngaysinh;
int phai; //0:nu , 1:nam
char noisinh[20];
long phucap;
long lcb;
int ngaycong;
};
struct Node{
NhanVien dataNV;
Node *next;
};
typedef Node *DSNV;
//=====================================
void manHinh(){
printf("\n ################### QUAN LY NHAN VIEN ##################");
printf("\n #======================================================#");
printf("\n #======================================================#");
printf("\n # 1-Them nhan vien ## 5-Xoa mot nhan vien #");
printf("\n # 2-Tim 1 nhan vien ## 6-In bang luong #");
printf("\n # 3-Sua thong tin nv ## 7-Xoa tat ca nhan vien #");
printf("\n # 4-Sap xep nv theo ten ## 0-Thoat chuong trinh #");
printf("\n #======================================================#");
printf("\n #=========== SV KHOA CNTT- DH GTVT TPHCM ===========#");
printf("\n ########################################################");
}
void BangLuong(DSNV first);
//===========ham kiem tra nam nhuan=======
int ktNam(int nam){
if(nam%400==0 || ((nam%4==0) && (nam%100 != 0))) //nam nhuan tra ve 366 ngay
return 366;
return 365;
}
//==========ham kiem tra so ngay trong thang==
int soNgay(int thang,int nam){
int songay;
switch(thang){
case 4:
case 6:
case 9:
case 11:
songay = 30;
break;
case 2:
if(ktNam(nam)==366)
songay = 29;
songay = 28;
break;
default:
songay = 31;
break;
}
return songay;
}
//=======ham nhap ngay thang===========
void NhapThoiGian(Date &tg){
do{
fflush(stdin);
printf("\t Nhap ngay thang nam(dinh dang dd/mm/yy): ");
scanf("%d/%d/%d",&tg.ngay,&tg.thang,&tg.nam);
fflush(stdin);
if((tg.ngay>0)&&(tg.ngay<=soNgay(tg.thang,tg.nam))&&(tg.thang>0)&&(tg.thang<=12)) break; printf("\nvui long kiem tra lai ngay thang \n"); }while(1); } //=============ham khoi tao ====== void KhoiTao(DSNV *head){ *head=NULL; } //=========ham tim nhan vien====== DSNV TimNhanVien(DSNV head,char key[10]) { DSNV p; p=head; while((p!=NULL)&&strcmp(p->dataNV.manv,key)!=0)
p=p->next;
return p;
}
//============ham nhap nhan vien ==
void NhapNhanVien(NhanVien &nv){
// Rang buoc du lieu nhap :Phai la 0 hay 1 ,0<= ngay cong <= 31; //clrscr(); printf("\n\t Nhap cac thong tin cua nhan vien : Ho lot , ten , ....\n\n"); fflush(stdin); printf("\n\t Ho lot : "); gets(nv.holot); fflush(stdin); printf("\n\t Ten : "); gets(nv.ten); fflush(stdin); NhapThoiGian(nv.ngaysinh); // nhap vao gioi tinh ( rang buoc la 0 hay 1) do{ fflush(stdin); printf("\n\t Phai (1:Nu, 0:Nam ): "); scanf("%d" ,&nv.phai); }while(nv.phai!=0 && nv.phai!=1); fflush(stdin); printf("\n\t Noi sinh : "); gets(nv.noisinh); fflush(stdin); printf("\n\t Phu cap :"); scanf("%ld",&nv.phucap); fflush(stdin); printf("\n\t Luong co ban :"); scanf("%ld",&nv.lcb); fflush(stdin); printf("\n\t Ngay cong :"); scanf("%d",&nv.ngaycong); } //=============ham in nhan vien==== void InNhanVien(NhanVien nv){ float luong;// bien tam chua luong luong = float(nv.lcb*nv.ngaycong)/26 + nv.phucap; // tinh luong printf("\n\t Ma so :%s ",nv.manv); printf("\n\t Ho va ten :%s %s",nv.holot,nv.ten); printf("\n\t Ngay sinh :%d/%d/%d ",nv.ngaysinh.ngay,nv.ngaysinh.thang,nv.ngaysinh.nam); if(nv.phai==1) printf("\n\t Phai :nu "); else printf("\n\t phai: nam "); printf("\n\t Noi sinh :%s",nv.noisinh); printf("\n\t Phu cap :%ld",nv.phucap); printf("\n\t Luong co ban :%ld",nv.lcb); printf("\n\t Ngay cong :%d",nv.ngaycong); printf("\n\t Luong :%.1f",luong); } //===============ham tim nhan vien====== void Tim(DSNV first){ NhanVien nv; DSNV pos; printf("\n\t Ma nhan vien : "); fflush(stdin); gets(nv.manv); pos=TimNhanVien(first,nv.manv); // kiem tra xem nhan vien co trong danh sach hay chua if(pos==NULL) // nhan vien nay chua co trong danh sach printf("khong co nhan vien nay"); else InNhanVien(pos->dataNV);
}
// =========ham them cuoi =========
void ThemCuoiNV(DSNV *first,NhanVien NV_Them)
{
DSNV ps,ns;
ns=new Node;
ns->dataNV=NV_Them;
ps=*first;
if(ps==NULL)//danh sach rong
{
ns->next=*first;
*first=ns;
}
else
{
while(ps->next!=NULL)
ps=ps->next;
ns->next=ps->next;
ps->next=ns;
}
}
//====================
//=======================================//
void Them(DSNV *first){
NhanVien nv;//bien tam chua cac thong tin cua nhan vien
char tieptuc;
// vong lap them nhan vien
do{
printf("\n\t Them nhan vien vao danh sach ");
printf("\n\t Ma nhan vien : ");
fflush(stdin);
gets(nv.manv);
// kiem tra xem nhan vien co trong danh sach hay chua
if(TimNhanVien(*first,nv.manv)==NULL) // nhan vien nay chua co trong danh sach
{
NhapNhanVien(nv);// nhap cac thong tin ho lot , ten ....
ThemCuoiNV(first,nv); // them nhan vien nv vao danh sach
}
else{
printf("\n\t Nhan vien co ma so :%s da co trong danh sach ",nv.manv);
}
printf("\n\t Tiep tuc (Y/N)?: ");
tieptuc=getch();
}while((tieptuc=='Y')||(tieptuc=='y'));
}
//=========sua nhan vien========
void SuaNhanVien(DSNV *first)
{
DSNV ps;
NhanVien a;
Date tgTam;
do{
printf("\n nhap ma so nhan vien can sua:");
fflush(stdin);
gets(a.manv);
ps=TimNhanVien(*first,a.manv);
if(ps!=NULL)
break;
printf("\n Ma so nhan vien khong ton tai . vui long nhap lai");
}while(1);
printf("\n Tim thay ma so nhan vien \n");
InNhanVien(ps->dataNV);
printf("\n Tien hanh chinh sua: \n");
NhapNhanVien(a);
ps->dataNV=a;
printf("\n da sua xong");
}
//==========xoa mot nhan vien===
void XoaNhanVien(DSNV *first)
{
DSNV ps,qs;
char k[10] ;
ps = *first;
printf("\n nhap ma so nhan vien can xoa:");
fflush(stdin);
gets(k);
if(TimNhanVien(*first,k)==NULL)
printf("\n Ma so nhan vien khong ton tai . vui long thu lai");
else
{
if(ps->next==NULL)
{
*first=NULL;
delete ps;
}
else{
while(ps!=NULL)
{
qs=ps;
ps=ps->next;
if(strcmp(ps->dataNV.manv,k)==0)
break;
}
qs->next=ps->next;
delete ps;
}
printf("\n Da xoa xong");
}
}
//================sap xep danh sach theo ten=========
void SapXep(DSNV *first){
DSNV p,q;
NhanVien tam;
p= *first;
while(p->next!=NULL){
q=p->next;
while(q!=NULL){
if(strcmp(q->dataNV.ten,p->dataNV.ten)<0){ tam="q-">dataNV;
q->dataNV=p->dataNV;
p->dataNV=tam;
}
q=q->next;
}
p=p->next;
}
printf("\nsap xep xong");
BangLuong(*first);
}
//============= in bang luong ========
void BangLuong(DSNV first){
float luong,tongluong=0;
int stt=1;
char gach[80],tua[80];
DSNV p;
p = first;
strcpy(gach,"--------------------------------------------------------------------------------");
strcpy(tua,"|STT | Ho Ten | LCB | Phu cap |Ngay cong | Luong ");
clrscr();
printf("\n cong ty Hong Minh ");
printf("\n\t\t\t BANG LUONG \n");
printf("%s",gach);
printf("%s",tua);
while(p!=NULL)
{
luong=float(p->dataNV.lcb*p->dataNV.ngaycong)/26+p->dataNV.phucap;
tongluong+=luong;
// in cac thong tin chi tiet cua nhan vien
printf("\n|%-5d|%-15s %10s|%10ld|%12ld|%10d|%10.1f",stt,p->dataNV.holot
,p->dataNV.ten,p->dataNV.lcb,p->dataNV.phucap,p->dataNV.ngaycong
,luong);
stt++;
p=p->next;
// xu ly ngat dong :10 dong
if(stt%10==0){
getch();
}
}
printf("\n\t\t Tong luong : %f",tongluong);
}
//=========xoa tat ca danh sach ==========
void XoaTatCaNV(DSNV *first)
{
DSNV ps,qs;
ps=*first;
*first=NULL;
while(ps!=NULL)
{
qs=ps;
ps=ps->next;
delete qs;
}
printf("\n Da xoa xong");
}
//==========ghi file===========
void GhiFileDuLieu(DSNV first)
{
FILE *f;
DSNV ps;NhanVien a;
f=fopen("DULIEU.NTL","wb");
ps=first;
while(ps!=NULL)
{
a=ps->dataNV;
fwrite(&a,sizeof(a),1,f);
ps=ps->next;
}
fclose(f);
}
//============doc file =============
void DocFileDuLieu(DSNV *first)
{
FILE *f;
NhanVien a;
f=fopen("DULIEU.NTL","rb");
if (f==NULL )
printf("\n DU LIEU KO TON TAI .DA TAO MOI FILE DU LIEU \n");
else
{
printf("\n DA TAI DU LIEU TU FILE DU LIEU \n");
while(fread(&a,sizeof(a),1,f)==1)
{
ThemCuoiNV(first,a);
}
fclose(f);
}
}
int main(){
DSNV head;
KhoiTao(&head);
DocFileDuLieu(&head);
//NhanVien n;
//NhapNhanVien(n);
//InNhanVien(n);
int luaChon;
giaoDien:
manHinh();
printf("\n Nhap lua chon : ");
scanf("%d",&luaChon);
clrscr();;
switch(luaChon)
{
case 0:
goto thoatChuongTrinh;
break;
case 1:
Them(&head);
break;
case 2:
Tim(head);
break;
case 3:
SuaNhanVien(&head);
break;
case 4:
SapXep(&head);
break;
case 5:
XoaNhanVien(&head);
break;
case 6:
BangLuong(head);
break;
case 7:
XoaTatCaNV(&head);
break;
default:
printf("\n CHUONG TRINH KHONG CO CHUC NANG NAY");
}
do
{
cout<<"\n An phim Enter de ve menu : ";
getch();
break;
}while(1);
clrscr();
goto giaoDien;
thoatChuongTrinh:
do {
printf("\n Chuong trinh ket thuc.Ban se luu lai nhung gi vua thuc hien chu"); printf("\n chon Y de luu lai va thoat,N de thoat khong luu\n
chon M de ve menu tiep tuc cong viec \n
chon S de luu file roi tiep tuc cong viec\n");
fflush(stdin);
int chonThoat=getch();
if(toupper(chonThoat)=='Y')
{
GhiFileDuLieu(head);
break;
}
if(toupper(chonThoat)=='N')
{ break; }
if(toupper(chonThoat)=='M')
{ clrscr;
goto giaoDien;
break; }
if(toupper(chonThoat)=='S')
{ GhiFileDuLieu(head);
clrscr();
goto giaoDien;
break; }
clrscr();
printf("\n ban nhap sai chuc nang hay kiem tra lai\n"); }while(1);
return 0; }
Comments[ 0 ]
Post a Comment