The last job has just finished, I haven't come to summarize, the next homework is coming, it is really troubled! First come, let's talk about the problem:
[description]
Design a class that can receive a data stream (now the level can be replaced by receiving keyboard input), there will be a newline in the data stream, and the leadfire and the leading space of each new line are removed, and the received data is reserved. All received data (required branch display) can be output at any time, but also can be randomly output according to the supplied line number.
[enter]
ABC
DEF
GH IJ
KL (lasting two spaces, note)
Mno (no return to the train yet, note)
[Output]
ABC
DEF
GH IJ
KL (the end of these two spaces does not output, note)
/ ************************************* /
This topic looks not very difficult, huh, huh.
I have to solve the following questions:
1. Save the received data. The reason is to be stored in the file, but I don't have a file now, there are still a lot of problems to be solved, and the file operation is temporarily put. I plan to use a structural variable or class to store all received data. You can store a linked list. I plan to use the structure shown below:
Description: I use Surface, Line, Point, Point, Points, Points to describe this structure, which is good.
My idea is that each time there is a character pressed, put it in the leftmost line, the top is the recent (new). In case of a newline, generate new LINE is connected to the left.
I think so certain is that I can complete the teacher's homework, this is still a reason, that is, I want to use this structure, so that I have further skilled the operation of the pointer, I can't judge that the consequences of this. If it is good or harmful, maybe the character array, but I think that, the array of storage strings should be designed to design how much is a problem, which may cause a lot of waste, and it may not be able to put it down (of course we You can put some flags in the structure indicate that this string has not ended), I don't know, this design will have a lot of pointer movement when reading or writing, will this reduce efficiency? I really hope that the expert guidance. But I think, just use the array, maybe we are in the output array, this is also such a large number of pointers (huh, I don't know).
2, a question I am thinking is: I have designed three structures, they are bundled together to form the structure of the character stream, this structure will have a lot of small objects, and the members of these objects should be designed to be private of? Below I still write a few Struct (what is prematured? Oh, I invented, it means the meaning of the draft).
Struct Point // Points for storing a character
{
Char C;
Point * next;
}
Struct line // Stores a string line with header
{
Point * head;
Line * Next;
}
Struct Surface // Store the face of the entire tree, take the lead node
{
Line * head;
}
// Then design a class, which uses this structure. Class ctxtMonster // This class has not been designed,
{
Private:
Surface txt_tree;
INT Printline (Point *);
PUBLIC:
CTXTMonster ();
~ CtxtMonster ();
INT INPUTCHAR (CHAR); / / Pressing a character to MONSTER
INT OUTPUTLINE (INT); // Parameter represents the line number required to output, starting with 1
INT outputsurface (); // outputs the entire structure
Int reset (); // Reset the entire object
}
Everyone may notice that in this class, my so-called Surface structure is actually a pointer. I think it is like a princess hat, and there is a lot of gallows outside, swaying, it seems to feel a little. polymerization. I always make me a feeling of packaging. I don't understand, I declare TXT_TREE this Surface structure as a private, which makes the three structures of this txt_tree will be very good? After writing this program, you must study this problem, you will then put it first. (Oh, the eyes have two problems, huh, huh)
First, INT Printline (POINT *);
Int CtxtMonster :: PrintLine (Point * Pme)
{
IF (PME-> Next! = NULL)
Printline (PME-> Next);
Cout << Pme-> C;
}
Note: This is to output a string to the screen, and the function passed to the function is the address of the first Point of Line.
Using a recursive method, so the first output should be the lower character, that character is the first pressed, just in order.
Second, INT INPUTCHAR (CHAR);
INT CTXTMonster :: Inputchar (Char Nosh)
{
Point * pnosh;
Pnosh = new point;
Pnosh-> c = NOSH;
Pnosh-> next = NULL;
... (here is inserting the Pnosh above, slightly)
Return 0;
}
Note: This is incoming character to the entire structure. There is a problem here, that is: Do we want to judge the type of the incoming character, make different processing according to different types? For example, see if there is a newline, if you create a new line, see if it is the beginning of the new line, is it a repeated wrap, etc. If it is not judged, it is also possible, but this small function should become one The internal private function is specifically used to extend the LINE structure. Which method is used? Still use full-time! Because we write the function should try to make a special one, this function of XX sex is a little bit (huh, there is a word forgot here, it seems to be a coefficient, what is coupling, huh, it is really embarrassed). Then we have to make a little modification, change the name to: int protonline (char);
In this case, this function is to do is:
1, generate a point;
2, initialize it with parameters;
3, connect it into the corresponding line;
I think so many things should be. Ok, copy this time again to modify: int CTXTMonster :: Prolongline (Char Nosh)
{
Point * pnosh;
Pnosh = new point;
Pnosh-> c = NOSH;
Pnosh-> next = NULL;
Return 0;
}
In this case, you have to add a public function. This function is used to manage the characters entered, what should be dealt with by him to determine the characters entered.
Third, since this, simply add a few private functions, responsible for several tasks below:
1. Produce a new line; (just, we can let him first judge the last line of Line, if it is, first delete it first)
2, (I haven't thought about it, huh, huh)
Int ctxtMonster :: newline ()
{
IF (txt_tree.head-> head == null) // Don't worry TXT_TREE.HEAD is a NULL,
Return 1; // Added a line in the configuration
// remove the space at the end of LINE
While (txt_tree.head-> head-> c == 32) // Note: This process of deleting spaces is not checked
/ / Will be deleted all, you must judge it before calling this function
{
Point * t = NULL;
T = txt_tree.head-> head-> next;
Delete txt_tree.head-> head;
TXT_TREE.HEAD-> Head = T;
}
Line * newline = NULL;
NEWLINE = New line;
NEWLINE-> Head = NULL;
NEWLINE-> Next = NULL;
NEWLINE-> Next = txt_tree.head;
TXT_TREE.HEAD = Newline;
}
Fourth, let's take a look at the constructor and destructor
The constructor is still more convenient:
CTXTMonster :: ctxtMonster ()
{
TXT_TREE.HEAD = NULL;
Line * first_line = null;
First_LINE-> Head = NULL;
First_LINE-> Next = NULL;
TXT_TREE.HEAD = first_LINE;
}
The deshematuring function is more troublesome, she wants to traverse the entire structure, look like it, but also add one internal function to delete Line
-Int DellinePoint (line *) This function is just to remove all Point under LINE referred to in Pkill.
CTXTMonster :: ~ ctxtMonster ()
{
/ *********************
Release all units
********************* /
Killme (TXT_TREE.HEAD);
}
INT CTXTMonster :: Killme (Line * PKILL)
{
IF (pkill-> next! = null)
Killme (pkill-> next);
DellinePoint (pkill);
Delete pkill;
Return 0;
}
INT CTXTMonster :: DellinePoint (Line * Pkill)
// This function just removes all of the POINT {below PKILL.
While (pkill-> head! = null)
{
Point * t = NULL;
T = pkill-> head;
Pkill = t-> next;
Delete T;
}
Return 0;
}
The above three functions complete the recovery of all spaces.