472,986 Members | 2,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 software developers and data experts.

java calculator using nested expressions

4
Im fairly new to java programming although i have some experience in python.

I have to create a calculator program (command line) which takes as input a mathematical expression consisting of decimal numbers and the four basic mathematical operators (+,-,/,*) and correctly handles nested mathematical expressions.

Some examples:
• 5 + 6 * 2 results in 17
• (5 + 4) * (4 - 2) results in 18

i can do the basics ie taking an input and getting answers from simple maths expressions. but i am having trouble coming up with a way to sort out the nested expressions, my current thinking is to either put the input string into an array list and cycle through putting the expressions in a stack based on finding the characters "(" and ")" or possibly splitting the string first based on the same criteria then putting them into different collections but im unsure on whether this will work and how to sort out the ordering

if anyone can throw some light on how this might be implemented i would be very grateful

Thanks

Jon
Jan 31 '08 #1
2 2445
r035198x
13,262 8TB
Im fairly new to java programming although i have some experience in python.

I have to create a calculator program (command line) which takes as input a mathematical expression consisting of decimal numbers and the four basic mathematical operators (+,-,/,*) and correctly handles nested mathematical expressions.

Some examples:
• 5 + 6 * 2 results in 17
• (5 + 4) * (4 - 2) results in 18

i can do the basics ie taking an input and getting answers from simple maths expressions. but i am having trouble coming up with a way to sort out the nested expressions, my current thinking is to either put the input string into an array list and cycle through putting the expressions in a stack based on finding the characters "(" and ")" or possibly splitting the string first based on the same criteria then putting them into different collections but im unsure on whether this will work and how to sort out the ordering

if anyone can throw some light on how this might be implemented i would be very grateful

Thanks

Jon
Have a read at Jos' compilers series.
Jan 31 '08 #2
jtanz0
4
thanks!

I've been researching think im going to go for the approch of changing the input from infix to post fix as i have already done a RPN calculator in python and i understand how this works
Feb 3 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
3
by: Thoro | last post by:
PractiCalc - A brand new CALCULATOR for your PC. More powerful than your usual calculator - more user-friendly than a spreadsheet. -> PractiCalc provides both conventional and unique calculator...
11
by: rajarao | last post by:
hi I want to remove the content embedded in <script> and </script> tags submitted via text box. My java script should remove the content embedded between <script> and </script> tag. my current...
6
by: Rafael | last post by:
Hi Everyone, I need some help with my calculator program. I need my program to do 2 arguments and a 3rd, but the 3rd with different operators. Any help would be great. Here is my code.... ...
3
by: PieMan2004 | last post by:
Hi, ive been looking for a solid java community to help me when im tearing out my hair :) Basically ive constructed a GUI that has to represent the same look and functions of the typical windows...
2
by: phjones | last post by:
Need help programming mortagage calculator for 3 different loans 7 year, 15 year and 30 year. using java array I am a beginner with Java, This is what I have so far. Need to know if I am off the...
5
by: erekose666 | last post by:
I have this program right now, but I need to modify it to output to the JTextPane. What I mean is, when someone clicks on the 1 button, it needs to output to the TextArea a 1. None of the add,...
3
by: itsmichelle | last post by:
This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However,...
1
by: Techno3000 | last post by:
import java.io.*; import java.awt.*; import hsa.Console; public class Calculator { static Console c; public static void main (String args) throws IOException
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.