COSC 125 Operating Systems
Spring 2006
Homework Assignment #1
All Your Base Are Belong To Us
Due: Wednesday, February 01, 1:00PM CST
Submit: E-mail electronic
copy to professor, with a subject header "COSC 125 HW#1".
This assignment is to be completed individually.
The Multi-Base Calculator
|
Write a calculator program that reads in expressions consisting of
integers and the operators +, -, *, /, %, ^ (exponentiation), & (bitwise AND),
| (bitwise OR), << (left shift), and >> (right shift), and
prints the results when evaluated in simple left-to-right order.
Your calculator should understand positive integers in binary
(starting with "0b",) octal, decimal, and hexadecimal, but all output
will be in decimal (base-10).
You should use the getchar library function to read console
input one character at a time. The use of any other library functions
for input is not recommended at this time.
The professor has provided an example program for your reference,
executable on Morbius as ~brylow/cosc125/projects/calculator.
There are also a number of sample test inputs in that same directory. You
would do well to assemble additional testcases.
Morbius is the primary development platform for COSC 125. You should be
able to login to morbius.mscs.mu.edu via ssh; you will have the same home
directory and password as on Pascal. You can also login directly using
X clients in the center section of CU 310, or using the X emulators present
on any of the Windows machines in any of the Cudahy labs.
If you need a refresher on using the UNIX environment, please see the
UNIX Tutorial.
This project can seem deceptively complex, but is quite tractable if
you first take the time to design suitable helper functions, and test
those function thorougly before moving on to the overall calculator.
The internal data representation of the calculator values can be
assumed to be a signed int. You are not required to deal with
overflow and underflow issues.
Parsing input is always tedious, particularly when the input is not
rigidly constrained with rules like, "all operators must be surrounded by
space on both sides," (which is NOT a constraint for this assignment.)
Powerful tools for parsing are covered in COSC 170 Compilers and to a much
lesser extent, COSC 152 Programming Languages. In lieu of a parser-generator,
consider drawing simple state diagrams for how to recognize an integer in
octal, or how to recognize an integer in hexadecimal. In many cases with
input of this type, it is useful to be able to put back a character
of input once you realize you have read too far. Useful C routines for
implementing this kind of buffering are presented in Section 4.3 of K&R,
(getch() and ungetch()).
Consider various operational and parsing errors that can take place.
See the reference implementation for appropriate error messages.
Back