Article editor

Keywords: C++

1) Design purpose:

(1) Memorize and understand common data structures such as linear table, stack, queue, string, array, tree and graph from three aspects of data logical structure, storage structure and operation;

(2) Understand the sorting and search operations implemented on various common data structures;

(3) Have a certain ability to analyze the time and space complexity of the algorithm;

(4) For common application problems, be able to select appropriate data structures and design effective algorithms to solve them;


2) Basic requirements:

(1) Establish functional modules according to the specific requirements of curriculum design, including:

    a) Requirements analysis: describe the functional requirements of each module in this part.

    b) Detailed design: the algorithm design description of each part (which can be the flow chart describing the algorithm) and the design description of the storage structure used in each program (if the storage structure is specified, please write out the definition of the storage structure).

    c) Code and results: the core code of each algorithm should have a clear structure and attach the debugging results.

(2) The summary of the article can include:

Harvest, thinking about the process of solving problems, thinking about program debugging ability, thinking about data structure, etc.

(3) The programming is concise and available, so as to make the function of the system more perfect and comprehensive as far as possible;

(4) If possible, give instructions or flow charts;


3) Demand analysis:

Function: input a page of Western text, and the program can count the number of words, numbers and spaces.

Static storage of a page of articles, each line of no more than 80 characters, a total of N lines;

The storage structure uses a linear table, and several sub functions are used to realize the corresponding functions;

Form and range of input data: you can input uppercase and lowercase English letters, any numbers and punctuation marks.

Output form:

(1) Output the characters of each line entered by the user in the branch;

(2) Output "all letters", "numbers", "spaces" and "total words of the article" in 4 lines;

(3) Output the article after deleting a string;


4) Detailed design:

1. Module analysis

(1) Structure

Define the structure link and store it in sequence. Row to row chained storage.

(2) Functions

The article function is responsible for displaying articles


The init function initializes the article.

When you enter @, end the input and generate an article.


The function letter counts the number of letters

Traverse each structure link and calculate the number of letters encountered


The function number counts the number of numbers

Traverse each structure link and calculate the number of numbers encountered


The function blank counts the number of spaces

Traverse each structure link and calculate the number of spaces encountered


The sum function counts the total number of characters

Traverse each structure link and calculate the number of characters encountered


The del function deletes the first substring of a line

The parameter is a string and the string to be retrieved. Removes the first string from the string.


The function deletestr deletes the specified substring in the text

The parameter is a string and the string to be retrieved. This function will loop through the sel function to delete all strings of the specified string.


2. Flow chart





5) Codes and results:

Main code:

The article function takes a parameter link * & head and prints all data of the head and all its next, that is, the whole article.

The init function accepts a parameter link * & head to initialize it. The article is typed by the user. If @ @, it stops accepting characters and saves the article in the linear table.

The Letter, number, blank and sum functions all accept a parameter link * & head to count and print the total English characters, numbers, spaces and characters in the linear table data.

The del function accepts a char  * Type, where str is the substring of the string to be searched. This function deletes the first str string in s.

The deletestr function takes a parameter link * & head, and the user types a string str. This function circularly traverses all data of head and its next, and executes del function on all data to delete all STR substrings in the article referred to by head.

The main function initializes a linear table link and executes letter, number, blank and sum functions on it respectively. After that, execute the deletestr function.


Test data and results:




6) Summary:

This course design mainly relies on the linear table to do the article editing program. Users can input articles through the keyboard and get feedback on the relevant information of the articles. This course design has understood the storage mechanism and use mode of linear table, deepened my understanding of pointer and address operation, and played a warm-up role for future research and learning. Good basic skills.



7) Appendix:


