carry on:
The program is basically written, and of course there are some to add.
Let's post the original code, there are several places to modify:
TXTMONSTER.H:
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;
}
Class CTXTMonster
{
Private:
Surface txt_tree;
INT Printline (Point *);
INT Prolongline (char); / / Pressing a character to the LINE structure
INT newline ();
INT DellinePoint (Line *); // Clear all the Point under a LINE
INT PRINTTREE (LINE *); // Outputs the entire structure
INT Killme (Line *);
PUBLIC:
CTXTMonster ();
~ CtxtMonster ();
INT Eatchar (char); / / Input characters to objects, any character can be
INT Showline (int); // parameter represents the line number required to output, starting with 1
Void showme ();
}
The character tree structure has not changed, but the member function has changed.
---------------------------------
TXTMONSTER.CPP:
#include
#include "txtmonster.h"
Using namespace std;
CTXTMonster :: ctxtMonster ()
{
TXT_TREE.HEAD = NULL;
Line * first_line = null;
First_LINE = New line;
First_LINE-> Head = NULL;
First_LINE-> Next = NULL;
TXT_TREE.HEAD = first_LINE;
}
CTXTMonster :: ~ ctxtMonster ()
{
/ *********************
Release all units
********************* /
Killme (TXT_TREE.HEAD);
}
Int CtxtMonster :: PrintLine (Point * Pme)
{
IF (PME == null) Return 1;
IF (PME-> Next! = NULL)
Printline (PME-> Next);
Cout << Pme-> C;
Return 0;
}
Int CtxtMonster :: Prolongline (char NOSH)
{
Point * pnosh;
Point * t;
Pnosh = new point;
Pnosh-> c = NOSH;
Pnosh-> next = NULL;
T = txt_tree.head-> head;
TXT_TREE.HEAD-> Head = Pnosh;
Pnosh-> next = t;
Return 0;
}
Int ctxtMonster :: newline () {
IF (txt_tree.head-> head == null) // Don't worry TXT_TREE.HEAD is a NULL, because it adds a line when it is constructed.
Return 1;
// remove the space at the end of LINE
While (txt_tree.head-> head-> c == 32) // Note: This process of deleting spaces does not check if it will be removed,
// Judgment it before calling this function
{
Point * t = NULL;
T = txt_tree.head-> head-> next;
Delete txt_tree.head-> head;
TXT_TREE.HEAD-> Head = T;
}
Prolongline ('/ n');
Line * newline = NULL;
NEWLINE = New line;
NEWLINE-> Head = NULL;
NEWLINE-> Next = NULL;
NEWLINE-> Next = txt_tree.head;
TXT_TREE.HEAD = Newline;
Return 0;
}
INT CTXTMonster :: DellinePoint (Line * Pkill)
// This function just removes all the Point under LINE referred to in Pkill
{
While (pkill-> head! = null)
{
Point * t = NULL;
T = pkill-> head;
Pkill-> Head = t-> next;
Delete T;
}
Return 0;
}
INT CTXTMonster :: Killme (Line * PKILL)
{// Remove the beginning of the PKILL pointer
IF (pkill-> next! = null)
Killme (pkill-> next);
DellinePoint (pkill);
Delete pkill;
Return 0;
}
INT CTXTMonster :: PrintTree (Line * PL) / / Output of the back character tree to the screen
{
IF (PL == Null) Return 1;
IF (PL-> Next! = NULL)
{
PRINTTREE (PL-> Next);
}
PRINTLINE (PL-> HEAD);
Return 0;
}
Int ctxtMonster :: Eatchar (Char Food)
{
IF (FOOD == 10)
{
Newline ();
Return 1;
}
IF (FOOD == 32)
{
IF (txt_tree.head-> head == null) Return 2;
}
Prolongline (Food);
Return 0;
}
INT CTXTMONSTER :: Showline (int index) / / gives the line number, output corresponding to the screen
{
INT i = 0;
Line * p = txt_tree.head;
While (p! = null) // Get the total line number here, the last last LINE meter is also
{
i = i 1;
P = P-> next;
}
Cout << "Total" << i << "line." << Endl;
IF (INDEX == i) {
COUT << "The line receives the end of the data, can't output ..." << endl;
Return 1;
}
IF (INDEX> i || index <= 0)
{
Cout << "The number of rows provided exceeds the total number of records recorded!" << Endl;
Return 2;
}
i = -index;
P = txt_tree.head;
For (int J = 0; j
P = P-> next;
PRINTLINE (P-> Head);
Return 0;
}
Void CtxtMonster :: Showme ()
{
PRINTTREE (txt_tree.head-> next);
}
Main.cpp:
//main.cpp
#include
#include
#include "txtmonster.h"
Using namespace std;
void main ()
{
SYSTEM ("CLS");
Cout << "This program receives the characters entered by the keyboard, and the branch is stored in the tree structure," << endl;
Cout << "Note: Character '.' indicates an output" << Endl;
Cout << "abeni@sina.com" << Endl;
// Test
CTXTMonster * Pme;
PME = New CTXTMONSTER;
While (1)
{
CHAR T = GetChar ();
IF (t == '.')
{
COUT << Endl << "↓↓↓ 输 输 输 Output" begins ↓↓↓↓↓ "" << endl;
PME-> showme ();
Cout << "← ← ← ← ← ← ← Output end" << ENDL;
}
Else
PME-> Eatchar (T);
PME-> showline (3);
}
}
(Today is Friday, I have to go back to sleep early, I have to go to school in the morning! The above procedure has a change, I will discuss it next time.)