Taking brackets into account for calculator input

This is a discussion on Taking brackets into account for calculator input within the C Programming forums, part of the General Programming Boards category; 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 ...

  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,185
    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, 09:41 AM
  2. Recursion Problem I think
    By clearrtc in forum C Programming
    Replies: 4
    Last Post: 07-31-2006, 08:10 PM
  3. Simple Bank Account Uni Project Help!!!
    By griff in forum C Programming
    Replies: 3
    Last Post: 05-09-2003, 06:49 PM
  4. Taking chars into account.
    By kirby1024 in forum C Programming
    Replies: 4
    Last Post: 06-02-2002, 06:29 AM
  5. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-07-2002, 12:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21