If you encounter ' # ', it means to back one box, that is, the previous character is invalid, if you encounter @, indicating that the previous word is invalid, you exit to a space or a costume. Implemented with stacks. Input:whli# #ilr #e (s#*s++) input contains several lines, consisting of a variety of characters. Output:while(*s++) uses the description rule to output the final text content.
Seqstack.h
#ifndef _seqstack_h_#define _ Seqstack_h_#include<iostream> #include <assert.h>using namespace std;typedef char elemtype; #define STACK_ max_size 20typedef struct Stack{elemtype *base;int top;int capacity;} Stack;void initstack (Stack *stack), bool Isfull (stack *stack), bool IsEmpty (stack *stack), BOOL push (Stack *stack, Elemtype x); Elemtype POPs (Stack *stack,elemtype *x); bool Show_stack (stack *stack); Elemtype GetTop (Stack *stack,elemtype *x); void clear (Stack *stack); void destroy (Stack *stack); #endif//_seqstack_h_
Seqstack.cpp
#include "Seqstack.h" void Initstack (Stack *stack) {stack->base = (Elemtype *) malloc (sizeof (elemtype) *stack_max_ SIZE);//stack->base = (Elemtype *) malloc (sizeof (elemtype)); assert (stack->base! = NULL); stack->top = 0;stack- >capacity = stack_max_size;} BOOL Isfull (Stack *stack) {return stack->top = = stack->capacity;} BOOL IsEmpty (Stack *stack) {return stack->top = = 0;} BOOL Push (stack *stack,elemtype x) {if (Isfull (stack)) return false;stack->base[stack->top++] = X;return true;} Elemtype GetTop (Stack *stack,elemtype *x) {if (IsEmpty (stack)) {cout<< "Stack empty!" <<endl;} return stack->base[stack->top-1];} BOOL Show_stack (Stack *stack) {if (IsEmpty (stack)) return false;for (int i = stack->top-1;i>=0;--i) cout<< Stack->base[i]<<endl;return true;} void Clear (Stack *stack) {stack->top = 0;} void Destroy (Stack *stack) {clear (stack); free (stack->base); stack->capacity = 0;} Elemtype pop (Stack *stack,elemtype *e) {if (IsEmpty (stack)) return false;*e = stack->Base[--stack->top];return true;}
Main.cpp
<pre name= "code" class= "CPP" > #include "Seqstack.h" void LineEdit () {Stack st; Stack st2;int Ch;char e;initstack (&st); Initstack (&st2);cout<< "please input:"; ch = getchar (); while (ch! = EOF) {while (ch!=eof && ch!= ' \ n ') {switch (ch) {case ' # ': if (!isempty (&st)) pop (&st,&e); Break;case ' @ ': Clear (&st); Break;default:if (!isfull (&st)) push (&st,ch); break;} ch = GetChar ();} while (st.top>0) {pop (&st, &e); Push (&st2, e);} cout<< "Now:", while (st2.top>0) {pop (&st2,&e); cout<<e;} Cout<<endl;clear (&st); Clear (&st2);cout<< "please input:"; if (ch!=eof) ch = GetChar ();} Destroy (&ST);d Estroy (&ST2);} int main (void) {LineEdit (); return 0;}
Output:
Stack && Line editing program