CSP 301 : Design Practices in Computer Science

Assignment 1 : Big Number Arithmetic

Problem :

To build a simple calculator that can perform the following operations : +, -, *, / and sqrt() on big numbers. The idea is to be able to manipulate very large numbers that are out of range for the usual numeric data types (int, float, long int, double etc.)

Tools to use :

Flex & Bison

Input :

Read from file : calc.input.
Two lines of input.
First line contains the max_length to use - the maximum number of digits to manipulate at any stage. These include digits both before & after the decimal point, but not the point itself. You may assume that max_length would never exceed 100.
Second line contains the expression to be evaluated in the format specified by the grammar below.

Grammar :


exprn :	number |
	- exprn |
	exprn + exprn |
	exprn - exprn |
	exprn * exprn |
	exprn / exprn |
	SQRT( exprn ) |
	( exprn )
			

(Note the presence of spaces between before & after operators and within paranthesis. Please stick to this format.)

number : integer | integer.integer

integer : DIGIT | integer DIGIT 

(The space between 'integer' & 'DIGIT' here is present only for clarity. No integer is written with spaces between its digits!)
DIGIT : 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 

Operations :

The operations to be performed are : addition, subtraction, multiplication, division and square root. You may use the easiest method to perform these operations on large numbers - pen & paper method would suffice. Accuracy, not efficiency is key.

Usual precedence rules apply : () > unary - > *,/ > +,-

Adopt a policy of rounding towards zero. Round off may be required at any stage - at input if the number in expression has more digits than max_length, at intermediate stages or at output.

Output :

Write to file : calc.output.
Single line output.
If the expression could be evaluated to a valid value, print the value as a number. Redundant leading & trailing zeros should be removed. If the result is negative, precede the number by a '-' and a space ('- num').
If evaluation could not proceed due to syntax errors, print : SynErr
If evaluation could not proceed due to Divide by Zero error, print : DivErr
If evaluation could not proceed due to need to calculate Square root of negative number, print : SqrtErr
If evaluation could not proceed or be printed due to inadequate max_length to represent the digits in front of the decimal point, print : LowPrec

Please stick to the given input/output format. Remember to remove any prompts or debugging help output before submitting.

Examples :

Sample 1 :

Input :

20
11111111111111111111 + 22222222222222222222 * 3

Output :

77777777777777777777


Sample 2 :

Input :

20
( 11111111111111111111 + 22222222222222222222 ) * 3

Output :

99999999999999999999


Sample 3 :

Input :

4
1 + SQRT( - 5 * ( 70 / - 14 ) )

Output :

6


Sample 4 :

Input :

4
1 + SQRT( - 5 * ( 70 / ( - 14 + 2 * 7 ) ) )

Output :

DivErr


Sample 5 :

Input :

4
1 + SQRT( - 5 * ( 70 / 14 ) )

Output :

SqrtErr


Sample 6 :

Input :

4
1 + SQRT( - 5 * ( *70 / - 14 ) )

Output :

SynErr


Sample 7 :

Input :

5
5.555 * 2000

Output :

11110



Sample 8 :

Input :

4
5.555 * 2000

Output :

LowPrec



Submit :

  • Complete source code (only source code!) in folder (no sub-folders) : src
  • Executable - file name : bignumcalc
  • Upload zip file containing the above two, named [your_entry_number_ass1].zip

Due date : 20/08/2011