Hi Guys

Does anyone know of any classes that process string mathmatical equations ? E.g. 2+2(6*5(2-1)) - It is specified on the command line and stored as a string.

Dani AI

Generated

: parsing command-line strings like 2+2(6*5(2-1)) requires more than the standard math functions; you either write a small parser or reuse a library. is right that writing your own is a viable learning path, and 's math-library pointer is useful for numeric functions but does not parse infix strings by itself.

A practical way to implement your own parser is the shunting-yard approach (infix -> RPN -> evaluate). Key steps to get right:

  • Tokenize (numbers, identifiers, functions, operators, parentheses).
  • Handle unary operators (unary minus) separately from binary minus.
  • Detect and insert implicit multiplication (e.g., 2( or )( or 2x -> 2*().
  • Convert to postfix with operator precedence and associativity (exponent is usually right-associative).
  • Evaluate the postfix expression with a stack and strong error reporting (mismatched parens, unknown token, overflow).

If you prefer a ready-made solution, consider:

Troubleshooting tips: add unit tests for tricky cases (implicit multiplication, nested functions, unary minus, large nesting depth). Decide on semantics for ambiguous input (is 2(3) allowed?), choose a numeric type (double vs long double), and watch locale decimal separators. For production, prefer a battle-tested library for performance and robustness; write your own only if you need custom behavior or as an exercise.

Recommended Answers

All 2 Replies

Only the ones you write.

there is a math library.
Here's a link:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.