![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2005
Posts: 11
Rep Power: 0
![]() |
function for precedence
Iam building a calculator in c++.I have implement a recursive function to do + - /
and *.But the calculator must be be based on level of precedence of operator.Can anyone know how to make a function to recognize precedence?? (the function must not using stack,or infix to postfix) but may be recursive or iterative?? Hope u can help |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Multiplication and division have the same precedence as each other. Addition has the same precedence as subtraction, The precedence of multiplication is greater than that of addition. That's why (mathematically) 10-2*4 equates to 2 rather than 32 (which would be the result if subtraction had higher precedence than multiplication).
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Apr 2006
Posts: 14
Rep Power: 0
![]() |
Just to add on to grumpy. If you wanted to manipulate the order of operations, you could add parenthases. So..
10 - 2 * 4 = 8 but.. (10 - 2) * 4 = 32 Stuff inside the parenthases is evaluated before anything else. (there are a few exceptions in normal math) |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Mar 2006
Location: Tennessee
Posts: 41
Rep Power: 0
![]() |
I can suggest, since that would obviously be one line of input (10-2*4), to have an if>then statement that looks first for an * or / symbol, then evaluate that, then after that to look for a + or - symbol and evaluate for that.
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Apr 2006
Posts: 15
Rep Power: 0
![]() |
we used to have to take tests with huge equations/functions and have to break them down. Honestly, its easiest to use parentheses all the time. Even if you dont have to, its just reassuring to know you have to presenence right
|
|
|
|
|
|
#6 | |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Quote:
![]() |
|
|
|
|
|
|
#7 |
|
Professional Programmer
|
Maybe you should take a look at converting from infix to postfix expressions.
|
|
|
|
|
|
#8 | ||
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
||
|
|
|
|
|
#9 | |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Quote:
|
|
|
|
|
|
|
#10 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
c'mon guys, everybody knows that 8 = 2, duh.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|