Results 1 to 2 of 2
 08122010, 03:25 AM #1Member
 Join Date
 Jul 2010
 Posts
 19
 Rep Power
 0
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
XML 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; } }
XML 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; } }
Last edited by Debonairj; 08122010 at 03:30 AM.
 08122010, 03:35 AM #2Member
 Join Date
 Jul 2010
 Posts
 19
 Rep Power
 0
Similar Threads

Evaluate the environmental flexibility of programming in Java?
By azzic7 in forum New To JavaReplies: 2Last Post: 06262010, 10:18 PM 
Evaluate a string expression
By aparna_pall in forum New To JavaReplies: 2Last Post: 04102010, 04:53 AM 
need help to create a GUI for this program
By placidphyll in forum AWT / SwingReplies: 1Last Post: 11172009, 05:53 PM 
Help! URGENT.. i need to be able to evaluate PEMDAS expressions using stacks..
By a_aagain in forum New To JavaReplies: 4Last Post: 04012009, 01:25 PM 
Help with Prefix cnt
By trill in forum New To JavaReplies: 1Last Post: 08072007, 08:26 AM
Bookmarks