• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Would someone be so kinds as to....

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could someone take a look at this and perhaps tell me why it isn't working. Node.java and Token.java compile file....it's the Calculator.java that is giving me fits. Thanks in advance, JK.

import java.util.* ;

// class Calculator reads expressions from standard in that may be in postfix,
// prefix, or fully parenthesized infix notation.
// It then creates an expression tree and can create expression strings
// in other notations and/or evaluate the original expression.
public class Calculator
{
private String postfix, infix, prefix ;
private double value ;
private Node myRoot ;
public static void main(String[] args)
{
System.out.println("This program evaluates fully parenthesized and spaced infix expressions, postfix expressions, and prefix expressions") ;
System.out.println("Enter 'quit' to exit.") ;

// open a console reader
TextReader console = new TextReader(System.in) ;
console.eolIsSignificant(true) ;

// process the console input
VectorQueue tokens = new VectorQueue() ;
while(true)
{

char c = console.peek() ;

if(c=='\n')
{
// end of line, process the expression
console.readChar() ;

if(!tokens.isEmpty())
{

// obtain the first element of the queue
Token firstToken = (Token) tokens.elementAt(0) ;

// check for a quit command
if(firstToken.getType() == Token.OPERATOR &&
firstToken.getOp().equals("quit"))
return ;

// create a new calculator object
Calculator calc = new Calculator(tokens) ;

// print the expression tree to console
System.out.println("Tree:") ;
calc.printTree() ;

// print the expression
System.out.println("Postfix:"+calc.getPostfix()) ;
System.out.println("Prefix:"+calc.getPrefix()) ;
System.out.println("Infix:"+calc.getInfix()) ;
System.out.println("Value:"+calc.getValue()) ;
System.out.println() ;
}
}
else if(c=='(' || c==')')
{
// enqueue a PAREN token
tokens.enqueue(new Token()) ;
console.readChar() ;
}
else if( Character.isDigit(c) )
{
// enqueue a number
tokens.enqueue(new Token(console.readDouble())) ;
}
else if(c==' ')
{
console.readChar() ;
}
else
{
// enqueue a symbol
tokens.enqueue(new Token(console.readWord())) ;
}
}
}

public Calculator(VectorQueue tokens)
{
Token firstToken = (Token) tokens.elementAt(0) ;

// convert this queue into a binary tree by checking the first token
if(firstToken.getType() == Token.PAREN)
buildInfix(tokens) ;
else if(firstToken.getType() == Token.OPERATOR)
buildPrefix(tokens) ;
else
buildPostfix(tokens) ;

// initialize object state
postfix = "" ;
prefix = "" ;
infix = "" ;
value = evaluate(myRoot) ;
}

// getter methods - return object state

public String getPrefix()
{
return prefix ;
}

public String getPostfix()
{
return postfix ;
}

public String getInfix()
{
return infix ;
}

public double getValue()
{
return value ;
}

// printTree prints the expression tree to standard out
public void printTree()
{
printTreeHelp(myRoot, "") ;
}

private void printTreeHelp(Node root, String offset)
{
if(root == null)
return ;

printTreeHelp(root.left, offset + " ") ;
System.out.println(offset + root.data) ;
printTreeHelp(root.right, offset + " ") ;

}

// evaluate recursively traverses the expression tree and in
// doing so builds infix, postfix, and prefix versions of the
// expression and returns the value of the expression
private double evaluate(Node root)
{
if(root.left == null && root.right == null)
{
prefix += " " + root.data ;
postfix += " " + root.data ;
infix += " " + root.data ;
return ((Double)root.data).doubleValue() ;
}
else
{
prefix += " " + root.data ;
infix += " (" ;
double arg1 = evaluate(root.left) ;
infix += " " + root.data ;
double arg2 = evaluate(root.right) ;
postfix += " " + root.data ;
infix += " )" ;
return evaluate((String)root.data, arg1, arg2) ;
}
}

// buildPostfix converts a postfix expression to an expression tree
private void buildPostfix(Queue tokens)
{
Stack stk = new VectorStack() ;

while(!tokens.isEmpty())
{
Token tkn = (Token) tokens.dequeue() ;

if(tkn.getType() == Token.OPERAND)
{
// this token is a number, push it
stk.push(new Node(new Double(tkn.getNum()))) ;
}
else
{
// this token is an operator

// pop the last two elements on the stack
Node arg2 = (Node) stk.pop() ;
Node arg1 = (Node) stk.pop() ;

// create a new node, push it
stk.push(new Node(tkn.getOp(), arg1, arg2)) ;

}
}
myRoot = (Node) stk.pop() ;
}

// buildPrefix converts a prefix expression into an expression tree
private void buildPrefix(Queue tokens)
{
myRoot = buildPrefixHelp(tokens) ;
}

private Node buildPrefixHelp(Queue tokens)
{
Token tkn = (Token) tokens.dequeue() ;

if(tkn.getType() == Token.OPERAND)
{
return new Node(new Double(tkn.getNum())) ;
}
else
{
return new Node(tkn.getOp(), buildPrefixHelp(tokens), buildPrefixHelp(tokens)) ;
}
}

// buildInfix converts an infix expression into an expression tree
private void buildInfix(Queue tokens)
{
myRoot = buildInfixHelp(tokens) ;
}

private Node buildInfixHelp(Queue tokens)
{
Token tkn = (Token) tokens.dequeue() ;

if(tkn.getType() == Token.OPERAND)
{
// this token must correspond to a number
return new Node(new Double(tkn.getNum())) ;
}
else
{
// this is a non-leaf node
Node tmp = new Node() ;

// process the left sub expression
tmp.left = buildInfixHelp(tokens) ;

// next token we encounter is an operator
tmp.data = ((Token)tokens.dequeue()).getOp() ;

// process the right sub expression
tmp.right = buildInfixHelp(tokens) ;

tokens.dequeue() ;

return tmp ;
}

}

private static double evaluate(String operator, double operand1, double operand2)
{
if (operator.equals("+"))
return operand1 + operand2 ;
else if (operator.equals("-"))
return operand1 - operand2 ;
else if (operator.equals("*"))
return operand1 * operand2 ;
else if (operator.equals("/"))
return operand1/operand2 ;
else if (operator.equals("^"))
return Math.pow(operand1, operand2) ;
else
throw new RuntimeException("Illegal operator: " + operator) ;
}
}

