Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.84 KB | None | 0 0
  1. #include<iostream>
  2. #include<cassert>
  3.  
  4. using namespace std;
  5.  
  6. class Punkt
  7. {
  8.   private:
  9.   string nazwa;
  10.   double x;
  11.   double y;
  12.  
  13.   public:
  14.   Punkt()
  15.   {
  16.       nazwa = " ";
  17.       x=0;
  18.       y=0;
  19.   }
  20.  
  21.   Punkt(string, double, double);
  22.  
  23.   double suma()
  24.   {
  25.       return x+y;
  26.   }
  27.  
  28.   string jakanazwa()
  29.   {
  30.       return nazwa;
  31.   }
  32.  
  33.   double jakix()
  34.   {
  35.       return x;
  36.   }
  37.  
  38.   double jakiy()
  39.   {
  40.       return y;
  41.   }
  42.  
  43.    // friend bool operator > (Punkt a, Punkt b);
  44.  
  45. };
  46.  
  47. Punkt::Punkt(string nazwa_,double x_, double y_)
  48. : nazwa(nazwa_),x(x_), y(y_){}
  49.  
  50.  
  51.  
  52. ostream &operator << (ostream &wyjscie, Punkt p)
  53. {
  54.     return wyjscie << p.jakanazwa() << " " << p.jakix() << " " << p.jakiy()<<" ";
  55. }
  56.  
  57.  
  58.  
  59.   bool operator < (Punkt a, Punkt b)
  60.   {
  61.       if(a.suma()!=b.suma())
  62.       {
  63.       if (a.suma() < b.suma())
  64.             return true;
  65.       else
  66.             return false;
  67.       }
  68.         else if(a.suma()==b.suma())
  69.         {
  70.             if(a.jakix() < b.jakix())
  71.  
  72.                 return true;
  73.             else
  74.                 return false;
  75.  
  76.         }
  77.  
  78.   }
  79.  
  80.  
  81. bool operator == (Punkt a, Punkt b)
  82. {
  83.     if(a.jakix()==b.jakix() && a.jakiy()==b.jakiy())
  84.         {
  85.             if(a.jakanazwa() < b.jakanazwa())
  86.                 return true;
  87.             else
  88.                 return false;
  89.         }
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96. template <class T> class link;
  97. template <class T> class Iterator;
  98.  
  99. // ======================================================================
  100. //                    Class Template list
  101. // ======================================================================
  102. template <class T>
  103. class list {
  104. protected:
  105.    link <T> *First;                // data field
  106.  
  107. public:
  108.    list() : First(nullptr) {}      // default constructor
  109.    list(const list &source);       // copy constructor
  110.    virtual ~list() {};                // destructor
  111.  
  112.    virtual void add(T value);      // insert a new item
  113.    virtual void delete_all();
  114.    T first_element() const;        // access the first item
  115.    virtual bool includes(T value) const;  // inclusion test
  116.    bool is_empty() const;
  117.    virtual void remove_first();
  118.  
  119.    friend class Iterator <T>;
  120. };
  121. // ======================================================================
  122. //                    Class Template link
  123. // ======================================================================
  124. template <class T>
  125. class link {
  126. private:
  127.    T Value;
  128.    link <T> *Next;
  129.  
  130.    link(T val, link *ptr) : Value(val), Next(ptr) { }
  131. public:
  132.    link <T> *insert(T value);      // after current link
  133.  
  134.    friend class list <T>;
  135.    friend class Iterator <T>;
  136. };
  137.  
  138.  
  139. // ======================================================================
  140. //                    Class Template Iterator
  141. // ======================================================================
  142. template <class T> class Iterator {
  143. public:
  144.    Iterator(list <T> &aList);
  145.    virtual bool init();
  146.    virtual T operator()();
  147.    virtual bool operator !();
  148.    virtual bool operator++(); // for prefix, for postfix add dummy int
  149.    virtual bool operator++(int);
  150.    virtual void operator=(T value);
  151.    void remove_current();
  152.    void add_before(T new_value);
  153.    void add_after(T new_value);
  154. protected:
  155.    list <T> &my_list;             // data fields
  156.    link <T> *previous;
  157.    link <T> *current;
  158. };
  159.  
  160. // ======================================================================
  161. //                    Class Template link - attributes
  162. // ======================================================================
  163. template <class T> link <T> * link <T> :: insert(T value)
  164. {
  165.   Next = new link <T>(value, Next);
  166.   return Next;
  167. }
  168.  
  169. // ======================================================================
  170. //                    Class Template list - attributes
  171. // ======================================================================
  172. template <class T> void list <T> :: add(T value)
  173. {
  174.    First = new link <T>(value, First);
  175. }
  176.  
  177. template <class T> T list <T> :: first_element() const
  178. {
  179.    assert(First != nullptr);
  180.    return First->Value;
  181. }
  182.  
  183.  
  184. template <class T> bool list <T> :: is_empty() const
  185. {
  186.    return First == nullptr;
  187. }
  188.  
  189.  
  190. template <class T> bool list <T> :: includes(T value) const
  191. {
  192.    for (link <T> *p = First; p; p = p->Next)
  193.        if (value == p->Value)return true;
  194.    return false;
  195. }
  196.  
  197. template <class T> void list <T> :: remove_first() {
  198.    assert(First != nullptr);
  199.    link <T> *ptr = First;  // save pointer to the first item
  200.    First = ptr->Next;      // reassign the First node
  201.    delete ptr;
  202. }
  203.  
  204. template <class T> void list <T> :: delete_all() {
  205.    link <T> *next;
  206.    for (link <T> *p = First; p; p = next){
  207.        next = p->Next;
  208.        delete p;
  209.    }
  210.    First = nullptr;
  211. }
  212.  
  213. // ======================================================================
  214. //                    Class Template Iterator - attributes
  215. // ======================================================================
  216. template <class T> Iterator <T> ::
  217.    Iterator(list <T> &aList) : my_list(aList){
  218.    init();
  219. }
  220. template <class T> bool Iterator <T> :: init(){
  221.    previous = nullptr;
  222.    current = my_list.First;
  223.    return current != nullptr;
  224. }
  225. template <class T> T Iterator <T> :: operator()(){
  226.    assert(current != nullptr);
  227.    return current->Value;
  228. }
  229. template <class T> void Iterator <T> :: operator=(T val){
  230.    assert(current != nullptr);
  231.    current->Value = val;
  232. }
  233.  
  234. template <class T> void Iterator <T> :: remove_current()
  235. {
  236.    assert(current != nullptr);
  237.    if (previous == nullptr)
  238.       my_list.First = current->Next;
  239.    else
  240.       previous->Next = current->Next;
  241.    delete current;
  242.    current = nullptr;
  243. }
  244.  
  245. template <class T> bool Iterator <T> :: operator++(){
  246.    if (current == nullptr){           // move current pointer
  247.       if (previous == nullptr)        // to next element
  248.          current = my_list.First;
  249.       else
  250.          current = previous->Next;
  251.    }
  252.    else {
  253.       previous = current;
  254.       current = current->Next;
  255.    }
  256.    return current != nullptr;
  257. } // valid for prefix operator only!
  258.  
  259. template <class T> bool Iterator <T> :: operator++(int){
  260.    return operator++();
  261. }
  262.  
  263. template <class T> bool Iterator <T> :: operator !(){
  264.    if (current == nullptr and previous != nullptr)
  265.       current = previous->Next;
  266.    return current != nullptr;       // termination of iterator
  267. }
  268.  
  269.  
  270. template <class T> void Iterator <T> :: add_before(T val)
  271. {
  272.    if (previous)previous = previous->insert(val);
  273.    else {
  274.       my_list.list<T>::add(val);     // to avoid subclass
  275.       previous = my_list.First;
  276.       current = previous->Next;      // if current is NULL
  277.    }
  278. }
  279.  
  280. template <class T> void Iterator <T> :: add_after(T val)
  281. {
  282.    if (current){current->insert(val); return;}  // not shown
  283.    if (previous)current = previous->insert(val);
  284.    else my_list.list<T>::add(val);
  285. }
  286.  
  287.  
  288. template <class T> class SortedList : public list<T>
  289. {
  290.     public:virtual void add (T value);
  291. };
  292.  
  293.  
  294. template <class T> void SortedList<T> :: add (T value)
  295. {
  296.     Punkt pom;
  297.  
  298.     Iterator <T> itr(*this);
  299.     for(itr.init(); ! itr; ++itr)
  300.     {
  301.        pom=itr();
  302.  
  303.         if(value <pom)
  304.         {
  305.             itr.add_before(value);
  306.             return;
  307.         }
  308.     }
  309.     itr.add_before(value);
  310. }
  311.  
  312.  
  313. int main()
  314. {
  315.  
  316.     SortedList <Punkt> lista;
  317.  
  318.     Iterator <Punkt> iterator(lista);
  319.  
  320.     int n;
  321.     cin>>n;
  322.  
  323.     string nazwa;
  324.     double x, y;
  325.  
  326.     for(int i=0; i<n; i++)
  327.     {
  328.         cin>>nazwa>>x>>y;
  329.  
  330.         Punkt p(nazwa,x,y);
  331.  
  332.         lista.add(p);
  333.     }
  334.  
  335.      int a;
  336.      cin>>a;
  337.  
  338.  
  339.     for(iterator.init(); !iterator; iterator++)
  340.     {
  341.         Punkt p=iterator();          //operator () - zwraca wartosc
  342.  
  343.         if(p.suma()==a)
  344.         iterator.remove_current();
  345.  
  346.     }
  347.  
  348.  
  349.  
  350.  
  351.     for(iterator.init(); !iterator; iterator++)
  352.     {
  353.         cout << iterator();
  354.     }
  355.  
  356.  
  357.  
  358.  
  359.     return 1;
  360. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement