khushitshah

Program 2

Dec 22nd, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.22 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class MovingPoint {
  6. int x, y;
  7. bool a = false;
  8. public:
  9. void initialize(int _x, int _y) {
  10. if(a)
  11. return;
  12. a = true;
  13. x = _x;
  14. y = _y;
  15. }
  16.  
  17. void move_left(int units) {
  18. x -= units;
  19. }
  20.  
  21. void move_right(int units) {
  22. x += units;
  23. }
  24.  
  25. void move_up(int units) {
  26. y += units;
  27. }
  28.  
  29. void move_down(int units) {
  30. y -= units;
  31. }
  32.  
  33. void print_current_position() {
  34. std::cout << x << " " << y << std::endl;
  35. }
  36. };
  37.  
  38. class Rectangle {
  39. MovingPoint top_left, top_right, bottom_left, bottom_right;
  40. bool init = false;
  41. public:
  42. void initialize(int tl_x, int tl_y, int tr_x, int tr_y, int bl_x, int bl_y, int br_x, int br_y) {
  43. if(init)
  44. return;
  45. top_left.initialize(tl_x, tl_y);
  46. bottom_left.initialize(bl_x, bl_y);
  47. top_right.initialize(tr_x, tr_y);
  48. bottom_right.initialize(br_x, br_y);
  49. init = true;
  50. }
  51. void moveLeft(int unit) {
  52. top_left.move_left(unit);
  53. bottom_left.move_left(unit);
  54. top_right.move_left(unit);
  55. bottom_right.move_left(unit);
  56. }
  57. void moveRight(int unit) {
  58. top_left.move_right(unit);
  59. bottom_left.move_right(unit);
  60. top_right.move_right(unit);
  61. bottom_right.move_right(unit);
  62. }
  63. void moveUp(int unit) {
  64. top_left.move_up(unit);
  65. bottom_left.move_up(unit);
  66. top_right.move_up(unit);
  67. bottom_right.move_up(unit);
  68. }
  69. void moveDown(int unit) {
  70. top_left.move_down(unit);
  71. bottom_left.move_down(unit);
  72. top_right.move_down(unit);
  73. bottom_right.move_down(unit);
  74. }
  75. void alterDecreaseLeft(int unit) {
  76. top_left.move_right(unit);
  77. bottom_left.move_right(unit);
  78. }
  79. void alterIncreaseLeft(int unit) {
  80. top_left.move_left(unit);
  81. bottom_left.move_left(unit);
  82. }
  83. void alterDecreaseRight(int unit) {
  84. top_right.move_left(unit);
  85. bottom_right.move_left(unit);
  86. }
  87. void alterIncreaseRight(int unit) {
  88. top_right.move_left(unit);
  89. bottom_right.move_left(unit);
  90. }
  91. void alterDecreaseTop(int units) {
  92. top_left.move_down(units);
  93. top_right.move_down(units);
  94. }
  95. void alterIncreaseTop(int units) {
  96. top_left.move_up(units);
  97. top_right.move_up(units);
  98. }
  99. void alterDecreaseBottom(int units) {
  100. bottom_left.move_up(units);
  101. bottom_right.move_up(units);
  102. }
  103. void alterIncreaseBottom(int units) {
  104. bottom_left.move_down(units);
  105. bottom_right.move_down(units);
  106. }
  107. void display() {
  108. top_left.print_current_position();
  109. top_right.print_current_position();
  110. bottom_left.print_current_position();
  111. bottom_right.print_current_position();
  112.  
  113. }
  114. };
  115.  
  116. int main()
  117. {
  118. Rectangle robj;
  119. int tl_x,tl_y,tr_x,tr_y,bl_x,bl_y,br_x,br_y;
  120. int numberOfActions;
  121. char inputletter;
  122. int units;
  123. cin>>tl_x>>tl_y;
  124. cin>>tr_x>>tr_y;
  125. cin>>bl_x>>bl_y;
  126. cin>>br_x>>br_y;
  127. robj.initialize(tl_x,tl_y,tr_x,tr_y,bl_x,bl_y,br_x,br_y);
  128. //cout<<"robj after initialization : ";
  129. //robj.display();
  130. cin>>numberOfActions;
  131. //cout<<"Number of actions read "<<numberOfActions<<endl;
  132. while(numberOfActions)
  133. {
  134. cin>>inputletter;
  135. //cout<<"Input Letter read "<<inputletter;
  136. switch(inputletter)
  137. {
  138. case 'L'://Left Move
  139. cin>>units;
  140. //cout<<"You are moving left by "<<units;
  141. robj.moveLeft(units);
  142. break;
  143. case 'R'://Right Move
  144. cin>>units;
  145. robj.moveRight(units);
  146. break;
  147. case 'U'://Up Move
  148. cin>>units;
  149. robj.moveUp(units);
  150. break;
  151. case 'D'://Down Move
  152. cin>>units;
  153. robj.moveDown(units);
  154. break;
  155. case 'A'://Alter example A D L 5
  156. cin>>inputletter;
  157. //cout<<inputletter;
  158. switch(inputletter)
  159. {
  160. case 'D'://Decrease
  161.  
  162. cin>>inputletter;
  163. //cout<<inputletter;
  164. cin>>units;
  165. //cout<<units;
  166. switch(inputletter)
  167. {
  168. case 'L':// A D L
  169. robj.alterDecreaseLeft(units);
  170. break;
  171. case 'R': // A D R
  172. robj.alterDecreaseRight(units);
  173. break;
  174. case 'T': // A D T
  175. robj.alterDecreaseTop(units);
  176. break;
  177. case 'B': // A D B
  178. robj.alterDecreaseBottom(units);
  179. break;
  180. default:
  181. cout<<"Input letter not supported. Read Place: 3"<<endl;
  182.  
  183. };
  184. break;
  185.  
  186. case 'I'://Increase
  187. cin>>inputletter;
  188. //cout<<inputletter;
  189. cin>>units;
  190. //cout<<units;
  191. switch(inputletter)
  192. {
  193. case 'L':// A I L
  194. robj.alterIncreaseLeft(units);
  195. break;
  196. case 'R':// A I R
  197. robj.alterIncreaseRight(units);
  198. break;
  199. case 'T':// A I T
  200. robj.alterIncreaseTop(units);
  201. break;
  202. case 'B':// A I B
  203. robj.alterIncreaseBottom(units);
  204. break;
  205. default:
  206. cout<<"Input letter not supported. Read Place: 4"<<endl;
  207. };
  208.  
  209. break;
  210. default:
  211. cout<<"Iput letter not supported. Read Place: 2 "<<inputletter;
  212.  
  213. };
  214. break;
  215. default:
  216. cout<<"Iput letter not supported. Read Place: 1 "<<inputletter;
  217. };
  218. numberOfActions--;
  219. }
  220. robj.display();
  221. return (0);
  222. }
Add Comment
Please, Sign In to add comment