Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- class Number {
- public:
- virtual const double doubleValue() = 0;
- virtual const int intValue() = 0;
- virtual const void print() = 0;
- };
- bool operator==(Number *left, Number &right){
- if(left->doubleValue() == right.doubleValue())
- return true;
- else
- return false;
- }
- class Integer : public Number {
- private:
- int integer;
- public:
- Integer(){
- this->integer = 0;
- }
- Integer(const int integer){
- this->integer = integer;
- }
- ~Integer(){}
- const int intValue(){
- return this->integer;
- }
- const double doubleValue(){
- return double (this->integer);
- }
- const void print(){
- cout << "Integer: " << this->integer << endl;
- }
- };
- class Double : public Number{
- private:
- double floatingNo;
- public:
- Double(){
- this->floatingNo = 0.0;
- }
- Double(const double floatingNo){
- this->floatingNo = floatingNo;
- }
- ~Double(){}
- const int intValue(){
- return int (this->floatingNo);
- }
- const double doubleValue(){
- return this->floatingNo;
- }
- const void print(){
- cout << "Double: " << this->floatingNo << endl;
- }
- };
- class Numbers{
- private:
- Number **number;
- int noElements;
- void copy(const Numbers &orig){
- this->number = new Number * [orig.noElements];
- for(int i=0; i<orig.noElements; i++)
- this->number[i] = orig.number[i];
- this->noElements = orig.noElements;
- }
- public:
- Numbers(){
- this->number = new Number * [1];
- this->noElements = 0;
- }
- Numbers(const Number &orig){
- this->copy(orig);
- }
- Numbers &operator=(const Numbers &orig){
- if(this != &orig){
- delete [] this->number;
- this->copy(orig);
- }
- return *this;
- }
- ~Numbers(){
- delete [] this->number;
- }
- Numbers &operator += (Number * orig){
- int flag = 1;
- for(int i=0; i<this->noElements; i++){
- if(orig->doubleValue() == this->number[i]->doubleValue())
- flag = 0;
- }
- if(flag){
- Number ** tmp = new Number * [this->noElements + 1];
- for(int i=0; i<this->noElements; i++)
- tmp[i] = this->number[i];
- tmp[this->noElements++] = orig;
- delete [] this->number;
- this->number = tmp;
- }
- return *this;
- }
- const int intSum(){
- int sum = 0;
- Number * tmp;
- for(int i=0; i<this->noElements; i++){
- tmp = dynamic_cast<Integer *>(this->number[i]);
- if(tmp)
- sum += tmp->intValue();
- }
- return sum;
- }
- const double doubleSum(){
- double sum = 0.0;
- Number * tmp;
- for(int i=0; i<this->noElements; i++){
- tmp = dynamic_cast<Double *>(this->number[i]);
- if(tmp)
- sum += tmp->doubleValue();
- }
- return sum;
- }
- const int intCount(){
- int count = 0;
- Number * tmp;
- for(int i=0; i<this->noElements; i++){
- tmp = dynamic_cast<Integer *>(this->number[i]);
- if(tmp)
- count++;
- }
- return count;
- }
- const int doubleCount(){
- int count = 0;
- Number * tmp;
- for(int i=0; i<this->noElements; i++){
- tmp = dynamic_cast<Double *>(this->number[i]);
- if(tmp)
- count++;
- }
- return count;
- }
- const void statistics(){
- cout << "Count of numbers: " << this->noElements << endl;
- cout << "Sum of all numbers: " << this->doubleSum() + double (this->intSum()) << endl;
- cout << "Count of integer numbers: " << this->intCount() << endl;
- cout << "Sum of integer numbers: " << this->intSum() << endl;
- cout << "Count of double numbers: " << this->doubleCount() << endl;
- cout << "Sum of double numbers: " << this->doubleSum() << endl;
- }
- const void integersLessThan(Integer n){
- int count = 0;
- for(int i=0; i<this->noElements; i++){
- Integer * tmp = dynamic_cast<Integer *>(this->number[i]);
- if(tmp){
- if(this->number[i]->intValue() < n.intValue()){
- this->number[i]->print();
- count++;
- }
- }
- }
- if(count == 0)
- cout << "None" << endl;
- }
- const void doublesBiggerThan(Double n){
- int count = 0;
- for(int i=0; i<this->noElements; i++){
- Double * tmp = dynamic_cast<Double *>(this->number[i]);
- if(tmp){
- if(this->number[i]->intValue() > n.doubleValue()){
- this->number[i]->print();
- count++;
- }
- }
- }
- if(count == 0)
- cout << "None" << endl;
- }
- };
- int main() {
- int n;
- cin>>n;
- Numbers numbers;
- for (int i=0;i<n;i++){
- int type;
- double number;
- cin>>type>>number;
- if (type==0){//Integer object
- Integer * integer = new Integer((int) number);
- numbers+=integer;
- }
- else {
- Double * doublee = new Double(number);
- numbers+=doublee;
- }
- }
- int lessThan;
- double biggerThan;
- cin>>lessThan;
- cin>>biggerThan;
- cout<<"STATISTICS FOR THE NUMBERS\n";
- numbers.statistics();
- cout<<"INTEGER NUMBERS LESS THAN "<<lessThan<<endl;
- numbers.integersLessThan(Integer(lessThan));
- cout<<"DOUBLE NUMBERS BIGGER THAN "<<biggerThan<<endl;
- numbers.doublesBiggerThan(Double(biggerThan));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement