Advertisement
P1ayer4312

7) Полиморфизам C++

May 1st, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.25 KB | None | 0 0
  1. ============================================ Жичани инструменти =============================================================
  2. #include <iostream>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. class ZicanInstrument {
  9. protected:
  10.     char ime[20];
  11.     int brojZici;
  12.     int osnovnaCena;
  13. public:
  14.     ZicanInstrument (const char *ime="",int brojZici=0,int osnovnaCena=0)
  15.     {
  16.         strcpy(this->ime,ime);
  17.         this->brojZici = brojZici;
  18.         this->osnovnaCena = osnovnaCena;
  19.     }
  20.    
  21.     ~ZicanInstrument () {}
  22.    
  23.     bool operator== (ZicanInstrument &z1)
  24.     {
  25.         if (z1.brojZici == brojZici)
  26.             return true;
  27.         else
  28.             return false;
  29.     }
  30.    
  31.     virtual int cena ()=0;
  32.    
  33. };
  34.  
  35. class Mandolina : public ZicanInstrument {
  36. protected:
  37.     char forma[20];
  38. public:
  39.    
  40.         Mandolina (const char *ime="",int brojZici=0,int osnovnaCena=0,const char *forma="") : ZicanInstrument (ime,brojZici,osnovnaCena)
  41.     {
  42.         strcpy(this->forma,forma);
  43.     }
  44.    
  45.     ~Mandolina () {}
  46.    
  47.     int cena ()
  48.     {
  49.         if (!strcmp(forma,"Neapolitan"))
  50.         {
  51.                 return osnovnaCena + osnovnaCena * 0.15;
  52.         }
  53.         else
  54.             return osnovnaCena;
  55.     }
  56.    
  57.     friend ostream &operator<< (ostream &o,const Mandolina &m)
  58.     {
  59.         o<<m.ime<<" "<<m.brojZici<<" "<<m.osnovnaCena<<" "<<m.forma<<endl;
  60.         return o;
  61.     }
  62.    
  63. };
  64.  
  65.  
  66. class Violina: public ZicanInstrument {
  67. protected:
  68.     float golemina;
  69. public:
  70.    
  71.     Violina (const char *ime="",int brojZici=0,int osnovnaCena=0,float golemina=0) : ZicanInstrument (ime,brojZici,osnovnaCena)
  72.     {
  73.         this->golemina = golemina;
  74.     }
  75.    
  76.     ~Violina () {}
  77.    
  78.     int cena ()
  79.     {
  80.         if (golemina == 0.25)
  81.             return osnovnaCena + osnovnaCena * 0.1;
  82.         else if (golemina == 1.00)
  83.             return osnovnaCena + osnovnaCena * 0.2;
  84.         else
  85.             return osnovnaCena;
  86.     }
  87.        
  88.     friend ostream &operator<< (ostream &o,const Violina &v)
  89.     {
  90.         o<<v.ime<<" "<<v.brojZici<<" "<<v.osnovnaCena<<" "<<v.golemina<<endl;
  91.     }
  92. };
  93.  
  94.  
  95. void pecatiInstrumenti (ZicanInstrument &zi,ZicanInstrument **i,int n)
  96. {
  97.         for (int k=0;k<n;k++)
  98.     {
  99.         if (zi == *(i[k]))
  100.         {
  101.                 cout<< i[k] -> cena() << endl;
  102.         }
  103.            
  104.     }
  105.  
  106. }
  107.  
  108.  
  109. int main() {
  110.         char ime[20];
  111.         int brojZici;
  112.         float cena;
  113.         char forma[20];
  114.         cin >> ime >> brojZici >> cena >> forma;
  115.         Mandolina m(ime, brojZici, cena, forma);
  116.         int n;
  117.         cin >> n;
  118.         ZicanInstrument **zi = new ZicanInstrument*[2 * n];
  119.         for(int i = 0; i < n; ++i) {
  120.                 cin >> ime >> brojZici >> cena >> forma;
  121.                 zi[i] = new Mandolina(ime, brojZici, cena, forma);
  122.         }
  123.         for(int i = 0; i < n; ++i) {
  124.                 float golemina;
  125.                 cin >> ime >> brojZici >> cena >> golemina;
  126.                 zi[n + i] = new Violina(ime, brojZici, cena, golemina);
  127.         }
  128.         pecatiInstrumenti(m, zi, 2 * n);
  129.         for(int i = 0; i < 2 * n; ++i) {
  130.                 delete zi[i];
  131.         }
  132.         delete [] zi;
  133.         return 0;
  134. }
  135.  
  136. ================================================== Numbers ===============================================================
  137. #include<iostream>
  138. using namespace std;
  139.  
  140. class Number {
  141.     public:
  142.     virtual double doubleValue() = 0;
  143.     virtual int intValue() = 0;
  144.     virtual void print() = 0;
  145.     bool operator==(Number& n)
  146.     {
  147.         return (this->doubleValue() == n.doubleValue());
  148.     }
  149. };
  150.  
  151.  
  152. class Integer : public Number{
  153.     private:
  154.         int broj;
  155.     public:
  156.  
  157.     Integer(int broj)
  158.     {
  159.         this->broj = broj;
  160.     }
  161.  
  162.     int intValue()
  163.     {
  164.         return broj;
  165.     }
  166.     double doubleValue()
  167.     {
  168.         return double(broj);
  169.     }
  170.     void print()
  171.     {
  172.         cout << broj;
  173.     }
  174. };
  175.  
  176. class Double : public Number{
  177.     private:
  178.  
  179.         double broj;
  180.     public:
  181.  
  182.     Double(double broj)
  183.     {
  184.         this->broj = broj;
  185.     }
  186.  
  187.     int intValue()
  188.     {
  189.         return int(broj);
  190.     }
  191.     double doubleValue()
  192.     {
  193.         return broj;
  194.     }
  195.     void print()
  196.     {
  197.         cout << broj;
  198.     }
  199.    
  200. };
  201.  
  202. class Numbers{
  203.     private:
  204.  
  205.         Number** niza;
  206.         int brElem;
  207.     public:
  208.  
  209.     Numbers()
  210.     {
  211.         niza = new Number*[0];
  212.         brElem = 0;
  213.     }
  214.  
  215.     Numbers(const Numbers& n)
  216.     {
  217.         brElem = n.brElem;
  218.         niza = new Number*[brElem];
  219.         for (int i = 0; i < n.brElem; i++)
  220.         {
  221.             niza[i] = n.niza[i];
  222.         }
  223.     }
  224.  
  225.     Numbers& operator=(const Numbers& n)
  226.     {
  227.         brElem = n.brElem;
  228.         niza = new Number*[brElem];
  229.         for (int i = 0; i < n.brElem; i++)
  230.         {
  231.             niza[i] = n.niza[i];
  232.         }
  233.         return *this;
  234.     }
  235.  
  236.     ~Numbers()
  237.     {
  238.         delete[] niza;
  239.     }
  240.  
  241.     Numbers& operator+=(Number* n)
  242.     {
  243.         bool add = true;
  244.         for (int i = 0; i < brElem; i++)
  245.         {
  246.             if (*niza[i] == *n)
  247.             {
  248.                 add = false;
  249.             }
  250.         }
  251.         if (add)
  252.         {
  253.             Number** nova = new Number*[brElem + 1];
  254.             for (int i = 0; i < brElem; i++)
  255.             {
  256.                 nova[i] = niza[i];
  257.             }
  258.             if (add)
  259.             {
  260.                 nova[brElem] = n;
  261.                 brElem++;
  262.             }
  263.             delete[] niza;
  264.             niza = nova;
  265.         }
  266.         return *this;
  267.        
  268.     }
  269.  
  270.     void statistics()
  271.     {
  272.         cout << "Count of numbers: " << brElem << endl;
  273.         double allSum = 0;
  274.         for (int i = 0; i < brElem; i++)
  275.         {
  276.             allSum += niza[i]->doubleValue();
  277.         }
  278.         cout << "Sum of all numbers: " << allSum << endl;
  279.         int countIntegers = 0;
  280.         int intSum = 0;
  281.         for (int i = 0; i < brElem; i++)
  282.         {
  283.             if (dynamic_cast<Integer*>(niza[i]))
  284.             {
  285.                 countIntegers++;
  286.                 intSum += niza[i]->intValue();
  287.             }
  288.         }
  289.         cout << "Count of integer numbers: " << countIntegers << endl;
  290.         cout << "Sum of integer numbers: " << intSum << endl;
  291.         int countDoubles = 0;
  292.         double doubleSum = 0;
  293.         for (int i = 0; i < brElem; i++)
  294.         {
  295.             if (dynamic_cast<Double*>(niza[i]))
  296.             {
  297.                 countDoubles++;
  298.                 doubleSum += niza[i]->doubleValue();
  299.             }
  300.         }
  301.         cout <<"Count of double numbers: " << countDoubles << endl;
  302.         cout <<"Sum of double numbers: " << doubleSum << endl;
  303.     }
  304.  
  305.     void integersLessThan(Integer n)
  306.     {
  307.         bool none = true;
  308.         for (int i = 0; i < brElem; i++)
  309.         {
  310.             if (dynamic_cast<Integer*>(niza[i]))
  311.             {
  312.                 if (niza[i]->intValue() < n.intValue())
  313.                 {
  314.                     none = false;
  315.                     cout << "Integer: " << niza[i]->intValue() << endl;
  316.                 }
  317.             }
  318.         }
  319.         if (none) cout << "None" << endl;
  320.     }
  321.     void doublesBiggerThan(Double n)
  322.     {
  323.         bool none = true;
  324.         for (int i = 0; i < brElem; i++)
  325.         {
  326.             if (dynamic_cast<Double*>(niza[i]))
  327.             {
  328.                 if (niza[i]->doubleValue()  > n.doubleValue())
  329.                 {
  330.                     none = false;
  331.                     cout << "Double: " << niza[i]->doubleValue() << endl;
  332.                 }
  333.             }
  334.         }
  335.         if (none) cout << "None" << endl;
  336.     }
  337. };
  338.  
  339. int main() {
  340.    
  341.     int n;
  342.     cin>>n;
  343.     Numbers numbers;
  344.     for (int i=0;i<n;i++){
  345.         int type;
  346.         double number;
  347.         cin>>type>>number;
  348.         if (type==0){//Integer object
  349.             Integer * integer = new Integer((int) number);
  350.             numbers+=integer;
  351.         }
  352.         else {
  353.             Double * doublee = new Double(number);
  354.             numbers+=doublee;
  355.         }
  356.     }
  357.    
  358.     int lessThan;
  359.     double biggerThan;
  360.    
  361.     cin>>lessThan;
  362.     cin>>biggerThan;       
  363.    
  364.     cout<<"STATISTICS FOR THE NUMBERS\n";
  365.     numbers.statistics();
  366.     cout<<"INTEGER NUMBERS LESS THAN "<<lessThan<<endl;
  367.     numbers.integersLessThan(Integer(lessThan));
  368.     cout<<"DOUBLE NUMBERS BIGGER THAN "<<biggerThan<<endl;
  369.     numbers.doublesBiggerThan(Double(biggerThan));
  370.    
  371.     return 0;
  372. }
  373.  
  374. ================================================ Тајни пораки ===============================================================
  375. #include<cstring>
  376. #include<iostream>
  377. using namespace std;
  378.  
  379. class Secret {
  380.     public:
  381.     virtual double simpleEntropy() const = 0;
  382.     virtual int total() const = 0;
  383. };
  384.  
  385. class DigitSecret : public Secret{
  386.     private:
  387.     int digits [100] ;
  388.     int length;
  389.    
  390.     public:
  391.     DigitSecret(const int * digits=0, int length=0){
  392.         this->length=length;
  393.         for (int i=0;i<length;i++)
  394.             this->digits[i]=digits[i];
  395.     }
  396.    
  397.     int total()const {
  398.         return length;
  399.     }
  400.    
  401.     double simpleEntropy() const {
  402.         double different=0.0;
  403.         int count = 0;
  404.        
  405.         for (int i=0;i<length;i++){
  406.             count = 0;
  407.             for (int j=0;j<length;j++){
  408.                 if ((i!=j)&&(digits[i]==digits[j]))
  409.                     count++;
  410.             }
  411.             if (count==0)
  412.                 different+=1.0;
  413.         }
  414.        
  415.         return different/total();
  416.     }
  417.    
  418.     friend ostream &operator << (ostream &out, const DigitSecret &ds){
  419.         for (int i=0;i<ds.total();i++)
  420.             out<<ds.digits[i];
  421.         out<<" Simple entropy: " << ds.simpleEntropy() << " Total: "<<ds.total();
  422.         return out;
  423.     }
  424. };
  425.  
  426. class CharSecret : public Secret{
  427.     private:
  428.     char chars [100] ;
  429.     int length;
  430.    
  431.     public:
  432.     CharSecret(const char * chars=""){
  433.         for (int i=0;i<strlen(chars);i++)
  434.             this->chars[i]=chars[i];
  435.         this->length=strlen(chars);
  436.     }
  437.    
  438.     int total() const {
  439.         return length;
  440.     }
  441.    
  442.     double simpleEntropy() const {
  443.         double different=0.0;
  444.         int count = 0;
  445.        
  446.         for (int i=0;i<length;i++){
  447.             count = 0;
  448.             for (int j=0;j<length;j++){
  449.                 if ((i!=j)&&(chars[i]==chars[j]))
  450.                     count++;
  451.             }
  452.             if (count==0)
  453.                 different+=1.0;
  454.         }
  455.        
  456.         return different/total();
  457.     }
  458.     friend ostream &operator << (ostream &out, const CharSecret &cs){
  459.         for (int i=0;i<cs.total();i++)
  460.             out<<cs.chars[i];
  461.         out<<" Simple entropy: " << cs.simpleEntropy() << " Total: "<<cs.total();
  462.         return out;
  463.     }
  464.    
  465.    
  466. };
  467. bool operator ==(const Secret &s1, const Secret &s2){
  468.         return s1.total()==s2.total()&&s1.simpleEntropy()==s2.simpleEntropy();
  469. }
  470.  
  471. bool operator !=(const Secret &s1, const Secret &s2){
  472.         return s1.total()!=s2.total() || s1.simpleEntropy()!=s2.simpleEntropy();
  473. }
  474.  
  475.  
  476.  
  477. void process (Secret ** secrets, int n){
  478.         double max = -1;
  479.         int idx = -1;
  480.         for (int i=0;i<n;i++){
  481.             if (secrets[i]->simpleEntropy()>max){
  482.                 max=secrets[i]->simpleEntropy();
  483.                 idx=i;
  484.             }
  485.         }
  486.        
  487.         DigitSecret * ds = dynamic_cast<DigitSecret *>(secrets[idx]);
  488.         if (ds!=0){
  489.             cout<<*ds;
  490.         }
  491.        
  492.         CharSecret * cs = dynamic_cast<CharSecret *>(secrets[idx]);
  493.         if (cs!=0){
  494.             cout<<*cs;
  495.         }
  496. }
  497.  
  498. void printAll(Secret ** secrets, int n){
  499.     for (int i=0;i<n;i++){
  500.         DigitSecret * ds = dynamic_cast<DigitSecret *>(secrets[i]);
  501.         if (ds!=0){
  502.             cout<<*ds<<endl;
  503.         }
  504.        
  505.         CharSecret * cs = dynamic_cast<CharSecret *>(secrets[i]);
  506.         if (cs!=0){
  507.             cout<<*cs<<endl;
  508.         }
  509.     }
  510. }
  511.  
  512.  
  513.  
  514. int main() {
  515.     int n;
  516.     cin >> n;
  517.     if(n == 0) {
  518.         cout << "Constructors" << endl;
  519.         int numbers [] = {1,2,3,4,5};
  520.         DigitSecret ds(numbers,5);
  521.         CharSecret cs("abcabc");
  522.         cout << "OK" << endl;
  523.     } else if(n == 1) {
  524.         cout << "operator <<" << endl;
  525.         int numbers [] = {1,2,3,4,5};
  526.         DigitSecret ds(numbers,5);
  527.         CharSecret cs("abcabc");
  528.         cout << ds << endl;
  529.         cout << cs << endl;
  530.     }  else if(n == 2) {
  531.         cout << "== and !=" << endl;
  532.         int numbers [] = {1,2,3,4,5};
  533.         DigitSecret ds(numbers,5);
  534.         CharSecret cs("abcabc");
  535.         CharSecret css("abcabc");
  536.         cout << (ds == cs) << endl;
  537.         cout << (cs != ds) << endl;
  538.         cout << (cs == css) << endl;
  539.         cout << (cs != css) << endl;
  540.     } else if(n == 3) {
  541.         cout << "Secret processor" << endl;
  542.         int numbers1 [] = {1,2,3,4,5,6,4,3,2,1,1,2,3,4,5};
  543.         DigitSecret ds1(numbers1,15);
  544.         int numbers2 [] = {1,2,3,4,5,0,0,0,5,5,4,4,3,3,2};
  545.         DigitSecret ds2(numbers2,15);
  546.         int numbers3 [] = {1,0,9,4,3,8,4,0,9,3,1,4,3,2,1,4,4,3,7,2};
  547.         DigitSecret ds3(numbers3,20);
  548.         CharSecret cs1("fhdaieowujkfjdsafjdsakjhueiqoyroq");
  549.         CharSecret cs2("nvzcfsadrqipqhfjdfncxoqw");
  550.         CharSecret cs3("uyoihfdsanmqeqrzvdhfeqyrq");
  551.         Secret** s = new Secret*[6];
  552.         s[0] = &ds1;
  553.         s[1] = &ds2;
  554.         s[2] = &ds3;
  555.         s[3] = &cs1;
  556.         s[4] = &cs2;
  557.         s[5] = &cs3;
  558.         process(s,6);
  559.         delete [] s;
  560.     }
  561.     else if (n==4){
  562.         cout << "Print all secrets" << endl;
  563.         int numbers1 [] = {1,2,3,4,5,5,4,3,2,1,1,2,3,4,5};
  564.         DigitSecret ds1(numbers1,15);
  565.         int numbers2 [] = {1,2,3,4,5,0,0,0,5,5,4,4,3,3,2};
  566.         DigitSecret ds2(numbers2,15);
  567.         int numbers3 [] = {1,0,9,4,3,8,4,0,9,3,1,4,3,2,1,4,4,3,7,2};
  568.         DigitSecret ds3(numbers3,20);
  569.         CharSecret cs1("fhdaieowujkfjdsafjdsakjhueiqoyroq");
  570.         CharSecret cs2("nvzcfsadrqipqhfjdfncxoqw");
  571.         CharSecret cs3("uyoihfdsanmqeqrzvdhfeqyrq");
  572.         Secret** s = new Secret*[6];
  573.         s[0] = &ds1;
  574.         s[1] = &ds2;
  575.         s[2] = &ds3;
  576.         s[3] = &cs1;
  577.         s[4] = &cs2;
  578.         s[5] = &cs3;
  579.         printAll(s,6);
  580.         delete [] s;
  581.     }
  582.     return 0;
  583. }
  584. ====================================================== Shapes =================================================================
  585. #include <iostream>
  586. #include <cmath>
  587. using namespace std;
  588.  
  589. class Shape{
  590. protected:
  591.     int a;
  592. public:
  593.     Shape(){}
  594.     Shape(int a){
  595.     this->a=a;
  596.     }
  597.     virtual double plostina() {}
  598.     virtual void pecati() {}
  599.     virtual int getType() {}
  600. };
  601.  
  602. class Square : public Shape{
  603. public:
  604.     Square(int a) : Shape(a){}
  605.     double plostina(){
  606.     return (double)a*a;
  607.     }
  608.     void pecati(){
  609.     cout<<"Kvadrat so plostina = "<<plostina()<<endl;
  610.     }
  611.     int getType(){
  612.     return 1;
  613.     }
  614. };
  615.  
  616. class Krug : public Shape{
  617. public:
  618.     Krug(int a) : Shape(a){}
  619.     double plostina(){
  620.     return 3.14 * a * a;
  621.     }
  622.     void pecati(){
  623.      cout<<"Krug so plostina = "<<plostina()<<endl;
  624.     }
  625.     int getType(){
  626.     return 2;
  627.     }
  628. };
  629.  
  630. class Triagolnik : public Shape{
  631. public:
  632.     Triagolnik(int a) : Shape(a){}
  633.     double plostina(){
  634.     return (sqrt(3)/4) * (double)a * a;
  635.     }
  636.     void pecati(){
  637.      cout<<"Triagolnik so plostina = "<<plostina()<<endl;
  638.     }
  639.     int getType(){
  640.     return 3;
  641.     }
  642. };
  643.  
  644. void checkNumTypes(Shape** niza, int n){
  645. int square=0, krug=0, triagolnik=0;
  646.    for(int i=0;i<n;++i){
  647.     if(niza[i]->getType()==1)
  648.        square++;
  649.     if(niza[i]->getType()==2)
  650.        krug++;
  651.     if(niza[i]->getType()==3)
  652.        triagolnik++;
  653.        }
  654.     cout<<"Broj na kvadrati vo nizata = "<<square<<endl;
  655.     cout<<"Broj na krugovi vo nizata = "<<krug<<endl;
  656.     cout<<"Broj na triagolnici vo nizata = "<<triagolnik<<endl;
  657. }
  658.  
  659. int main(){
  660.     int n;
  661.     cin >> n;
  662.  
  663.     Shape **niza;
  664.  
  665.     niza=new Shape*[50];
  666.  
  667.     int classType;
  668.     int side;
  669.  
  670.     for(int i = 0; i < n; ++i){
  671.  
  672.         cin >> classType;
  673.         cin >> side;
  674.         if(classType==1)
  675.         niza[i]=new Square(side);
  676.         if(classType==2)
  677.            niza[i]=new Krug(side);
  678.         if(classType==3)
  679.             niza[i]=new Triagolnik(side);
  680.     }
  681.  
  682. for(int i = 0; i < n; ++i){
  683.  
  684.         niza[i]->pecati();
  685.     }
  686.  
  687.     checkNumTypes(niza, n);
  688.  
  689.     return 0;
  690. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement