The Artima Developer Community
Sponsored Link

Java Answers Forum
Arithmetic Expression Evaluation

5 replies on 1 page. Most recent reply: Oct 2, 2006 2:06 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Vina V.

Posts: 3
Nickname: vinav
Registered: Sep, 2006

Arithmetic Expression Evaluation Posted: Sep 26, 2006 7:43 PM
Reply to this message Reply
Advertisement
Hi, everyone, this is my first time posting. I was wondering if anyone can point me or help me with this project. It's a school homework! yeah it is...

User into ( 2 * 2 ) + 5
program would evaluate it into 9
it works by multiply 2 * 2 first and add 5
my teacher wants me to use Stack and Queue..I started the project, but i couldn't read in the input.

This is how i approach it. I used array just to store the each String and then print them back out...but no clue!...help me

import java.util.*;
public class arithmetic
{
	public static void main(String args[])
	{
		Scanner input = new Scanner(System.in);
		
		/*
		 *	Sample array and print!
			String [] list1 = new String [2];
			for (int x = 0; x < list1.length; x++)
				System.out.println(list1[x]);*/
		String [] _String = new String [3];
		int [] _Integer = new int [3];
		int x = 0;
		int y = 0;
		
		while (input.hasNext())
		{
			if (input.nextInt())
			{
				_Integer[y] = input.nextInt();
				y++;
			}
			else
			{
				_String[x] = input.next();
				x++;
			}
		}
	}
}


Thank


Vina V.

Posts: 3
Nickname: vinav
Registered: Sep, 2006

Re: Arithmetic Expression Evaluation Posted: Sep 26, 2006 8:51 PM
Reply to this message Reply
the sameple input and output

Sample Input
2 + 2
1 + 2 - 3 + 4
1 + 2 * 3 + 4
1 * ( 2 + 3 ) + ( 4 + 5 ) * ( 6 + 7 )

Sample Output
2 2 +
4
1 2 + 3 - 4 +
4
1 2 3 * + 4 +
11
1 2 3 + * 4 5 + 6 7 + * +
122

Todd Blanchard

Posts: 316
Nickname: tblanchard
Registered: May, 2003

Helping you would be wrong Posted: Sep 28, 2006 11:21 PM
Reply to this message Reply
The point is for you to figure it out. If you can't figure it out, you should find a different profession.

Vina V.

Posts: 3
Nickname: vinav
Registered: Sep, 2006

Re: Helping you would be wrong Posted: Sep 29, 2006 7:18 AM
Reply to this message Reply
well since i didn't get the answer on this forums since i posted until now...I read the book and understood the problem....i got to the point where u take it from a Scanner input and turn it into String and Int, but any pointer on how above on searching for the prarentheses and then do the multiplication and then do the order of operation?

Todd Blanchard

Posts: 316
Nickname: tblanchard
Registered: May, 2003

The classic version uses a stack Posted: Sep 30, 2006 8:30 PM
Reply to this message Reply
so there's a hint

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Helping you would be wrong Posted: Oct 2, 2006 2:06 AM
Reply to this message Reply
Identify an expression in prarentheses (you have to count the open ones until you find the correspojnfding closing one), then call your method recursively with this inner expression. Do this until you don't have any prarentheses left. Now all you have are additions, multiplications, ...
Find the multiplication operators in your String, execute the operation and replace the operation with the result.
Now do the same fir divisions, then for additions and subtractions.
If you called your method recursively, then place the result in the String instead of the operation.

Flat View: This topic has 5 replies on 1 page
Topic: Maven-TestNG using JDk 1.4 Previous Topic   Next Topic Topic: New user to Java.  Need some help with my program

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use