函数题:Rectangle, the states
We want to implement a program to draw a rectangle interactively:
1. Trigger the rectangle mode.
2. Move the mouse and press the left mouse button to determine the coordinates of the first point (if the right mouse button is pressed, then exit the rectangle mode).
3. Move the mouse to select the second point while continuously drawing the rectangle that changes with the mouse (if the right mouse button is pressed, the rectangle is abandoned). At this phase, press the left mouse button to determine the second point and draw the final rectangle.
4. Then everything starts again.
The above description can be expressed as the following state diagram.

Each square in the diagram is a state, and the system starts from the Idle state. The capital letters above the lines indicate the received input. Next to the letter (followed by a colon), the text indicates the action to take when this input is received. The arrows indicate the state transfer when the input is received.
The program reads an integer which is the number of the case. After that, the program reads a string from the standard input containing the sequence of "SMLR" characters and then performs the appropriate action based on this sequence. Once it enters a state, the program outputs a capital letter that represents the state. The letters of the three states are:
I for IDLE
F for FIRST
S for SECOND
The program also outputs the action to be performed, and there are only two actions to output:
D for DRAW
E for ERASE
The program ends when the input string ends, and does not need to output anything to indicate the end.
If an undefined input is received at one state, e.g., M received at IDLE, the program does nothing and keeps on the IDLE state.
You need to implement a class called State, and the subclasses of State for each state mentioned above. The State has a count variable to trace the number of objects instantiated by the derived classes of State.
There is another class StatePool which maintains all the possible objects of State. All the objects of State are created at the beginning of the program. And they can be retrieved by their names via get_state().
We provide some parts of State and StatePool for you. So do not forget to include them in your submission.
The following code is the main function of the program. From this code, you can derive all the necessary functions required for the State class. You must include all the provided lines from /* Your submit begins here*/ to /* Your submit ends here*/ in your submission.
### Class Definition:
c++
class State {
};
class StatePool {
};
### Main Function:
c++
#include <iostream>
#include <string>
#include <map>
using namespace std;
/* Your submit begins here*/
class State {
public:
static int get_count() { return count; }
protected:
State() {count++;}
private:
static int count;
/* Feel free to add anthing needed.*/
};
class StatePool {
public:
static void create_states();
static void free_states() {
/* put your code here*/
}
static void set_state(string name, State* state) {
mStates[name] = state;
};
static State* get_state(string name) {
return mStates[name];
};
private:
static map<string, State*> mStates;
};
/* Feel free to add anything needed here.*/
/* Find a suitable place to put the subclasses of State.*/
/* Your submit ends here*/
class StateTest : public State {
virtual string toString() {
return "T";
};
public:
~StateTest() {
cout << "TEST";
}
};
void StatePool::create_states() {
set_state("IDLE", new StateIdle());
set_state("FIRST", new StateFirst());
set_state("SECOND", new StateSecond());
}
int main()
{
int case_num;
cin >> case_num;
cout << case_num;
StatePool::create_states();
string cmd;
cin >> cmd;
State *current_state = StatePool::get_state("IDLE");
cout << current_state->toString();
for ( int i=0; i<cmd.length(); i++ ) {
// cout << cmd[i] << " ";
switch (cmd[i]) {
case 'S':
current_state = current_state->cmdS();
break;
case 'L':
current_state = current_state->cmdL();
break;
case 'R':
current_state = current_state->cmdR();
break;
case 'M':
current_state = current_state->cmdM();
break;
}
cout << current_state->toString();
}
cout << endl;
if (case_num==3) {
StatePool::set_state("TEST", new StateTest());
}
if (case_num>=2) {
cout << State::get_count() << endl;
StatePool::free_states();
cout << State::get_count() << endl;
}
}
### Sample Input:
in
1LLSMMMLMML
### Sample Output:
out
1IIIFFFFDSDSDSDI
答案:若无答案欢迎评论
1. Trigger the rectangle mode.
2. Move the mouse and press the left mouse button to determine the coordinates of the first point (if the right mouse button is pressed, then exit the rectangle mode).
3. Move the mouse to select the second point while continuously drawing the rectangle that changes with the mouse (if the right mouse button is pressed, the rectangle is abandoned). At this phase, press the left mouse button to determine the second point and draw the final rectangle.
4. Then everything starts again.
The above description can be expressed as the following state diagram.

Each square in the diagram is a state, and the system starts from the Idle state. The capital letters above the lines indicate the received input. Next to the letter (followed by a colon), the text indicates the action to take when this input is received. The arrows indicate the state transfer when the input is received.
The program reads an integer which is the number of the case. After that, the program reads a string from the standard input containing the sequence of "SMLR" characters and then performs the appropriate action based on this sequence. Once it enters a state, the program outputs a capital letter that represents the state. The letters of the three states are:
I for IDLE
F for FIRST
S for SECOND
The program also outputs the action to be performed, and there are only two actions to output:
D for DRAW
E for ERASE
The program ends when the input string ends, and does not need to output anything to indicate the end.
If an undefined input is received at one state, e.g., M received at IDLE, the program does nothing and keeps on the IDLE state.
You need to implement a class called State, and the subclasses of State for each state mentioned above. The State has a count variable to trace the number of objects instantiated by the derived classes of State.
There is another class StatePool which maintains all the possible objects of State. All the objects of State are created at the beginning of the program. And they can be retrieved by their names via get_state().
We provide some parts of State and StatePool for you. So do not forget to include them in your submission.
The following code is the main function of the program. From this code, you can derive all the necessary functions required for the State class. You must include all the provided lines from /* Your submit begins here*/ to /* Your submit ends here*/ in your submission.
### Class Definition:
c++
class State {
};
class StatePool {
};
### Main Function:
c++
#include <iostream>
#include <string>
#include <map>
using namespace std;
/* Your submit begins here*/
class State {
public:
static int get_count() { return count; }
protected:
State() {count++;}
private:
static int count;
/* Feel free to add anthing needed.*/
};
class StatePool {
public:
static void create_states();
static void free_states() {
/* put your code here*/
}
static void set_state(string name, State* state) {
mStates[name] = state;
};
static State* get_state(string name) {
return mStates[name];
};
private:
static map<string, State*> mStates;
};
/* Feel free to add anything needed here.*/
/* Find a suitable place to put the subclasses of State.*/
/* Your submit ends here*/
class StateTest : public State {
virtual string toString() {
return "T";
};
public:
~StateTest() {
cout << "TEST";
}
};
void StatePool::create_states() {
set_state("IDLE", new StateIdle());
set_state("FIRST", new StateFirst());
set_state("SECOND", new StateSecond());
}
int main()
{
int case_num;
cin >> case_num;
cout << case_num;
StatePool::create_states();
string cmd;
cin >> cmd;
State *current_state = StatePool::get_state("IDLE");
cout << current_state->toString();
for ( int i=0; i<cmd.length(); i++ ) {
// cout << cmd[i] << " ";
switch (cmd[i]) {
case 'S':
current_state = current_state->cmdS();
break;
case 'L':
current_state = current_state->cmdL();
break;
case 'R':
current_state = current_state->cmdR();
break;
case 'M':
current_state = current_state->cmdM();
break;
}
cout << current_state->toString();
}
cout << endl;
if (case_num==3) {
StatePool::set_state("TEST", new StateTest());
}
if (case_num>=2) {
cout << State::get_count() << endl;
StatePool::free_states();
cout << State::get_count() << endl;
}
}
### Sample Input:
in
1LLSMMMLMML
### Sample Output:
out
1IIIFFFFDSDSDSDI
答案:若无答案欢迎评论