Advertisement
Josif_tepe

Untitled

Jun 19th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. class Secret{
  5.     public:
  6.     virtual double simpleEntropy() = 0;
  7.     virtual int total() = 0;
  8. };
  9.  
  10. //preoptovaruvanje na operator ==
  11.  
  12. //preoptovaruvanje na operator !=
  13.  
  14. class DigitSecret : public Secret { //da se dopolni so izraz za nasleduvanje
  15.     private:
  16.     int cifri[101];
  17.     int n;
  18.    
  19.     public:
  20.     DigitSecret() {}
  21.     DigitSecret(int *c, int _n) {
  22.         n = _n;
  23.         for(int i = 0; i < n; i++) {
  24.             cifri[i] = c[i];
  25.         }
  26.     }
  27.     virtual double simpleEntropy() override {
  28.         double unikatni = 0;
  29.         for(int i = 0; i < n; i++) {
  30.             int pom = 0;
  31.             for(int j = 0; j < n; j++) {
  32.                 if(i != j) {
  33.                     if(cifri[i] == cifri[j]) {
  34.                         pom++;
  35.                     }
  36.                 }
  37.             }
  38.             if(pom == 0) {
  39.                 unikatni++;
  40.             }
  41.         }
  42.         return unikatni / (double) n;
  43.     }
  44.     virtual int total() override {
  45.         return n;
  46.     }
  47.     bool operator == (DigitSecret ds) {
  48.         if(ds.n != n) {
  49.             return false;
  50.         }
  51.         for(int i = 0; i < n; i++) {
  52.             if(ds.cifri[i] != cifri[i]) {
  53.                 return false;
  54.             }
  55.         }
  56.         return true;
  57.     }
  58.     bool operator != (DigitSecret ds) {
  59.         if(n == ds.n) {
  60.             int tocno = 1;
  61.             for(int i = 0; i < n; i++) {
  62.                 if(cifri[i] != ds.cifri[i]) {
  63.                     tocno = 0;
  64.                     break;
  65.                 }
  66.             }
  67.             if(tocno == 1) {
  68.                 return false;
  69.             }
  70.         }
  71.         return true;
  72.     }
  73.    
  74.     //preoptovaruvanje na operatorot za pechatenje <<
  75.     friend ostream& operator << (ostream &stream, DigitSecret &ds);
  76.     void printDigit() {
  77.         for(int i = 0; i < n; i++) {
  78.             cout << cifri[i];
  79.         }
  80.         cout << " ";
  81.         cout << "Simple entropy: " << simpleEntropy() << " Total: " << total() << endl;
  82.     }
  83.    
  84. };
  85. ostream& operator << (ostream &stream, DigitSecret &ds) {
  86.     for(int i = 0; i < ds.n; i++) {
  87.         stream << ds.cifri[i];
  88.     }
  89.     stream << " ";
  90.     stream << "Simple entropy: " << ds.simpleEntropy() << " Total: " << ds.total() << endl;
  91.     return stream;
  92. }
  93.  
  94. class CharSecret : public Secret { //da se dopolni so izraz za nasleduvanje
  95.     private:
  96.     char bukvi[101];
  97.    
  98.     public:
  99.     CharSecret() {}
  100.     CharSecret(const char * b) {
  101.         strcpy(bukvi, b);
  102.     }
  103.    
  104.    
  105.     virtual double simpleEntropy() override {
  106.         double unikatni = 0;
  107.         for(int i = 0; i < strlen(bukvi); i++) {
  108.             int pom = 0;
  109.             for(int j = 0; j < strlen(bukvi); j++) {
  110.                 if(i != j) {
  111.                     if(bukvi[i] == bukvi[j]) {
  112.                         pom++;
  113.                     }
  114.                 }
  115.             }
  116.             if(pom == 0) {
  117.                 unikatni++;
  118.             }
  119.         }
  120.         return unikatni / (double) strlen(bukvi);
  121.     }
  122.     virtual int total() override {
  123.         return (int) strlen(bukvi);
  124.     }
  125.  
  126.     bool operator == (CharSecret cs) {
  127.         if(strlen(cs.bukvi) == strlen(bukvi)) {
  128.             for(int i = 0; i < strlen(bukvi); i++) {
  129.                 if(bukvi[i] != cs.bukvi[i]) {
  130.                     return false;
  131.                 }
  132.             }
  133.             return true;
  134.         }
  135.         return false;
  136.     }
  137.     bool operator != (CharSecret cs) {
  138.         if(strlen(cs.bukvi) == strlen(bukvi)) {
  139.             int isti = 1;
  140.             for(int i = 0; i < strlen(bukvi); i++) {
  141.                 if(bukvi[i] != cs.bukvi[i]) {
  142.                     isti = 0;
  143.                     break;
  144.                 }
  145.             }
  146.             if(isti == 1) {
  147.                 return false;
  148.             }
  149.         }
  150.         return true;
  151.     }
  152.    
  153.     void printChar() {
  154.         for(int i = 0; i < strlen(bukvi); i++) {
  155.             cout << bukvi[i];
  156.         }
  157.         cout << " ";
  158.         cout << "Simple entropy: " << simpleEntropy() << " Total: " << total() << endl;
  159.     }
  160.     friend ostream& operator << (ostream& stream, CharSecret &cs);
  161.    
  162. };
  163. ostream& operator << (ostream& stream, CharSecret &cs) {
  164.     for(int i = 0; i < strlen(cs.bukvi); i++) {
  165.         stream << cs.bukvi[i];
  166.     }
  167.     stream << " ";
  168.     stream << "Simple entropy: " << cs.simpleEntropy() << " Total: " << cs.total() << endl;
  169.     return stream;
  170. }
  171.  
  172. void process(Secret ** secrets, int n){
  173.     double maks = -2000000000;
  174.     int indeks = -1;
  175.     for(int i = 0; i < n; i++) {
  176.         if(maks < secrets[i]->simpleEntropy()) {
  177.             maks = secrets[i] -> simpleEntropy();
  178.             indeks = i;
  179.         }
  180.     }
  181.     if(dynamic_cast<CharSecret*>(secrets[indeks])) {
  182.         CharSecret cs = *dynamic_cast<CharSecret*>(secrets[indeks]);
  183.         cout << cs << endl;
  184.     }
  185.     else {
  186.         DigitSecret ds = *dynamic_cast<DigitSecret*>(secrets[indeks]);
  187.         cout << ds << endl;
  188.     }
  189. }
  190.  
  191. void printAll (Secret ** secrets, int n) {
  192.     for(int i = 0; i < n; i++) {
  193.         if(dynamic_cast<CharSecret*>(secrets[i])) {
  194.             CharSecret cs = *dynamic_cast<CharSecret*>(secrets[i]);
  195.             cout << cs << endl;
  196.         }
  197.         else {
  198.             DigitSecret ds = *dynamic_cast<DigitSecret*>(secrets[i]);
  199.             cout << ds << endl;
  200.         }
  201.     }
  202. }
  203.  
  204. bool operator == (CharSecret cs, DigitSecret ds) {
  205.     return false;
  206. }
  207. bool operator == (DigitSecret cs, CharSecret ds) {
  208.     return false;
  209. }
  210. bool operator != (CharSecret cs, DigitSecret ds) {
  211.     return true;
  212. }
  213. bool operator != (DigitSecret ds, CharSecret cs) {
  214.     return true;
  215. }
  216.  
  217. int main() {
  218.     int n;
  219.     cin >> n;
  220.     if(n == 0) {
  221.         cout << "Constructors" << endl;
  222.         int numbers [] = {1,2,3,4,5};
  223.         DigitSecret ds(numbers,5);
  224.         CharSecret cs("abcabc");
  225.         cout << "OK" << endl;
  226.     } else if(n == 1) {
  227.           cout << "operator <<" << endl;
  228.         int numbers [] = {1,2,3,4,5};
  229.         DigitSecret ds(numbers,5);
  230.         CharSecret cs("abcabc");
  231.         cout << ds << endl;
  232.         cout << cs << endl;
  233.     }  else if(n == 2) {
  234.           cout << "== and !=" << endl;
  235.         int numbers [] = {1,2,3,4,5};
  236.         DigitSecret ds(numbers,5);
  237.         CharSecret cs("abcabc");
  238.         CharSecret css("abcabc");
  239.         cout << (ds == cs) << endl;
  240.         cout << (cs != ds) << endl;
  241.         cout << (cs == css) << endl;
  242.         cout << (cs != css) << endl;
  243.     } else if(n == 3) {
  244.           cout << "Secret processor" << endl;
  245.         int numbers1 [] = {1,2,3,4,5,6,4,3,2,1,1,2,3,4,5};
  246.         DigitSecret ds1(numbers1,15);
  247.         int numbers2 [] = {1,2,3,4,5,0,0,0,5,5,4,4,3,3,2};
  248.         DigitSecret ds2(numbers2,15);
  249.         int numbers3 [] = {1,0,9,4,3,8,4,0,9,3,1,4,3,2,1,4,4,3,7,2};
  250.         DigitSecret ds3(numbers3,20);
  251.         CharSecret cs1("fhdaieowujkfjdsafjdsakjhueiqoyroq");
  252.         CharSecret cs2("nvzcfsadrqipqhfjdfncxoqw");
  253.         CharSecret cs3("uyoihfdsanmqeqrzvdhfeqyrq");
  254.         Secret** s = new Secret*[6];
  255.         s[0] = &ds1;
  256.         s[1] = &ds2;
  257.         s[2] = &ds3;
  258.         s[3] = &cs1;
  259.         s[4] = &cs2;
  260.         s[5] = &cs3;
  261.         process(s,6);
  262.         delete [] s;
  263.     }
  264.     else if (n==4){
  265.         cout << "Print all secrets" << endl;
  266.         int numbers1 [] = {1,2,3,4,5,5,4,3,2,1,1,2,3,4,5};
  267.         DigitSecret ds1(numbers1,15);
  268.         int numbers2 [] = {1,2,3,4,5,0,0,0,5,5,4,4,3,3,2};
  269.         DigitSecret ds2(numbers2,15);
  270.         int numbers3 [] = {1,0,9,4,3,8,4,0,9,3,1,4,3,2,1,4,4,3,7,2};
  271.         DigitSecret ds3(numbers3,20);
  272.         CharSecret cs1("fhdaieowujkfjdsafjdsakjhueiqoyroq");
  273.         CharSecret cs2("nvzcfsadrqipqhfjdfncxoqw");
  274.         CharSecret cs3("uyoihfdsanmqeqrzvdhfeqyrq");
  275.         Secret** s = new Secret*[6];
  276.         s[0] = &ds1;
  277.         s[1] = &ds2;
  278.         s[2] = &ds3;
  279.         s[3] = &cs1;
  280.         s[4] = &cs2;
  281.         s[5] = &cs3;
  282.         printAll(s,6);
  283.         delete [] s;
  284.     }
  285.    
  286.     return 0;
  287. }
  288. /*
  289.  n
  290.  v
  291.  z
  292.  c
  293.  f
  294.  s
  295.  a
  296.  d
  297.  r
  298.  i
  299.  p
  300.  q
  301.  h
  302. j
  303.  x
  304.  o
  305.  w
  306.  */
  307.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement