[ create a new paste ] login | about

Link: http://codepad.org/AwnFO36M    [ raw code | output | fork ]

C++, pasted on Aug 1:
#include <stack>
#include <string>
#include <iostream>
using namespace std;

stack<char> reverseStack(const stack<char>& s){
   stack<char> res;
   stack<char> src(s);
   while(!src.empty()){
      res.push( src.top() );
      src.pop();
   }
 return res;
}

void printStack(const std::stack<char>& stk){
 stack<char> s(stk);
 while(!s.empty()){
   cout << s.top() << endl;
   s.pop();
 }
}

stack<char> processLine(const stack<char> &stk){
  stack<char> src(reverseStack(stk));
  stack<char> resultStack;
  char prev = -1;  
  while(! src.empty()){
	  char top = src.top();
	  //if no sign of backspace just add it
	  if(top != '-' && top != '<' && prev != '<'){		
		  resultStack.push(top);
	  }
	  //else its either '-' or '<', so do proper checking
	  else{
		  //check if the 2 sequenced character created a backspace? 
		  if(prev == '<' && top == '-'){
			  //backspace detected, so pop top if any
			  if(!resultStack.empty()) resultStack.pop();
		  }
		  prev = top; //update previous		 
	  }
	  src.pop();
  }
  return reverseStack(resultStack);
}

int main(){
  std::string str = "Nn<-<-<-<-No <-w iz<-sthe<-<-<- the time.";
  std::stack<char> stk;
  for(unsigned i = 0; i < str.size(); ++i){
     stk.push(str[i]);
  }
  printStack(processLine(stk));
 return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
N
o
w
 
i
s
 
t
h
e
 
t
i
m
e
.


Create a new paste based on this one


Comments: