# Create program to evaluate prefix expression

• 08-12-2010, 02:25 AM
Debonairj
Create program to evaluate prefix expression
Ok, being totally lost falling to my death on my last piece of code.. I'm coming for assistance in writing this one because I'm clueless and I'm using cide from the last program. Here goes...

This project involves writing a program that will evaluate a prefix expression. The operators can be any of the following (+, - * or /). The operands can be either an unsigned constant or a call to a polynomial function. The user should be prompted first for the prefix expression. The input might look as follows:
+ * 2.5 + P 7.4 6.3 8
The infix equivalent for the above is:
((2.5 * (P(7.4) + 6.3)) + 8)
After the prefix expression is input one polynomial should be input that corresponds to the polynomial (P) in the expression. Format example, 5 3 4 1 8 0 represents the polynomial 5x3 + 4x + 8.
As the prefix expression is read in, a binary tree should be built that represents the expression. Rather than having an abstract method (draw) defined in (Node). Define a method called (evaluate). The operators should become the interior nodes and the operands the exterior nodes. Use a similar strategy used by the (makeNode) method in the (Tree) class to build your binary tree. The Polynomial class that I''m providing should be modified to include a method "evaluate" that returns the value of the polynomial for the supplied value.

// So this is my shell for the this program

Code:

```// Class:  Node // Author:  instructorX // Date:    dd/mm/yyyy // Purpose: An abstract class that defines a binary tree node import java.awt.*; abstract public class Node {   protected static final int SIZE = 10, SPACING = 25; // Class variable   protected static int x; // Abstract method for drawing the proper binary tree   abstract public Point draw(Graphics graphics, int depth); // Class method that must be called before drawing is begun   public static void initDraw()   {       x = SPACING;   } }```
// This is thepolynomial from my first botched program, that I should use for this program. Which I know I had some thing wrong in here.

Code:

```import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.lang.Integer; import java.util.*; public class Polynomial implements Comparable<Polynomial> {          // Used to store the text representation of the polynomial         private int[][] polydata;         private int length;         //private enum POLY_PART { COEFF, EXPONENT };         public Polynomial()         {     }                 // (This input doesn't return anything) public Polynomial input(BufferedReader reader) throws Exception         public void input(BufferedReader reader) throws Exception         {                         //        System.out.println(reader.readLine());                 Scanner snapped = new Scanner(reader.readLine());                 int max = 0;                 int temp[]=new int[100], temp2=0;                 while( snapped.hasNextInt() )                 {                         temp[max]=snapped.nextInt();                         max++;                 }                 length = max/2;                                                 // This will check to see if the pairs or even, if not error will occur?                 if( max % 2 != 0 || max == 0 )                         throw new Exception("Invalid line format!");                 // Calculate the number of terms by dividing the pairs, which there are two // pairs in this case.                 polydata = new int[length][2];                                 // To be reused as a counter                 max = 0;                                                 // bufferedreader reset                                                                   for (max=0;max<length;max++)                 {                         polydata[max][0] = temp[temp2];                         polydata[max][1] = temp[temp2+1];                          System.out.println(polydata[max][0] + " " +polydata[max][1]);                 temp2++;                 temp2++;                 }         //        return new Polynomial(file.readLine());         }                 public String toString()         {                                 StringBuilder sb = new StringBuilder(" ");                 for(int i = 0; i < length; i++)                 {                 // May need to use abs value for negative exponents                                         if(i>0)                           sb.append("+");                   //        poly_part hard coded instead if( polydata[i][POLY_PART.EXPONENT] > 1 )                         if(polydata[i][1]>0)                                 sb.append(polydata[i][0]+"x^");                         else                                 sb.append(polydata[i][0]);                                 //poly_part hard coded        else if( polydata[i][POLY_PART.EXPONENT] == 1 )                                                         // Print without ^ just x                         if (polydata[i][1]>0)                                 sb.append(polydata[i][1]+ " ");                                                                                   }                 return sb.toString();         }         public int compareTo(Polynomial omega)         {                 int i=0,j=0;                 if (this.polydata[0][1] > omega.polydata[0][1])                 {                 return 1;                 }                 //Check coefficients                 else if (polydata== omega.polydata)                         return 0;                 else                 for(i= 0; i<length;i++)                 {                         if(polydata[i][1] > omega.polydata[i][1])                         {                                 return 1;                         }                         else if (polydata[i][1]== omega.polydata[i][1])                         {                                 for(j=0;j<2;j++)                                 if(polydata[i][0] > omega.polydata[i][0])                                         return 1;                                                         }                 }                                         return -1;                                 } }```
:confused:
• 08-12-2010, 02:35 AM
Debonairj
Prefix Expression: Additional thought
So as I look at this more and more, I think I'm suppose to have sevral different classes? RIght!

Abstract Node class which I provided
Interior class
Extrior class
tree Class
TreeApplet
and maybe a few others:confused::(