indexing - Shunting-Yard Algorithm Implementation in Java giving incorrect output and String index out of range? -


i implementing shunting-yard algorithm , evaluating results reference-based implementation using nodes (one operators , 1 operands) , stacks (one operators , 1 operands).


the input file contains (just first few lines):

5 5-3 5*(3+4) 7*3+5*6 2+5*(7+8)/3 

the output:

53 = 53 53513 = 1 535152 exception in thread "main" java.lang.stringindexoutofboundsexception:  string index out of range: 7     @ java.lang.string.charat(string.java:646)     @ labiii.main(labiii.java:48)  process completed. 

main:

public class labiii { public static string expr; public static string line;  public static void main(string[] args) throws ioexception {            try     {         bufferedreader input = new bufferedreader(new filereader("input.txt")); // create input reader          char token;         int tokeni;         char popop;         int popint1;         int popint2;         int result;          while ((line = input.readline()) != null) // while input file still has line characters         {             operatorstack opstack = new operatorstack(); // initalize operator stack             opstack.push(';');             operandstack intstack = new operandstack();             expr = line;             int count = 1;              while(count <= expr.length())             {                 int index = count - 1;                  if(character.isdigit(expr.charat(index))) // if token operand                 {                     tokeni = expr.charat(index);                     system.out.print(tokeni);                     intstack.push(tokeni);                     count++;                 }                 else                 {                     token = expr.charat(count);                      if(token == ')')                     {                            while(opstack.peek() != '(')                         {                             popop = opstack.pop();                             system.out.print(popop);                             popint1 = intstack.pop();                             popint2 = intstack.pop();                             result = evaluate(popint1, popint2, popop);                             intstack.push(result);                                                   }                         opstack.pop(); // pop "(" , discard                         count++;                     }                     else                     {                         while(inputpriority(token) <= stackpriority(opstack.peek()))                         {                             popop = opstack.pop();                             system.out.print(popop);                             popint1 = intstack.pop();                             popint2 = intstack.pop();                             result = evaluate(popint1, popint2, popop);                             intstack.push(result);                         }                         opstack.push(token);                         count++;                     }                 }             }              while (opstack.peek() != ';')             {                 popop = opstack.pop();                 system.out.print(popop);                 popint1 = intstack.pop();                 popint2 = intstack.pop();                 result = evaluate(popint1, popint2, popop);                 intstack.push(result);             }              system.out.print(" = " + intstack.pop());             system.out.println();             count = 0;                       }     }      catch (ioexception ex)     {         system.err.println("exception:" + ex);     } } } 

operandstack (also 1 operatorstack. same, uses char instead of int):

public class operandstack {     int integ;     nodeint top;     nodeint temp;      public operandstack() // default constructor: empty stack     {         top = null;     }      public boolean isempty() // returns true if top of stack null     {         return top == null;     }      public void push(int integ) // push item onto top of stack     {         top = new nodeint(integ, top);     }      public int pop()     {         nodeint temp = top;         top = top.getnext();         return temp.getitem();     }      public void popall()     {         top = null;     }      public int peek()     {         return top.getitem();     }        } 

node (also 1 operands/integers):

public class node{  private char item; private node next;  public node(char newitem) {     item = newitem;     next = null; }  public node(char newitem, node nextnode) {     item = newitem;     next = nextnode; }  public char getitem() {     return item; }  public void setnext(node nextnode) {     next = nextnode; }  public node getnext() {     return next; } } 

the algorithm follows:

initialize operator stack contain ‘;’ (bottom of stack operator)

get first token

while end of expression not reached

if token operand then

print token

push operand onto operand stack

else if token ‘)’ then

while top of operator stack not equal ‘(’

pop operator stack

print operator

pop operand stack twice

apply designated operation 2 operands

push operation result onto operand stack

end while

pop ‘(’ , discard it

else

while inputpriority(token) ≤ stackpriority(top of operator stack)

pop operator stack

print operator

pop operand stack twice

apply designated operation 2 operands

push operation result onto operand stack

end while

push token onto operator stack

get next token

end while

while top of operator stack not equal ‘;’

pop operator stack

print operator

pop operand stack twice

apply designated operation 2 operands

push operation result onto operand stack

end while

pop operand stack , print result

any appreciated.

token = expr.charat(count); 

this should be

token = expr.charat(index); 

i don't know why you're bothering maintain both index , count. leads trouble this.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -