// Edit the article
// Time: 2005-7-6
// Program: Zhang jianbo
# Include <iostream. h>
# Include <string. h>
# Include <stdio. h>
# Include "menu. H"
# Include "key. H"
# Include <stdlib. h>
Typedef struct line {
// Char data [80];
Char * data; // dynamically allocates memory when the string pointer is required
Struct line * next;
} Line;
Void createtxt (line * & head); // create a list and input text data to it.
Int count_space (line * & head); // counts the number of spaces.
Int count_zm (line * & head); // count letters
Int count_all_word (line * & head); // count the total number of words in the document
Int find_word (line * & head, char * Sch); // count the number of times Sch appears in the article
Int count_num (line * & head); // number of statistics
Void del_string (line * & head, char * Sch); // delete a specified string
Void outputtxt (line * & head); // output the article to the screen
Void TJ (line * & head); // statistics
Int _ f5_main (){
Line * head; // The first node of the article.
Menu M [10];
M [1]. Name = "new text linked list ";
M [2]. Name = "Browse input text ";
M [3]. Name = "text Statistics ";
M [4]. Name = "string Statistics ";
M [5]. Name = "deleting strings ";
M [6]. Name = "return ";
Int T = 1, ID;
While (t)
{
Showmenu ("Data Structure ---- document editing", M, 6); // display menu
Id = selectmenuid ();
Switch (ID)
{
Case 1: createtxt (head); break;
Case 2 :{
// System ("CLS"); // clear the screen and call the System Shell Command
Outputtxt (head); // output the article to the screen
Initkey ();
Break;
}
Case 3 :{
// System ("CLS"); // clear the screen and call the System Shell Command
TJ (head );
Initkey (); // keyboard interrupt
Break;
}
Case 4 :{
// System ("CLS"); // clear the screen and call the System Shell Command
Cout <"/n enter the string to be counted/N ";
Char Sch [20];
Cin> Sch;
Cout <"/N" <Sch <"appears:" <find_word (Head, Sch );
Initkey ();
Break;
}
Case 5 :{
Cout <"/n enter a string to be deleted:" <Endl;
Char tmp_sch [20];
Cin> tmp_sch;
Del_string (Head, tmp_sch); // delete a specified character
Outputtxt (head); // output the article to the screen
Initkey ();
Break;
}
Case 6: Return 0;
}
}
Return 0;
}
Void createtxt (line * & head ){
Printf ("/n please input text, each line can be up to 80 characters! /N ");
Printf ("Enter Ctrl + E (^ e) to end input/N ");
Line * P = new line; // first create an additional header node for the linked list
Head = P; // pay P to the header pointer.
Char TMP [80];
While (1)
{
Gets (TMP); // input string! Use C Input Functions
// Printf ("% d", strlen (TMP ));
If (TMP [0] = 5) break; // If input ^ e is found, exit the input.
P = p-> next = new line;
P-> DATA = new char [strlen (TMP) + 1]; // allocate space for nodes
Strcpy (p-> data, TMP );
If (TMP [strlen (TMP)-1] = 5) {// remove the last controller ^ e
P-> data [strlen (TMP)-1] = '/0 ';
Break;
}
}
P-> next = NULL; // The Last pointer is null.
Head = head-> next;
}
Int count_space (line * & head) {// count the number of spaces
Line * P = head;
Int asc_space = 32; // The ascic code value of the space
Int COUNT = 0;
Do
{
Int Len = strlen (p-> data); // calculate the number of data elements in the current data
For (INT I = 0; I <Len; I ++)
If (p-> data [I] = asc_space) Count ++; // calculates the number of spaces
}
While (P = p-> next )! = NULL); // traverse the linked list
Return count;
}
Int count_num (line * & head) {// number of statistics
Line * P = head;
Int COUNT = 0;
Do
{
Int Len = strlen (p-> data); // calculate the number of data elements in the current data
For (INT I = 0; I <Len; I ++)
If (p-> data [I] >=48 & P-> data [I] <= 57) Count ++; // calculates the number of spaces
}
While (P = p-> next )! = NULL); // traverse the linked list
Return count;
}
Int count_zm (line * & head) {// count letters
Int COUNT = count_all_word (head); // The total number of characters, including Spaces
Int space_count = count_space (head); // number of spaces
Return count-space_count; // return the total number of letters in the article
}
Int count_all_word (line * & head) {// count the total number of words in the article
Line * P = head; // Save the first address of the linked list
Int COUNT = 0; // The total number of letters.
Do
{Count + = strlen (p-> data);} // calculates the number of characters in the current row! Except the '/0' Terminator! Note that the statistics contain "length of space !"
While (P = p-> next )! = NULL); // traverse the linked list
Return count;
}
Int find_word (line * & head, char * Sch) {// count the number of times Sch appears in the article
Line * P = head;
Int COUNT = 0;
Int h = 0;
Int len1 = 0; // The total number of characters in the current row.
Int len2 = strlen (Sch); // length of the string to be counted
Int I, J, K;
Do
{
Len1 = strlen (p-> data); // number of characters in the current row
For (I = 0; I <len1; I ++)
{
If (p-> data [I] = Sch [0])
{
K = 0;
For (j = 0; j <= len2-1; j ++)
If (p-> data [I + J] = Sch [J]) k = k + 1;
If (k = len2) {count ++; I = I + k-1 ;}
}
}
}
While (P = p-> next )! = NULL); // traverse the linked list
Return count;
}
Void del_string_word (char * s, char * Sch)
{
// * S is the input string
// * Sch is the character to be deleted
Char * P = strstr (S, Sch); // query result
Char TMP [80];
Int Len = strlen (s );
Int I = len-strlen (P );
Int J = I + strlen (Sch );
Int COUNT = 0;
For (int K = 0; k <I; k ++) TMP [count ++] = s [k];
For (int kk = J; KK <Len; KK ++) TMP [count ++] = s [Kk];
TMP [count] = '/0 ';
Strcpy (S, TMP); // returns the New String
}
Void del_string (line * & head, char * Sch) {// delete a specified string
Line * P = head;
Do
{
If (strstr (p-> data, Sch )! = NULL) del_string_word (p-> data, Sch );
} While (P = p-> next )! = NULL); // traverse the linked list
}
Void outputtxt (line * & head) {// output the article to the screen
Line * P = head;
Do
{Cout <p-> data <Endl;
} While (P = p-> next )! = NULL); // traverse the linked list
}
Void TJ (line * & head) {// statistics
Cout <"the statistical results of the article are as follows:/N ";
Cout <"/n English letters:" <count_zm (head );
Cout <"/n Space:" <count_space (head );
Cout <"/n contains numbers:" <count_num (head );
Cout <"/n total number of words in the statistical article:" <count_all_word (head );
}