Full code:


  1 #include<iostream>
  2 using namespace std;
  4 typedef struct link{
  5     char *data;
  6     struct link *next;
  7 }LINK;
  9 void article(link * &head){
 10     LINK *p=head;
 11     cout<<"The article is as follows:"<<endl;
 12     do{
 13         cout<<p->data<<endl;
 14         p=p->next;
 15     }while(p!=NULL);
 16     cout<<endl;
 17 }
 19 void init(link * &head){
 20     cout<<"Input articles (no more than 80 words per line)@@End):"<<endl;
 21     link *p=new link;
 22     head=p;
 23     char line[100];
 24     //When input is not@@Time
 25     while(1){
 26         gets(line);
 27         if (strlen(line)>80){
 28             cout<<"Too many words"<<endl;
 29             break;
 30         }
 31         if(line[0]==64&&line[1]==64)break;
 32         p=p->next=new LINK;
 33         p->data=new char[strlen(line)+1];
 34         //Assign a value to the address pointed to by the structure pointer
 35         strcpy(p->data, line);
 36         int j=1;
 37         for(int i=0;i<strlen(line)-1;i++){
 38             if(line[i]==64&&line[i+1]==64){
 39                 p->data[i]='\0';
 40                 j=0;
 41                 goto A;
 42             }
 44         }
 46     }
 47 A:
 48     p->next=NULL;
 49     head=head->next;
 50     article(head);
 51     cout<<endl;
 53 }
 55 void letter(link * &head){
 56     link *p=head;
 57     int letter=0;
 58     do{
 59         //Number of elements
 60         int len=strlen(p->data);
 61         for(int i=0;i<len;i++)
 62         //If it's a letter
 63         if((p->data[i]>='a'&&p->data[i]<='z')||(p->data[i]<='Z'&&p->data[i]>='A'))letter++;
 64     }while ((p=p->next)!=NULL);
 65     cout<<"All letters:"<<letter<<endl;
 67 }
 69 void number(link * &head){
 70     link *p=head;
 71     int number=0;
 72     do{
 73         //Number of elements
 74         int len=strlen(p->data);
 75         for(int i=0;i<len;i++)
 76         //If it's a number
 77         if((p->data[i]>='0'&&p->data[i]<='9'))number++;
 78     }while ((p=p->next)!=NULL);
 79     cout<<"Number of numbers:"<<number<<endl;
 81 }
 83 void blank(link * &head){
 84     link *p=head;
 85     int blank=0;
 86     do{
 87         //Number of elements
 88         int len=strlen(p->data);
 89         for(int i=0;i<len;i++)
 90         //If it's a space
 91         if(p->data[i]==' ')blank++;
 92     }while ((p=p->next)!=NULL);
 93     cout<<"Number of spaces:"<<blank<<endl;
 95 }
 97 void sum(link * &head){
 98     link *p=head;
 99     int words=0;
100     do{
101         words+=strlen(p->data);
102     }while ((p=p->next)!=NULL);
103     cout<<"Total words of the article:"<<words<<endl;
105 }
108 void del(char *s,char *str){
109     //str Is a substring
110     char *p=strstr(s,str);    //seek str Location of the first occurrence
111     char ch[80];
112     int len=strlen(s);
113     int i=len-strlen(p);
114     int j=i+strlen(str);
115     int number=0;
116     for(int m=0;m<i;m++)ch[number++]=s[m];
117     for(int n=j;n<len;n++)ch[number++]=s[n];
118     ch[number]='\0';
119     //hold ch Copy the string pointed to to to s
120     strcpy(s, ch);
122 }
124 void deletestr(link * &head){
125     link *p=head;
126     char str[20];
127     cout<<"Please enter a string to delete:";
128     cin>>str;
129     int i=0,j=0;
130     //Recycling del()function
131     while(p!=NULL)
132     {
133         if(strstr(p->data,str)!=NULL){
134             del(p->data,str);
135             i++;
136             cout<<p->data<<endl;
137         }else j++;
138         if(i==j){
139             p=p->next;
140             break;
141         }
143     }
144     cout<<"String deleted!"<<endl;
145     article(head);
146     cout<<endl;
147 }
149 int main(){
150     link* link;
151     //initialization
152     init(link);
154     letter(link);
155     number(link);
156     blank(link);
157     sum(link);
159     //delete
160     deletestr(link);
162     return 0;
163 };






Posted by gaza165 on Sat, 04 Dec 2021 13:47:58 -0800