Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- /* Define your class and its methods here*/
- class MovingPoint {
- int x, y;
- bool a = false;
- public:
- void initialize(int _x, int _y) {
- if(a)
- return;
- a = true;
- x = _x;
- y = _y;
- }
- void move_left(int units) {
- x -= units;
- }
- void move_right(int units) {
- x += units;
- }
- void move_up(int units) {
- y += units;
- }
- void move_down(int units) {
- y -= units;
- }
- void print_current_position() {
- std::cout << x << " " << y << std::endl;
- }
- };
- int main()
- {
- MovingPoint mp;
- int x, y, n, i, units;
- char direction;
- std::cin >> x >> y;
- mp.initialize(x, y);
- std::cin >> n;
- for(i = 0; i < n; i++)
- {
- std::cin >> direction >> units;
- switch(direction)
- {
- case 'L':
- mp.move_left(units);
- break;
- case 'R':
- mp.move_right(units);
- break;
- case 'U':
- mp.move_up(units);
- break;
- case 'D':
- mp.move_down(units);
- break;
- }
- }
- // This call to initialize method should be ingored
- // If this method is called twice, it should be ignored as per specification
- // This call should not change the state of the object
- mp.initialize(0, 0);
- // Printing final position of the point as output
- mp.print_current_position();
- return 0;
- }
Add Comment
Please, Sign In to add comment