Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <exception>
- #include <strings.h>
- #include <vector>
- class Department;
- using namespace std;
- enum Position {Developer, Engineer, Assistant};
- class DateException: exception {
- public:
- const char* what() {
- return "Invalid Date";
- }
- }DateException;
- class NoSubj:exception{
- public:
- const char* what(){
- return "No such subj";
- }
- }NoSubj;
- class Date {
- friend class Subject;
- public:
- int day;
- int month;
- int year;
- Date(int d, int m, int y) {
- if (y <= 0) {
- throw DateException;
- }
- if (m <= 0 || m > 12){
- throw DateException;
- }
- if (d <= 0 || d > 31) {
- throw DateException;
- }
- day = d;
- month = m;
- year = y;
- }
- Date() {
- day = 1;
- month = 1;
- year = 2007;
- }
- Date& operator =(const Date& right) {
- day = right.day;
- month = right.day;
- year = right.year;
- return *this;
- }
- friend istream& operator >>(istream& is, Date& a) {
- is >> a.day>>a.month>>a.year;
- if (a.year <= 0) {
- throw DateException;
- }
- if (a.month <= 0 || a.month > 12){
- throw DateException;
- }
- if (a.day <= 0 || a.day > 31) {
- throw DateException;
- }
- return is;
- }
- friend ostream& operator <<(ostream& os, Date& a){
- os<<a.day<<endl<<a.month<<endl<<a.year;
- return os;
- }
- };
- class FIO {
- friend class Employee;
- friend class Department;
- public:
- char *first = new char[256];
- char *midle = new char[256];
- char *last = new char[256];
- FIO(char *f, char *m, char *l) {
- strcpy(first, f);
- strcpy(midle, m);
- strcpy(last, l);
- }
- FIO() {
- strcpy(first, "A");
- strcpy(midle, "B");
- strcpy(last, "C");
- }
- char* ToString() {
- char* buf = new char[256];
- strcat(buf, first);
- strcat(buf, " ");
- strcat(buf, midle);
- strcat(buf, " ");
- strcat(buf, last);
- return buf;
- }
- friend istream& operator >>(istream& is, FIO& a) {
- is >> a.first >> a.midle >> a.last;
- return is;
- }
- friend ostream &operator<<(ostream &os, const FIO &fio) {
- os << fio.first<<endl<<fio.midle<<endl<< fio.last;
- return os;
- }
- ~FIO(){
- delete [] first;
- delete [] midle;
- delete [] last;
- }
- };
- class Subject {
- friend class Date;
- char *name = new char[256];
- Date deadline;
- int number=0;
- public:
- Subject(char* n, Date dl, int num) {
- strcmp(name, n);
- deadline = dl;
- number = num;
- }
- Subject()= default;
- Subject(int){
- Subject();
- cin>>*this;
- }
- friend istream& operator >>(istream& is, Subject& a) {
- is >> a.name >> a.deadline>> a.number;
- return is;
- }
- friend ostream &operator<<(ostream &os, Subject &subj) {
- os<<subj.deadline<<endl<<subj.name<<endl<<subj.number;
- return os;
- }
- char* ToString(){
- char* buf = new char[256];
- char* bb = new char[16];
- strcat(buf,name);
- strcat(buf," ");
- itoa(number,bb,10);
- strcat(buf,bb);
- return buf;
- }
- Subject& operator=(Subject& rhs){
- deadline = rhs.deadline;
- strcpy(name,rhs.name);
- number = rhs.number;
- return *this;
- }
- ~Subject(){
- delete [] name;
- }
- };
- class Employee {
- friend class Department;
- FIO* FullName;
- Position position;
- int subject_num;
- Subject* subj;
- public:
- Employee() {
- FullName = new FIO;
- position = Position::Developer;
- subject_num = -1;
- }
- friend ifstream& operator >>(ifstream& is, Employee& a) {
- int b;
- is >> *a.FullName>>b>>a.subject_num;
- a.position = (Position)b;
- return is;
- }
- friend ostream &operator<<(ostream &os, const Employee &employee) {
- os << *employee.FullName <<endl<< employee.position <<endl<< employee.subject_num;
- return os;
- }
- // int ToString(){
- // char *buf = new char[512];
- // strcat(buf,FullName->ToString());
- // strcat(buf," ");
- // strcat(buf,subj->ToString());
- // return 0;
- // }
- };
- class Department {
- friend class Employee;
- FIO CEO;
- vector<Employee*> Staff;
- vector<Subject*> Subjects;
- int EmployeeCount = 0;
- int SubjectCount = 0;
- public:
- Department()= default;
- int Load(const char* LIB_NAME) {
- ifstream lib;
- Staff.reserve(32);
- Subjects.reserve(32);
- lib.open(LIB_NAME);
- if (!lib.fail()) {
- lib >> EmployeeCount >> SubjectCount;
- lib >> CEO;
- for (int i = 0; i < EmployeeCount; i++) {
- Staff[i] = new Employee;
- lib >> *Staff[i];
- if (Staff[i]->subject_num >= SubjectCount){
- throw NoSubj;
- }
- }
- for (int i = 0; i < SubjectCount; i++) {
- Subjects[i] = new Subject;
- lib >> *Subjects[i];
- }
- }
- return 0;
- }
- int Save(const char* LIB_NAME) {
- ofstream lib;
- lib.open(LIB_NAME);
- if (!lib.fail()) {
- lib << EmployeeCount << SubjectCount<< endl;
- lib << CEO;
- for (int i = 0; i < EmployeeCount; i++) {
- lib << *Staff[i];
- }
- for (int i = 0; i < SubjectCount; i++) {
- lib << (*Subjects[i]);
- }
- }
- return 0;
- }
- int AddEmployee(Employee* emp) {
- Staff[EmployeeCount] = emp;
- EmployeeCount += 1;
- return 0;
- }
- int AddSubj(Subject* subj){
- Subjects[SubjectCount] = subj;
- SubjectCount += 1;
- return 0;
- }
- int PrintEmp(int subj_num){
- if (subj_num>= SubjectCount) throw NoSubj;
- for(int i = 0; i< EmployeeCount; i++){
- if (Staff[i]->subject_num==subj_num){
- cout<<*Staff[i];
- }
- }
- return 0;
- }
- ~Department(){
- for(Employee* a:Staff){
- delete a;
- }
- for(Subject* a:Subjects){
- delete a;
- }
- }
- };
- int main()
- {
- Department dprt;
- try {
- dprt.Load("f.txt");
- int a;
- cin>>a;
- dprt.PrintEmp(a);
- }
- catch (class DateException e){
- cout<<e.what();
- }
- catch (class NoSubj e){
- cout<<e.what();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement