In: Computer Science
package mac286.LinkedLists; public class Postfix { private rStack<String> S; private String inSt; private ourLinkedList<String> inList, outList; public Postfix(String s) { S = new rStack<String>(); inSt = s; outList = new ourLinkedList<String>(); inList = new ourLinkedList<String>(); } public void reset(String s) { S = new rStack<String>(); inSt = s; outList = new ourLinkedList<String>(); inList = new ourLinkedList<String>(); } private boolean isOperator(char c) { if(c=='-'||c=='+'||c=='*'||c=='/') return true; return false; } private boolean isParenthesis(char c) { if(c == '(' || c==')') return true; return false; } private int precedence(char c) { if(c=='-'||c=='+') return 1; else return 2; } private void makeList() { //go through the string and extrat operands and operators for(int i = 0; i<inSt.length(); i++) { if(inSt.charAt(i) == ' ') continue; else if (Character.isDigit(inSt.charAt(i))) { String num = ""; while(i<inSt.length() && Character.isDigit(inSt.charAt(i))) { num += inSt.charAt(i); i++; } i--; //add the number to the list inList.add(num); } else if(isOperator(inSt.charAt(i))) { String num = ""+inSt.charAt(i); inList.add(num); }else if(isParenthesis(inSt.charAt(i))) { String num = ""+inSt.charAt(i); inList.add(num); }else { System.out.println("Something is wrong !!"); } } } public void verify() { System.out.println(inList.toString()); } public String convert() { this.makeList(); while(inList.isEmpty() != true) { String token = inList.remove(); if(Character.isDigit(token.charAt(0))){ //this is an operand append it to the out list outList.add(token); }else if(token.charAt(0) == '(') { //push it to the stack S.push(token); }else if(token.charAt(0) == ')') { String temp; while(!(temp = S.pop()).equals("(")) { //append temp to outList outList.add(temp); } }else { //token is an operator while(!S.isEmpty() && this.isOperator(S.peek().charAt(0)) && this.precedence(token.charAt(0)) <= this.precedence(S.peek().charAt(0))) { outList.add(S.pop()); } S.push(token); } } //empty the stack into outputList while(!S.isEmpty()) { outList.add(S.pop()); } String st = ""; for(int i = 0; i < outList.size(); i++) { st += outList.elementAt(i) + " "; } return st; } public void evaluate() { //evaluate the expression in outList. } }
Can you please do the evaluate the expression in outList. thank you. complete the evaluate.
public class Postfix {
private rStack<String> S;
private String inSt;
private ourLinkedList<String> inList, outList;
public Postfix(String s) {
S = new rStack<String>();
inSt = s;
outList = new ourLinkedList<String>();
inList = new ourLinkedList<String>();
}
public void reset(String s) {
S = new rStack<String>();
inSt = s;
outList = new ourLinkedList<String>();
inList = new ourLinkedList<String>();
}
private boolean isOperator(char c) {
if(c=='-'||c=='+'||c=='*'||c=='/')
return true;
return false;
}
private boolean isParenthesis(char c) {
if(c == '(' || c==')')
return true;
return false;
}
private int precedence(char c) {
if(c=='-'||c=='+')
return 1;
else
return 2;
}
private void makeList() {
//go through the string and extrat operands and operators
for(int i = 0; i<inSt.length(); i++) {
if(inSt.charAt(i) == ' ')
continue;
else if (Character.isDigit(inSt.charAt(i))) {
String num = "";
while(i<inSt.length() && Character.isDigit(inSt.charAt(i))) {
num += inSt.charAt(i);
i++;
}
i--;
//add the number to the list
inList.add(num);
} else if(isOperator(inSt.charAt(i))) {
String num = ""+inSt.charAt(i);
inList.add(num);
}else if(isParenthesis(inSt.charAt(i))) {
String num = ""+inSt.charAt(i);
inList.add(num);
}else {
System.out.println("Something is wrong !!");
}
}
}
public void verify() {
System.out.println(inList.toString());
}
public String convert() {
this.makeList();
while(inList.isEmpty() != true) {
String token = inList.remove();
if(Character.isDigit(token.charAt(0))){
//this is an operand append it to the out list
outList.add(token);
}else if(token.charAt(0) == '(') {
//push it to the stack
S.push(token);
}else if(token.charAt(0) == ')') {
String temp;
while(!(temp = S.pop()).equals("(")) {
//append temp to outList
outList.add(temp);
}
}else {
//token is an operator
while(!S.isEmpty() && this.isOperator(S.peek().charAt(0)) && this.precedence(token.charAt(0)) <= this.precedence(S.peek().charAt(0))) {
outList.add(S.pop());
}
S.push(token);
}
}
//empty the stack into outputList
while(!S.isEmpty()) {
outList.add(S.pop());
}
String st = "";
for(int i = 0; i < outList.size(); i++) {
st += outList.elementAt(i) + " ";
}
return st;
}
public void evaluate() {
string token= outlist.remove();
}
}