==================================================
// class node is a simple implementation of a binary tree node
public class Node {

public Node(Object d) {
this(d, null, null) ;
}

public Node() {
this(null, null, null) ;
}

public Node(Object d, Node l, Node r) {
data = d ;
left = l ;
right = r ;
}

public Node right ;
public Node left ;
public Object data ;
}
===========================================================================

class Token {

public Token(String op) {
type = OPERATOR ;
this.op = op ;
}

public Token(double num) {
type = OPERAND ;
this.num = num ;
}

public Token() {
type = PAREN ;
}

public int getType() {
return type ;
}

public double getNum() {
if(type != OPERAND)
throw new RuntimeException("Invalid call to Token::getNum") ;
return num ;
}

public String getOp() {
if(type != OPERATOR)
throw new RuntimeException("Invalid call to Token::getOp") ;
return op ;
}

public final static int OPERATOR = 1 ;
public final static int OPERAND = 2 ;
public final static int PAREN = 3 ;

private int type ;
private double num ;
private String op ;
}
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What error message does the compiler churn out?
 
John Keith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cannot find symbol class VectorQueue
.
.
.
cannot find symbol class TextReader
.
.
.
cannot find symbol class VectorStack
.
.
.
cannot find symbol callas dequeue()
.
.
.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No such thing as a VectorQueue, TextReader or VectorStack in Java 1.5.0. Where did you pick up those class names?
 
John Keith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code is an example I found on the internet (http://www.cs.arizona.edu/classes/cs227/summer03/handouts/calculator.html). I have seen TextReader before; however, never VectorStack, VectorQueue, etc.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You just need to browse around a bit more.
 
John Keith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both for the help. Much appreciated. Have a great week!
 
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic