PROGRAM TenKey;

{  This program emulates a dedicated '10 key' calculator.  It allows  }
{  you to add, subtract, divide, and multiply.  The program operates  }
{  like a TI calculator; you must type a number, then operation,      }
{  another number and the SPACE or EQUALS keys to get the result.     }

{                       123   +                                       }
{                         1   =                                       }
{                       124  ANSWER                                   }

{  Written for Turbo Pascal by Jeff Firestone. June 1984.             }

CONST
  BS = #8;
  CR = #13;

VAR
  StackPtr, code, i, j : INTEGER;
  edit : STRING[20];
  value, oldvalue : REAL;
  Key, Operator : CHAR;

{-------------------------------------------------------------}

PROCEDURE Initialize;
BEGIN
  GOTOXY(25,15); WRITE('TEN KEY CALCULATOR PROGRAM');
  GOTOXY(20,17); WRITE('Type the numbers first followed by +,-,/,*');
  GOTOXY(30,18); WRITE('Use ESC to exit.');
  value:= 0; oldvalue:= 0;
  Edit:= ''; Operator:= ' ';
  GOTOXY(1, 25); WRITE('0':14); CLREOL;
END;

{-------------------------------------------------------------}

PROCEDURE PrintValue;
BEGIN
  GOTOXY(1, 25); WRITELN;
  GOTOXY(1, 24);
  WRITE(value:20:5);
  IF Key IN ['+','-','*','/','='] THEN
    WRITELN('   ', Key)
  ELSE
    IF Key = 'A' THEN WRITELN('  ANSWER');
  edit:= '';
END;

{-------------------------------------------------------------}

PROCEDURE DoMath;
BEGIN
  oldvalue:= value;
  VAL(edit, value, code);
  PrintValue;
  CASE Operator OF
    '+' : value:= oldvalue + value;
    '-' : value:= oldvalue - value;
    '*' : value:= oldvalue * value;
    '/' : IF value <> 0 THEN value:= oldvalue / value;
  END;
END;

{-------------------------------------------------------------}

PROCEDURE BackSpace;
BEGIN
  IF (LENGTH(edit) > 0) THEN
    edit:= COPY(edit, 1, (LENGTH(edit)-1));
END;

{-------------------------------------------------------------}

PROCEDURE Equals;
BEGIN
  Key:= '=';
  DoMath;
  Key:= 'A';
  PrintValue;
  value:= 0; oldvalue:= 0; Operator:=' ';
  WRITELN; WRITELN;
END;

{-------------------------------------------------------------}

PROCEDURE MathFunc;
BEGIN
  DoMath;
  Operator:= Key;
END;

{-------------------------------------------------------------}

BEGIN
  Initialize;
  REPEAT
    GOTOXY(14, 25);
    READ(KBD, Key);
    CASE Key OF
      '+' : MathFunc;
      '-' : MathFunc;
      '/' : MathFunc;
      '*' : MathFunc;
      BS  : BackSpace;
      '=' : Equals;
      ' ' : Equals;
    ELSE
      IF (Key IN ['0'..'9','.']) AND ((LENGTH(edit) < 10)) THEN
        Edit := Edit + key;
    END;
    GOTOXY(1,25);
    IF edit = '' THEN
      WRITE('0':14)
    ELSE
      WRITE(edit:14);
    CLREOL;
  UNTIL Key = #27;
END.
   begin
          NewX:= 320+round(H * K1);
    