Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to implement a simple text editor
How to implement a simple text editor
1. Design objective

Simple text editor is a user-oriented system service program, which is widely used in a series of operations such as input, deletion, replacement, search and modification of source programs, and even for editing and typesetting newspapers and books, drafting and polishing office documents and letters. It is a practical application software. This program is specially designed for users to edit text and can meet the requirements of most text operations.

2. Design content and requirements

(1) Read the (text) file to create a new string (2) View the content and composition information of the source (text) file (3) View the composition information of the current string (4) View the string with a specified length from the specified position (5) Find the location of the specified string (7) Insert the string at the specified position of the current string (8) Delete the specified string from the current string.

(1 1) Save as a (text) file (12) and exit the program.

3. The data structure adopted in this design

The data structure used by this program is only linked list.

The basic operation and storage structure used in the program adopts the chain storage structure of strings, and the corresponding operation functions are defined.

Typedef struct _String// defines the chain string structure.

{char character; //Characters that make up a string (data fields)

Struct _ String * next// Pointer to the next string structure (chain domain)

} string; //String type

4. Detailed design of functional modules

4. 1 Detailed design concept

The basic operation and storage structure used in this program is a chain storage structure with strings, and the corresponding operation functions are defined. The operation of text editing calls the following functions, and the graphical interface of text editing is realized by combining library functions.

Define a chained storage structure:

Typedef struct _String// defines the chain string structure.

{

Char character; //Characters that make up a string (data fields)

Struct _ String * next// Pointer to the next string structure (chain domain)

} string; //String type

Defines the information structure of a string:

Typedef struct _ stringinformation//defines the string information structure.

{

String * str// chain string head node

Int letter _ count; //Number of letters

Int figures _ count// digits

Int space _ count// number of spaces

Int endline _ count// Number of line breaks

Int others _ count// Number of other characters

Int characters _ count// Total number of characters

} StringInformation// String information type

Define the cable structure of the string:

Typedef struct _Index// defines the string index structure.

{

Int sub _ index// The position (or index) of the substring in its parent string.

Struct _ Index * next _ index// Pointer to the next string index structure of substring.

} index; //String index type

The subfunctions to be called are:

int StringToUnsignedInt( char* s,int max); //Converts a string to an unsigned integer.

void InitalizeStringInformation(string information * SI,int initalizeAll); //String information initialization

void creatstringfromraray(String * * S,char * cString); //Create a string from an array of characters

int CreatStringFromFile(String * * S,char * file path); //Create a string from a file

int String length(String * S); //Find the length of the string

int String compare(String * s 1,String * S2); //String comparison

void String copy(String * * target String,String * source String); //serial replication

void SubString(String* S,int index,int subLength,String * * subS); //substring extraction

int SubStringIndex( String* S,String * subS); //Find the position where the substring first appears in the parent string.

int StringInsert( String** S,int index,String * inserted String); //String insertion

int SubStringDelete( String** S,int index,int subLength); //substring deletion

void print String(String * S); //serial output

void print Index(Index * subIndex); //All index output of substring

void PrintStringInformation(StringInformation * SI); //Character string composition information output

void SaveToFile( String* S,char * file path); //Save the string to a file.

void destroy String(String * * S); //String destruction

void destroy Index(Index * * subIndex); //substring index destruction

(1) Read a (text) file to create a new string.

The design idea of this function is to create a string from a character array or file. As far as file creation strings are concerned, you must first define a string StringInformation.

S, tempString = S.str, and then open the txt file gets( filePath) in the computer.

Call the function creatstringfromfile (&; S.str, filePath) writes filePath into the string s.

(2) Check the content and composition information of the source (text) file.

First call the function PrintString (

Source.str) for string output, and then call the subfunction printstringinformation (&; source

) output string composition information, and finally destroy string (& source. str).

(3) View the current string

Screen clearing system ("CLS"); First; Then call the subfunction PrintString( S.str) to output the string S.

(4) View the composition information of the current string.

Call the subfunction printstringinformation (&; S) outputting the composition information of the character string S.

(5) Searching for a character string with a specified length from the specified position.

Enter the starting position of the string, get (

input),position = StringToUnsignedInt(input,- 1)

Call this function to convert the input into an unsigned integer and assign the value to position;; Then enter the length of the substring gets( input), subLength =

StringToUnsignedInt (input,-1

Call this function to convert the input into an unsigned integer and assign the value to subLength, and then call this function SubString( S.str, position,

Sub-length & ampSubString) for substring extraction;

(6) Find the location of the specified string

First, you need to enter the string gets (

cString),creatstringfromraray(& amp; Substring

) and then call this function to set cString to the string subString, and call substringindexall (s.str, substring,

& ampSubIndex) outputs all the positions where the substring appears in the parent string S, and then destroys the substring.

(7) Insert the string at the position specified by the current string.

First, please enter the location of the string to be inserted, gets( input), and then call the function position = stringtousignedint (input,-1) to convert the input into an unsigned integer and assign the value to position;

Then enter the string to insert.

gets( cString),creatstringfromraray(& amp; News string

) Use the character array to create a string method to create a newString, and then call the function String Insert (&; S string, position,

NewString) inserts the newString into the file S.str, and then destroys the newString.

(8) Delete the specified substring from the current string.

First, enter the substring gets (

CString), and then call the function creatstringfromraray (&; Substring

) create a new string subString, and call the function count = substringindexall (s.str, substring,

& ampSubIndex) outputs all the positions where the substring appears in the parent string s.

Then enter the location of the substring to be deleted, and get (

input),position = StringToUnsignedInt(input,

-1) Call this function to convert the input into an unsigned integer and assign a value to position;

Then call the function to delete subStringplacesingle(& amps. str, position, substring,

Empty)

(9) Replace the substring specified by the current string.

Enter the substring gets( cString) to be replaced first.

,creatstringfromraray(& amp; Substring

) call the function to generate the string subString from cString, and call the function substringindexall (s.str, substring,

& ampSubIndex) outputs all the positions where the substring appears in the parent string s.

Then enter the location of the substring to be replaced, and get (

Input), which converts the input into unsigned plastic position = stringtousignedint (input,-1);

Then enter a string to replace the above string gets( cString), and call CreatStringFromArray (

& ampReplaceString, cString) will enter cString CreatStringFromArray (

& replace string

) to a new string replaceString, and then call the function substringplaceingle (&; S string, position,

SubString, replaceString), and then destroy the substring.

(10) Save the changes to the (text) file.

Call the function SaveToFile( S.str, filePath) to save the modified text.

(1 1) Save as a (text) file.

First, enter the absolute path gets( anotherPath) of the file to be saved as. Then call the function SaveToFile( S.str, anotherPath) to save the file.

(12) Exit the program

Call the function destructystring (&; S.str) destroys the string s and exits the program.