Advertisement
metalni

OOP Ispitni Zadaci SMS Poraki

Jun 19th, 2020
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. class SMS{
  7.     private:
  8.         char number[12];
  9.         double price;
  10.     public:
  11.         SMS(){
  12.             this->price = 0.0;
  13.             strcpy(this->number, "N/A");
  14.         }
  15.         SMS(const char * number, const double price){
  16.             strcpy(this->number, number);
  17.             this->price = price;
  18.         }
  19.         virtual ~SMS(){}
  20.         virtual const double SMS_cena(){
  21.             return this->price;
  22.         }
  23.         friend ostream &operator << (ostream &os, SMS &orig){
  24.             os << "Tel: " << orig.number << " - cena: " << orig.SMS_cena() << "den.\n";
  25.  
  26.             return os;
  27.         }
  28. };
  29.  
  30. class RegularSMS : public SMS{
  31.     private:
  32.         char msg[1000];
  33.         bool isRoaming;
  34.         static int rProcent;
  35.     public:
  36.         RegularSMS(){
  37.             strcpy(this->msg, "No Message");
  38.             this->isRoaming = false;
  39.         }
  40.         RegularSMS(const char * number, const double price, const char * msg, const bool isRoaming) : SMS(number, price){
  41.             strcpy(this->msg, msg);
  42.             this->isRoaming = isRoaming;            
  43.         }
  44.         ~RegularSMS(){}
  45.         static const void set_rProcent(const int newProcent){
  46.             rProcent = newProcent;
  47.         }
  48.         const double SMS_cena(){
  49.             int length = ((strlen(this->msg)-1)/160) + 1;
  50.             double total = SMS::SMS_cena();
  51.             if(this->isRoaming == true)
  52.                 total += total * rProcent/100;
  53.             else
  54.                 total += total * 0.18;
  55.             return total * length;
  56.         }
  57. };
  58. int RegularSMS::rProcent = 300;
  59.  
  60. class SpecialSMS : public SMS{
  61.     private:
  62.         bool isCharity;
  63.         static int sProcent;
  64.     public:
  65.         SpecialSMS(){
  66.             this->isCharity = false;
  67.         }
  68.         SpecialSMS(const char * number, const double price, const bool isCharity) : SMS(number, price){
  69.             this->isCharity = isCharity;
  70.         }
  71.         ~SpecialSMS(){}
  72.         static const void set_sProcent(const int newProcent){
  73.             sProcent = newProcent;
  74.         }
  75.         const double SMS_cena(){
  76.             if(this->isCharity)
  77.                 return SMS::SMS_cena();
  78.             else
  79.                 return SMS::SMS_cena() + SMS::SMS_cena() * sProcent/100;
  80.         }
  81. };
  82. int SpecialSMS::sProcent = 150;
  83.  
  84. const void vkupno_SMS(SMS** poraka, int n){
  85.     int regCount = 0; int specCount = 0;
  86.     double regTotal = 0.0; double specTotal = 0.0;
  87.     for(int i=0; i<n; i++){
  88.         RegularSMS *r = dynamic_cast<RegularSMS *>(poraka[i]);
  89.         if(r){
  90.             regCount++;
  91.             regTotal += poraka[i]->SMS_cena();
  92.         }
  93.         SpecialSMS *s = dynamic_cast<SpecialSMS *>(poraka[i]);
  94.         if(s){
  95.             specCount++;
  96.             specTotal += poraka[i]->SMS_cena();
  97.         }
  98.     }
  99.     cout << "Vkupno ima " << regCount << " regularni SMS poraki i nivnata cena e: " << regTotal << "\n";
  100.     cout << "Vkupno ima " << specCount << " specijalni SMS poraki i nivnata cena e: " << specTotal << "\n";
  101. }
  102.  
  103. int main(){
  104.  
  105.     char tel[20], msg[1000];
  106.     float cena;
  107.     float price;
  108.     int p;
  109.     bool roam, hum;
  110.     SMS  **sms;
  111.     int n;
  112.     int tip;
  113.  
  114.     int testCase;
  115.     cin >> testCase;
  116.  
  117.     if (testCase == 1){
  118.         cout << "====== Testing RegularSMS class ======" << endl;
  119.         cin >> n;
  120.         sms = new SMS *[n];
  121.  
  122.         for (int i = 0; i < n; i++){
  123.             cin >> tel;
  124.             cin >> cena;
  125.             cin.get();
  126.             cin.getline(msg, 1000);
  127.             cin >> roam;
  128.             cout << "CONSTRUCTOR" << endl;
  129.             sms[i] = new RegularSMS(tel, cena, msg, roam);
  130.             cout << "OPERATOR <<" << endl;
  131.             cout << *sms[i];
  132.         }
  133.         for (int i = 0; i<n; i++) delete sms[i];
  134.         delete[] sms;
  135.     }
  136.     if (testCase == 2){
  137.         cout << "====== Testing SpecialSMS class ======" << endl;
  138.         cin >> n;
  139.         sms = new SMS *[n];
  140.  
  141.         for (int i = 0; i < n; i++){
  142.             cin >> tel;
  143.             cin >> cena;
  144.             cin >> hum;
  145.             cout << "CONSTRUCTOR" << endl;
  146.             sms[i] = new SpecialSMS(tel, cena, hum);
  147.             cout << "OPERATOR <<" << endl;
  148.             cout << *sms[i];
  149.         }
  150.         for (int i = 0; i<n; i++) delete sms[i];
  151.         delete[] sms;
  152.     }
  153.     if (testCase == 3){
  154.         cout << "====== Testing method vkupno_SMS() ======" << endl;
  155.         cin >> n;
  156.         sms = new SMS *[n];
  157.  
  158.         for (int i = 0; i<n; i++){
  159.  
  160.             cin >> tip;
  161.             cin >> tel;
  162.             cin >> cena;
  163.             if (tip == 1) {
  164.  
  165.                 cin.get();
  166.                 cin.getline(msg, 1000);
  167.                 cin >> roam;
  168.                
  169.                 sms[i] = new RegularSMS(tel, cena, msg, roam);
  170.  
  171.             }
  172.             else {
  173.                 cin >> hum;
  174.  
  175.                 sms[i] = new SpecialSMS(tel, cena, hum);
  176.             }
  177.         }
  178.  
  179.         vkupno_SMS(sms, n);
  180.         for (int i = 0; i<n; i++) delete sms[i];
  181.         delete[] sms;
  182.     }
  183.     if (testCase == 4){
  184.         cout << "====== Testing RegularSMS class with a changed percentage======" << endl; 
  185.         SMS *sms1, *sms2;  
  186.             cin >> tel;
  187.             cin >> cena;
  188.             cin.get();
  189.             cin.getline(msg, 1000);
  190.             cin >> roam;
  191.             sms1 = new RegularSMS(tel, cena, msg, roam);
  192.             cout << *sms1;
  193.        
  194.             cin >> tel;
  195.             cin >> cena;
  196.             cin.get();
  197.             cin.getline(msg, 1000);
  198.             cin >> roam;   
  199.             cin >> p;
  200.             RegularSMS::set_rProcent(p);
  201.             sms2 = new RegularSMS(tel, cena, msg, roam);
  202.             cout << *sms2;
  203.        
  204.         delete sms1, sms2;
  205.     }
  206.     if (testCase == 5){
  207.         cout << "====== Testing SpecialSMS class with a changed percentage======" << endl; 
  208.         SMS *sms1, *sms2;  
  209.             cin >> tel;
  210.             cin >> cena;
  211.             cin >> hum;
  212.             sms1 = new SpecialSMS(tel, cena, hum);
  213.             cout << *sms1;
  214.        
  215.             cin >> tel;
  216.             cin >> cena;
  217.             cin >> hum;
  218.             cin >> p;
  219.             SpecialSMS::set_sProcent(p);
  220.             sms2 = new SpecialSMS(tel, cena, hum);
  221.             cout << *sms2;
  222.        
  223.         delete sms1, sms2;
  224.     }
  225.    
  226.     return 0;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement