d mib

Greenhorn
+ Follow
since Nov 12, 2003
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by d mib

and this is another attampt i did but i really need help
public class a{
public static int evaluate(String postfix)
{
int result = 0;
for(int i=0;i<postfix.length();i++){
String ch = postfix.substring(i,i+1);
//if ch is a constant push it onto the stack
if(!ch.equals("+")&&!ch.equals("-")&&!ch.equals("*")&&!ch.equals("/"))
stack.push(ch);
else{
if(ch.equals("+")){

try{
int op1 = Integer.parseInt((String)stack.pop());
int op2 = Integer.parseInt((String)stack.pop());
result = op1 + op2;
System.out.println(op1+"+"+op2+"="+result);
stack.push(new Integer(result).toString());
}//try
catch(StackUnderflowException ex){
System.out.println(ex);
}//catch
}
if(ch.equals("-")){
try{
int op1 = Integer.parseInt((String)stack.pop());
int op2 = Integer.parseInt((String)stack.pop());
result = op2 - op1;
System.out.println(op2+"-"+op1+"="+result);
stack.push(new Integer(result).toString());
}//try
catch(StackUnderflowException ex){
System.out.println(ex);
}//catch
}
if(ch.equals("*")){
try{
int op1 = Integer.parseInt((String)stack.pop());
int op2 = Integer.parseInt((String)stack.pop());
result = op1 * op2;
System.out.println(op1+"*"+op2+"="+result);
stack.push(new Integer(result).toString());
}//try
catch(StackUnderflowException ex){
System.out.println(ex);
}//catch
}

if(ch.equals("/")){
try{
int op1 = Integer.parseInt((String)stack.pop());
int op2 = Integer.parseInt((String)stack.pop());
result = op2 /op1;
System.out.println(op2+"/"+op1+"="+result);
stack.push(new Integer(result).toString());
}//try
catch(StackUnderflowException ex){
System.out.println(ex);
}//catch
}
}//else
}//for
try{
result = Integer.parseInt((String)stack.pop());
}
catch(StackUnderflowException ex){
System.out.println(ex);
}
return result;
}//evaluate();
}//Expressions
20 years ago
this is what i got done so far but need help desperatly
public class postfix{
private String postfixString, outputString;
private boolean isOperator(char c){
return (c=='+'||c=='-'||c=='*'||c=='/');
}
private boolean is Space(char c){
return(c=='');
}
public void interpretPostfix(){
Stack evalStack = new Stack();
double leftOperand, rightOperand;
char c;
StringTokenizer parser = new String Tokenizer(postfixString,"+-*/", true);
while(parser.hasMoreTokens()){
String token = parser.nextToken();
c = token.charAt(0);
if ((token.lenth()==1)&&isOperator(c){
rightOperand = ((Double) evalStack.pop()).doubleValue();
leftOperand = ((Double) evalStack.pop()).doubleValue();
switch (c){
case '+':evalStack.push(newDouble (leftOperand+rightOperand));
break;
case '-':evalStack.push(newDouble (leftOperand-rightOperand));
break;
case '*':evalStack.push(newDouble (leftOperand*rightOperand));
break;
case '/':evalStack.push(newDouble (leftOperand/rightOperand));
break;
default:
break;
}
}else if ((token.length()==1)&&isSpace(c)){
;
}else{
evalStack.push(Double.valueOf(token));
}
}
output("Value of postfix expression =" + evalStack.pop());
}
private void output(String s){
outputString = s;
}
public void setInput(String input){
postfixString = input;
}
public String getOutput(){
return outputString;
}

------------------------------------------------------------------------------
//stack code
public class Stack{
private int count;
private int capacity;
private int capacityIncrement;
private Object[]itemArray;
public Stack(){
count =0;
capacity = 10;
capacityIncrement = 5;
itemArray = new Object[capacity];
}
public boolean empty(){
return (count ==0);
}
public void push(Object X){
if (count==capacity){
capacity+=capacityIncrement;
Object[]tempArray = new Object[capacity];
for(int i = 0; i<count; i++){
tempArray[i] = itemArray[i];
}
itemArray = tempArray;
}
itemArray[count++] = X;
}
public Object pop(){
if(count ==0){
return null;
}else{
return itemArray[--count];
20 years ago
i need helpwith setting this up
user types in a calculation i.e. 2+2. the program sends this to a stack and then converts it to a postfix i.e. 22+. it then displays the postix and results to the user.
how can i do this???
20 years ago