~ Introduction to Programming: Pascal ~

Session 6 - Further Exercise - Calculator Program

<<  Back to Contents Page

Target

Write a simple program that will perform the addition, subtraction, multiplication and division functions of a calculator, until the user terminates the program.

Instructions

Write the program, following these instructions:

When you run the program, have it prompt you as follows:-

            Enter a number:

Read a number into a variable called readNum1 (make it a real value) from the keyboard - hint, use readLn.

Next, start a repeat loop - we'll finish it later.

Next (remember to indent this, as we have started a loop), perform the following instructions within another (second) repeat ... until style loop:-

The purpose of this loop is to obtain an operator (i.e. plus, minus, times or divide or T to terminate) from the user, and to loop around until one of these keys is pressed. This ensure that only one of these keys is pressed. The key pressed will be placed in the readOperator variable.

Note that the function upCase takes the single-character (char) value in brackets and turns it into an Upper Case character if it was lower case. This allows us to test for whether the user presses T as Upper or lower case.

We need to get the second value to operate on now, but only if the user didn't press the T key:-

if upcase(readOperator)<>"T" then

It's now time to finish the repeat statement from earlier on. Make the condition for the until as follows:-

        until upcase(readOperator)="T"

Finish the program with a friendly message, such as:-

        Program has finished. Thank you.

You now have a simple calculator that will calculate on successive numbers until you press the T key to quit the main (outer) loop.

Sample Results

The following box shows a sample session when you run the program as described above:-

Enter a number: 10
Which operator? +, -, *, / or T to terminate? +
Enter second number: 20
Result is: 30.000
Which operator? +, -, *, / or T to terminate? /
Enter second number: 2
Result is: 15.000
Which operator? +, -, *, / or T to terminate? *
Enter second number: 10
Result is: 150.000
Which operator? +, -, *, / or T to terminate? -
Enter second number: 50
Result is: 100.000
Which operator? +, -, *, / or T to terminate? /
Enter second number: 3
Result is: 33.333
Which operator? +, -, *, / or T to terminate? -
Enter second number: 33
Result is: 0.333
Which operator? +, -, *, / or T to terminate? -
Enter second number: 1
Result is: -0.667
Which operator? +, -, *, / or T to terminate? t
Program has finished. Thank you.

You can use this to test the functioning of your own program.

Sample Program

Programming is (contrary to popular belief) an inexact and quite individual process. Therefore, if you find that your program works effectively but looks a little different to the sample program given below, don't worry too much.

You may however find a few useful tricks in it that may inspire you to play with your own design.
program Calculator;

uses Crt;

var readNum1,readNum2:real;
    readOperator:char;

begin

  clrScr;
  write('Enter a number: ');
  readln(readNum1);

  repeat

    repeat

      write('Which operator? +, -, *, / or T to terminate? ');
      readOperator := readKey;
      write(readOperator);
      writeLn;

    until (readOperator = '+') or (readOperator ='-') or (readOperator='*') or
          (readOperator = '/') or (upCase(readOperator)='T');

    if upCase(readOperator) <> 'T' then
    begin

      write('Enter second number: ');
      readln(readnum2);

      if readOperator = '+' then readNum1 := readNum1 + readNum2
        else if readOperator = '-' then readNum1 := readNum1 - readNum2
          else if readOperator = '*' then readNum1 := readNum1 * readNum2
            else if readOperator = '/' then readNum1 := readNum1 / readNum2;

      writeln('Result is: ',readNum1:10:3);

    end;

  until upCase(readOperator) = 'T';

  writeLn('Program has finished. Thank you.');
  readKey;

end.