Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <exception>
  5. #include <strings.h>
  6. #include <vector>
  7.  
  8. class Department;
  9.  
  10. using namespace std;
  11.  
  12.  
  13. enum Position {Developer, Engineer, Assistant};
  14.  
  15. class DateException: exception {
  16. public:
  17.     const char* what() {
  18.         return "Invalid Date";
  19.     }
  20. }DateException;
  21.  
  22. class NoSubj:exception{
  23. public:
  24.     const char* what(){
  25.         return "No such subj";
  26.     }
  27. }NoSubj;
  28.  
  29. class Date {
  30.     friend class Subject;
  31.  
  32. public:
  33.     int day;
  34.     int month;
  35.     int year;
  36.  
  37.     Date(int d, int m, int y) {
  38.         if (y <= 0) {
  39.             throw DateException;
  40.         }
  41.         if (m <= 0 || m > 12){
  42.             throw DateException;
  43.         }
  44.         if (d <= 0 || d > 31) {
  45.             throw DateException;
  46.         }
  47.  
  48.         day = d;
  49.         month = m;
  50.         year = y;
  51.     }
  52.     Date() {
  53.         day = 1;
  54.         month = 1;
  55.         year = 2007;
  56.     }
  57.  
  58.     Date& operator =(const Date& right) {
  59.         day = right.day;
  60.         month = right.day;
  61.         year = right.year;
  62.         return *this;
  63.     }
  64.     friend istream& operator >>(istream& is, Date& a) {
  65.         is >> a.day>>a.month>>a.year;
  66.         if (a.year <= 0) {
  67.             throw DateException;
  68.         }
  69.         if (a.month <= 0 || a.month > 12){
  70.             throw DateException;
  71.         }
  72.         if (a.day <= 0 || a.day > 31) {
  73.             throw DateException;
  74.         }
  75.         return is;
  76.     }
  77.     friend ostream& operator <<(ostream& os, Date& a){
  78.         os<<a.day<<endl<<a.month<<endl<<a.year;
  79.  
  80.         return  os;
  81.     }
  82. };
  83.  
  84. class FIO {
  85.     friend class Employee;
  86.     friend class Department;
  87.  
  88. public:
  89.     char *first = new char[256];
  90.     char *midle = new char[256];
  91.     char *last = new char[256];
  92.  
  93.     FIO(char *f, char *m, char *l) {
  94.         strcpy(first, f);
  95.         strcpy(midle, m);
  96.         strcpy(last, l);
  97.     }
  98.     FIO() {
  99.         strcpy(first, "A");
  100.         strcpy(midle, "B");
  101.         strcpy(last, "C");
  102.     }
  103.  
  104.     char* ToString() {
  105.         char* buf = new char[256];
  106.         strcat(buf, first);
  107.         strcat(buf, " ");
  108.         strcat(buf, midle);
  109.         strcat(buf, " ");
  110.         strcat(buf, last);
  111.         return buf;
  112.     }
  113.  
  114.     friend istream& operator >>(istream& is, FIO& a) {
  115.         is >> a.first >> a.midle >> a.last;
  116.         return is;
  117.     }
  118.     friend ostream &operator<<(ostream &os, const FIO &fio) {
  119.         os << fio.first<<endl<<fio.midle<<endl<< fio.last;
  120.         return os;
  121.     }
  122.     ~FIO(){
  123.         delete [] first;
  124.         delete [] midle;
  125.         delete [] last;
  126.     }
  127. };
  128.  
  129. class Subject {
  130.     friend class Date;
  131.     char *name = new char[256];
  132.     Date deadline;
  133.     int number=0;
  134.  
  135. public:
  136.     Subject(char* n, Date dl, int num) {
  137.         strcmp(name, n);
  138.         deadline = dl;
  139.         number = num;
  140.     }
  141.     Subject()= default;
  142.  
  143.     Subject(int){
  144.         Subject();
  145.         cin>>*this;
  146.     }
  147.  
  148.     friend istream& operator >>(istream& is, Subject& a) {
  149.         is >> a.name >> a.deadline>> a.number;
  150.         return is;
  151.     }
  152.     friend ostream &operator<<(ostream &os, Subject &subj) {
  153.         os<<subj.deadline<<endl<<subj.name<<endl<<subj.number;
  154.         return os;
  155.     }
  156.  
  157.     char* ToString(){
  158.         char* buf = new char[256];
  159.         char* bb = new char[16];
  160.         strcat(buf,name);
  161.         strcat(buf," ");
  162.         itoa(number,bb,10);
  163.         strcat(buf,bb);
  164.         return buf;
  165.     }
  166.     Subject& operator=(Subject& rhs){
  167.         deadline = rhs.deadline;
  168.         strcpy(name,rhs.name);
  169.         number = rhs.number;
  170.         return *this;
  171.     }
  172.     ~Subject(){
  173.         delete [] name;
  174.     }
  175. };
  176.  
  177. class Employee {
  178.     friend class Department;
  179.     FIO* FullName;
  180.     Position position;
  181.     int subject_num;
  182.     Subject* subj;
  183. public:
  184.     Employee() {
  185.         FullName = new FIO;
  186.         position = Position::Developer;
  187.         subject_num = -1;
  188.     }
  189.     friend ifstream& operator >>(ifstream& is, Employee& a) {
  190.         int b;
  191.         is >> *a.FullName>>b>>a.subject_num;
  192.         a.position = (Position)b;
  193.         return is;
  194.     }
  195.  
  196.     friend ostream &operator<<(ostream &os, const Employee &employee) {
  197.         os << *employee.FullName <<endl<< employee.position <<endl<< employee.subject_num;
  198.         return os;
  199.     }
  200.  
  201. //    int ToString(){
  202. //        char *buf = new char[512];
  203. //        strcat(buf,FullName->ToString());
  204. //        strcat(buf," ");
  205. //        strcat(buf,subj->ToString());
  206. //        return 0;
  207. //    }
  208.  
  209. };
  210. class Department {
  211.     friend class Employee;
  212.     FIO CEO;
  213.     vector<Employee*> Staff;
  214.     vector<Subject*> Subjects;
  215.     int EmployeeCount = 0;
  216.     int SubjectCount = 0;
  217.  
  218. public:
  219.     Department()= default;
  220.     int Load(const char* LIB_NAME) {
  221.         ifstream lib;
  222.         Staff.reserve(32);
  223.         Subjects.reserve(32);
  224.         lib.open(LIB_NAME);
  225.         if (!lib.fail()) {
  226.             lib >> EmployeeCount >> SubjectCount;
  227.             lib >> CEO;
  228.             for (int i = 0; i < EmployeeCount; i++) {
  229.                 Staff[i] = new Employee;
  230.                 lib >> *Staff[i];
  231.                 if (Staff[i]->subject_num >= SubjectCount){
  232.                     throw NoSubj;
  233.                 }
  234.             }
  235.             for (int i = 0; i < SubjectCount; i++) {
  236.                 Subjects[i] = new Subject;
  237.                 lib >> *Subjects[i];
  238.             }
  239.  
  240.         }
  241.         return 0;
  242.     }
  243.     int Save(const char* LIB_NAME) {
  244.         ofstream lib;
  245.  
  246.         lib.open(LIB_NAME);
  247.         if (!lib.fail()) {
  248.             lib << EmployeeCount << SubjectCount<< endl;
  249.             lib << CEO;
  250.             for (int i = 0; i < EmployeeCount; i++) {
  251.                 lib << *Staff[i];
  252.             }
  253.             for (int i = 0; i < SubjectCount; i++) {
  254.                 lib << (*Subjects[i]);
  255.  
  256.             }
  257.  
  258.         }
  259.         return 0;
  260.     }
  261.     int AddEmployee(Employee* emp) {
  262.         Staff[EmployeeCount] = emp;
  263.         EmployeeCount += 1;
  264.         return 0;
  265.     }
  266.     int AddSubj(Subject* subj){
  267.         Subjects[SubjectCount] = subj;
  268.         SubjectCount += 1;
  269.         return 0;
  270.     }
  271.     int PrintEmp(int subj_num){
  272.         if (subj_num>= SubjectCount) throw NoSubj;
  273.         for(int i = 0; i< EmployeeCount; i++){
  274.             if (Staff[i]->subject_num==subj_num){
  275.                 cout<<*Staff[i];
  276.             }
  277.         }
  278.         return 0;
  279.     }
  280.     ~Department(){
  281.         for(Employee* a:Staff){
  282.             delete a;
  283.         }
  284.         for(Subject* a:Subjects){
  285.             delete a;
  286.         }
  287.     }
  288. };
  289.  
  290.  
  291.  
  292.  
  293. int main()
  294. {
  295.  
  296.     Department dprt;
  297.     try {
  298.         dprt.Load("f.txt");
  299.  
  300.         int a;
  301.         cin>>a;
  302.         dprt.PrintEmp(a);
  303.     }
  304.     catch (class DateException e){
  305.         cout<<e.what();
  306.     }
  307.     catch (class NoSubj e){
  308.         cout<<e.what();
  309.     }
  310.     return 0;
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement