接受用户从终端输入的数据,当用户发现刚刚键入的字符是错误的,‘#’表示前一个字符无效;‘@’表示当前行或之前的数据无效。
#include<iostream>
#include<stdio.h>
using namespace std;
const int MAX_LENGTH=100;
struct Stack{
char *top;
char *base;
int stacksize;
};
void InitStack(Stack &S){
S.base=new char[MAX_LENGTH];
S.top=S.base;
S.stacksize=MAX_LENGTH;
}
void Push(Stack &S,char e){
*(S.top++)=e;
}
void Pop(Stack &S,char &e){
if(S.top==S.base){
return;
}
e=*(--S.top);
}
void ClearStack(Stack &S){
S.top=S.base;
}
void PrintStack(Stack &S){
char *e=S.base;
while(e != S.top){
cout<<*e;
e++;
}
cout<<'
';
}
void LineEdit(){
Stack S,t;
InitStack(S);
char e=getchar();
while(e!=EOF){
while(e!=EOF && e!='
'){
switch(e){
case '#':Pop(S,e); break;
case '@': ClearStack(S);break;
default:Push(S,e);
}
e=getchar();
}
PrintStack(S);
ClearStack(S);
if(e != EOF){
e=getchar();
}
};
}
int main(){
LineEdit();
return 0;
}