Thread: Identifying specific characters in a string

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    Oregon, USA
    Posts
    9

    Identifying specific characters in a string

    Hi, I'm working on writing my own calculator, just a simple basic programming exercise to help develop my skills. I've already figured out one way to do it but it's a really awkward way to have the user input the function they want calculated. What I really want is for someone to be able to input say "1 + 2" and have it output "3" or input "1 + 3 + 1" and output "5". The thing I'm having trouble figuring out is how exactly to identify and pull out the operators used, and then how to identify and pull out the integers entered so they can be passed to a function. Any help on this first step would be fantastic. Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The general notion would be the shunting yard algorithm. That may well be "ultra fancy" at this point in time, though. To start you may want to decide between (a) $1 calculator left-to-right (b) APL right-to-left (c) PEMDAS in terms of order-of-operation, as that will influence what you want to do.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    This is commonly done by converting the expression from infix notation ("1 + 2"), to postfix notation ("1 2 +"). You can read up on postfix notation here, and you should be able to see how it helps you out when evaluating expressions -- especially when it comes to handling different operator precedence.
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You should break this up into parts: parsing the string into a usable structure, and then evaluating that structure. In, there are several functions to help you parse strings like strtok, etc... but you may probably want your own functions to evaluate this char-by-char.

    What I would do, is first remove all whitespace, then break the expression up into an array, where each element consists of a sub-expression, followed by an operation (+, -), etc..., followed by another sub-expression. Keep doing this with each sub-expression until it's small enough to be evaluated as simply "5". If you structure this correctly, you'll end up with a tree structure, in which you can simply evaluate each branch, working your way back to the main "trunk" of the tree, and by the time you evaluate the trunk, everything will be evaluated for you.

    Now, that's quite complex considering the example you proposed, but it will be accurate and scalable. Consider things like if you need to account for ()'s, order of operations, and what operators you need to support.

  5. #5
    Registered User
    Join Date
    May 2009
    Location
    Oregon, USA
    Posts
    9
    Wow, here I was thinking I took on a simple task but it got a lot more complex once I started to actually get deeper into it. Unfortunately my understanding of Structures is very, very limited. I've been planning on doing some studying on them soon, so from what I'm reading I'm going to need to give this project a break until I have a better understanding of what structures are and how to implement them.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Well, you can do it without all the above complexities, but you won't be able to get too fancy with your syntax.

    For instance, parsing and evaluating "1+3" or even "1 + 3" is not that big a deal. But, parsing "1+(3-4)*(5/4)" and evaluating it properly is a bigger deal. For the latter type of syntax, you need a structured approach. With the first syntax, you can hack through it reasonably fast. And, if you are doing this to sharpen your skills, I say do it.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    It's not necessarily C structures you need for this. sean was using the word structure in the normal English way. Not referring to the C conglomerate data type.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    It's not necessarily C structures you need for this. sean was using the word structure in the normal English way. Not referring to the C conglomerate data type.
    Indeed - I did a similar parser, and actually did it with nested arrays and strings. I also agree with Dino - if this is something that piques your interest, it's a good idea to try it, even if you find it challenging.

  9. #9
    Registered User
    Join Date
    May 2009
    Location
    Oregon, USA
    Posts
    9
    Awesome, I'm getting started on looking up Structures right now and how they work. If I need any more help I'll be sure to ask.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  5. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM