Home > Back-end >  Line editor/stack and queue
Line editor/stack and queue

Time:09-28

Line edit program [stack and queue]
The Description


The function is a simple line editor: receive input from a terminal user programs or data, and deposited in the user data area, due to a user input on the terminal, cannot guarantee that don't make any mistakes, so if in the editor, "each receive a character which deposited in the user data area" it is not very appropriate, a better approach is to set up an input buffer, that receives input from the user's line of characters, and then deposited in the user data area line by line, allows the user to enter gone wrong, and found errors can be corrected in a timely manner, for example, when a user find just typing a character is wrong, can fill in a backspace character "#", to indicate that the previous character is invalid; If it is found that the current type more inline error or remedy hard, then you can type in a regression operator "@", to indicate the current line of characters is void, for example, assume that from the terminal to receive such a line or two characters:

Whil# # ilr# e (s # * s)
Outcha @ putchar (* s=# + +);
The real effective is the following two lines:
While (* s)
Putchar (* s++);
Therefore, to set the input buffer for a stack structure, every time after a character from the terminal receives the first as the following criterion: if it is not back space is not regression, press the character into the stack; If it is a back space, delete a character from the stack; If it is a regression operator, the character qing is empty stack,

Input

A number of programs or data, each not more than 200 characters,

The Output

After processing by a line edit process output,

The Sample Input:

Whil# # ilr# e (s # * s)
Outcha @ putchar (* s=# + +);

The Sample Output:

While (* s)
Putchar (* s++);

The following is the code I wrote, don't understand why should submit has been relying on the platform runtime the
And I felt there was something wrong with this question, he said the topic of @, the front line of characters is all gone, but his example of the output of ah, example is' putchar (* s++); "But should not be" putchar (* s++); A: '
 # include 
#include
#include
# define sizezifu 200
Typedef struct {
Char * top;
Char * base;
Int size_zifu;
} zifu;
Void init_zifu (zifu * s)
{
S - & gt; The base=(char *) malloc (sizeof (char) * sizezifu);
if(! S - & gt; Base)
{
The exit (1);
}
S - & gt; Top=s - & gt; The base;
S - & gt; Size_zifu=sizezifu;
}
Void push zifu * s, char (e)
{

* (s - & gt; Top)=e;
S - & gt; Top++;
}
Pop (zifu is void * s, int panduan)
{
If (s - & gt; Top==s - & gt; Base)
{
The exit (1);
}
If (panduan==0)
{
- s - & gt; Top;
}
Else if (panduan==1)
{
S - & gt; Top=s - & gt; The base;
}
}
Int main (void)
{
Char * st_r;
St_r=(char *) malloc (sizeof (char) * 200);
While (the scanf (" % s ", st_r)!=(EOF)
{
Zifu s;
Init_zifu (& amp; S);
Int I=0, panduan_1=0, panduan_2=1, panduan_3=strlen (st_r);
For (I=0; I & lt; The strlen (st_r); I++)
{
The switch (st_r [I])
{

Case '#' :
Pop (& amp; S, panduan_1);
break;
Case: '@'
Pop (& amp; S, panduan_2);
break;
Default:
Push (& amp; S, st_r [I]);
break;
}
}
St_r=s.b ase;
While (st_r!=s.t op)
{
Printf (" % c ", * st_r);
St_r + +;
}
printf("\n");
}
return 0;
}

CodePudding user response:

  • Related