Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- class Secret
- {
- public:
- virtual double simpleEntropy() = 0;
- virtual int total() = 0;
- };
- class DigitSecret : public Secret
- {
- private:
- int di[100];
- int n;
- public:
- DigitSecret(int *d, int nn)
- {
- for(int i = 0; i < n; ++i)
- di[i] = d[i];
- n = nn;
- }
- int total()
- {
- return n;
- }
- double simpleEntropy() {
- double different = 0.0;
- int count = 0;
- for (int i = 0;i < n; i++){
- count = 0;
- for (int j = 0;j < n; j++){
- if ((i != j) && (di[i] == di[j]))
- count++;
- }
- if (count==0)
- different+=1.0;
- }
- return different/total();
- }
- friend ostream &operator << (ostream &o, DigitSecret &ds){
- for (int i = 0 ; i < ds.total(); i++)
- o << ds.di[i];
- o <<" Simple entropy: " << ds.simpleEntropy() << " Total: "<< ds.total();
- return o;
- }
- };
- bool operator ==( Secret &s1, Secret &s2){
- return s1.total() == s2.total() && s1.simpleEntropy() == s2.simpleEntropy();
- }
- bool operator != ( Secret &s1, Secret &s2){
- return s1.total() != s2.total() || s1.simpleEntropy() != s2.simpleEntropy();
- }
- class CharSecret : public Secret
- {
- private:
- char ch[100];
- int n;
- public:
- CharSecret(char *c)
- {
- for(int i = 0; i < strlen(ch) ; i++)
- ch[i] = c[i];
- n = strlen(ch);
- }
- int total()
- {
- return n;
- }
- double simpleEntropy() {
- double different = 0.0;
- int count = 0;
- for (int i = 0;i < n; i++){
- count = 0;
- for (int j = 0; j < n; j++){
- if ((i!=j) && (ch[i] == ch[j]))
- count++;
- }
- if (count == 0)
- different += 1.0;
- }
- return different/total();
- }
- friend ostream &operator << (ostream &o, CharSecret &cs){
- for (int i = 0; i < cs.total(); i++)
- o << cs.ch[i];
- o<<" Simple entropy: " << cs.simpleEntropy() << " Total: "<< cs.total();
- return o;
- }
- };
- void process(Secret **secrets, int nn)
- {
- int max = 0;
- int j = 0;
- for(int i = 0; i < nn; i++)
- {
- if(secrets[i] ->simpleEntropy() > max)
- {
- max = secrets[i] ->simpleEntropy();
- j = i;
- }
- }
- DigitSecret *ds = dynamic_cast <DigitSecret *>(secrets[j]);
- if(ds != 0)
- cout << *ds;
- CharSecret *cs = dynamic_cast <CharSecret *>(secrets[j]);
- if(cs != 0)
- cout << *cs;
- }
- void printAll(Secret **secrets, int n)
- {
- for(int i = 0; i < n; i++)
- {
- DigitSecret *ds = dynamic_cast<DigitSecret*>(secrets[i]);
- if(ds != 0)
- cout << *ds << endl;
- CharSecret *cs = dynamic_cast <CharSecret *>(secrets[i]);
- if(cs != 0)
- cout << *cs;
- }
- }
- int main() {
- int n;
- cin >> n;
- if(n == 0) {
- cout << "Constructors" << endl;
- int numbers [] = {1,2,3,4,5};
- DigitSecret ds(numbers,5);
- CharSecret cs("abcabc");
- cout << "OK" << endl;
- } else if(n == 1) {
- cout << "operator <<" << endl;
- int numbers [] = {1,2,3,4,5};
- DigitSecret ds(numbers,5);
- CharSecret cs("abcabc");
- cout << ds << endl;
- cout << cs << endl;
- } else if(n == 2) {
- cout << "== and !=" << endl;
- int numbers [] = {1,2,3,4,5};
- DigitSecret ds(numbers,5);
- CharSecret cs("abcabc");
- CharSecret css("abcabc");
- cout << (ds == cs) << endl;
- cout << (cs != ds) << endl;
- cout << (cs == css) << endl;
- cout << (cs != css) << endl;
- } else if(n == 3) {
- cout << "Secret processor" << endl;
- int numbers1 [] = {1,2,3,4,5,6,4,3,2,1,1,2,3,4,5};
- DigitSecret ds1(numbers1,15);
- int numbers2 [] = {1,2,3,4,5,0,0,0,5,5,4,4,3,3,2};
- DigitSecret ds2(numbers2,15);
- int numbers3 [] = {1,0,9,4,3,8,4,0,9,3,1,4,3,2,1,4,4,3,7,2};
- DigitSecret ds3(numbers3,20);
- CharSecret cs1("fhdaieowujkfjdsafjdsakjhueiqoyroq");
- CharSecret cs2("nvzcfsadrqipqhfjdfncxoqw");
- CharSecret cs3("uyoihfdsanmqeqrzvdhfeqyrq");
- Secret** s = new Secret*[6];
- s[0] = &ds1;
- s[1] = &ds2;
- s[2] = &ds3;
- s[3] = &cs1;
- s[4] = &cs2;
- s[5] = &cs3;
- process(s,6);
- delete [] s;
- }
- else if (n==4){
- cout << "Print all secrets" << endl;
- int numbers1 [] = {1,2,3,4,5,5,4,3,2,1,1,2,3,4,5};
- DigitSecret ds1(numbers1,15);
- int numbers2 [] = {1,2,3,4,5,0,0,0,5,5,4,4,3,3,2};
- DigitSecret ds2(numbers2,15);
- int numbers3 [] = {1,0,9,4,3,8,4,0,9,3,1,4,3,2,1,4,4,3,7,2};
- DigitSecret ds3(numbers3,20);
- CharSecret cs1("fhdaieowujkfjdsafjdsakjhueiqoyroq");
- CharSecret cs2("nvzcfsadrqipqhfjdfncxoqw");
- CharSecret cs3("uyoihfdsanmqeqrzvdhfeqyrq");
- Secret** s = new Secret*[6];
- s[0] = &ds1;
- s[1] = &ds2;
- s[2] = &ds3;
- s[3] = &cs1;
- s[4] = &cs2;
- s[5] = &cs3;
- printAll(s,6);
- delete [] s;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement