March 5, 2005
     

Program / Task Name:

   Arithmetic Expressions

Level:

 

 Novice / Advanced

Point Value:

 

 18

Solution(s):

 

 prob12.cpp

Data File(s):

 

 prob12.in

 
C programmers: your program name must be: prob12.exe
JAVA programmers: your program name must be: prob12.class

 

Task Description

Write a program to evaluate arithmetic expressions that include basic operations (addition, subtraction, multiplication, and division), exponents, factorials, operator precedence, and parenthetical grouping.

Program Input

Each line of the input file (prob12.in) is a complete arithmetic expression. The program must interpret all numbers as real numbers (floating point), and it must be able to handle 16 nested parenthetical groups. The program must ignore spaces in the input line. The maximum length of an input line is 80 characters.  Parentheses have the highest precedence, followed by the factorial operator !, then by the exponent operator ^, then by multiplication and division, then (finally) by addition and subtraction. The expression X ^ Y should be evaluated as XY, X to the Y power. The last line of input is a single asterisk character. Assume all expressions are valid, parentheses will match, etc.

 

2 + 3! * -5

3.2 + 7.091 * (-19.4 - 6.3)

1.5 + 24.0/2^4*2

(1.3+0.1*(4.3+7.4*19.3-72/(3.14+2.4*9.2)+96))/(64.3-13*(9-4/-3))

7 - - 3

(2^3-3)! 2^0.5

2 + 4 * 3 + 5

*

 

Program Output

The program will print the result of evaluating the arithmetic expression. Rounding discrepancies past four digits will not be considered errors.

-28.000000

-179.038700

4.500000

-0.361635

10.000000

118.585786

19.000000

 

 

   
  (c) 2005 Hewlett-Packard Company