• Post Reply Bookmark Topic Watch Topic
  • New Topic

Making a calculator with stack (only adding and multiplication)  RSS feed

 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay guys, here comes the next one :P

I have to make a calculator just for ADDING and MULTIPLICATION, but I have to be aware of the mathematical rules, (dots before lines)... first I have to do multiplications then add...

the format of the numbers will be in in Infix notations: 2+2*2*2*2*2*2+2*2 ...
Any ideas how to be careful around the * and + sign please?
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search for reduce and shift parsing. You may find it called shunting yard algorithm or similar.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaspersky Ukshini wrote:the format of the numbers will be in in Infix notations: 2+2*2*2*2*2*2+2*2 ...
Any ideas how to be careful around the * and + sign please?

As Campbell said, you should look up the Shunting Yard algorithm. However, that's usually used to convert infix to postfix, which you then pass to an RPN calculator (which is basically a glorified stack). However, since in your case you only have two operators to worry about, you could possibly implement it more simply.

(dots before lines)

Never heard that one. There is, however, BODMAS.

HIH

Winston
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Never heard that one.


That's a quote we use to easily remeber that multiplication and division have priority ( * and : ) lol ...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!