Thread: Taking brackets into account for calculator input

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    15

    Taking brackets into account for calculator input

    Hi

    I'm doing question 2 from http://olympiad.org.uk/papers/2006/bio/bio06-exam.pdf (Out of personal interest)

    How can I write a program that can handle the brackets? I suppose a more general view would be:

    a+b*c/d

    I can easily write a program that does ((a+b)*c)/d that goes from left to right.

    However, how could could I do a+((b*c)/d) that takes into account operator presedence (annoyingly my £5 calculator can do this, although I can't).

    If I wrote in brackets e.g.

    (a+b)*(c+d)

    how could I write a program that knows which operations to do first?

    Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You could take a look at the shunting yard algorithm, which really allows you to build a RPN/postfix string out of the expression, but should be adaptable to say building an expression tree or maybe even evaluating the expression.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bank Account Problem
    By JayBlay77 in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2009, 08:41 AM
  2. Recursion Problem I think
    By clearrtc in forum C Programming
    Replies: 4
    Last Post: 07-31-2006, 07:10 PM
  3. Simple Bank Account Uni Project Help!!!
    By griff in forum C Programming
    Replies: 3
    Last Post: 05-09-2003, 05:49 PM
  4. Taking chars into account.
    By kirby1024 in forum C Programming
    Replies: 4
    Last Post: 06-02-2002, 05:29 AM
  5. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM