Code C\C++: Ví dụ 2, xử lý cấu trúc (struct) - 14-01-13


/* vi du 2 - struct
Viet cac ham:
1. Nhap vao thong tin 1 nhan vien IT, gom:
- Ho ten
- Tuoi
- Vi tri lam viec
- He so luong
- So nam cong tac
2. In thong tin nhan vien vua nhap
3. Tinh luong cho nhan vien
luong=he so luong * luong co ban + phu cap
trong do:   phu cap = 1,2 luong co ban neu so nam cong tac > 3
nguoc lai phu cap = 1 luong co ban.
4. Tinh thue thu nhap ma nhan vien phai tra.
neu luong>5.000.000 thi thue 10% luong
nguoc lai thue =0.
*/

#include<stdio.h>
#include<conio.h>
#include<string.h>

#define LCB 1050000 // khai bao hang LCB

// Dinh nghia struc NV
struct NV{
  char hoTen[30], viTriLV[30];
  int tuoi, soNamCT;
  float heSoLuong;
};

// khai bao bien
NV t;

// Nhap thong tin nhan vien
void NhapTT(){
printf("\n NHAP THONG TIN NHAN VIEN: \n");

printf("\n Ho ten: ");
fflush(stdin);
gets(t.hoTen);

printf("\n Vi tri lam viec: ");
fflush(stdin);
gets(t.viTriLV);

  do{
printf("\n Tuoi nhan vien: ");
scanf("%d",&t.tuoi);
if (t.tuoi<18||t.tuoi>65)
printf("\n Tuoi khong hop le, nhap lai tuoi");
  } while(t.tuoi<18||t.tuoi>65);

  do{
printf("\n So nam cong tac: ");
scanf("%d",&t.soNamCT);
if (t.soNamCT<0|| t.soNamCT>t.tuoi-18)
printf("\n So nam cong tac khong hop le, nhap lai nam cong tac");
  } while (t.soNamCT<0|| t.soNamCT>t.tuoi-18);
        
  do{
printf("\n He so luong: ");
scanf("%f",&t.heSoLuong);
if (t.heSoLuong<=0||t.heSoLuong>15)
printf("\n He so luong khong hop le, nhap lai he so luong.");
  } while (t.heSoLuong<=0||t.heSoLuong>15);

}
// In thong tin nhan vien
void InTT(){
printf("\n IN THONG TIN NHAN VIEN: \n");
printf("\n - Ho ten: %s",t.hoTen);
printf("\n - Vi tri lam viec: %s",t.viTriLV);
printf("\n - Tuoi: %d",t.tuoi);
printf("\n - So nam cong tac: %d",t.soNamCT);
printf("\n - He so luong: %f",t.heSoLuong);


}
// Tinh luong
float TinhLuong(){
 float phuCap;
 if(t.soNamCT>=3) phuCap=1.2*LCB;
 else phuCap=LCB;

 return (t.heSoLuong*LCB+phuCap);
}
// Tinh thue thu nhap
float ThueTN(){
 float thue=0;
 if (TinhLuong()>=5000000) thue=0.1*TinhLuong();
 return thue;
}
// ham main
int main(){
NhapTT();
InTT();
printf("\n LUONG THANG CUA NHAN VIEN: %f",TinhLuong() );
printf("\n THUE THU NHAP CUA NHAN VIEN: %f",ThueTN() );

getch();
return 0;
}