-------------------------------------------------------------------------------------------------
Problem Statement
When editing a single line of text, there are four keys that can be used to move the cursor: end, home, left-arrow and right-arrow. As you would expect, left-arrow and right-arrow move the cursor one character left or one character right, unless the cursor is at the beginning of the line or the end of the line, respectively, in which case the keystrokes do nothing (the cursor does not wrap to the previous or next line). The home key moves the cursor to the beginning of the line, and the end key moves the cursor to the end of the line. You will be given a int, N, representing the number of character in a line of text. The cursor is always between two adjacent characters, at the beginning of the line, or at the end of the line. It starts before the first character, at position 0. The position after the last character on the line is position N. You should simulate a series of keystrokes and return the final position of the cursor. You will be given a String where characters of the String represent the keystrokes made, in order. 'L' and 'R' represent left and right, while 'H' and 'E' represent home and end.
Definition
Class:
CursorPosition
Method:
getPosition
Parameters:
String, int
Returns:
int
Method signature:
int getPosition(String keystrokes, int N)
(be sure your method is public)
Constraints
keystrokes will be contain between 1 and 50 'L', 'R', 'H', and 'E' characters, inclusive.
N will be between 1 and 100, inclusive.
Examples
0)
"ERLLL"
10
Returns: 7
First, we go to the end of the line at position 10. Then, the right-arrow does nothing because we are already at the end of the line. Finally, three left-arrows brings us to position 7.
1)
"EHHEEHLLLLRRRRRR"
2
Returns: 2
All the right-arrows at the end ensure that we end up at the end of the line.
2)
"ELLLELLRRRRLRLRLLLRLLLRLLLLRLLRRRL"
10
Returns: 3
3)
"RRLEERLLLLRLLRLRRRLRLRLRLRLLLLL"
19
Returns: 12
-------------------------------------------------------------------------------------------------
solve using C++
/************************************************************************************************
cursor.h************************************************************************************************/ #ifndef _CURSOR_H
#define _CURSOR_H
#include "string"
class CCursor
{
private:
unsigned int ui_cursor_position;
unsigned int ui_character_number;
public:
CCursor();
CCursor( const unsigned int, const unsigned int );
~CCursor();
void left_move();
void right_move();
void home_move();
void end_move();
void keystroke_simulate( const char );
int get_position( const std :: string, const int );
};
#endif
/************************************************************************************************
cursor.cc
************************************************************************************************/
#include "cursor.h"
#include "string"
//default constructor
CCursor :: CCursor()
{
ui_cursor_position = 0;
ui_character_number = 0;
}
//constructor
CCursor :: CCursor( const unsigned int cursor_postion = 0,
const unsigned int character_number = 0 )
{
ui_cursor_position = cursor_postion;
ui_character_number = character_number;
}
//destructor
CCursor :: ~CCursor()
{
true;
}
//left move
void CCursor :: left_move()
{
if ( 0 == ui_cursor_position )
{
return;
}
else
{
--ui_cursor_position;
}
}
//right move
void CCursor :: right_move()
{
if ( ui_character_number == ui_cursor_position )
{
return;
}
else
{
++ui_cursor_position;
}
}
//end move
void CCursor :: end_move()
{
ui_cursor_position = ui_character_number;
}
//home move
void CCursor :: home_move()
{
ui_cursor_position = 0;
}
void CCursor :: keystroke_simulate( const char ch )
{
//left
if ( 'L' == ch )
{
return ( this -> left_move() );
}
//right
if ( 'R' == ch )
{
return ( this -> right_move() );
}
//end
if ( 'E' == ch )
{
return ( this -> end_move() );
}
//home
if ( 'H' == ch )
{
return ( this -> home_move() );
}
//false mode
return;
}
int CCursor :: get_position( const std :: string KEY, const int N )
{
ui_character_number = static_cast
for ( int i = 0; i <= KEY.size(); ++i ) { this -> keystroke_simulate( KEY[i] );
}
return ui_cursor_position;
}
/***********************************************************************************************
test.cc
************************************************************************************************/
#include "cursor.h"
#include "iostream"
#include "string"
int main()
{
CCursor *cc = new CCursor();
std :: string s = "RRLEERLLLLRLLRLRRRLRLRLRLRLLLLL";
int N = 19;
std :: cout << ( cc -> get_position( s, N ) );
delete cc;
return 0;
}
compile and link
g++ -c cursor.cc
g++ -c test.cc
g++ -o test test.o cursor.o



