Program Problem1;
type
    point = array [1..2] of integer;

Var
   Count,Count2,Count3 : Integer;
   Error,valueC : integer;
   Cell : String;
   Grid : Array [1..5,1..5] of longInt;

   Formulas : array [1..25] of string;
   form_cells : array [1..25,1..2] of integer;
   formula_count : integer;
   inputfile,outputfile : text;

function Get_val(formula : String):longint;
Var
   Counter,size : Integer;
   Hold_val,Result1 : integer;
   operation : char;
   target : point;
Begin
result1 := 0;
Size := length(formula);
counter := 2;

writeln(formula);

      if (Formula[counter] >= 'A') AND (Formula[counter] <= 'E') then
         Begin
         target[1] := ORD(formula [counter])-64;
         target[2] := ORD(formula [counter+1])-48;
         result1 := Grid[Target[1],Target[2]];
         Counter := Counter + 3;

         End
      else
          Begin
          result1 := (ORD(Formula[Counter]) - 48);
          counter := counter + 2;
          End;

While (Counter <= size) do
      Begin
      Operation := Formula[Counter];
      Counter := Counter + 2;

      if (Formula[counter] >= 'A') AND (Formula[counter] <= 'E') then
         Begin
         target[1] := ORD(formula [counter])-64;
         target[2] := ORD(formula [counter+1])-48;
         Hold_val := Grid[Target[1],Target[2]];
         Counter := Counter + 3;
         End
      else
          Begin
          Hold_Val := (ORD(Formula[Counter]) - 48);
          counter := counter + 2;
          End;

      Case Operation of
           '+' : Result1 := Result1 + Hold_Val;
           '*' : Result1 := Result1 * Hold_Val;
           '-' : Result1 := Result1 - Hold_Val;
            End; {Case Statement}
      End;
Get_val := result1;
End;
(*****************)
Begin
assign (inputfile,'DATA5.txt');
reset (inputfile);
assign (outputfile,'OUT5.txt');
rewrite (outputfile);
formula_count := 1;

for Count := 1 to 5 do
    For Count2 := 1 to 5 do
        Grid[Count2,Count] := 0;

for count := 1 to 5 do
    For count2 := 1 to 5 do
        Begin
        Readln(inputfile,Cell);
        if (Cell[1] = '=') then
           Begin
           formulas[Formula_count] := Cell;
           form_cells[Formula_count,1] := count;
           form_cells[Formula_count,2] := count2;
           Formula_count := formula_count +1;
           End
        else
            Begin
            Val(Cell,valueC,Error);
            Grid[Count,Count2] :=valueC;
            End;
        End;

for count := 1 to formula_count-1 do

        Begin
           Grid[form_cells[count,1],form_cells[count,2]] := Get_Val(formulas[count]);
           writeln('I get here');

        End;


for Count := 1 to 5 do
    Begin
    For Count2 := 1 to 5 do
        Writeln (outputfile,Grid[Count,Count2]);
    End;

close (outputfile);
readln;
end.

