473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 2465
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.