# Thread: Identifying specific characters in a string

1. ## 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. 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. 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.

4. 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. 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. 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.

7. 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. 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. 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