Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Stack<String> splitter(String exp) {
- String str = exp.replaceAll("\\s", "");
- Stack<String> stack = new Stack<String>();
- Stack<String> ReverseStack = new Stack<String>();
- char[] cArr = str.toCharArray();
- Stack<String> stackOutput = new Stack<String>();
- String helper = "";
- char lastChar = ' ';
- for(int i = 0; i<str.length(); i++) {
- if (str.charAt(i) == ('-') && lastChar == '(') {
- if(!stack.empty()) {
- while(!stack.empty())
- ReverseStack.push(stack.pop());
- while(!ReverseStack.empty())
- helper += ReverseStack.pop();
- stackOutput.push(helper);
- helper = "";
- }
- stack.push(String.valueOf(str.charAt(i)));
- }
- else if(str.charAt(i) == ('(') || str.charAt(i) == ('+') || str.charAt(i) == (')')
- ||str.charAt(i) == ('*') ||str.charAt(i) == ('/') ||str.charAt(i) == ('^') || str.charAt(i) == ('-') && i != 0 ){
- if(!stack.empty()) {
- while(!stack.empty())
- ReverseStack.push(stack.pop());
- while(!ReverseStack.empty())
- helper += ReverseStack.pop();
- stackOutput.push(helper);
- helper = "";
- }
- stack.push(String.valueOf(str.charAt(i)));
- if(!stack.empty()) {
- while(!stack.empty())
- ReverseStack.push(stack.pop());
- while(!ReverseStack.empty())
- helper += ReverseStack.pop();
- stackOutput.push(helper);
- helper = "";
- }
- }
- else
- stack.push(String.valueOf(str.charAt(i)));
- lastChar = str.charAt(i);
- }
- if(!stack.empty()) {
- while(!stack.empty())
- ReverseStack.push(stack.pop());
- while(!ReverseStack.empty())
- helper += ReverseStack.pop();
- stackOutput.push(helper);
- helper = "";
- }
- return stackOutput;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement