There is nothing ready-made in the standard library for parsing expressions, no. However, it's a nice exercise to roll a parser/evaluator yourself. I don't want to spoil the fun, but here are some thoughts:
The idea is to first parse the input string into some sort of data structure which represents the expression (usually some sort of tree structure) and then 'evaluate' that data structure with some given variable bindings.
The data structure might be a tagged union, something like this:
enum ValueType {
ConstantValue, VariableValue, Addition, Division
};
struct Value {
enum ValueType type;
/* The 'representation' of the value. */
union {
int constantValue;
const char *variableValue;
struct {
struct Value *summand1;
struct Value *summand2;
} additionValue;
struct {
struct Value *dividend;
struct Value *divisor;
} divisionValue;
} repr;
};
For the parsing part, I suggest to read up on 'recursive descent' parsers, which area quite easy to understand and write by hand. The goal is to define a function
Value *parse( const char *s );
which returns a representation for the given string.
The evaluation part is quite straightforward and lends itself to recursion. The goal is to define a function
int eval( const Value *v, ??? bindings );
...where ???
would be some type appropriate for holding variable bindings (e.g. a string to int
mapping). Depending on the 'type' of the given value, it will perform the arithmetic operation, e.g.:
int eval( const Value *v, ??? bindings ) {
switch ( v->type ) {
case ConstantValue:
return v->repr.constantValue;
case Addition:
return eval( v->repr.additionValue.summand1 ) + eval( v->repr.additionValue.summand2 );
...