khushitshah

Untitled

Dec 22nd, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. /* Define your class and its methods here*/
  4. class MovingPoint {
  5.     int x, y;
  6.     bool a = false;
  7.     public:
  8.         void initialize(int _x, int _y) {
  9.             if(a)
  10.                 return;
  11.  
  12.             a = true;
  13.             x = _x;
  14.             y = _y;
  15.         }
  16.        
  17.         void move_left(int units) {
  18.             x -= units;
  19.         }
  20.    
  21.    
  22.         void move_right(int units) {
  23.             x += units;
  24.         }
  25.    
  26.         void move_up(int units) {
  27.             y += units;
  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. int main()
  39. {
  40.  
  41.   MovingPoint mp;
  42.   int x, y, n, i, units;
  43.   char direction;
  44.  
  45.   std::cin >> x >> y;
  46.   mp.initialize(x, y);
  47.  
  48.   std::cin >> n;
  49.   for(i = 0; i < n; i++)
  50.   {
  51.     std::cin >> direction >> units;
  52.     switch(direction)
  53.     {
  54.       case 'L':
  55.         mp.move_left(units);
  56.         break;
  57.       case 'R':
  58.         mp.move_right(units);
  59.         break;
  60.       case 'U':
  61.         mp.move_up(units);
  62.         break;
  63.       case 'D':
  64.         mp.move_down(units);
  65.         break;
  66.     }
  67.   }
  68.  
  69.   // This call to initialize method should be ingored
  70.   // If this method is called twice, it should be ignored as per specification
  71.   // This call should not change the state of the object
  72.   mp.initialize(0, 0);
  73.  
  74.   // Printing final position of the point as output
  75.   mp.print_current_position();
  76.  
  77.   return 0;
  78. }
Add Comment
Please, Sign In to add comment