#Trying to make a calculator with JFrame that can calculate infite numbers with each other.

7 messages ยท Page 1 of 1 (latest)

digital pine
#

So I am trying to make a calculator that can calculate more than just 1-2 / 1-3 numbers with each other.
Maybe someone can help me out. I know how to save the variables but I don't know how I can calculate "possibly not existing" varibales with each other.

cold boltBOT
#

โŒ› This post has been reserved for your question.

Hey @digital pine! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

formal igloo
# digital pine So I am trying to make a calculator that can calculate more than just 1-2 / 1-3 ...
  1. Try refactoring a little bit. I can hardly read what is going on just by the amount of unnecessary code. Instead of declaring, initialising and finally configuring each button separately try to use an array and do all configuration in a for loop.
  2. As far as I can tell you are trying to handle multiple numbers by scanning the entered expression. Then you are trying to evaluate the partial results. This is really difficult to do and requires you to scan the expression multiple times. I would recommend you take a look at postfix notation or prefix notation which is easier to work with in this environment.
digital pine
dark oyster
# digital pine Thanks. Ill take a look at that tomorrow! Gotta go sleep now though

I have made you these simple code optimizations which should be about plug and play into your application. I strongly reccomand you to keep the program simple as in try to implement just the numbers and the basic buttons like +, - and =. You should design these modular enough so your methods can be reused for the other operations :).

As for the point 2 @formal igloo made, I didn't really either understand why you want to handle input by scanning the expression haha.

A better approach would be to have a member holding the current "number" until an operation like "+, -, .." has been pressed. You could than save this "number" and the operation in an array. Than you could do the process all over again. This would result in 2 arrays, one with your expressions and one with your operators.
If you register the "=" being pressed, you could than run a method that figures out the mathematical equation. Since you have an array with all the numbers and one with the operations it shouldnt be too hard to figure out some basic algoritms to return a result. There will be some more complex problems though like checking the basic rules of math of course since "*" and "/" have to be evaluated before "+" and "-". ( Part of the fun haha )
There are plenty of ways to tackle this problem I just thought of this while writing which doesn't mean this is the best way. Hope this gets you started a bit ๐Ÿ™‚

If there is anything else or if you don't know what we mean were glad to help you!

dark oyster