Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<cstring>
- #include<iostream>
- using namespace std;
- class Secret{
- public:
- virtual double simpleEntropy() const =0;
- virtual int total()const =0;
- };
- bool operator==(Secret &levo ,Secret &desno)
- {
- return levo.total()==desno.total()&&levo.simpleEntropy()==desno.simpleEntropy();
- }
- bool operator!=(Secret &levo ,Secret &desno)
- {
- return levo.total()!=desno.total() || levo.simpleEntropy()!=desno.simpleEntropy();
- }
- class DigitSecret :public Secret {
- private:
- int cifri[50];
- int n;
- public:
- DigitSecret(){}
- DigitSecret(int *cifri,int n)
- {
- this->n=n;
- for(int i = 0 ; i < n ; i++)
- {
- this->cifri[i]=cifri[i];
- }
- }
- int total()const{return n;}
- double simpleEntropy()const{
- int count=0;
- double razlika=0.0;
- for(int i= 0 ; i < n ; i++)
- {
- count=0;
- for(int j = 0 ; j < n ; j++)
- {
- if((i!=j)&&cifri[i]==cifri[j])
- count++;
- }
- if(count==0)
- {
- razlika+=1.0;
- }
- }
- return razlika/total();
- }
- friend ostream &operator << (ostream &out, const DigitSecret &d){
- for (int i=0;i<d.total();i++)
- out<<d.cifri[i];
- out<<" Simple entropy: " << d.simpleEntropy() << " Total: "<<d.total();
- return out;
- }
- };
- class CharSecret :public Secret{//da se dopolni so izraz za nasleduvanje
- private:
- char karakteri[100];
- int n;
- public:
- CharSecret(char*karakteri)
- {
- for(int i = 0 ; i < strlen(karakteri); i ++)
- {
- this->karakteri[i]=karakteri[i];
- }
- this->n=strlen(karakteri);
- }
- int total()const{return n ; }
- double simpleEntropy()const
- {
- int count=0;
- double razlika=0.0;
- for(int i= 0 ; i < n ; i++)
- {
- count = 0;
- for(int j = 0 ; j < n ; j++)
- {
- if((i!=j)&&karakteri[i]==karakteri[j])
- {
- count++;
- }
- }
- if(count==0)
- razlika+=1.0;
- }
- return razlika/total();
- }
- friend ostream &operator << (ostream &out, const CharSecret &c){
- for (int i=0;i<c.n ; i++)
- out<<c.karakteri[i];
- out<<" Simple entropy: " << c.simpleEntropy() << " Total: "<<c.total()<<endl;
- return out;
- }
- };
- void process(Secret ** secrets, int n)
- {
- double max = -1;
- int idx = -1;
- for (int i = 0 ; i < n ; i ++){
- if (secrets[i]->simpleEntropy()>max){
- max=secrets[i]->simpleEntropy();
- idx=i;
- }
- }
- DigitSecret * d = dynamic_cast<DigitSecret*>(secrets[idx]);
- if (d!=0){
- cout<<*d;
- }
- CharSecret * c = dynamic_cast<CharSecret*>(secrets[idx]);
- if (c!=0){
- cout<<*c;
- }
- }
- void printAll (Secret ** secrets, int N)
- {
- for(int i = 0 ; i < N; i++){
- DigitSecret *d = dynamic_cast<DigitSecret *>(secrets[i]);
- if (d!=0){
- cout<<*d<<endl;
- }
- CharSecret * c = dynamic_cast<CharSecret *>(secrets[i]);
- if (c!=0){
- cout<<*c;
- }
- }
- }
- 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