Advertisement
StefiIOE

Tajni poraki Polimorfizam

May 17th, 2020
1,292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.61 KB | None | 0 0
  1. #include<cstring>
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. class Secret{
  6.     public:
  7.     virtual double simpleEntropy() const =0;
  8.     virtual int total()const =0;
  9. };
  10. bool operator==(Secret &levo ,Secret &desno)
  11. {
  12. return levo.total()==desno.total()&&levo.simpleEntropy()==desno.simpleEntropy();
  13. }
  14. bool operator!=(Secret &levo ,Secret &desno)
  15. {
  16. return levo.total()!=desno.total() || levo.simpleEntropy()!=desno.simpleEntropy();
  17. }
  18.  
  19. class DigitSecret :public Secret {
  20.     private:
  21.     int cifri[50];  
  22.     int n;
  23.     public:
  24.     DigitSecret(){}
  25.     DigitSecret(int *cifri,int n)
  26.     {
  27.     this->n=n;
  28.         for(int i = 0 ; i < n ; i++)
  29.         {
  30.         this->cifri[i]=cifri[i];
  31.         }
  32.     }
  33.    
  34.     int total()const{return n;}
  35.    
  36.     double simpleEntropy()const{
  37.     int count=0;
  38.     double razlika=0.0;
  39.    
  40.         for(int i= 0 ; i < n ; i++)
  41.         {
  42.             count=0;
  43.             for(int j = 0 ; j < n ; j++)
  44.             {
  45.                 if((i!=j)&&cifri[i]==cifri[j])                
  46.                 count++;                
  47.             }
  48.             if(count==0)
  49.             {
  50.             razlika+=1.0;
  51.             }
  52.            
  53.         }
  54.     return razlika/total();
  55.     }
  56.         friend ostream &operator << (ostream &out, const DigitSecret &d){
  57.         for (int i=0;i<d.total();i++)
  58.             out<<d.cifri[i];
  59.         out<<" Simple entropy: " << d.simpleEntropy() << " Total: "<<d.total();
  60.         return out;
  61.     }
  62.  
  63.    
  64. };
  65.  
  66. class CharSecret :public Secret{//da se dopolni so izraz za nasleduvanje
  67.     private:
  68.     char karakteri[100];
  69.     int n;
  70.     public:
  71.     CharSecret(char*karakteri)
  72.     {
  73.         for(int i = 0 ; i < strlen(karakteri); i ++)
  74.         {
  75.         this->karakteri[i]=karakteri[i];
  76.            
  77.         }
  78.         this->n=strlen(karakteri);
  79.     }
  80.     int total()const{return n ; }
  81.    
  82.     double simpleEntropy()const
  83.     {
  84.         int count=0;
  85.         double razlika=0.0;
  86.    
  87.         for(int i= 0 ; i < n ; i++)
  88.         {
  89.             count = 0;
  90.             for(int j = 0 ; j < n ; j++)
  91.             {
  92.                 if((i!=j)&&karakteri[i]==karakteri[j])
  93.                
  94.                 {
  95.                 count++;
  96.                 }
  97.             }
  98.             if(count==0)
  99.            
  100.             razlika+=1.0;
  101.            
  102.            
  103.         }
  104.     return razlika/total();
  105.    
  106.    
  107.     }
  108.     friend ostream &operator << (ostream &out, const CharSecret &c){
  109.         for (int i=0;i<c.n ; i++)
  110.             out<<c.karakteri[i];
  111.         out<<" Simple entropy: " << c.simpleEntropy() << " Total: "<<c.total()<<endl;
  112.         return out;
  113.     }
  114.  
  115.        
  116.    
  117. };
  118.  
  119. void process(Secret ** secrets, int n)
  120. {
  121.         double max = -1;
  122.         int idx = -1;
  123.         for (int i = 0 ; i < n ; i ++){
  124.                 if (secrets[i]->simpleEntropy()>max){
  125.                
  126.                     max=secrets[i]->simpleEntropy();
  127.                
  128.                     idx=i;
  129.             }
  130.         }
  131.        
  132.         DigitSecret * d = dynamic_cast<DigitSecret*>(secrets[idx]);
  133.         if (d!=0){
  134.             cout<<*d;
  135.         }
  136.        
  137.         CharSecret * c = dynamic_cast<CharSecret*>(secrets[idx]);
  138.         if (c!=0){
  139.             cout<<*c;
  140.         }
  141.        
  142. }
  143.  
  144. void printAll (Secret ** secrets, int N)
  145. {
  146.     for(int i = 0 ; i < N; i++){
  147.     DigitSecret *d = dynamic_cast<DigitSecret *>(secrets[i]);
  148.         if (d!=0){
  149.             cout<<*d<<endl;
  150.         }
  151.        
  152.         CharSecret * c = dynamic_cast<CharSecret *>(secrets[i]);
  153.         if (c!=0){
  154.             cout<<*c;
  155.         }
  156.     }
  157. }
  158.  
  159.  
  160.  
  161.  
  162. int main() {
  163.     int n;
  164.     cin >> n;
  165.     if(n == 0) {
  166.         cout << "Constructors" << endl;
  167.         int numbers [] = {1,2,3,4,5};
  168.         DigitSecret ds(numbers,5);
  169.         CharSecret cs("abcabc");
  170.         cout << "OK" << endl;
  171.     } else if(n == 1) {
  172.         cout << "operator <<" << endl;
  173.         int numbers [] = {1,2,3,4,5};
  174.         DigitSecret ds(numbers,5);
  175.         CharSecret cs("abcabc");
  176.         cout << ds << endl;
  177.         cout << cs << endl;
  178.     }  else if(n == 2) {
  179.         cout << "== and !=" << endl;
  180.         int numbers [] = {1,2,3,4,5};
  181.         DigitSecret ds(numbers,5);
  182.         CharSecret cs("abcabc");
  183.         CharSecret css("abcabc");
  184.         cout << (ds == cs) << endl;
  185.         cout << (cs != ds) << endl;
  186.         cout << (cs == css) << endl;
  187.         cout << (cs != css) << endl;
  188.     } else if(n == 3) {
  189.         cout << "Secret processor" << endl;
  190.         int numbers1 [] = {1,2,3,4,5,6,4,3,2,1,1,2,3,4,5};
  191.         DigitSecret ds1(numbers1,15);
  192.         int numbers2 [] = {1,2,3,4,5,0,0,0,5,5,4,4,3,3,2};
  193.         DigitSecret ds2(numbers2,15);
  194.         int numbers3 [] = {1,0,9,4,3,8,4,0,9,3,1,4,3,2,1,4,4,3,7,2};
  195.         DigitSecret ds3(numbers3,20);
  196.         CharSecret cs1("fhdaieowujkfjdsafjdsakjhueiqoyroq");
  197.         CharSecret cs2("nvzcfsadrqipqhfjdfncxoqw");
  198.         CharSecret cs3("uyoihfdsanmqeqrzvdhfeqyrq");
  199.         Secret** s = new Secret*[6];
  200.         s[0] = &ds1;
  201.         s[1] = &ds2;
  202.         s[2] = &ds3;
  203.         s[3] = &cs1;
  204.         s[4] = &cs2;
  205.         s[5] = &cs3;
  206.         process(s,6);
  207.         delete [] s;
  208.     }
  209.     else if (n==4){
  210.         cout << "Print all secrets" << endl;
  211.         int numbers1 [] = {1,2,3,4,5,5,4,3,2,1,1,2,3,4,5};
  212.         DigitSecret ds1(numbers1,15);
  213.         int numbers2 [] = {1,2,3,4,5,0,0,0,5,5,4,4,3,3,2};
  214.         DigitSecret ds2(numbers2,15);
  215.         int numbers3 [] = {1,0,9,4,3,8,4,0,9,3,1,4,3,2,1,4,4,3,7,2};
  216.         DigitSecret ds3(numbers3,20);
  217.         CharSecret cs1("fhdaieowujkfjdsafjdsakjhueiqoyroq");
  218.         CharSecret cs2("nvzcfsadrqipqhfjdfncxoqw");
  219.         CharSecret cs3("uyoihfdsanmqeqrzvdhfeqyrq");
  220.         Secret** s = new Secret*[6];
  221.         s[0] = &ds1;
  222.         s[1] = &ds2;
  223.         s[2] = &ds3;
  224.         s[3] = &cs1;
  225.         s[4] = &cs2;
  226.         s[5] = &cs3;
  227.         printAll(s,6);
  228.         delete [] s;
  229.     }
  230.    
  231.     return 0;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement