Advertisement
metalni

OOP Labs 7 Tajni poraki

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