Advertisement
HeatPulse

Secret

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