Advertisement
kostadinovska

[АВ 7.3] Тајни пораки

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