Enter a string of characters in the terminal when you find that the character you just entered is wrong, you can enter # to indicate that the previous character is invalid; Enter @ When you want to clear the line
For example:
' input: hellow#
Output: Hello
Input:hellow@
Output:
#ifndef _edit_h_#define_edit_h_#include <iostream> #include <stdlib.h> #include <malloc.h > using namespace std; #define STACKSIZE typedef char ELEMTYPE;TYPEDEF struct{elemtype stack[stacksize]; int top;} Seqstack;void Initstack (Seqstack *s);//Initialize stack int stackempty (seqstack s);//Determine if the stack is empty int GetTop (Seqstack s, Elemtype *e);//Take stack top element int pushstack (Seqstack *s, elemtype e);//Enter stack int popstack (seqstack *s, Elemtype *e);//Stack int stacklength (seqstack S);//calculate stack length void Clearstack (Seqstack *s);//empty stack void LineEdit ();//Line edit function # endif
#include "Edit.h" void Initstack (Seqstack *s)//Initialize stack S to empty stack {s->top = 0;} int Stackempty (Seqstack S)//To determine if the stack is empty, the stack is empty to return 1, otherwise 0 {if (0 = = s.top) {return 1;} Else{return 0;}} int GetTop (Seqstack S, elemtype *e)//Take the top element of the stack, return the stack top element value to E, and return 1 for success, return 0 for failure {if (s.top <= 0) {cout << "stack is empty! "<<endl;return 0;} Else{*e = s.stack[s.top-1];//Take the top element of return 1;}} int Pushstack (Seqstack *s, elemtype e)//stack operation//Put element e into the stack, element into the stack successfully returned 1, otherwise 0 {if (s->top >= STACKSIZE-1) {cout<< "stack Full, can't enter the stack! "; return 0;} Else{s->stack[s->top] = e; S->top++;return 1;}} int Popstack (Seqstack *s, elemtype *e)//stack operation {if (s->top <= 0) {cout<< "Stack already has no elements and cannot be out of the stack! "<<endl;return 0;} Else{s->top--;*e = S->stack[s->top];return 1;}} int Stacklength (Seqstack S)//return stack length {return s.top;} void Clearstack (Seqstack *s)//empty stack {s->top = 0;} void LineEdit ()//Line edit function {seqstack S;char ch; Elemtype e; Elemtype A[50];int I, j = 0;initstack (&s);cout<< "input character sequence (#表示前一个字符无效, @ means the current line character is invalid)" <<endl;ch = GetChar (); WhiLe (ch! = ' \ n ') {switch (ch) {case ' # ': if (! Stackempty (S)) {Popstack (&s, &ch);//stack top element out stack}break;case ' @ ': Clearstack (&s);//Empty Stack Break;default:pushstack (&s, ch);//Word Fujin}ch = GetChar ();//Read into the next character}while (! Stackempty (S)) {Popstack (&s, &e);//characters are stacked and stored in the array a[j++] = e;} for (i = j-1; I >= 0; i--) {cout<<a[i];//outputs the correct character sequence}cout << Endl; Clearstack (&s);//prepare for the next input}
#include "Edit.h" int main (void) {LineEdit (); return 0;}
"Data structure" stack app row editor