Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cassert>
- using namespace std;
- class Punkt
- {
- private:
- string nazwa;
- double x;
- double y;
- public:
- Punkt()
- {
- nazwa = " ";
- x=0;
- y=0;
- }
- Punkt(string, double, double);
- double suma()
- {
- return x+y;
- }
- string jakanazwa()
- {
- return nazwa;
- }
- double jakix()
- {
- return x;
- }
- double jakiy()
- {
- return y;
- }
- // friend bool operator > (Punkt a, Punkt b);
- };
- Punkt::Punkt(string nazwa_,double x_, double y_)
- : nazwa(nazwa_),x(x_), y(y_){}
- ostream &operator << (ostream &wyjscie, Punkt p)
- {
- return wyjscie << p.jakanazwa() << " " << p.jakix() << " " << p.jakiy()<<" ";
- }
- bool operator < (Punkt a, Punkt b)
- {
- if(a.suma()!=b.suma())
- {
- if (a.suma() < b.suma())
- return true;
- else
- return false;
- }
- else if(a.suma()==b.suma())
- {
- if(a.jakix() < b.jakix())
- return true;
- else
- return false;
- }
- }
- bool operator == (Punkt a, Punkt b)
- {
- if(a.jakix()==b.jakix() && a.jakiy()==b.jakiy())
- {
- if(a.jakanazwa() < b.jakanazwa())
- return true;
- else
- return false;
- }
- }
- template <class T> class link;
- template <class T> class Iterator;
- // ======================================================================
- // Class Template list
- // ======================================================================
- template <class T>
- class list {
- protected:
- link <T> *First; // data field
- public:
- list() : First(nullptr) {} // default constructor
- list(const list &source); // copy constructor
- virtual ~list() {}; // destructor
- virtual void add(T value); // insert a new item
- virtual void delete_all();
- T first_element() const; // access the first item
- virtual bool includes(T value) const; // inclusion test
- bool is_empty() const;
- virtual void remove_first();
- friend class Iterator <T>;
- };
- // ======================================================================
- // Class Template link
- // ======================================================================
- template <class T>
- class link {
- private:
- T Value;
- link <T> *Next;
- link(T val, link *ptr) : Value(val), Next(ptr) { }
- public:
- link <T> *insert(T value); // after current link
- friend class list <T>;
- friend class Iterator <T>;
- };
- // ======================================================================
- // Class Template Iterator
- // ======================================================================
- template <class T> class Iterator {
- public:
- Iterator(list <T> &aList);
- virtual bool init();
- virtual T operator()();
- virtual bool operator !();
- virtual bool operator++(); // for prefix, for postfix add dummy int
- virtual bool operator++(int);
- virtual void operator=(T value);
- void remove_current();
- void add_before(T new_value);
- void add_after(T new_value);
- protected:
- list <T> &my_list; // data fields
- link <T> *previous;
- link <T> *current;
- };
- // ======================================================================
- // Class Template link - attributes
- // ======================================================================
- template <class T> link <T> * link <T> :: insert(T value)
- {
- Next = new link <T>(value, Next);
- return Next;
- }
- // ======================================================================
- // Class Template list - attributes
- // ======================================================================
- template <class T> void list <T> :: add(T value)
- {
- First = new link <T>(value, First);
- }
- template <class T> T list <T> :: first_element() const
- {
- assert(First != nullptr);
- return First->Value;
- }
- template <class T> bool list <T> :: is_empty() const
- {
- return First == nullptr;
- }
- template <class T> bool list <T> :: includes(T value) const
- {
- for (link <T> *p = First; p; p = p->Next)
- if (value == p->Value)return true;
- return false;
- }
- template <class T> void list <T> :: remove_first() {
- assert(First != nullptr);
- link <T> *ptr = First; // save pointer to the first item
- First = ptr->Next; // reassign the First node
- delete ptr;
- }
- template <class T> void list <T> :: delete_all() {
- link <T> *next;
- for (link <T> *p = First; p; p = next){
- next = p->Next;
- delete p;
- }
- First = nullptr;
- }
- // ======================================================================
- // Class Template Iterator - attributes
- // ======================================================================
- template <class T> Iterator <T> ::
- Iterator(list <T> &aList) : my_list(aList){
- init();
- }
- template <class T> bool Iterator <T> :: init(){
- previous = nullptr;
- current = my_list.First;
- return current != nullptr;
- }
- template <class T> T Iterator <T> :: operator()(){
- assert(current != nullptr);
- return current->Value;
- }
- template <class T> void Iterator <T> :: operator=(T val){
- assert(current != nullptr);
- current->Value = val;
- }
- template <class T> void Iterator <T> :: remove_current()
- {
- assert(current != nullptr);
- if (previous == nullptr)
- my_list.First = current->Next;
- else
- previous->Next = current->Next;
- delete current;
- current = nullptr;
- }
- template <class T> bool Iterator <T> :: operator++(){
- if (current == nullptr){ // move current pointer
- if (previous == nullptr) // to next element
- current = my_list.First;
- else
- current = previous->Next;
- }
- else {
- previous = current;
- current = current->Next;
- }
- return current != nullptr;
- } // valid for prefix operator only!
- template <class T> bool Iterator <T> :: operator++(int){
- return operator++();
- }
- template <class T> bool Iterator <T> :: operator !(){
- if (current == nullptr and previous != nullptr)
- current = previous->Next;
- return current != nullptr; // termination of iterator
- }
- template <class T> void Iterator <T> :: add_before(T val)
- {
- if (previous)previous = previous->insert(val);
- else {
- my_list.list<T>::add(val); // to avoid subclass
- previous = my_list.First;
- current = previous->Next; // if current is NULL
- }
- }
- template <class T> void Iterator <T> :: add_after(T val)
- {
- if (current){current->insert(val); return;} // not shown
- if (previous)current = previous->insert(val);
- else my_list.list<T>::add(val);
- }
- template <class T> class SortedList : public list<T>
- {
- public:virtual void add (T value);
- };
- template <class T> void SortedList<T> :: add (T value)
- {
- Punkt pom;
- Iterator <T> itr(*this);
- for(itr.init(); ! itr; ++itr)
- {
- pom=itr();
- if(value <pom)
- {
- itr.add_before(value);
- return;
- }
- }
- itr.add_before(value);
- }
- int main()
- {
- SortedList <Punkt> lista;
- Iterator <Punkt> iterator(lista);
- int n;
- cin>>n;
- string nazwa;
- double x, y;
- for(int i=0; i<n; i++)
- {
- cin>>nazwa>>x>>y;
- Punkt p(nazwa,x,y);
- lista.add(p);
- }
- int a;
- cin>>a;
- for(iterator.init(); !iterator; iterator++)
- {
- Punkt p=iterator(); //operator () - zwraca wartosc
- if(p.suma()==a)
- iterator.remove_current();
- }
- for(iterator.init(); !iterator; iterator++)
- {
- cout << iterator();
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement