Java This program will be working to build a calculator that can evaluate expres
ID: 3772767 • Letter: J
Question
Java
This program will be working to build a calculator that can evaluate expressions of arbitrary length. We will be using a data structure known as a binary expression tree to build this calculator. Basically the user will enter a string of operators amd operands. We create a binary tree using those operators and operands. It will be having postfix, infix and prefix traversal (which are post oder...I think). Then we will be evaluating the expressions to make sure the expressions can be typed.
Also, you can extend this lab so that the user can enter an expression in prefix notation as well as in postfix notation. The algorithm for prefix notation is different from postfix notation - you start the same way but you move right to left across the string instead of left to right. You push operands onto the stack as before, and when you find an operator, you pop the first two things off the stack and make them the left and right children of the operator's node, then push that back onto the stack. When you are done, there should only be one thing in the stack and it should be the root node of your expression tree.
Here's the link of the requirement of the project: http://web.cse.ohio-state.edu/cse2123/currentsem/projects/Project06.html
I've completed half of this project but it's not working.
Here's mine:
import java.util.IllegalFormatCodePointException;
import java.util.Scanner;
import java.util.Stack;
import javax.swing.plaf.basic.BasicSplitPaneUI.KeyboardDownRightHandler;
import.TreeNode;
public class ExpresssionTree {
public static void main(String[] args){
Scanner keyboard=new Scanner(System.in);
System.out.println("No expression in memory");
System.out.println("Enter your choice: ");
System.out.println("[S]et the display format");
System.out.println("[E]nter a new expression");
System.out.println("[Q]uit");
char answer = (keyboard.nextLine()).charAt(0);
TreeNode<String> node1=new TreeNode<String>(" ");
while (answer!='Q'||answer!='q'){
String expr1="";
if (answer=='E'||answer=='e'){
System.out.println("Enter your expression in postfix notation: ");
String expr=keyboard.nextLine();
TreeNode<String> node=buildTreeFromString(expr);
expr1=expr;
node1=node;
}
else if (answer=='S'||answer=='s'){
System.out.println("Enter your preferred output display: ");
System.out.println("[P]ostfix");
System.out.println("[I]nfix");
System.out.println("p[R]efix");
char answer2=(keyboard.nextLine()).charAt(0);
if (answer2=='P'||answer2=='p'){
String strp= toPostfixString(node1);
}
if (answer2=='I'||answer2=='i'){
String stri=toPostfixString(node1);
}
if (answer2=='R'||answer2=='r'){
String strr=toInfixString(node1);
}
else{
System.out.println("ERROR! You must enter one of [R], [P] or [I]!");
}
}
else{
System.out.println("ERROR! You must enter one of [E], [S] or [Q]!");
}
System.out.println("Enter your choice: ");
System.out.println("[S]et the display format");
System.out.println("[E]nter a new expression");
System.out.println("[Q]uit");
answer = (keyboard.nextLine()).charAt(0);
}
System.out.println("Goodbye!");
}
public static TreeNode<String> buildTreeFromString(String expr){
String[] arr = expr.split("\s+");
Stack<TreeNode<String>> exprStack=new Stack<TreeNode<String>>();
TreeNode<String> node=new TreeNode<String>(" ");
for (int i=0;i<arr.length;i++){
if (Character.isDigit(arr[i].charAt(0))){
int num=arr[i].charAt(0);
node = new TreeNode<String>(Integer.toString(num));
exprStack.push(node);
}
else{
Character operator=arr[i].charAt(0);
node = new TreeNode<String>(Character.toString(operator));
if (exprStack.size()<2){
return null;
}
System.out.print(node.setRightChild(exprStack.pop()));
System.out.print(arr[i]);
System.out.println(node.setLeftChild(exprStack.pop()));
exprStack.push(node);
}
}
if (exprStack.size()>1){
return null;
}
return exprStack.pop();
}
public static String toPostfixString(TreeNode<String> expr){
String str="";
if (expr.getLeftChild()==null&&expr.getRightChild()==null){
return expr.toString();
}
else {
str=toPostfixString(expr.getLeftChild());
str=toPostfixString(expr.getRightChild());
System.out.println(str);
}
return str;
}
public static String toInfixString(TreeNode<String> expr){
String str="";
if (expr.getLeftChild()==null&&expr.getRightChild()==null){
return expr.toString();
}
else {
String strleft=toInfixString(expr.getLeftChild());
String strright="";
str= strleft+strright+expr.toString();
strright=toInfixString(expr.getRightChild());
}
return str;
}
public static String toPrefixString(TreeNode<String> expr){
String str="";
if (expr.getLeftChild()==null&&expr.getRightChild()==null){
expr.toString();
}
else {
String strright="";
String strleft="";
str= strleft+strright+expr.toString();
str=toPrefixString(expr.getLeftChild());
str=toPrefixString(expr.getRightChild());
}
return str;
}
public static int Evaluate(TreeNode<String> expr){
}
// //If the element at the current node is an operator:
// Evaluate the left child
// Evaluate the right child
// Perform the operation given by the element at the current node ( left or right) and return the result
// If the element at the current node is an operand:
// Return the integer value of the element at the current node.
}
Thanks!!!!