Advertisement
metalni

OOP Labs 7 Numbers

Jun 2nd, 2020
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class Number {
  6.     public:
  7.         virtual const double doubleValue() = 0;
  8.         virtual const int intValue() = 0;
  9.         virtual const void print() = 0;
  10. };
  11.  
  12. bool operator==(Number *left, Number &right){
  13.     if(left->doubleValue() == right.doubleValue())
  14.         return true;
  15.     else
  16.         return false;
  17. }
  18.  
  19. class Integer : public Number {
  20.     private:
  21.         int integer;
  22.     public:
  23.         Integer(){
  24.             this->integer = 0;
  25.         }
  26.         Integer(const int integer){
  27.             this->integer = integer;
  28.         }
  29.         ~Integer(){}
  30.         const int intValue(){
  31.             return this->integer;    
  32.         }
  33.         const double doubleValue(){
  34.             return double (this->integer);
  35.         }
  36.         const void print(){
  37.             cout << "Integer: " << this->integer << endl;
  38.         }
  39. };
  40.  
  41. class Double : public Number{
  42.     private:
  43.         double floatingNo;
  44.     public:
  45.         Double(){
  46.             this->floatingNo = 0.0;
  47.         }
  48.         Double(const double floatingNo){
  49.             this->floatingNo = floatingNo;
  50.         }
  51.         ~Double(){}
  52.         const int intValue(){
  53.             return int (this->floatingNo);
  54.         }
  55.         const double doubleValue(){
  56.             return this->floatingNo;
  57.         }
  58.         const void print(){
  59.             cout << "Double: " << this->floatingNo << endl;
  60.         }
  61. };
  62.  
  63. class Numbers{
  64.     private:
  65.         Number **number;
  66.         int noElements;
  67.         void copy(const Numbers &orig){
  68.             this->number = new Number * [orig.noElements];
  69.             for(int i=0; i<orig.noElements; i++)
  70.                 this->number[i] = orig.number[i];
  71.             this->noElements = orig.noElements;
  72.         }
  73.     public:
  74.         Numbers(){
  75.             this->number = new Number * [1];
  76.             this->noElements = 0;
  77.         }
  78.         Numbers(const Number &orig){
  79.             this->copy(orig);        
  80.         }
  81.         Numbers &operator=(const Numbers &orig){
  82.             if(this != &orig){
  83.                 delete [] this->number;
  84.                 this->copy(orig);
  85.             }
  86.             return *this;
  87.         }
  88.         ~Numbers(){
  89.             delete [] this->number;
  90.         }
  91.         Numbers &operator += (Number * orig){
  92.             int flag = 1;
  93.            
  94.             for(int i=0; i<this->noElements; i++){
  95.                 if(orig->doubleValue() == this->number[i]->doubleValue())
  96.                     flag = 0;  
  97.             }
  98.            
  99.             if(flag){
  100.                 Number ** tmp = new Number * [this->noElements + 1];
  101.                 for(int i=0; i<this->noElements; i++)
  102.                     tmp[i] = this->number[i];
  103.                 tmp[this->noElements++] = orig;
  104.                 delete [] this->number;
  105.                 this->number = tmp;
  106.             }
  107.  
  108.             return *this;
  109.         }
  110.         const int intSum(){
  111.             int sum = 0;
  112.             Number * tmp;
  113.             for(int i=0; i<this->noElements; i++){
  114.                 tmp = dynamic_cast<Integer *>(this->number[i]);
  115.                 if(tmp)
  116.                     sum += tmp->intValue();
  117.             }
  118.             return sum;
  119.         }
  120.         const double doubleSum(){
  121.             double sum = 0.0;
  122.             Number * tmp;
  123.             for(int i=0; i<this->noElements; i++){
  124.                 tmp = dynamic_cast<Double *>(this->number[i]);
  125.                 if(tmp)
  126.                     sum += tmp->doubleValue();
  127.             }
  128.             return sum;
  129.         }
  130.         const int intCount(){
  131.             int count = 0;
  132.             Number * tmp;
  133.             for(int i=0; i<this->noElements; i++){
  134.                 tmp = dynamic_cast<Integer *>(this->number[i]);
  135.                 if(tmp)
  136.                     count++;
  137.             }
  138.             return count;
  139.         }
  140.         const int doubleCount(){
  141.             int count = 0;
  142.             Number * tmp;
  143.             for(int i=0; i<this->noElements; i++){
  144.                 tmp = dynamic_cast<Double *>(this->number[i]);
  145.                 if(tmp)
  146.                     count++;
  147.             }
  148.             return count;
  149.         }
  150.         const void statistics(){
  151.             cout << "Count of numbers: " << this->noElements << endl;
  152.             cout << "Sum of all numbers: " << this->doubleSum() + double (this->intSum()) << endl;
  153.             cout << "Count of integer numbers: " << this->intCount() << endl;
  154.             cout << "Sum of integer numbers: " << this->intSum() << endl;
  155.             cout << "Count of double numbers: " << this->doubleCount() << endl;
  156.             cout << "Sum of double numbers: " << this->doubleSum() << endl;
  157.         }
  158.         const void integersLessThan(Integer n){
  159.             int count = 0;
  160.             for(int i=0; i<this->noElements; i++){
  161.                 Integer * tmp = dynamic_cast<Integer *>(this->number[i]);
  162.                 if(tmp){
  163.                     if(this->number[i]->intValue() < n.intValue()){
  164.                         this->number[i]->print();
  165.                         count++;
  166.                     }
  167.                 }
  168.             }
  169.             if(count == 0)
  170.                 cout << "None" << endl;
  171.         }
  172.         const void doublesBiggerThan(Double n){
  173.             int count = 0;
  174.             for(int i=0; i<this->noElements; i++){
  175.                 Double * tmp = dynamic_cast<Double *>(this->number[i]);
  176.                 if(tmp){
  177.                     if(this->number[i]->intValue() > n.doubleValue()){
  178.                         this->number[i]->print();
  179.                         count++;
  180.                     }
  181.                 }
  182.             }
  183.             if(count == 0)
  184.                 cout << "None" << endl;
  185.         }
  186. };
  187.  
  188. int main() {
  189.    
  190.     int n;
  191.     cin>>n;
  192.     Numbers numbers;
  193.     for (int i=0;i<n;i++){
  194.         int type;
  195.         double number;
  196.         cin>>type>>number;
  197.         if (type==0){//Integer object
  198.             Integer * integer = new Integer((int) number);
  199.             numbers+=integer;
  200.         }
  201.         else {
  202.             Double * doublee = new Double(number);
  203.             numbers+=doublee;
  204.         }
  205.     }
  206.    
  207.     int lessThan;
  208.     double biggerThan;
  209.    
  210.     cin>>lessThan;
  211.     cin>>biggerThan;       
  212.    
  213.     cout<<"STATISTICS FOR THE NUMBERS\n";
  214.     numbers.statistics();
  215.     cout<<"INTEGER NUMBERS LESS THAN "<<lessThan<<endl;
  216.     numbers.integersLessThan(Integer(lessThan));
  217.     cout<<"DOUBLE NUMBERS BIGGER THAN "<<biggerThan<<endl;
  218.     numbers.doublesBiggerThan(Double(biggerThan));
  219.    
  220.     return 0;
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement