Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 16th, 2006, 2:36 AM   #1
secrecy230
Newbie
 
Join Date: May 2005
Posts: 11
Rep Power: 0 secrecy230 is on a distinguished road
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
secrecy230 is offline   Reply With Quote
Old Apr 16th, 2006, 2:46 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
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).
grumpy is offline   Reply With Quote
Old Apr 16th, 2006, 8:05 AM   #3
a3orange
Newbie
 
Join Date: Apr 2006
Posts: 14
Rep Power: 0 a3orange is on a distinguished road
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)
a3orange is offline   Reply With Quote
Old Apr 17th, 2006, 12:06 AM   #4
PPoA
Programmer
 
PPoA's Avatar
 
Join Date: Mar 2006
Location: Tennessee
Posts: 41
Rep Power: 0 PPoA is on a distinguished road
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.
PPoA is offline   Reply With Quote
Old Apr 17th, 2006, 12:09 AM   #5
Nickyj1211
Newbie
 
Join Date: Apr 2006
Posts: 15
Rep Power: 0 Nickyj1211 is on a distinguished road
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
Nickyj1211 is offline   Reply With Quote
Old Apr 17th, 2006, 10:18 AM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Quote:
Originally Posted by a3orange
10 - 2 * 4 = 8
You sure about that?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 17th, 2006, 1:12 PM   #7
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 319
Rep Power: 4 andro is on a distinguished road
Send a message via AIM to andro
Maybe you should take a look at converting from infix to postfix expressions.
andro is offline   Reply With Quote
Old Apr 17th, 2006, 3:54 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
Originally Posted by Ooble
Quote:
Originally Posted by a3orange
10 - 2 * 4 = 8
You sure about that?
He's using oranges, not apples, they're different.
__________________
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
DaWei is offline   Reply With Quote
Old Apr 17th, 2006, 4:36 PM   #9
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Quote:
Originally Posted by DaWei
He's using oranges, not apples, they're different.
Ah... that makes sense. I'm more a fan of lemons myself, but hey, whatever floats yer boat.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 18th, 2006, 1:20 AM   #10
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:26 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC