Source code for Operation: Arithmetic!


Program Arithmetic;

   {Program to read in and compute the answers to simple
    arithmetic problems in the followin format:
    operand-operator-operand, e.g. 4+2.
    ADaM Howard, Hr. 8, 12/12/95, Chpt. 8 Test.}

USES
    CRT;

VAR
   Int1, Int2 : Integer;
   Operator, Done : Char;
   Answer : Real;
   Error : Boolean;

{**************************************************************}

Procedure GetInfo (VAR X, Y : Integer; VAR Op : Char);

   {Tells user what program does, what operators they may use, and how
    to enter their input.}

BEGIN {getinfo}

   WRITELN ('This program performs simple arithmetic operations for you.');
   WRITELN ('You may choose from the following operators:');
   WRITELN (' / -- Division');
   WRITELN (' * -- Multiplication');
   WRITELN (' + -- Addition');
   WRITELN (' - -- Subtraction');
   WRITELN (' ^ -- Raise to power, e.g. 2^2 is 2 squared');
   WRITELN;
   WRITE ('Enter in this format --> 2+2 or 5^1, hitting the ');
   WRITELN (' Key after each');
   WRITELN ('of the Operands (Integers) and Operators. Integers Only!!');
   WRITELN ('Example --> 2  +  2 ');
   WRITELN;
   WRITE ('Enter your problem now --> ');
   READLN (X);
   READLN (Op);
   READLN (Y);

END; {getinfo}

{**************************************************************}

Procedure Solve (X, Y : Integer; VAR Ans : Real; Op : Char;
                                         VAR Err : Boolean);

{Performs arithmetic after checking for operation-type with a
 case statement, sets errorlevel if invalid operator}

VAR
   Count : Integer;

BEGIN {solve}

   CASE Op OF
      '+' : Ans := X + Y;
      '-' : Ans := X - Y;
      '*' : Ans := X * Y;
      '/' : Ans := X / Y;
      '^' : BEGIN {power}
             Count := 0;
             Ans := 1;
             REPEAT
              Ans := Ans * X;
              Count := Count + 1;
             UNTIL Count >= Y;
             IF Y = 0
                THEN Ans := 1; {Any number to 0 power equals 1}
            END; {power}
      ELSE BEGIN {CASE -else}
            WRITELN ('Sorry, ''', Op, ''' isn''t a valid operator.');
            Err := True;
           END; {CASE -else}
   END; {CASE}
END; {solve}

{**************************************************************}

Procedure PrintResults (X, Y : Integer; Op : Char; Err : Boolean ;
                                                       Ans : Real);

{Prints out the original problme followed by a '=' and the answer
 unless there was an error}

BEGIN {printresults}

   IF Err
      THEN BEGIN {then}
            WRITE ('Since you didn''t enter a valid operator, there ');
            WRITELN ('is no answer.');
           END {then}
      ELSE WRITELN (X, Op, Y, '=', Ans:6:3);
   WRITELN;
END; {printresults}

{**************************************************************}

BEGIN {main}

   CLRSCR;
   GotoXY (29,2);
   WRITELN ('-=-=-=-=-=-=-=-=-=-=-=-');
   GotoXY (29,3);
   WRITELN ('Operation:  Arithmetic!');
   GotoXY (29,4);
   WRITELN ('=-=-=-=-=-=-=-=-=-=-=-=');
   WRITELN;
   Done := 'N';
   REPEAT
    Error := False;
    GetInfo (Int1, Int2, Operator);
    Solve (Int1, Int2, Answer, Operator, Error);
    PrintResults (Int1, Int2, Operator, Error, Answer);
    WRITE ('Quit? (Y)es to quit, almost any other key to continue --> ');
    READLN (Done);
   UNTIL (Done = 'Y') OR (Done = 'y');
   WRITELN ('Please hit .');
   READLN;

END. {main}

Return to Adamz Hoemwurk Paij.