#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class Employ
{
public:
Employ()
{name="NULL";}
Employ(string na)
{name=na;}
void set()
{
cout<<"請輸入名字:"<<endl;
cin>>name;
}
void display()
{cout<<"name:"<<name<<endl;}
protected:
string name;
};
class Manager:virtual public Employ
{
public:
Manager(string na,float s):Employ(na)
{s=salary1;}
Manager()
{
name="NULL";
salary1=0;
}
void set()
{
cout<<"請輸入管理者名字:"<<endl;
cin>>name;
cout<<"請輸入管理者的固定工資:"<<endl;
cin>>salary1;
}
void display()
{
cout<<"Type:Manager"<<endl;
cout<<"name:"<<name<<endl;
cout<<"salary:"<<salary1<<endl;
}
protected :
float salary1;
};
class Hourly_Worker:virtual public Employ
{
public:Hourly_Worker(string na,float wh,float p):Employ(na)
{
workhour=wh;
pay=p;
}
Hourly_Worker()
{
name="NULL";
workhour=0;
pay=0;
}
void set()
{
cout<<"請輸入計時工人名字:"<<endl;
cin>>name;
cout<<"請輸入計時工人作業時長:"<<endl;
cin>>workhour;
cout<<"請輸入計時工人的時薪:"<<endl;
cin>>pay;
}
float salary2()
{
float salary2;
salary2=workhour*pay;
return salary2;
}
void display()
{
cout<<"Type:Hourly worker"<<endl;
cout<<"name:"<<name<<endl;
cout<<"salary:"<<salary2()<<endl;
}
protected :
float workhour;
float pay;
};
class Piece_Worker:virtual public Employ
{
public:
Piece_Worker(string na,int wp,float p):Employ(na)
{
workpiece=wp;
pay=p;
}
Piece_Worker()
{
name="NULL";
workpiece=0;
pay=0;
}
float salary3()
{
float salary3;
salary3=workpiece*pay;
return salary3;
}
void set()
{
cout<<"請輸入計件工人名字:"<<endl;
cin>>name;
cout<<"請輸入計件工人生產的工件數:"<<endl;
cin>>workpiece;
cout<<"請輸入計件工人工件單價:"<<endl;
cin>>pay;
}
void display()
{
cout<<"Type:piece worker"<<endl;
cout<<"name:"<<name<<endl;
cout<<"salary:"<<salary3()<<endl;
}
protected :
int workpiece;
float pay;
};
class Commission_Worker:virtual public Manager
{
public:Commission_Worker(string na,float s,int wp,int bp,float p):Manager(na,s)
{
workpiece=wp;
basicpiece=bp;
pay=p;
}
Commission_Worker()
{
name="NULL";
salary1=0;
workpiece=0;
basicpiece=0;
pay=0;
}
float salary4()
{
float salary4;
if(workpiece<=basicpiece)
salary4=salary1;
else salary4=salary1+(workpiece-basicpiece)*pay;
return salary4;
}
void set()
{
cout<<"請輸入合同工名字:"<<endl;
cin>>name;
cout<<"請輸入合同工底薪:"<<endl;
cin>>salary1;
cout<<"請輸入合同工的實際工件數:"<<endl;
cin>>workpiece;
cout<<"請輸入合同工最低工件數:"<<endl;
cin>>basicpiece;
cout<<"請輸入合同工超過最低作業數以后工件單價:"<<endl;
cin>>pay;
}
void display()
{
cout<<"Type:Commission worker"<<endl;
cout<<"name:"<<name<<endl;
cout<<"salary:"<<salary4()<<endl;
}
protected :
int workpiece;
int basicpiece;
float pay;
};
int main()
{
int i;
Manager m1;
Hourly_Worker h1;
Piece_Worker p1;
Commission_Worker c1;
cin>>i;
switch (i)
{
case 1:
m1.set();
m1.display();
case 2:
h1.set();
h1.display();
case 3:
p1.set();
p1.display();
case 4:
c1.set();
c1.display();
}
return 0;
}
請問這個程式怎么實作通過檔案的方式輸入雇員資訊,及工資標準。并將不同雇員的工資情況輸入到檔案中。
uj5u.com熱心網友回復:
用這套檔案api實作如:#include <stdio.h>
#define SIZE 2
struct student_type
{
char name[10];
int num;
int age;
char addr[10];
}stud[SIZE];
void save()
{
FILE *fp;
int i;
if((fp=fopen("stu_list","wb"))==NULL)
{
printf("cant open the file");
exit(0);
}
for(i=0;i<SIZE;i++)
{
if(fwrite(&stud[i],sizeof(struct student_type),1,fp)!=1)
printf("file write error\n");
}
fclose(fp);
}
main()
{
int i;
for(i=0;i<SIZE;i++)
{
scanf("%s%d%d%s",&stud[i].name,&stud[i].num,&stud[i].age,&stud[i].addr);
save();
}
for(i=0;i<SIZE;i++)
{
printf("%s,%d,%d",stud[i].name,stud[i].num,stud[i].age,stud[i].addr);
}
}
fread.c
#include <stdio.h>
#define SIZE 2
struct student_type
{
char name[10];
int num;
int age;
char addr[10];
}stud[SIZE];
void read()
{
FILE *fp;
int i;
if((fp=fopen("stu_list","rb"))==NULL)
{
printf("cant open the file");
exit(0);
}
for(i=0;i<SIZE;i++)
{
if(fread(&stud[i],sizeof(struct student_type),1,fp)!=1)
printf("file write error\n");
}
fclose(fp);
}
main()
{
int i;
read();
for(i=0;i<SIZE;i++)
{
printf("%s,%d,%d,%s",stud[i].name,stud[i].num,stud[i].age,stud[i].addr);
printf("\n");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/84647.html
標籤:基礎類
下一篇:視窗默認最大化
