Question

I'm writing a derivative calculator in C++, and in order to properly perform the derivation operations, I need to parse the input equation at various character indexes along the equation's string. I'm using isdigit() to parse out the numerical values from the equation and then store them into a separate string array, however now I need to parse out the mathematical symbols from the equation to identify which operation I need to perform.

Is there any way I can modify (overwrite?) isdigit() to recognize custom values from a string array? I'd like to avoid iteration to make my code a little less cluttered, since I'm already going to be using plenty of loops for the rest of this program and I want my code to be easy to follow. Does overwriting and inheritance in C++ work similarly to inheritance in Java (with the exception of multiple inheritance/interfaces)?

Please refrain from posting solutions that are irrelevant to the scope of this question, IE; different approaches to deriving equations in C++, as I've used this approach for some fairly specific reasons.

Thanks

Was it helpful?

Solution 2

You can just use strchr. (Not everyone will like the macros here, but they do make combining character classes easy.)

#define OPERATOR "+-*/"
#define DIGIT "0123456789"

// Is c an operator
if (strchr(OPERATOR, c)) {
  // Yes it is
}

or:

// Is c an operator or a digit?
if (strchr(OPERATOR DIGIT, c)) {
  // Yup
}

OTHER TIPS

You can use the new powerful C++11 regular expressions library that does almost what ever parsing you want. This way, you'll avoid iterations and code cluttering.

Overriding and Inheritance works more or less the same as in Java. You need to define a function as virtual and redefine it in derived class.

I know the "Please refrain from posting...", but I've written a library that does function parsing and derivation.

It is available at https://github.com/B3rn475/MathParseKit

I hope you can find some tips there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top