Advertisement
metalni

zadaca 1

Jul 20th, 2020
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.01 KB | None | 0 0
  1.         #include<iostream>
  2.         #include<cstring>
  3.  
  4.         using namespace std;
  5.  
  6.         class NotValidStudentException{
  7.             private:
  8.                 char msg[256];
  9.             public:
  10.                 NotValidStudentException(const char * msg){
  11.                     strcpy(this->msg, msg);
  12.                 }
  13.                 const void message(){
  14.                     cout << this->msg;
  15.                 }
  16.         };
  17.  
  18.         class Student{
  19.             private:
  20.                 char ime[50];
  21.                 int index;
  22.                 int * poeni;
  23.                 int broj;
  24.                 double zavrsenPoeni;
  25.                 static double udelIspit;
  26.                 static double udelTest;
  27.                 const void copy_obj(const Student &copy){
  28.                     strcpy(this->ime, copy.ime);
  29.                     this->index = copy.index;
  30.                     this->poeni = new int[copy.broj + 1];
  31.                     for(int i=0; i<copy.broj; i++)
  32.                         this->poeni[i] = copy.poeni[i];
  33.                     this->broj = copy.broj;
  34.                     this->zavrsenPoeni = copy.zavrsenPoeni;
  35.                 }
  36.             public:
  37.                 Student(){
  38.                     strcpy(this->ime, "Unknown");
  39.                     this->index = 0;
  40.                     this->poeni = new int[0];
  41.                     this->broj = 0;
  42.                     this->zavrsenPoeni = 0.0;
  43.                 }
  44.                 Student(const char * ime, const int index, const int * poeni, const int broj, const double zavrsenPoeni){
  45.                     strcpy(this->ime, ime);
  46.                     this->index = index;
  47.                     this->poeni = new int[broj + 1];
  48.                     for(int i=0; i<broj; i++)
  49.                         this->poeni[i] = poeni[i];
  50.                     this->broj = broj;
  51.                     this->zavrsenPoeni = zavrsenPoeni;
  52.                 }
  53.                 Student(const Student &copy){
  54.                     this->copy_obj(copy);
  55.                 }
  56.                 Student &operator=(const Student &copy){
  57.                     if(this != &copy){
  58.                         delete [] this->poeni;
  59.                         this->copy_obj(copy);
  60.                     }
  61.                     return *this;
  62.                 }
  63.                 ~Student(){
  64.                     delete [] this->poeni;
  65.                 }
  66.                 static void examContribution(double newPercent){
  67.                     udelIspit = newPercent;
  68.                     udelTest = 1.0 - newPercent;
  69.                 }
  70.                 const double getPoeni(){
  71.                     double total=0.0;
  72.                     for(int i=0; i<this->broj; i++)
  73.                         total += this->poeni[i];
  74.                     return total;
  75.                 }
  76.                 const double sumarniPoeni(){
  77.                     double testPoints = this->getPoeni();
  78.                     if(this->getPoeni() > 100.0)
  79.                         testPoints = 100.0;
  80.                     return (this->zavrsenPoeni * udelIspit) + (testPoints * udelTest);
  81.                 }
  82.                 const int ocenka(){
  83.                     if(this->sumarniPoeni() > 0 && this->sumarniPoeni()<=49.99)
  84.                         return 5;
  85.                     else if(this->sumarniPoeni() >= 50 && this->sumarniPoeni()<=59.99)
  86.                         return 6;
  87.                     else if(this->sumarniPoeni() >= 60 && this->sumarniPoeni()<=69.99)
  88.                         return 7;
  89.                     else if(this->sumarniPoeni() >= 70 && this->sumarniPoeni()<=79.99)
  90.                         return 8;
  91.                     else if(this->sumarniPoeni() >= 80 && this->sumarniPoeni()<=89.99)
  92.                         return 9;
  93.                     else if(this->sumarniPoeni() >= 90 && this->sumarniPoeni()<=100.0)
  94.                         return 10;
  95.                 }
  96.                 friend ostream &operator << (ostream &os, Student &orig){
  97.                     os << orig.index << " " <<orig.ime << "\n";
  98.                     if(orig.getPoeni() > 100)
  99.                         os << "Testovi: " << 100 << "\n";
  100.                     else os << "Testovi: " << orig.getPoeni() << "\n";
  101.                     os << "Zavrshen ispit: " << orig.zavrsenPoeni << "\n";
  102.                     os << "Sumarni poeni: " << orig.sumarniPoeni() << "\n";
  103.                     os << "Ocenka: " << orig.ocenka() << "\n";
  104.  
  105.                     return os;
  106.                 }
  107.                 const int getBroj(){
  108.                     return this->broj;
  109.                 }
  110.         };
  111.  
  112.         class Course{
  113.             private:
  114.                 Student *stu;
  115.                 int broj;
  116.             public:
  117.                 Course(){
  118.                     this->stu = new Student[0];
  119.                     this->broj = 0;
  120.                 }
  121.                 ~Course(){
  122.                     delete [] this->stu;
  123.                 }
  124.                 Course &operator+=(Student &add){
  125.                     if(add.getBroj() == 0)
  126.                         throw NotValidStudentException("Korisnikot nema pravo na potpis\n");
  127.                     Student * tmp = new Student[this->broj + 1];
  128.                     for(int i=0; i<this->broj; i++)
  129.                         tmp[i] = this->stu[i];
  130.                     tmp[this->broj++] = add;
  131.                     delete [] this->stu;
  132.                     this->stu = tmp;
  133.  
  134.                     return *this;
  135.                 }
  136.                 int averageMark(){
  137.                     int sum = 0;
  138.                     for(int i=0; i<this->broj; i++){
  139.                         sum += this->stu[i].ocenka();
  140.                     }
  141.                        
  142.                     return (sum/this->broj) + 1;
  143.                 }
  144.                 friend ostream &operator << (ostream &os, Course &orig){
  145.                     for(int i=0; i<orig.broj; i++)
  146.                         os << orig.stu[i];
  147.                     os << "Prosecna ocenka na polozenite: " <<orig.averageMark() << "\n";
  148.                     return os;
  149.                 }
  150.         };
  151.         double Student::udelIspit = 0.75;
  152.         double Student::udelTest = 0.25;
  153.  
  154.         int main () {
  155.  
  156.             int testCase;
  157.             int index;
  158.             char name [50];
  159.             int n;
  160.             int * tests;
  161.             double exam;
  162.             double newPercent;
  163.  
  164.             cin>>testCase;
  165.  
  166.             if (testCase==1) {
  167.                 cout<<"Testing Student constructor and operator <<"<<endl;
  168.                 cin>>name;
  169.                 cin>>index;
  170.                 cin>>n;
  171.                 tests = new int [n];
  172.                 for (int i=0;i<n;i++){
  173.                     cin>>tests[i];
  174.                 }
  175.                 cin>>exam;
  176.                 Student s (name, index, tests, n, exam);
  177.                 cout<<s<<endl;
  178.  
  179.             }
  180.             else if (testCase == 2) {
  181.                 cout<<"Testing Student copy constructor and operator <<"<<endl;
  182.                 cin>>name;
  183.                 cin>>index;
  184.                 cin>>n;
  185.                 tests = new int [n];
  186.                 for (int i=0;i<n;i++){
  187.                     cin>>tests[i];
  188.                 }
  189.                 cin>>exam;
  190.                 Student s (name, index, tests, n, exam);
  191.                 Student s1 (s);
  192.                 cout<<s1<<endl;
  193.  
  194.             } else if (testCase == 3) {
  195.                 cout<<"Testing Student operator = and operator <<"<<endl;
  196.  
  197.                 cin>>name;
  198.                 cin>>index;
  199.                 cin>>n;
  200.                 tests = new int [n];
  201.                 for (int i=0;i<n;i++){
  202.                     cin>>tests[i];
  203.                 }
  204.                 cin>>exam;
  205.                 Student s (name, index, tests, n, exam);
  206.                 Student s1;
  207.                 s1=s;
  208.                 cout<<s1<<endl;
  209.  
  210.             } else if (testCase == 4) {
  211.                 cout<<"Testing Course class, operator += and operator <<"<<endl;
  212.                 Course c;
  213.                 int studentsCount;
  214.                 cin>>studentsCount;
  215.  
  216.                 for (int j=0;j<studentsCount;j++) {
  217.                     cin>>name;
  218.                     cin>>index;
  219.                     cin>>n;
  220.                     tests = new int [n];
  221.                     for (int i=0;i<n;i++){
  222.                         cin>>tests[i];
  223.                     }
  224.                     cin>>exam;
  225.                     Student s (name, index, tests, n, exam);
  226.                     c+=s;
  227.                 }
  228.  
  229.                 cout<<c;
  230.             } else if (testCase == 5) {
  231.                 cout<<"Testing Course class, operator += with exceptions and operator <<"<<endl;
  232.                 Course c;
  233.                 int studentsCount;
  234.                 cin>>studentsCount;
  235.  
  236.                 for (int j=0;j<studentsCount;j++) {
  237.                     cin>>name;
  238.                     cin>>index;
  239.                     cin>>n;
  240.                     tests = new int [n];
  241.                     for (int i=0;i<n;i++){
  242.                         cin>>tests[i];
  243.                     }
  244.                     cin>>exam;
  245.                     Student s (name, index, tests, n, exam);
  246.                     try {
  247.                         c+=s;
  248.                     }
  249.                     catch (NotValidStudentException e) {
  250.                         e.message();
  251.                     }
  252.                 }
  253.  
  254.                 cout<<c;
  255.             } else if (testCase == 6) {
  256.                 cout<<"Testing examContribution function in Student"<<endl;
  257.                 cin>>name;
  258.                 cin>>index;
  259.                 cin>>n;
  260.                 tests = new int [n];
  261.                 for (int i=0;i<n;i++){
  262.                     cin>>tests[i];
  263.                 }
  264.                 cin>>exam;
  265.                 Student s (name, index, tests, n, exam);
  266.                 cout<<s<<endl;
  267.                 Student::examContribution(0.6);
  268.                 cout<<s<<endl;
  269.             } else if (testCase == 7) {
  270.                 cout<<"Test all (without exceptions)"<<endl;
  271.                 Course c;
  272.                 int studentsCount;
  273.                 cin>>studentsCount;
  274.  
  275.                 for (int j=0;j<studentsCount;j++) {
  276.                     cin>>name;
  277.                     cin>>index;
  278.                     cin>>n;
  279.                     tests = new int [n];
  280.                     for (int i=0;i<n;i++){
  281.                         cin>>tests[i];
  282.                     }
  283.                     cin>>exam;
  284.                     Student s (name, index, tests, n, exam);
  285.                     c+=s;
  286.                 }
  287.  
  288.                 cout<<c;
  289.  
  290.                 cout<<"After change of exam percent"<<endl;
  291.                 Student::examContribution(0.55);
  292.  
  293.                 cout<<c;
  294.             }
  295.  
  296.  
  297.             return 0;
  298.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement