CS2301 - B-term 10
Due: Tuesday, November 2 at 5pm
Read the expectations on homework. You will be doing this assignment
and all subsequent homework assignments individually.
Outcomes After successfully completing this assignment, you will
be able to...
- Develop a C program on a Linux platform
- Design a program that contains nested selection and iteration constructs
- Use formatting strings and conversion specifiers to do I/O in a C program
- Write programs that correctly parse input that contains both numbers and
Read Chapters 1-4, particularly section 4.7.
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.
program accepts commands of the form
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.
- +, -, *, / (the four arithmetic operators)
- S (for Set the value of the "accumulator")
- E (for End the calculator program)
- stdio.h provides printf and scanf
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,
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.
Simple Calculator Program
(instructions to user go here...)
Enter your commands
==> 55 -
Division by zero not allowed!
Legal commands are S, 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
/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
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.