mplekunov / CalculatorApp

GNU General Public License v3.0
1 stars 0 forks source link

Postive and Negative numbers #7

Closed mplekunov closed 2 years ago

mplekunov commented 2 years ago

Currently there is no logic to distinguish between positive and negative numbers... "Evaluator" algorithm can calculate expression with negative numbers (needed logic for that exists), however, user can't "edit" negative numbers as they will be spitted runtime exception at them when there will be only "negative" sign left.

mplekunov commented 2 years ago

All logic regarding expressions happen in Expression... therefore, solution needs to be implemented there.

Since I don't have "(" and ")" implemented in my calculator (even though Evaluator can calculate result of an expression with brackets, user can't enter them yet), there is no need to think about what to do if user wants to add negative number.

So, the only thing that needs to be done right now is correct handling of cases when our Token has value of "-". This kind of thing can only happen when user got a negative result out of their expression, then pressed save button (equal button) and was editing (removing digits of a result) until there is only one thing left, which is "-".

mplekunov commented 2 years ago

So... there are several cases we can have:

  1. We have "-" in an expression of format "-" + "123"
  2. We have "-" in an expression of format "123" + "-" + "123"
  3. We have "-" in an expression of format "123" + "-"
  4. We have "-" in an expression of format "-"

Cases 1 and 2 are special cases... in those cases we can't simply "remove" "-" because then we will have an expression of format: +123 or 123 + + 123 both of which are illegal.

So... what can be done... The easiest way to solve it is to use the same logic I'm using right now for cases when "" is not allowed... That is to check if the only token left is a "-", if it is a "-" then we just remove it and place "0"

Cases 3 and 4 are easier to fix because we can just remove token (that is "-") altogether.

mplekunov commented 2 years ago

This has to be fixed...

mplekunov commented 2 years ago

So... I think there is no reason in implementing "negative" button specifically... I think I can use operator button for that... That being said, it leads us to two cases.

  1. We want to input negative number and that's the first number in the expression
  2. We want to input negative number and it's somewhere else

For the second case we can check previous token... if previous token is a number, we allow usage of "minus" operator and transform that operator to negative sign.

For the first case.... I think we can just allow entering of minus operator and transform it directly to negative sign

mplekunov commented 2 years ago

TODO:

Divide addOperator/addNumber/addFunction on several functions in a way that I would be able to extend functionality without editing current "base" logic for those functions...

Ex: The base logic for numbers should not have additional logic for numbers with exponent, or negative numbers or any other kind of numbers. The base logic for functions should not have all functions implemented inside of it...

I have to do this for the whole Expression class. Doing that will make adding new features easier and I will finally be able to implement negative numbers and exponents

mplekunov commented 2 years ago

TODO:

(In Progress)

Divide addOperator/addNumber/addFunction on several functions in a way that I would be able to extend functionality without editing current "base" logic for those functions...

(Should be easy to do right now)

Ex: The base logic for numbers should not have additional logic for numbers with exponent, or negative numbers or any other kind of numbers. The base logic for functions should not have all functions implemented inside of it...

(In Progress)

I have to do this for the whole Expression class. Doing that will make adding new features easier and I will finally be able to implement negative numbers and exponents

Ended up rewriting Token class and a bit of other logic... I could have implemented negative numbers support without all this rewriting but it would ended up being a shit code...

mplekunov commented 2 years ago

In Evaluator class... I need to, somehow, move all bunch of things into different functions. Like percentages should be calculated in their own function, exponents in their own, negative numbers in their own and etc... Right now I just threw bunch of things into the same place just to see if my "overall" logic works or not..

mplekunov commented 2 years ago

Also removed usage of Token.value from everywhere... Right now, Token usage/manipulation doesn't depend on the type of "value"... In other words... Value can be String, List or any other Type/Collection... The only thing should change is the interface implementation in the Token class... and that's it.

mplekunov commented 2 years ago

There will be day when I will start writing "good" code from the get go... but it's clearly not today 😞

mplekunov commented 2 years ago

Delete, deleteall and all other stuff is fixed... Decoupled Expression evaluator on two classes... ExpressionEvaluator now has PostfixEvaluator through composition. Need to fix ".0" and percentage formula and then I will be able to tackle concurrency...

mplekunov commented 2 years ago

Everything is fixed except "%"