2 kyu

Parsing and evaluation of mathematical expressions

182 of 183smile67

Description:

Your task is to parse and Calculate Mathematical Expressions.
 
Don't use any 'parse', 'compile' or 'eval' function of the Compiler (by the way C# has no 'eval' function like Java/Javascript/..., but surely there are ways to use similar methods, or parts of it) - write your own parser/evaluation function.

I don't want to write down each syntactical fineness of the mathematical expressions you have to evaluate, but here are some infos or rules and later on some examples:

  • Generally "normal programming code syntax and operator priority" for the expression/input string
  • Operators: *, /, +, -, & (last one instead of Math.Pow, i.e. 2&3= 8)
  • Operator- priority: "&" > "* /" > "+ -"; [hint: -2+2=0, but -2&2=-4 i.e. -2&2=-(2&2) and not (-2)&2]
  • -- => + is ok; ++ is not allowed (---5 [=-5] or (-5+1) [=-4] is allowed, but not ++5 or (+5+1))
  • Numbers: integers, doubles, scientific notation too (0.01= 1e-2 and 100= 1e2 or 1e+2, -0.1= -1e-1 for example)
  • List of allowed mathematical functions: "log", "ln", "exp", "sqrt", "abs", "atan", "acos", "asin", "sinh", "cosh", "tanh", "tan", "sin", "cos" (all exist in "C#/Math" and can be used for concrete calculation)
  • Simple error- handling is required (minimal output on errors is string "ERROR", details aren't necessary, for example (5+2)) or 5/0 are errors)

  • Now some easy examples:
     
  • --- 5* 2 = -10
  • 2 * 3 + 2 & 2 * 4 = 22
  • 5 -- 6 = 11
  • ((2 + 3) * (1 + 2)) * 4 & 2 = 240 or ((2 + 3) * (1 + 2)) * 4 & -2 = 0.9375
  • sqrt (sin(2 + 3)*cos (1+2)) * 4 & 2 = 15.5893529757165
  • sqrt (-2) = ERROR and 1 / 0 = ERROR (not 'NaN')
  • abs(-2 * 1e-3) = 0.002
  • 4 & 3 & 2 = 262144
  • I think it's clear and enough;-)

  • Some generall infos for coding and testcases:

    You have to implement a function "public string eval (string expression)" which returns the result of the calculated expression as a string. If the result is valid, you have to convert the double to a string (the expression value is a double, the eval output a string) and if an error occurs, you have to return "ERROR" (with optional message like "ERROR: Stack overflow"). The expression can contain [Spaces], so "sin  ( 2) ist ok, 5  + 6  -2 too, but not "si   n (2)" (function names not splitted). The included mathematical functions can use a mix of upper and lower letters, so Sin(2), SIN(2) and sin(2) are allowed.
     
    The test cases contain some "constant/static expressions", but the later and more difficult tests for your code- submission include random tests too. Here your results are accepted, if there's not more than 1e-8 difference to the calculated test- results.
     

    Hope you have fun;-)!

    Algorithms
    Data Structures
    Parsing
    Interpreters
    Mathematics

    Stats:

    CreatedNov 19, 2015
    PublishedNov 20, 2015
    Warriors Trained1152
    Total Skips250
    Total Code Submissions4033
    Total Times Completed183
    C# Completions182
    Total Stars143
    % of votes with a positive feedback rating90% of 64
    Total "Very Satisfied" Votes54
    Total "Somewhat Satisfied" Votes7
    Total "Not Satisfied" Votes3
    Total Rank Assessments5
    Average Assessed Rank
    2 kyu
    Highest Assessed Rank
    1 kyu
    Lowest Assessed Rank
    3 kyu
    Ad
    Contributors
    • smile67 Avatar
    • user5036852 Avatar
    • hobovsky Avatar
    Ad