码迷,mamicode.com
首页 > 其他好文 > 详细

行编辑程序

时间:2016-11-19 18:07:18      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:i++   pac   ring   const   namespace   ++   stack   str   names   

接受用户从终端输入的数据,当用户发现刚刚键入的字符是错误的,‘#’表示前一个字符无效;‘@’表示当前行或之前的数据无效。

#include<iostream>
#include<cstring>
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){
	e=*--S.top;
}
char GetTop(Stack &S){
	return *(--S.top);
}
void ClearStack(Stack &S){
	S.top=S.base;
}
void StackTraverse(Stack &S,Stack &t){
	InitStack(t);
	char e;
	while(S.top>S.base){
        Pop(S,e);
		Push(t,e);
	}
}
void PrintStack(Stack S){
    char a;
	while(S.top>S.base){
        Pop(S,a);
		cout<<a;
	}
	cout<<endl;
}
void LineEdit(){
	char a[MAX_LENGTH];
	Stack S,t;
	InitStack(S);
	char e;
	while(cin>>a){
        int len=strlen(a);
        for(int i=0;i<len;i++){
            if(a[i]==‘#‘)
                Pop(S,e);
            else if(a[i]==‘@‘)
                ClearStack(S);
            else
                Push(S,a[i]);
        }
		StackTraverse(S,t);
		PrintStack(t);
	}
}
int main(){
	LineEdit();
}

  

 

行编辑程序

标签:i++   pac   ring   const   namespace   ++   stack   str   names   

原文地址:http://www.cnblogs.com/-beyond/p/6081097.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!