Advertisement
Guest User

Untitled

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