Guest User

Untitled

a guest
Apr 13th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <list>
  4. #include <vector>
  5. #include <fstream>
  6. #include <regex>
  7. #include <map>
  8. #include <cstdlib>
  9. #include <list>
  10.  
  11.  
  12. using namespace std;
  13.  
  14.  
  15.  
  16. class Object{
  17. public:
  18.     enum obj_type{
  19.         IntObject,
  20.         DoubleObject,
  21.         StringObject,
  22.     };
  23.     //#override
  24.     //virtual enum Object::obj_type whatAmI() const =0;
  25.     virtual obj_type whatAmI() const =0;
  26.     virtual string toString() const=0;
  27.     virtual Object* clone() const=0;
  28.     virtual Object& append(Object&)=0;
  29.     virtual ~Object(){}
  30. };
  31.  
  32. class StringObject: public Object{
  33.     string text;
  34. public:
  35.     StringObject()=default;
  36.     StringObject(const StringObject& str){
  37.         text=str.toString();
  38.     }
  39.     ~StringObject(){
  40.         cout<<"To ja destruktor StringObject"<<endl<<endl;
  41.     }
  42.  
  43.     //    StringObject()=default;
  44.     //    StringObject(string str): text(str){}
  45.  
  46.     enum Object::obj_type whatAmI() const {
  47.         return Object::obj_type::StringObject;
  48.     }
  49.     string toString() const{
  50.         return text;
  51.     }
  52.  
  53.     StringObject* clone() const{
  54.         StringObject* deepcopy= new StringObject;
  55.         deepcopy->text=this->text;
  56.         return deepcopy;
  57.     }
  58.     StringObject& append( Object& string){
  59.         if(string.whatAmI() != this->whatAmI() ){
  60.             cerr<<"ERROR"<<endl;
  61.         }
  62.         else{
  63.             StringObject* conversion= dynamic_cast<StringObject*>(&string);
  64.             this->text+=conversion->text;
  65.         }
  66.         return *this;
  67.     }
  68.  
  69.     StringObject& operator=(const string text){
  70.         this->text=text;
  71.         return *this;
  72.     }
  73.  
  74.     friend std::ostream& operator<<(std::ostream& os, const StringObject& str);
  75.     friend std::istream& operator>>(std::istream& is, StringObject& str);
  76. };
  77. std::ostream& operator<<(std::ostream& os, const StringObject& str){
  78.     return os<<str.text;
  79. }
  80. std::istream& operator>>(std::istream& is, StringObject& str){
  81.     is >> str.text;
  82.     return is;
  83. }
  84.  
  85.  
  86.  
  87. class IntObject: public Object{
  88.     int number;
  89. public:
  90.     IntObject()=default;
  91.     IntObject(const IntObject& copy){
  92.         number=copy.number;
  93.     }
  94.     ~IntObject(){
  95.         cout<<"To ja destruktor IntObject"<<endl<<endl;
  96.     }
  97.     enum Object::obj_type whatAmI() const{
  98.         return Object::obj_type::IntObject;
  99.     }
  100.     string toString() const{
  101.         string text;
  102.         text= to_string(number);
  103.         return text;
  104.     }
  105.     IntObject* clone() const{
  106.         IntObject* deepcopy= new IntObject;
  107.         deepcopy->number=this->number;
  108.         return deepcopy;
  109.     }
  110.     IntObject& append( Object& number){
  111.         if(number.whatAmI() != Object::obj_type::IntObject){
  112.             cerr<<"ERROR"<<endl;
  113.         }
  114.         else{
  115.             IntObject* conversion= dynamic_cast<IntObject*>(&number);
  116.             this->number+=conversion->number;
  117.         }
  118.         return *this;
  119.     }
  120.     IntObject& operator=(const int number){
  121.         this->number=number;
  122.         return *this;
  123.     }
  124.  
  125.     int getInt(){
  126.         return this->number;
  127.     }
  128.  
  129.     friend std::ostream& operator<<(std::ostream& os, const IntObject& intnmb);
  130.     friend std::istream& operator>>(std::istream& is, IntObject& intnmb);
  131. };
  132. std::ostream& operator<<(std::ostream& os, const IntObject& intnmb){
  133.     return os<<intnmb.number;
  134. }
  135. std::istream& operator>>(std::istream& is, IntObject& intnmb){
  136.     is>>intnmb.number;
  137.     return is;
  138. }
  139.  
  140.  
  141.  
  142. class DoubleObject: public Object{
  143.     double number;
  144. public:
  145.     ~DoubleObject(){
  146.         cout<<"To ja destruktor DoubleObject"<<endl<<endl;
  147.     }
  148.     enum Object::obj_type whatAmI() const{
  149.         return Object::obj_type::DoubleObject;
  150.     }
  151.     string toString() const{
  152.         string text;
  153.         text= to_string(number);
  154.         return text;
  155.     }
  156.     DoubleObject* clone() const{
  157.         DoubleObject* deepcopy= new DoubleObject;
  158.         deepcopy->number=this->number;
  159.         return deepcopy;
  160.     }
  161.     DoubleObject& append( Object& number){
  162.         if(number.whatAmI() != Object::obj_type::DoubleObject){
  163.             cerr<<"ERROR"<<endl;
  164.         }
  165.         else{
  166.             DoubleObject* conversion= dynamic_cast<DoubleObject*>(&number);
  167.             this->number+=conversion->number;
  168.         }
  169.         return *this;
  170.     }
  171.     DoubleObject& operator=(const double number){
  172.         this->number=number;
  173.         return *this;
  174.     }
  175.     double getDouble(){
  176.         return this->number;
  177.     }
  178.  
  179.     friend std::ostream& operator<<(std::ostream& os, const DoubleObject& doublenmb);
  180.     friend std::istream& operator>>(std::istream& is, DoubleObject& doublenmb);
  181. };
  182. std::ostream& operator<<(std::ostream& os, const DoubleObject& doublenmb){
  183.     return os<<doublenmb.number;
  184. }
  185. std::istream& operator>>(std::istream& is, DoubleObject& doublenmb){
  186.     is>>doublenmb.number;
  187.     return is;
  188. }
  189.  
  190.  
  191.  
  192. class Node{
  193.     friend class List;
  194.     Node(const Node& node_copy){
  195.         obj=node_copy.obj->clone();
  196.         if(node_copy.next!=nullptr){
  197.             next= new Node(*(node_copy.next));
  198.         }
  199.         else{
  200.             next=nullptr;
  201.         }
  202.     }
  203. public:
  204.     Object* obj;
  205.     Node* next;
  206.     Node(): obj(nullptr), next(nullptr) {}
  207.     ~Node(){
  208.         //   cout<<"\tTO JA DESTRUKTOR NODE"<<endl;
  209.     }
  210.  
  211. };
  212.  
  213.  
  214. class List{
  215.     size_t number_of_elements;
  216.     Node* head;
  217.  
  218. public:
  219.  
  220.     List(): number_of_elements(0), head(nullptr) {
  221.         srand(time(NULL));
  222.     }
  223.  
  224.     List(const List& list_copy){
  225.         srand(time(NULL));
  226.         cout<<endl<<"Konstruktor kopiujacy"<<endl;
  227.         head= new Node(*(list_copy.head));
  228.         number_of_elements=list_copy.number_of_elements;
  229.     }
  230.  
  231.     List(List&& move){
  232.         srand(time(NULL));
  233.         cout<<endl<<"Konstruktor przenoszacy"<<endl;
  234.         number_of_elements=move.number_of_elements;
  235.         head=move.head;
  236.         move.head=nullptr;
  237.         move.number_of_elements=0;
  238.     }
  239.  
  240.     ~List(){
  241.         //cout<<"DESTRUKTOR LIST:"<<endl;
  242.         while(number_of_elements>0){
  243.             pop_back();
  244.         }
  245.     }
  246.  
  247.     List& operator=(const List& list_copy){
  248.         if(this->head!=list_copy.head){
  249.             cout<<endl<<"Operator kopiujacy"<<endl;
  250.             head= new Node(*(list_copy.head));
  251.             number_of_elements=list_copy.number_of_elements;
  252.         }
  253.         return *this;
  254.     }
  255.  
  256.     List& operator=(List&& move){
  257.         if(this->head!=move.head){
  258.             cout<<endl<<"Operator przenoszacy"<<endl;
  259.             number_of_elements=move.number_of_elements;
  260.             head=move.head;
  261.             move.head=nullptr;
  262.             move.number_of_elements=0;
  263.         }
  264.         return *this;
  265.     }
  266.  
  267.  
  268.  
  269.     void push_back(Object* obj){
  270.         number_of_elements++;
  271.         Node* push_node= new Node;
  272.         push_node->obj=obj;
  273.         push_node->next=nullptr;
  274.         if( head== nullptr){
  275.             head=push_node;
  276.         }
  277.         else{
  278.             Node* node_ptr=head;
  279.             while(node_ptr->next!=nullptr){
  280.                 node_ptr=node_ptr->next;
  281.             }
  282.             node_ptr->next=push_node;
  283.         }
  284.     }
  285.  
  286.     List& operator+=(Object* obj){
  287.         number_of_elements++;
  288.         Node* push_node= new Node;
  289.         push_node->obj=obj;
  290.         push_node->next=nullptr;
  291.         if( head== nullptr){
  292.             head=push_node;
  293.         }
  294.         else{
  295.             Node* node_ptr=head;
  296.             while(node_ptr->next!=nullptr){
  297.                 node_ptr=node_ptr->next;
  298.             }
  299.             node_ptr->next=push_node;
  300.         }
  301.         return *this;
  302.     }
  303.  
  304.     void pop_back(){
  305.         if(number_of_elements!=0){
  306.             if(number_of_elements==1){
  307.                 delete head;
  308.                 head=nullptr;
  309.             }
  310.             else{
  311.                 Node* node_ptr=head;
  312.                 while(node_ptr->next->next!=nullptr){
  313.                     node_ptr=node_ptr->next;
  314.                 }
  315.                 delete (node_ptr->next);
  316.                 node_ptr->next=nullptr;
  317.             }
  318.             number_of_elements--;
  319.         }
  320.     }
  321.     List& operator--(int){
  322.         if(number_of_elements!=0){
  323.             if(number_of_elements==1){
  324.                 delete head;
  325.                 head=nullptr;
  326.             }
  327.             else{
  328.                 Node* node_ptr=head;
  329.                 while(node_ptr->next->next!=nullptr){
  330.                     node_ptr=node_ptr->next;
  331.                 }
  332.                 delete (node_ptr->next);
  333.                 node_ptr->next=nullptr;
  334.             }
  335.             number_of_elements--;
  336.         }
  337.         return *this;
  338.     }
  339.  
  340.  
  341.  
  342.     Object& at(size_t index){
  343.         if(index >= number_of_elements){
  344.             cerr<<"INDEX OUT OF RANGE"<<endl;
  345.             exit(EXIT_FAILURE);
  346.         }
  347.         else{
  348.             Node* node_ptr=head;
  349.             for (size_t i=0; i<index; i++){
  350.                 node_ptr=node_ptr->next;
  351.             }
  352.             return *(node_ptr->obj);
  353.         }
  354.     }
  355.  
  356.     Object& operator[](size_t index) const{
  357.         if(index >= number_of_elements){
  358.             cerr<<"INDEX OUT OF RANGE"<<endl;
  359.             exit(EXIT_FAILURE);
  360.         }
  361.         else{
  362.             Node* node_ptr=head;
  363.             for (size_t i=0; i<index; i++){
  364.                 node_ptr=node_ptr->next;
  365.             }
  366.             return *(node_ptr->obj);
  367.         }
  368.     }
  369.  
  370.  
  371.     size_t size() const {
  372.         return number_of_elements;
  373.     }
  374.     operator size_t(){
  375.         return number_of_elements;
  376.     }
  377.  
  378.     bool operator&&(const Object& obj){
  379.         bool ret=0;
  380.         for (size_t i=0; i<number_of_elements; i++){
  381.             if((*this)[i].toString()==obj.toString()){
  382.                 ret=true;
  383.             }
  384.         }
  385.         return  ret;
  386.     }
  387.  
  388.  
  389.     Object* operator->(){
  390.         string empty_list="PUSTA LISTA!";
  391.         if(number_of_elements==0) throw empty_list;
  392.         size_t index=static_cast<size_t>((rand() % static_cast<int>(number_of_elements)));
  393.         return &((*this)[index]);
  394.     }
  395.  
  396.  
  397.  
  398.     class iterator{
  399.         Node* itr;
  400.  
  401.     public:
  402.         iterator(): itr(nullptr){}
  403.         iterator(Node* copy): itr(copy){}
  404.  
  405.         Object& operator*() const{
  406.             if(!itr) throw "Invalid iteretor derference";
  407.              return *(itr->obj);
  408.         }
  409.  
  410.        const iterator& operator++(){ //preinkrementacja
  411.             if(!itr) throw "Out-of-bounds iterator incerement";
  412.             itr= itr->next;
  413.             return *this;
  414.         }
  415.  
  416.         iterator operator++(int){ //postinkrementacja
  417.             if(!itr) throw "Out-of-bounds iterator incerement";
  418.             iterator tmp(*this);
  419.             itr= itr->next;
  420.             return tmp;
  421.         }
  422.  
  423.         bool operator!=(const iterator& it) const {
  424.             return this->itr!=it.itr;
  425.         }
  426.     };
  427.  
  428.     iterator begin()const{
  429.         iterator itr(head);
  430.         return itr;
  431.     }
  432.  
  433.     iterator end()const{
  434.         iterator end;
  435.         return end;
  436.     }
  437.  
  438.  
  439.  
  440. };
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449. bool isMail(const Object* obj){
  450.     const regex pattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");
  451.     return obj->whatAmI()==Object::obj_type::StringObject? regex_match(obj->toString(),pattern): false;
  452. }
  453.  
  454. List getElementsPassingPredicate(const List& myList, bool(*predicate)(const Object*)){
  455.     List ret;
  456.     for(size_t i=0; i< myList.size() ; i++){
  457.         if(predicate(&(myList[i]))==true){
  458.             ret+= &myList[i];
  459.         }
  460.     }
  461.     return ret;
  462. }
  463.  
  464. enum class Month{January,
  465.                  February,
  466.                  March,
  467.                  April,
  468.                  May,
  469.                  June,
  470.                  July,
  471.                  August,
  472.                  September,
  473.                  October,
  474.                  November,
  475.                  December,
  476.                 };
  477.  
  478. const map<Month, string> months{{Month::January,"January"},
  479.                                 {Month::February,"February"},
  480.                                 {Month::March,"March"},
  481.                                 {Month::April,"April"},
  482.                                 {Month::May,"May"},
  483.                                 {Month::June,"June"},
  484.                                 {Month::July,"July"},
  485.                                 {Month::August,"August"},
  486.                                 {Month::September,"September"},
  487.                                 {Month::October,"October"},
  488.                                 {Month::November,"November"},
  489.                                 {Month::December,"December"}};
  490.  
  491. pair<IntObject, StringObject> getMonthNumberAndText(Month month){
  492.     IntObject number;
  493.     number=static_cast<int>(month)+1;
  494.     StringObject name;
  495.     name= months.at(month);
  496.     return make_pair(number,name);
  497. }
  498.  
  499. struct Predicate{
  500.     virtual  bool operator()(Object*) const =0;
  501.     virtual ~Predicate(){}
  502. };
  503.  
  504. struct IsGreaterThan{
  505.     int number;
  506.     IsGreaterThan(int nmb):number(nmb){}
  507.     bool operator()(Object* obj){
  508.         bool ret=0;
  509.         if(obj->whatAmI()== Object::obj_type::StringObject){
  510.             ret=0;
  511.         }
  512.         else if(obj->whatAmI()== Object::obj_type::IntObject){
  513.             IntObject* object= dynamic_cast<IntObject*>(obj);
  514.             ret=(object->getInt()>number? true : false);
  515.         }
  516.         else if(obj->whatAmI()== Object::obj_type::DoubleObject){
  517.             DoubleObject* object= dynamic_cast<DoubleObject*>(obj);
  518.             ret=(object->getDouble()>number? true : false);
  519.         }
  520.         return ret;
  521.     }
  522. };
  523.  
  524. struct isMail: Predicate{
  525.     bool operator()(Object* obj) const override{
  526.         const regex pattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");
  527.         return regex_match(obj->toString(),pattern);
  528.     }
  529. };
  530.  
  531. size_t countElementsPassingPredicate(const List& myList, const Predicate& predicate){
  532.     size_t number=0;
  533.     for(size_t i=0; i< myList.size() ; i++){
  534.         if(predicate(&(myList[i]))==true){
  535.             number++;
  536.         }
  537.     }
  538.     return number;
  539. }
  540.  
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550. int main(){
  551.  
  552.     StringObject s1;
  553.     s1="[email protected]";
  554.     StringObject s2;
  555.     s2="Ala nie ma kota";
  556.     IntObject i1;
  557.     i1=0;
  558.     IntObject i2;
  559.     i2=29;
  560.  
  561.     DoubleObject d1;
  562.     d1=25.52;
  563.     DoubleObject d2;
  564.     d2=29.92;
  565.  
  566. List l1;
  567. try{
  568.     cout<<l1->toString()<<endl;
  569. }
  570. catch (string x){
  571.     cout<<"Wyjatek: "<<x<<endl;
  572. }
  573.  
  574.  
  575.     l1.push_back(&s1);
  576.     l1.push_back(&s2);
  577.     l1.push_back(&i1);
  578.     l1.push_back(&i2);
  579.     l1.push_back(&d1);
  580.  
  581. cout<<endl<<"#LIST ITERATOR operator: "<<endl;
  582. try {
  583.     for(auto it=l1.begin(); it!=l1.end(); it++){
  584.         cout<<(*it).toString()<<endl;
  585.     }
  586.     for(List::iterator it : l1){
  587.  
  588.     }
  589.  
  590. } catch (...) {
  591.     cout<<"ITERAOR-WYJATEK";
  592.  
  593. }
  594.  
  595.  
  596. cout<<endl<<"#Overload AND operator: "<<endl;
  597.     cout<< (l1&&s1)<<endl;
  598.     cout<< (l1&&s2)<<endl;
  599.     cout<< (l1&&i1)<<endl;
  600.     cout<< (l1&&i2)<<endl;
  601.     cout<< (l1&&d1)<<endl;
  602.     cout<< (l1&&d2)<<endl;
  603.  
  604.  
  605.  
  606. cout<<endl<<"#getElementsPassingPredicate: "<<endl;
  607. List l2=getElementsPassingPredicate(l1, isMail);
  608. for(size_t i=0; i< l2.size(); i++){
  609.     cout<<l2[i].toString()<<endl;
  610. }
  611.  
  612. cout<<endl<<"#countElementsPassingPredicate: "<<endl;
  613. struct isMail im;
  614. cout<<countElementsPassingPredicate(l1,im)<<endl;
  615.  
  616.  
  617.  
  618. cout<<endl<<"#getMonthNumberAndText: "<<endl;
  619. auto x=getMonthNumberAndText(Month::December);
  620. cout<<x.first<<" "<<x.second<<endl;
  621.  
  622.  
  623.  
  624.  
  625. cout<<endl<<"#IsGreaterThan: "<<endl;
  626. IsGreaterThan igt(1);
  627. cout<<igt(&s1)<<endl;
  628. cout<<igt(&i1)<<endl;
  629. cout<<igt(&i2)<<endl;
  630. cout<<igt(&d1)<<endl;
  631.  
  632.  
  633. cout<<endl<<"#LOSOWANIE: "<<endl;
  634. cout<<l1->toString()<<endl;
  635. cout<<l1->toString()<<endl;
  636. cout<<l1->toString()<<endl;
  637. cout<<l1->toString()<<endl;
  638. cout<<l1->toString()<<endl;
  639. cout<<l1->toString()<<endl<<endl<<endl<<endl;
  640.  
  641.  
  642.  
  643.  
  644. }
Advertisement
Add Comment
Please, Sign In to add comment