The George Washington University
School of Engineering and Applied Science
Department of Electrical Engineering and Computer Science
CSci 51 -- Fall 1996
Project #4
Due Date: Thurs. Nov. 21 (sect. 10); Mon. Nov. 25 (sect. 11)

This project continues the spreadsheet system we began in Project 3.

Part of the power of a real spreadsheet program is that the user can enter a formula in a cell, and this formula calculates a value that is displayed in that cell. In our case, formulas will be written in RPN, and stored in cells using procedure calls like

Set(Cell => G-1, A_Formula =>
  "B1 0.1 * C1 0.3 * + D1 0.15 * + E1 0.15 * + F1 0.3 * +");

which, in our class spreadsheet from Problem 3, represents a weighted sum of the five grades. Setting a cell of the spreadsheet to a formula allows the value of a cell to be computed using values in other cells. Formulas can only include floating- point constants, individual cell references and the four arithmetic operators.

Your project is to implement the following new capabilities:

1. One additional set procedure is to be added to the spreadsheet package specification:

SUBTYPE Formulas IS String (1..50);

PROCEDURE Set(Cell: IN Cells; A_Formula: Formulas);

2. The evaluation of the postfix expression is to be done using the generic stack package begun in Program 7.3, using an unbounded implementation as suggested in Sect. 9.3. The pseudocode for evaluating postfix expressions (a modification of the algorithm in Program 7.4) is given below:

LOOP
  EXIT WHEN End of Formula
  CASE Next character of formula IS
    WHEN Space =>
      Skip it;
    WHEN Numeric digits =>
      Convert from ASCII to floating point;
      Push value onto the stack;
    WHEN Letter =>
      Decode column and row;
      Extract value from spreadsheet;
      Push value onto the stack;
    WHEN Operator =>
      Pop two values off the stack;
      Perform the operation;
      Push the result onto the stack;
  END CASE;
END LOOP;
Pop the final result off the stack;

3. In the main program, the set formula procedure should be used to compute values of cells in a new column that contains a weighted average of the grades. The first grade should have weight 0.1, the second 0.3, the third and fourth 0.15 and the fifth should have weight 0.3.

4. To convert the row and column designators from their ASCII representation, it will be necessary to add two generic formal procedure parameters to the generic package specification. The new generic header for the spreadsheet package specification is shown below:

GENERIC
  TYPE Rows IS (<>);
  TYPE Columns IS (<>);
  Max_String_Length: IN Positive;
  WITH PROCEDURE Get(From: IN String; Item: OUT Rows; Last:
    OUT Positive) IS <>;
  WITH PROCEDURE Get(From: IN String; Item: OUT Columns;
    Last: OUT Positive) IS <>;
PACKAGE Spreadsheet_Package IS...

NOTE: converting a floating-point constant from ASCII to Float is quite easy using the version of Ada.Float_Text_IO.Get that reads from a string. See Appendix D.