Bài này hơi dài, tuy nhiên có những hàm chạy hay, các bạn tham khảo và viết lại code cho mình, có rảnh thì up bài do mình làm lên cho mọi người tham khảo nha. Nếu lưu lại nhớ đặt đuôi là .c nếu bạn muốn đuôi .cpp thì bạn thêm thư viện #include "ctype.h"
- Code:
#include "stdio.h"
#include "string.h"
#define MAX 100
typedef struct
{
char manv[8],hten[20],pban[10];
int nsinh;
float luong,thuong;
}nvien;
nvien dsach[MAX];
int n=0;
char tenfile[]="c:\\dsach.dat";
void nhapmoi()
{
int d=0,i;
char manv[8],hten[20],pban[10];
int nsinh;
float luong,thuong;
do{
printf("\nMa nhan vien(ENTER de thoat): ");fflush(stdin);
gets(manv);
if(strlen(manv)==0) d=1;
else
{
strcpy(dsach[n].manv,manv);
printf("Ho ten NV: ");
gets(dsach[n].hten);
printf("Phong ban: ");
gets(dsach[n].pban);
printf("Nam sinh: ");
scanf("%d",&dsach[n].nsinh);
printf("Luong co ban: ");
scanf("%f",&dsach[n].luong);
printf("Tien thuong: ");
scanf("%f",&dsach[n].thuong);
n++;
}
}while(!d);
}
void suadoi()
{
int i, d = 0, found, dump[MAX];
char manv[8],hten[20],pban[10],htentmp[20];
int nsinh;
float luong,thuong;
while (!d)
{
if (n!= 0)
{
printf("\nHo ten nhan vien can sua(ENTER de thoat): ");fflush(stdin);
gets(hten);
if (strlen(hten) == 0)
d = 1;
else
{
i = found = 0;
do {
strcpy(htentmp,dsach[i].hten);
if (strcmp(strupr(hten), strupr(htentmp)) == 0)
dump[found++] = i; i++;
} while (i
if (!found)
printf("\nKhong tim thay nhan vien co ten %s", hten);
else
{
printf("\n+--+--------+--------------------+----------+------+--------+--------+--------+");
printf("\n|TT| Ma NV | Ho ten | Phong | Nsinh| Luong | Thuong | Cong |");
printf("\n+--+--------+--------------------+----------+------+--------+--------+--------+\n");
for(i=0;i
{
printf("|%2d|%8s|%20s|%10s|%6d|%8.0f|%8.0f|%8.0f|",i+1,dsach[dump[i]].manv,dsach[dump[i]].hten,dsach[dump[i]].pban,
dsach[dump[i]].nsinh,dsach[dump[i]].luong,dsach[dump[i]].thuong,dsach[dump[i]].luong+dsach[dump[i]].thuong);
printf("\n+--+--------+--------------------+----------+------+--------+--------+--------+");
}
printf("\n\nTim thay %d nhan vien.", found);
printf("\nChon thu tu muon sua doi : ");
scanf("%d", &i);
if (i >= 1 && i <= found)
{
printf("\n%s\nMa nhan vien: ",dsach[dump[i-1]].manv);fflush(stdin);
gets(dsach[dump[i-1]].manv);
printf("\n%s\nHo ten : ",dsach[dump[i-1]].hten);
gets(dsach[dump[i-1]].hten);
printf("\n%s\nPhong ban: ",dsach[dump[i-1]].pban);
gets(dsach[dump[i-1]].pban);
printf("\n%4d\nNam sinh: ",dsach[dump[i-1]].nsinh);
scanf("%d",&dsach[dump[i-1]].nsinh);
printf("\n%f\nLuong co ban: ", dsach[dump[i-1]].luong);
scanf("%f", &dsach[dump[i-1]].luong);
printf("\n%f\nTien thuong: ", dsach[dump[i-1]].thuong);
scanf("%f", &dsach[dump[i-1]].thuong);
printf("\nDa thay doi");
}
else
printf("\nViec thay doi bi huy bo.");
}
d = 1;
}
getch();
}
else
d = 1;
}
}
void xoa()
{
int i, d = 0, found, dump[MAX];
char manv[8],hten[20],pban[10],htentmp[20];
int nsinh;
float luong,thuong;
while (!d)
{
if (n!= 0)
{
printf("\nHo ten nhan vien can xoa(ENTER de thoat): ");fflush(stdin);
gets(hten);
if (strlen(hten) == 0)
d = 1;
else
{
i = found = 0;
do {
strcpy(htentmp,dsach[i].hten);
if (strcmp(strupr(hten), strupr(htentmp)) == 0)
dump[found++] = i; i++;
} while (i
if (!found)
printf("\nKhong tim thay nhan vien co ten: %s", hten);
else
{
printf("\n+--+--------+--------------------+----------+------+--------+--------+--------+");
printf("\n|TT| Ma NV | Ho ten | Phong | Nsinh| Luong | Thuong | Cong |");
printf("\n+--+--------+--------------------+----------+------+--------+--------+--------+\n");
for(i=0;i
{
printf("|%2d|%8s|%20s|%10s|%6d|%8.0f|%8.0f|%8.0f|",i+1,dsach[dump[i]].manv,dsach[dump[i]].hten,dsach[dump[i]].pban,
dsach[dump[i]].nsinh,dsach[dump[i]].luong,dsach[dump[i]].thuong,dsach[dump[i]].luong+dsach[dump[i]].thuong);
printf("\n+--+--------+--------------------+----------+------+--------+--------+--------+");
}
printf("\n\nTim thay %d nhan vien.", found);
printf("\nChon thu tu muon xoa : ");
scanf("%d", &i);
if (i >= 1 && i <= found)
{
for (found=dump[i-1]; found
dsach[found] = dsach[found+1];
n--;
printf("\nDa xoa nhan vien: %s",hten);
}
else
printf("\nViec xoa bi huy bo.");
}
d = 1;
}
getch();
}
else
d = 1;
}
}
void lietke()
{
int i;
if (n!= 0)
{
printf("\n+--+--------+--------------------+----------+------+--------+--------+--------+");
printf("\n|TT| Ma NV | Ho ten | Phong | Nsinh| Luong | Thuong | Cong |");
printf("\n+--+--------+--------------------+----------+------+--------+--------+--------+\n");
for(i=0;i
{
printf("|%2d|%8s|%20s|%10s|%6d|%8.0f|%8.0f|%8.0f|",i+1,dsach[i].manv,dsach[i].hten,dsach[i].pban,
dsach[i].nsinh,dsach[i].luong,dsach[i].thuong,dsach[i].luong+dsach[i].thuong);
printf("\n+--+--------+--------------------+----------+------+--------+--------+--------+");
}
}
getch();
}
void dem()
{
int i, d ,dump[MAX];
char manv[8],hten[20],pban[10];
int nsinh;
float ltmp,luong,thuong;
printf("\nNhap vao so luong so sanh: ");
scanf("%f",<mp);
i = d = 0;
for(i=0;i
{
if(dsach[i].luong>=ltmp)
dump[d++]=i;
}
printf("\n+--+--------+--------------------+----------+------+--------+--------+--------+");
printf("\n|TT| Ma NV | Ho ten | Phong | Nsinh| Luong | Thuong | Cong |");
printf("\n+--+--------+--------------------+----------+------+--------+--------+--------+\n");
for(i=0;i
{
printf("|%2d|%8s|%20s|%10s|%6d|%8.0f|%8.0f|%8.0f|",i+1,dsach[dump[i]].manv,dsach[dump[i]].hten,dsach[dump[i]].pban,
dsach[dump[i]].nsinh,dsach[dump[i]].luong,dsach[dump[i]].thuong,dsach[dump[i]].luong+dsach[dump[i]].thuong);
printf("\n+--+--------+--------------------+----------+------+--------+--------+--------+");
}
printf("\nCo %d nhan vien co luong >=%8.0f",d,ltmp);
getch();
}
void ghilendia()
{
FILE *f;
if ((f = fopen(tenfile, "w+")) == NULL)
printf("\nKhong thay ghi tap tin %s",tenfile);
else
{
fwrite(&dsach[0], sizeof(nvien), n, f);
fclose(f);
printf("\nGhi %d nhan vien.",n);
getch();
}
}
void laydulieu()
{
char c;
int start, nrec;
FILE *fp;
long curpos, length;
start = 0;
if (n != 0)
{
printf("\nDang co du lieu trong bo nho. Co muon noi them vao khong?(C/K)");
c = toupper(getch());
if (c == 'C')
start = n;
else
start = 0;
}
if ((fp = fopen(tenfile, "r")) == NULL)
printf("\nKhong the mo tap tin %s", tenfile);
else
{
curpos = ftell(fp);
fseek(fp, 0L, SEEK_END);
length = ftell(fp);
fseek(fp, curpos, SEEK_SET);
nrec = length / sizeof(nvien);
fread(&dsach[start], sizeof(nvien), nrec, fp);
fclose(fp);
printf("\nThem vao %d records.", nrec);
n = n + nrec;
getch();
}
}
int menu()
{
char c;
printf("\n+------ Menu ------+");
printf("\n+------------------+");
printf("\n+ 1. Nhap NV +");
printf("\n+ 2. Xoa NV +");
printf("\n+ 3. Sua doi +");
printf("\n+ 4. Liet ke +");
printf("\n+ 5. Dem luong cao +");
printf("\n+------------------+");
printf("\n+ 6. Ghi vao file +");
printf("\n+ 7. Lay tu file +");
printf("\n+------------------+");
printf("\n+ 0. Thoat +");
printf("\n+------------------+");
printf("\nChon muc : ");
do {
c = getch();
} while (c<'0' || c>'7');
return c-'0';
}
int main()
{
int chon, d = 0;
do {
chon = menu();
switch(chon)
{
case 1 : nhapmoi();break;
case 2 : xoa();break;
case 3 : suadoi();break;
case 4 : lietke();break;
case 5 : dem();break;
case 6 : ghilendia();break;
case 7 : laydulieu();break;
case 0 : d = 1;
}
} while (!d);
}