Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <map>
  4. #include <ctime>
  5. #include <string>
  6. #include <stdlib.h>
  7. #include <vector>
  8. #include <list>
  9. #include <iterator>
  10. #include <algorithm>
  11. #include <utility>
  12. #include <Windows.h>
  13. #include <tchar.h>
  14. using namespace std;
  15.  
  16. class CPerson
  17. {
  18. private:
  19.     string name;
  20.     string EGN;
  21.  
  22. public:
  23.     CPerson()
  24.     {
  25.         name = " ";
  26.         EGN = " ";
  27.     }
  28.  
  29.     CPerson(const string n, const string e)
  30.     {
  31.         name = n;
  32.         EGN = e;
  33.     }
  34.  
  35.     string getname()const
  36.     {
  37.         return name;
  38.     }
  39.  
  40.     string getEGN()const
  41.     {
  42.         return EGN;
  43.     }
  44.  
  45.     void setname(const string n)
  46.     {
  47.         name = n;
  48.     }
  49.  
  50.     void setEGN(const string e)
  51.     {
  52.         EGN = e;
  53.     }
  54.  
  55.     virtual void print() = 0; //1.1
  56.     /*
  57.     int getAge() const //1.2
  58.     {
  59.         int age;
  60.         struct tm buf;
  61.         time_t now = time(0);
  62.         tm *ltm = localtime_s(&now,&buf);
  63.         int year = atoi(getEGN().substr(0, 2).c_str());
  64.         int month = atoi(getEGN().substr(2, 2).c_str());
  65.         int day = atoi(getEGN().substr(4, 2).c_str());
  66.         int cyear = 1900 + ltm->tm_year;
  67.         int cmonth = 1 + ltm->tm_mon;
  68.         int cday = 1 + ltm->tm_mday;
  69.  
  70.         age = cyear - (year + 1900);
  71.         if (cmonth < month)
  72.             age--;
  73.         if (cmonth == month && cday < day)
  74.             age--;
  75.         return age;
  76.     }
  77.     */
  78.  
  79.     int getAge() const //1.2
  80.     {
  81.         int age;
  82.         time_t now = time(0);
  83.         tm *ltm = localtime(&now);
  84.         int year = atoi(getEGN().substr(0, 2).c_str());
  85.         int month = atoi(getEGN().substr(2, 2).c_str());
  86.         int day = atoi(getEGN().substr(4, 2).c_str());
  87.         int cyear = 1900 + ltm->tm_year;
  88.         int cmonth = 1 + ltm->tm_mon;
  89.         int cday = 1 + ltm->tm_mday;
  90.  
  91.         age = cyear - (year + 1900);
  92.         if (cmonth < month)
  93.             age--;
  94.         if (cmonth == month && cday < day)
  95.             age--;
  96.         return age;
  97.     }
  98.     /*
  99.     SYSTEMTIME time;
  100.     while (true)
  101.     {
  102.         GetLocalTime(&time);
  103.  
  104.  
  105.     }
  106.     return 0;
  107.     */
  108. };
  109.  
  110. class CStudent : public CPerson
  111. {
  112.  
  113. private:
  114.     string FN;
  115.     map<int, int> st_tests;
  116.  
  117.  
  118.  
  119. public:
  120.     CStudent()
  121.     {
  122.         FN = " ";
  123.     }
  124.  
  125.     CStudent(const string n)
  126.     {
  127.         FN = n;
  128.     }
  129.  
  130.     CStudent(const string o, const string p, const string n) :CPerson(o, p)
  131.     {
  132.         FN = n;
  133.     }
  134.  
  135.     void setFN(const string n)
  136.     {
  137.         FN = n;
  138.     }
  139.  
  140.     void setst_tests(map<int, int> m)
  141.     {
  142.         st_tests = m;
  143.  
  144.     }
  145.  
  146.     string getFN() const
  147.     {
  148.         return FN;
  149.     }
  150.  
  151.     bool operator () (CStudent a, CStudent b) const
  152.     {
  153.         return a.average() < b.average();
  154.     }
  155.  
  156.     map<int, int> getst_tests()
  157.     {
  158.         return st_tests;
  159.     }
  160.  
  161.     void print()
  162.     {
  163.         cout << "Ime: " << getname() << endl;
  164.         cout << "EGN: " << getEGN() << endl;
  165.         cout << "FN: " << getFN() << endl;
  166.         auto it = st_tests.begin();
  167.         while (it != st_tests.end())
  168.         {
  169.             cout << it->first << " " << it->second << endl;
  170.             it++;
  171.         }
  172.     }
  173.  
  174.     void add_st_tests(int a, int b)
  175.     {
  176.        
  177.          st_tests.insert(pair<int, int>(a, b));
  178.        
  179.     }
  180.  
  181.     double average() //2.1
  182.     {
  183.         double sum = 0;
  184.         auto it = st_tests.begin();
  185.         for (it = st_tests.begin(); it != st_tests.end(); it++)
  186.             sum += it->second;
  187.         if (st_tests.size() != 0)
  188.             return sum / st_tests.size();
  189.         return -1;
  190.     }
  191.  
  192.     int search(const int a) //2.2
  193.     {
  194.         return st_tests.find(a)->second;
  195.     }
  196.  
  197. };
  198.  
  199. class CSpeciality
  200. {
  201. private:
  202.     string spec;
  203.     int kurs;
  204.     int grupa;
  205.     vector<CStudent> students;
  206.  
  207. public:
  208.     string getspec() const
  209.     {
  210.         return spec;
  211.     }
  212.  
  213.     int getkurs() const
  214.     {
  215.         return kurs;
  216.     }
  217.  
  218.     int getgrupa() const
  219.     {
  220.         return grupa;
  221.     }
  222.  
  223.     vector<CStudent> getstudents()
  224.     {
  225.         return students;
  226.     }
  227.  
  228.     void setstudents(vector<CStudent> a)
  229.     {
  230.         students = a;
  231.     }
  232.  
  233.     void setspec(const string n)
  234.     {
  235.         spec = n;
  236.     }
  237.  
  238.     void setkurs(const int n)
  239.     {
  240.         kurs = n;
  241.     }
  242.  
  243.     void setgrupa(const int n)
  244.     {
  245.         grupa = n;
  246.     }
  247.  
  248.     CSpeciality(const string s, const int k, const int g)
  249.     {
  250.         spec = s;
  251.         kurs = k;
  252.         grupa = g;
  253.  
  254.     }
  255.  
  256.     void addstudent(CStudent &a)
  257.     {
  258.         students.push_back(a);
  259.     }
  260.  
  261.     /* int ReadFile() //3.1
  262.     {
  263.     ifstream st;
  264.     st.open("students.txt",ios::in);
  265.     if(!st)
  266.     {
  267.     cout<<"Cannot open students.txt or file does not exist."<<endl;
  268.     return 0;
  269.     }
  270.     string a, b, c;
  271.     int d, e, i=0;
  272.     if (st.is_open())
  273.     {
  274.     do
  275.     {
  276.     st >> a >> b >> c;
  277.     students[i].setname(a);
  278.     students[i].setEGN(b);
  279.     students[i].setFN(c);
  280.     do
  281.     {
  282.     st >> d >> e;
  283.     students[i].add_st_tests(d,e);
  284.     }while(st.peek() != '\n' || st.peek() != '\r');
  285.     i++;
  286.     }while(!st.eof());
  287.     }
  288.     st.close();
  289.     }*/
  290.  
  291.     double averagetest(int a) //3.2
  292.     {
  293.         double sum = 0;
  294.         int br = 0;
  295.         vector<CStudent>::iterator itt;
  296.         for (itt = students.begin(); itt != students.end(); itt++)
  297.         {
  298.             map<int, int>::iterator it = (*itt).getst_tests().find(a);
  299.             while (itt != students.end())
  300.                 sum += it->second;
  301.             br++;
  302.         }
  303.         cout << sum / br;
  304.         return sum / br;
  305.     }
  306.  
  307.     list<CStudent> averageparam(const int a, const int b) //3.3
  308.     {
  309.         list<CStudent> l;
  310.         int i = 0;
  311.         vector<CStudent>::iterator itt = students.begin();
  312.         for (itt = students.begin(); itt != students.end(); itt++)
  313.         {
  314.             if ((*itt).average() >= a && (*itt).average() <= b)
  315.                 l.push_back(*itt);
  316.             i++;
  317.         }
  318.         cout << "List ot studenti sus sreden broi tochki mejdu " << a << " - " << b << endl;
  319.         list<CStudent>::iterator it = l.begin();
  320.         for (it = l.begin(); it != l.end(); it++)
  321.             (*it).print();
  322.         return l;
  323.     }
  324.  
  325.     int averageabove(const int a) //3.4
  326.     {
  327.         int br = 0;
  328.         vector<CStudent>::iterator itt = students.begin();
  329.         for (itt = students.begin(); itt != students.end(); itt++)
  330.             if ((*itt).average() > a)
  331.                 br++;
  332.         cout << "Broi studenti sus sreden broi tochki nad " << a << ": " << br << endl;
  333.         return br;
  334.     }
  335.     /*
  336.     void averageage(const int a) //3.5
  337.     {
  338.         cout << "Sreden uspeh na " << a << " godishni studenti." << endl;
  339.         vector<CStudent>::iterator itt = students.begin();
  340.         for (itt = students.begin(); itt != students.end(); itt++)
  341.         {
  342.             int b = (*itt).getAge();
  343.             if (a == b)
  344.                 cout << (*itt).getname() << " " << (*itt).average() << endl;
  345.         }
  346.     }
  347.     */
  348.     void beststudent() //3.6
  349.     {
  350.         cout << "Student s nai-visoka uspevaemost." << endl;
  351.         CStudent temp;
  352.         vector<CStudent>::iterator itt = students.begin();
  353.         for (itt = students.begin(); itt != students.end(); itt++)
  354.             if ((*itt).average() > temp.average())
  355.                 temp = (*itt);
  356.         temp.print();
  357.     }
  358.  
  359.     void sortaverage() //3.7
  360.     {
  361.         sort(students.begin(), students.end(), CStudent());
  362.     }
  363.    
  364.     /*
  365.     bool compare(CStudent &a, CStudent &b)
  366.     {
  367.         return a.average() < b.average();
  368.     }
  369.  
  370.     void sortaverage() //3.7
  371.     {
  372.         sort(students.begin(), students.end(), compare);
  373.  
  374.     }
  375.     */
  376. };
  377.  
  378. int main()
  379. {
  380.  
  381.     CStudent p1("Ivan", "9711156070", "61360140");
  382.     CStudent p2("Petar", "9703041020", "61360127");
  383.     CStudent p3("Mihail", "9708032540", "61360134");
  384.     p1.add_st_tests(1, 55);
  385.     p1.add_st_tests(2, 80);
  386.     p1.add_st_tests(3, 69);
  387.     p2.add_st_tests(1, 9);
  388.     p2.add_st_tests(2, 25);
  389.     p2.add_st_tests(3, 56);
  390.     p3.add_st_tests(1, 32);
  391.     p3.add_st_tests(2, 87);
  392.     p3.add_st_tests(3, 57);
  393.     CSpeciality g1("SIT", 1, 1);
  394.     g1.addstudent(p1);
  395.     g1.addstudent(p2);
  396.     g1.addstudent(p3);
  397.     g1.averagetest(2);
  398. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement