Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- class Number { //Да се дефинира апстрктна класа Number
- public: //со следните чисто виртуелни методи:
- virtual double doubleValue() = 0; //double doubleValue() -ја враќа децималната вредност на даден број
- virtual int intValue() = 0; //int intValue() = 0 ја враќа целобројната вредност на даден број
- virtual void print() = 0; //void print() - печати информации за бројот. (1 поен)
- };
- class Integer : public Number { //Од оваа класа да се изведат две класи:
- private:// Integer (во која што се чува
- int celbroj; //еден број од тип int)
- public: //За двете изведени класи да се имплементираaт соодветен конструктори,
- Integer() { // defaulten
- this->celbroj = 0;
- }
- Integer(int celbroj) { //TODO konstruktor so eden argument
- this->celbroj = celbroj;
- }
- double doubleValue(){ //како и да се препокријат методите од основната класа. (1 поени)
- return this->celbroj * 1.0;
- }
- int intValue() {
- return this->celbroj;
- }
- void print(){
- cout<<"Integer: "<<celbroj<<endl;
- }
- };
- class Double : public Number { // Double (во која што се чува
- private:
- double decimalen; //еден број од тип double) (1 поен).
- public:
- Double(){
- this->decimalen = 0;
- }
- Double(double decimalen) {
- this->decimalen = decimalen;
- }
- double doubleValue() {
- return this->decimalen;
- }
- int intValue() {
- return (int)decimalen;
- }
- void print() {
- cout<<"Double: "<<decimalen<<endl;
- }
- };
- bool operator==(Number &n1, Number &n2) { //Да се преоптовари операторот == којшто ќе споредува
- if(n1.doubleValue()==n2.doubleValue())
- return true;
- else return false;
- }//два броеви според нивната вредност (објекти од класа Number) (1 поен)
- class Numbers { //Дополнително да се дефинира класа Numbers
- private: //во која што ќе се чуваат:
- Number **niza; //динамички алоцирана низа од покажувачи кон објекти од класата Number
- int broj; //број на елементи во низата од покажувачи
- public: //За класата да се имплементира
- Numbers(){ //соодветен конструктор (default) , како и: (1 поени)
- this-> niza = new Number * [0];
- this->broj = 0;
- }
- Numbers(const Numbers &n) { //TODO copy konstruktor
- this->broj = n.broj;
- this->niza = new Number * [n.broj];
- for(int i = 0; i<n.broj; i++)
- this->niza[i] = n.niza[i];
- }
- Numbers &operator=(const Numbers &n) { //TODO operator =
- if(this!=&n) {
- this->broj = n.broj;
- this->niza = new Number * [n.broj];
- for(int i = 0; i<n.broj; i++)
- this->niza[i] = n.niza[i];
- }
- return *this;
- }
- Numbers &operator+=(Number *novo) { //операторот += за додавање на броеви во низата од броеви (1 поен)
- bool add = true; //Бројот се додава ако и само ако сите броеви
- for (int i = 0; i < broj; i++) //што се веќе додадени во низата се различни од него
- {
- if (*niza[i] == *novo)
- {
- add = false;
- }
- }
- if (add)
- {
- Number** nova = new Number*[broj + 1];
- for (int i = 0; i < broj; i++)
- {
- nova[i] = niza[i];
- }
- if (add)
- {
- nova[broj] = novo;
- broj++;
- }
- // delete[] niza;
- niza = nova;
- }
- return *this;
- }
- ~Numbers(){ //TODO destruktor
- // for(int i = 0; i<broj; i++)
- // delete [] niza;
- }
- void statistics() { // функција void statistics() која што печати информации за броевите во низата:
- int count = 0, countinteger = 0, suminteger = 0, countdouble = 0;
- double sumdouble = 0.0, sum = 0.0;
- for(int i=0; i<broj; i++) {
- Integer * p1 = dynamic_cast <Integer *> (niza[i]);
- if(p1!=0) {
- count++;
- countinteger++;
- suminteger = suminteger + niza[i]->intValue();
- sum=sum+niza[i]->intValue();
- }
- Double *p2 = dynamic_cast <Double *> (niza[i]);
- if(p2!=0) {
- count++;
- countdouble++;
- sumdouble = sumdouble + niza[i]->doubleValue();
- sum=sum+niza[i]->doubleValue();
- }
- }
- cout<<"Count of numbers: "<<count<<endl;
- cout<<"Sum of all numbers: "<<sum<<endl;
- cout<<"Count of integer numbers: "<<countinteger<<endl;
- cout<<"Sum of integer numbers: "<<suminteger<<endl;
- cout<<"Count of double numbers: "<<countdouble<<endl;
- cout<<"Sum of double numbers: "<<sumdouble<<endl;
- }
- void integersLessThan (Integer n) { //функција void integersLessThan (Integer n)
- int flag = 1;
- for(int i=0; i<broj; i++) {
- Integer * p1 = dynamic_cast <Integer *> (niza[i]);
- if(p1!=0)
- if(niza[i]->intValue() < n.intValue()) {
- niza[i]->print();
- flag = 0;
- }
- }
- if(flag==1)
- cout<<"None"<<endl;
- } //која што ги печати сите цели броеви помали од бројот n (1 поен)
- void doublesBiggerThan(Double n) { //функција void doublesBiggerThan (Double n)
- int flag=1;
- for(int i=0; i<broj; i++) {
- Double *p2 = dynamic_cast <Double *> (niza[i]);
- if(p2!=0)
- if(niza[i]->doubleValue() > n.doubleValue())
- {
- niza[i]->print();
- flag = 0;
- }
- } if(flag==1)
- cout<<"None"<<endl;
- } //која што ги печати сите децимални броеви поголеми од бројот n (1 поен)
- }; // ja da potenciram deka mojta programa rabote na codeblocks a ne znam so mu e na code problemot :)
- 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