# Homework help, don't understand teach's hint, need some direction

• December 8th, 2010, 01:28 PM
crazed8s
Homework help, don't understand teach's hint, need some direction
this is the assignment:

10. Recursive Equation Evaluator
Write a method that will evaluate an algebraic equation that is stored in a string and return its value. The string should contain the equation represented in text. The equation may contain floating point numeric values, the four basic algebraic operations ( +, -, * and / ), and sub expressions surrounded by parentheses. Evaluation should be performed using the standard rules of precedence:

1. Aub expressions in parentheses are evaluated first from inner most to outermost.

2. Within a sub expression or the expression, multiplication and division are evaluated next from left to right.

3. Addition and subtraction are evaluated next from left to right.

The following rules will help in guiding your programming:

Expression ==> Term { '+' | '-' Term }

Term ==> Factor { '*' | '/' Factor }

Factor ==> Number | '(' Expression ')'

In English, this would be read as:

An Expression consists of a Term followed by zero or more sequences consisting of a plus or minus operator followed by another Term.

A Term consists of a Factor followed by zero or more sequences consisting of a multiply or divide operator followed by another Factor.

A Factor consists of a number or a sub-expression which consists of a left parenthesis followed by an Expression (see the definition above) and finally followed by a right parenthesis. The fact that a Factor can be composed of another Expression enclosed in parentheses is where the recursion comes in.

You should also write a main program method that can be used to test your equation calculator. The main method should display a window with a text field for entering the equation, a calculate button, and a text field for displaying the answer. All calculations should be performed using either float or double values and arithmetic.

/end assignment

i've coded a fully functioning calculator with just straight iterative process and with shunting yard algorithm but i'm totally stumped by this. I don't have any code because its like gibberish to me. I need i guess 4 methods (main, expression, factor, and term) and i think all the math is done in the factor part. I just don't get it. ill post whatever i get done as i work through it but any help is appreciated.

I'm starting out with this equation on paper to try and figure this out: 4+4/(4-3)
this sucks...i don't know what to call first? its just a number and a plus for gods sake!

thanks
-crazy
• December 8th, 2010, 02:38 PM
javapenguin
Re: Homework help, don't understand teach's hint, need some direction
Though it may be slightly different, this other program of mine, though it didn't work fully on the toStirng(), might help.

http://www.javaprogrammingforums.com...essions-2.html
• December 8th, 2010, 03:56 PM
crazed8s
Re: Homework help, don't understand teach's hint, need some direction
Thank you for the response but i managed to get through it. atleast i think it works. It wasn't too too hard. i didn't use my teachers hints but is im pretty sure recursive so he should accept it.

heres the code if anyone wants to look at it, its pretty rough with no comments and i didn't make too great an effort to compact it so there may be a lot of redundancy:

Code java:

```import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class RecursionCalc extends JFrame { //Swing components// JPanel Panel; JTextField Equation; JButton Calculate; JTextField Output; //--------------// //String and string track// int get = 0; String Eq; //-------------// RecursionCalc(){ //GUI// Panel = new JPanel(); Equation = new JTextField(10); Output = new JTextField(10); Calculate = new JButton("Calculate"); Calculate.addActionListener(new CalculateListener()); Panel.add(Equation); Panel.add(Calculate); Panel.add(Output); add(Panel); pack(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //--------------// } //First runEq, this one does the math// public double runEq(double first, String op){ double temp;//Stores temporary doubles, for passing back String a = getNext();//Either a number or a ( if(a.equals("(")){ temp = runEq();//Calls the empty one because the parenthesis starts an entire sub equation } else{ temp = Double.parseDouble(a);//Otherwise gets its double value from a } if(get >= Eq.length()){//If its at the end then it has to do math because no more Operations System.out.println("P3"); System.out.println("First = " + first); System.out.println("Temp = " + temp); //Do math if(op.equals("+")){ return first + temp; } else if(op.equals("-")){ return first - temp; } else if(op.equals("*")){ return first * temp; } else{ return first / temp; } } else{//Not at the end String b = getNext(); if(b.equals(")")){//If it sees an end parenthesis its like the end of an equation except just a sub equation System.out.println("P4"); System.out.println("First = " + first); System.out.println("Temp = " + temp); //Do math if(op.equals("+")){ return first + temp; } else if(op.equals("-")){ return first - temp; } else if(op.equals("*")){ return first * temp; } else{ return first / temp; } } else if(getPower(op) > getPower(b) || getPower(op) == getPower(b)){//if the first op is greater or equal does math of last number and first number from here System.out.println("P5"); System.out.println("First = " + first); System.out.println("Temp = " + temp); System.out.println("b = " + b); //Do math if(op.equals("+")){ return runEq(first + temp, b); } else if(op.equals("-")){ return runEq(first - temp, b); } else if(op.equals("*")){ return runEq(first * temp, b); } else{ return runEq(first / temp, b); } } else if(getPower(op) < getPower(b)){//If last op is less, it does math of last number and op with the next set System.out.println("P6"); System.out.println("First = " + first); System.out.println("temp = " + temp); System.out.println("b = " + b); //Do math if(op.equals("+")){ return first + runEq(temp,b); } else if(op.equals("-")){ return first - runEq(temp,b); } else if(op.equals("*")){ return first * runEq(temp,b); } else{ return first / runEq(temp,b); } } } return 0; } public Double runEq(){//Only called at beginning or after parenthesis so it can't do any math System.out.println("NEW STEP"); double temp; String a = getNext(); if(a.equals('(')){//Start new subequation System.out.println("P1"); temp = runEq(); } else{//Temp gets turned straight to double temp = Double.parseDouble(a); } String b = getNext(); System.out.println("P2"); System.out.println("Temp = " + temp); System.out.println("b = " + b); return runEq(temp, b);//Continue equation } public int getPower(String a){ if(a.equals("+") || a.equals("-")){//Just the power levels associated with different math operations return 1; } else{ return 2; } } public String getNext(){ String temp = ""; //Runs through string until not a number anymore if(get<Eq.length()&&(Eq.charAt(get) < '0' || Eq.charAt(get) > '9') && Eq.charAt(get) != '.'){ temp = temp + Eq.charAt(get); get++; } //The ops I'm working with are only 1 character long so this method works. Not particularly portable else while(get<Eq.length()&&((Eq.charAt(get) >= '0' && Eq.charAt(get) <= '9')||Eq.charAt(get) == '.')){ temp = temp + Eq.charAt(get); get++; } return temp; } public class CalculateListener implements ActionListener{ public void actionPerformed(ActionEvent arg0) { Eq = Equation.getText();//Get Equation Output.setText(Double.toString(runEq()));//Messy function, starts equation, turns the final returned value to string, then outputs it get = 0;//Reset } } public static void main(String[] args) { new RecursionCalc();//Start } }```

edit: now it uses doubles