CS2301 - B-term 10

Homework 1
4-function Calculator

Due: Tuesday, November 2 at 5pm

Read the expectations on homework. You will be doing this assignment and all subsequent homework assignments individually.


After successfully completing this assignment, you will be able to...

Before Starting

Read Chapters 1-4, particularly section 4.7.

The Assignment

You are to write a C program that functions as a simple calculator. The calculator has an "accumulator" that can hold a single floating-point value. There is a command that sets the value in the accumulator. Arithmetic commands allow the user to add, subtract, multiply, or divide the number in the accumulator by a given value. The result of the arithmetic operation becomes the new value of the accumulator.

The program accepts commands of the form

<operand> <operator>


operand is a number of type float, and

operator is one of the characters:

After each command, the calculator performs the operation and prints out the value in the accumulator, or prints an error message.

Include files

Assumptions and Restrictions

You may assume that all input is in correct form, i.e. a number followed by one or more spaces, followed by a single-character command, followed by a newline. The program should be able to detect attempted division by zero, and be able to recover from the input of an illegal command.

You may use the following C statements and constructs in the completion of this project: for-, while-, and do-loops; if-statements, switch statements, assignment statements, scanf and printf. You may of course declare any variables you need. Do not use arrays, functions (other than main) or any other constructs/statements that haven't yet been covered in class.

Sample Execution

               Simple Calculator Program

(instructions to user go here...)

Enter your commands

==>10 S
= 10.000000

==>2       /
= 5.000000

==> 55  -
= -50.000000

==>100.25 S
= 100.250000

==>4 *
= 401.000000

==>0 /
Division by zero not allowed!
= 401.000000

==>0 Q
Legal commands are S, E, +, -, *, /
== 401.000000

==>0 E

Calculator program terminating...


Write a short text file called README that summarizes your program, how to run it, and detailing any assumptions and any problems you had. From your Unix account, submit your C source code file and your README file using the following turnin command:

/cs/bin/turnin submit cs2301 PROJECT1 calculator.c README

Programs submitted after 5pm on Nov 2 will be tagged as late, and will be subject to the late homework policy.


Here is the grade sheet we will use when we grade your programs. Note that programs must compile successfully in order to receive points for Correctness. Programs that do not compile with gcc will receive 0 out of 65 points for Correctness.