CSCI 150, Spring 2003

Home | | Course Schedule | | Assignments | | Lecture Notes

Laboratory 3
Due at the end of today's class

Problem #1
Write a Pascal program (named calc.pas) which inputs two numbers and an arithmetic operator (which will be '+', '-', '*', or '/') and computes the correct result. The two numbers should be of type real and the operator will be of type char (this type is used for a single character). If the user does not enter a valid operator, the program should inform the user and set the answer to zero. The program should ask the user if they want to perform a calculation at the start of the program and after each calculation is performed. It should continue to calculate until the user answers 'no' to the question about performing another calculation. At the end of the program, the program should say 'Thank you and goodbye.' You may use the program skeleton given below:

	{ your name and program description goes here. }
	program calc;
	var
		num1     : real;
		num2     : real;
		answer   : real;
		opcode   : char;
		response : string;
	begin
		{need some code here to ask user if they want to 
		calculate something and read in their response}

		while response = 'yes' do
		begin

			{need some code here to input num1, num2 and 
			opcode}

			if opcode = '+' then
			begin
				answer := num1 + num2;
			end

			{need some code here for opcode -, *, /}

			else
			begin
				answer := 0;
				writeln('That is not a valid operator.');
			end;

			{need some code here to output answer}

			{need some code here to ask user if they want to 
			do another calculation and read in their 
			response}
		end;
		{need some code here to say goodbye}
	end.  {end of main program calc}

 

A sample session for the problem might look like the following:

    Do you want to perform a calculation? 
    yes
    Please enter the first number: 
    5
    Please enter the second number: 
    7
    Please enter the operator: 
    *
    The result is 35.00
    Do you want to perform another calculation?
    yes
    Please enter the first number: 
    2
    Please enter the second number: 
    7.2
    Please enter the operator: 
    %
    That is not a valid operator.
    The result is 0.00
    Do you want to perform another calculation?
    yes
    Please enter the first number: 
    2
    Please enter the second number: 
    7.2
    Please enter the operator: 
    +
    The result is 9.20
    Do you want to perform another calculation?
    no
    Thank you and goodbye.
    

 

What To Turn In.

A printed listing of the lab program. Be sure that your name is in the program prologue’s comment section of the program.

Reminder.

Be sure to save a copy of each program on your P:\ drive, and delete your working copies from your D:\ drive.