# Thread: Create program to evaluate prefix expression

1. Member 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;
}
}```
// 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.

XML Code:
```import java.io.BufferedReader;
import java.io.File;
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
{

int max = 0;
int temp[]=new int, 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];

// To be reused as a counter
max = 0;

for (max=0;max<length;max++)
{

polydata[max] = temp[temp2];

polydata[max] = temp[temp2+1];
System.out.println(polydata[max] + " " +polydata[max]);
temp2++;
temp2++;
}
}

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]>0)
sb.append(polydata[i]+"x^");
else
sb.append(polydata[i]);

//poly_part hard coded	else if( polydata[i][POLY_PART.EXPONENT] == 1 )

// Print without ^ just x
if (polydata[i]>0)
sb.append(polydata[i]+ " ");

}
return sb.toString();
}

public int compareTo(Polynomial omega)
{
int i=0,j=0;
if (this.polydata > omega.polydata)
{
return 1;
}

//Check coefficients
else if (polydata== omega.polydata)
return 0;
else
for(i= 0; i<length;i++)
{
if(polydata[i] > omega.polydata[i])
{
return 1;
}
else if (polydata[i]== omega.polydata[i])
{
for(j=0;j<2;j++)
if(polydata[i] > omega.polydata[i])
return 1;

}
}

return -1;

}
}```
:confused:
Last edited by Debonairj; 08-12-2010 at 02:30 AM.  Reply With Quote

2. Member Join Date
Jul 2010
Posts
19
Rep Power
0

## 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::(  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•