بسم الله الرحمن الرحيم
السؤال:
Write a program that reads in all of the employees in the data file,
using the classes above to represent the data. As you read in the data,
make your program compute the average years of experience of the
PROFESSORS (not the Administrators because that information is not
stored for them, right?). Print out the resulting average to the screen.
الكود:
#include
#include
#include
#include
#include
class Employee
{
private:
char *name;
long id;
int age;
char *dept;
char *type;
public:
Employee(char *name, long id, int age, char *dept, char *type);
Employee(){};
~Employee();
char *getName();
long getID();
int getAge();
char *getDept();
char *getType();
bool setName(char *name);
bool setID(long id);
bool setAge(int age);
bool setDept(char *dept);
bool setType(char *type);
};
Employee::Employee(char *input_name, long input_id, int input_age, char *input_dept, char *input_type)
{
name=new char[strlen(input_name)+1];
strcpy(name,input_name);
id=input_id;
age=input_age;
dept=new char[strlen(input_dept)+1];
strcpy(dept,input_dept);
type=new char[strlen(input_type)+1];
strcpy(type,input_type);
}
Employee::~Employee()
{
delete[]name;
delete[]dept;
delete[]type;
}
class Professor : public Employee
{
private:
char *rank;
int yrs_of_exp;
public:
Professor( char *name, long id, int age, char *dept, char *type, char *rank, int yrs_of_exp);
~Professor();
char *getRank();
int getYrs_of_exp();
bool setRank(char *rank);
bool setYrs_of_exp(int yrs_of_exp);
};
Professor::Professor( char *input_name, long input_id, int input_age, char *input_dept, char *input_type, char *input_rank, int input_yrs_of_exp):Employee( input_name, input_id, input_age, input_dept, input_type)
{
rank=new char[strlen(input_rank)+1];
strcpy(rank,input_rank);
yrs_of_exp=input_yrs_of_exp;
}
Professor::~Professor()
{
delete[]rank;
}
class Administrator : public Employee
{
private:
char *title;
int s_id;
char *level;
public:
Administrator( char *name, long id, int age, char *dept, char *type, char *title, int s_id, char *level);
~Administrator();
char *getTitle();
int getS_ID();
char *getLevel();
bool setTilte(char *title);
bool setS_ID(int s_id);
bool setLevel(char *level);
};
Administrator::Administrator( char *input_name, long input_id, int input_age, char *input_dept, char *input_type, char *input_title, int input_s_id, char *input_level):Employee( input_name, input_id, input_age, input_dept, input_type)
{
title=new char[strlen(input_title)+1];
strcpy(title,input_title);
s_id=input_s_id;
level=new char[strlen(input_level)+1];
strcpy(level,input_level);
}
Administrator::~Administrator()
{
delete[]title;
delete[]level;
}
int main (void)
{
int sum=0,counter=0,i=0;
int avareg;
FILE *infile;
char line[8][200];
infile = fopen("employees.txt", "r");
i = 0;
while (fgets(line, 200, infile) != NULL)
{
if (strcmp(line, "n") != 0)
{
if (strcmp(line[3], "PROFESSOR") == 0)
{
Professor *p1;
p1=new Professor(line[0],atoi(line[1]),atoi(line[2]),line[3],line[4],line[5],atoi(line[6]));
counter=counter+1;
sum=atoi(line[6])+sum;
delete p1;
}
else
{
Administrator *a1;
a1=new Administrator(line[0],atoi(line[1]),atoi(line[2]),line[3],line[4],line[5],atoi(line[6]),line[7]);
delete a1;
}
i = 0;
}
else
i++;
}
avareg = sum / counter;
fclose(infile);
cout<< avareg <
return 0;
}
@ملاحظة: الكمبايلر يعطيني 0 أخطاء .
الملف:
--------
Muhammad Abdelrahmaan
1112222
34
ADMINISTRATOR
Accounting and Finance
Senior Accountant
1211234
NO
Dalia Baloushi
1312422
21
ADMINISTRATOR
Accounting and Finance
Financial Intern
2313345
NO
Muhammad Alhudhaifi
1112224
26
PROFESSOR
Computer Science
ASSISTANT
1
Ahmed Abdulghani
6546532
52
ADMINISTRATOR
Employee Affairs
Senior Manager
YES
Mukbel Elyas
6512345
29
ADMINISTRATOR
Employee Affairs
Secretary
NO
Simon Brown
1233334
46
PROFESSOR
English
FULL
15
Faisal Bisramian
4534123
44
PROFESSOR
Islamic Law
ASSOCIATE
8
-------------------