Char Math Symbols

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zxry
    New Member
    • Nov 2008
    • 1

    Char Math Symbols

    I'm new to C++ and need to write a program that reads and calculates math equations from a char string. My question: is there a way to convert a char math symbol such as '+' into a + the compiler can use in an equation?

    Right now I'm using a bunch of If statements to handle this, but would like to know if there's a way to work the char sign directly into the equation.

    i.e.

    char num1 = '5';
    char sign = '+';
    char num2 = '2';

    if (sign == '+')
    {
    int sum = (num1 - '0') + (num2 - '0');
    // want to do somthing like: int sum = (num1 - '0') sign (num2 - '0');
    }
    if (sign == '-')

    .... etc

    Thanks.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Not that I know of. The way you're doing it is the way I would do it.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      @OP: you're talking about the rudiments of an expression interpreter. Google for
      infix-expression and postfix-expression. The second form is easy to evaluate by
      using a simple stack.

      kind regards,

      Jos

      Comment

      Working...