The project involves developing a function to evaluate arithmetic expressions.
Expressions
An expression is formed from numbers and the four standard arithmetic operations:Arithmetic operations have their usual precedence: multiplication and division have higher precedence than addition and subtraction. All arithmetic operations are left-associative. Parenthesis are used to impose an precedence or associativity different from the default ones.A number is a decimal integer, possibly preceded by a minus sign:
The number-0
is identical to the number0
.White-space characters may appear anywhere within an expression, but may not appear within a number; in particular, no white-space characters should follow the minus sign in negative numbers.
Expression evaluation
Expressions are evaluated with 31 bits of precision; that is, the absolute value of any value computed is assumed to be representable with at most 31 bits. An overflow occurs when the absolute value of an expression can't be represented in at most 31 bits.
Input
The function should accept as input a string containing a single syntactically valid expression.Output
If the input string contains a single syntactically correct expression, the function should return a string containing the value of the expression contained in the input string. The string should contain a single syntactically valid number representing the expression's value; the number may be proceeded or followed by any number of white-space characters. The output string should contain only a syntactically valid number and white-space characters.If an error occurs during the expression's evaluation, the program should return a string with the following format:
Error n descriptionwhere n is a positive integer and description is some text describing the error.If the input string does not contain a single syntactically correct expression, the function should return a string with the following format:
Error n descriptionwhere n and description are as in the previous paragraphEnvironment
The program should be portable across Unix platforms (SunOS, Linux, Solaris, and so on) and will be run predominantly on Solaris systems.This page last modified on 27 January 2000.