Java Programming Prefix Calculator that uses a graphical use

Java Programming:

Prefix Calculator that uses a graphical user interface as well as binary tree data structure

An arithmetic expression is in prefix form when operators are written before their operands. Here are some examples of prefix expressions and the values they evaluate to:

Expression----|---Value

12                  |    12

+2 51             |    53

* 5 7             |    35

* + 16 4 +3 1 |   80

An expression(such as 12) that begins with an integer is a prefix expression that evaluates to itself. Otherwise, an expression is a prefix expression if it begins with an operator and is followed by two prefix expressions. In this latter case, the value of the expression is recursively computed from the values of its constituent prefix sub-expressions.

Write a program that allows the user to enter prefix expressions in a text field. The program reads the expression, evaluates it, and displays it in a suitable GUI component. Assume that the user enters expressions that use only positive integers and the two operators + and *. The Program should internally convert the prefix expression into a binary tree before evaluating it.

Use a graphical user interface

Use a binary tree data structure

Solution

Here I am giving my code in java that calculate the expression tree of given literals.

// Java program to construct an expression tree

import java.util.Stack;

// Java program for expression tree

class Node {

    char value;

    Node left, right;

    Node(char item) {

        value = item;

        left = right = null;

    }

}

Above code use binary tree as data structure for calculation of data.Tested on my local machine which is working fine.

class ExpressionTree {

    // A utility function to check if \'c\'

    // is an operator

    boolean isOperator(char c) {

        if (c == \'+\' || c == \'-\'

                || c == \'*\' || c == \'/\'

                || c == \'^\') {

            return true;

        }

        return false;

    }

    // Utility function to do inorder traversal

    void inorder(Node t) {

        if (t != null) {

            inorder(t.left);

            System.out.print(t.value + \" \");

            inorder(t.right);

        }

    }

    // Returns root of constructed tree for given

    // postfix expression

    Node constructTree(char postfix[]) {

        Stack<Node> st = new Stack();

        Node t, t1, t2;

        // Traverse through every character of

        // input expression

        for (int i = 0; i < postfix.length; i++) {

            // If operand, simply push into stack

            if (!isOperator(postfix[i])) {

                t = new Node(postfix[i]);

                st.push(t);

            } else // operator

            {

                t = new Node(postfix[i]);

                // Pop two top nodes

                // Store top

                t1 = st.pop();      // Remove top

                t2 = st.pop();

                // make them children

                t.right = t1;

                t.left = t2;

                // System.out.println(t1 + \"\" + t2);

                // Add this subexpression to stack

                st.push(t);

            }

        }

        // only element will be root of expression

        // tree

        t = st.peek();

        st.pop();

        return t;

    }

    public static void main(String args[]) {

        ExpressionTree et = new ExpressionTree();

        String postfix = \"ab+ef*g*-\";

        char[] charArray = postfix.toCharArray();

        Node root = et.constructTree(charArray);

        System.out.println(\"infix expression is\");

        et.inorder(root);

    }

}

// Java program to construct an expression tree

import java.util.Stack;

// Java program for expression tree

class Node {

    char value;

    Node left, right;

    Node(char item) {

        value = item;

        left = right = null;

    }

}

Above code use binary tree as data structure for calculation of data.Tested on my local machine which is working fine.

class ExpressionTree {

    // A utility function to check if \'c\'

    // is an operator

    boolean isOperator(char c) {

        if (c == \'+\' || c == \'-\'

                || c == \'*\' || c == \'/\'

                || c == \'^\') {

            return true;

        }

        return false;

    }

    // Utility function to do inorder traversal

    void inorder(Node t) {

        if (t != null) {

            inorder(t.left);

            System.out.print(t.value + \" \");

            inorder(t.right);

        }

    }

    // Returns root of constructed tree for given

    // postfix expression

    Node constructTree(char postfix[]) {

        Stack<Node> st = new Stack();

        Node t, t1, t2;

        // Traverse through every character of

        // input expression

        for (int i = 0; i < postfix.length; i++) {

            // If operand, simply push into stack

            if (!isOperator(postfix[i])) {

                t = new Node(postfix[i]);

                st.push(t);

            } else // operator

            {

                t = new Node(postfix[i]);

                // Pop two top nodes

                // Store top

                t1 = st.pop();      // Remove top

                t2 = st.pop();

                // make them children

                t.right = t1;

                t.left = t2;

                // System.out.println(t1 + \"\" + t2);

                // Add this subexpression to stack

                st.push(t);

            }

        }

        // only element will be root of expression

        // tree

        t = st.peek();

        st.pop();

        return t;

    }

    public static void main(String args[]) {

        ExpressionTree et = new ExpressionTree();

        String postfix = \"ab+ef*g*-\";

        char[] charArray = postfix.toCharArray();

        Node root = et.constructTree(charArray);

        System.out.println(\"infix expression is\");

        et.inorder(root);

    }

}

Java Programming: Prefix Calculator that uses a graphical user interface as well as binary tree data structure An arithmetic expression is in prefix form when o
Java Programming: Prefix Calculator that uses a graphical user interface as well as binary tree data structure An arithmetic expression is in prefix form when o
Java Programming: Prefix Calculator that uses a graphical user interface as well as binary tree data structure An arithmetic expression is in prefix form when o
Java Programming: Prefix Calculator that uses a graphical user interface as well as binary tree data structure An arithmetic expression is in prefix form when o
Java Programming: Prefix Calculator that uses a graphical user interface as well as binary tree data structure An arithmetic expression is in prefix form when o

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site