Computer Science 111a - Fall Semester, 2003
Homework #9
due: by 5:00 on Dec. 2 (NOTE: this is a
change from the due date on the syllabus.)
Your task in this project is to
convert this week's lab into a simple calculator program. The program will take a string as input,
where the string is a simple expression of the form
number operator number
The two numbers will be in base 10
(positive or negative), and the operator will be one of '+', '-', '*', or
'/'. There may be no whitespace in the
entire expression, e.g., "1*50", or there may be arbitrary white
space before or after any component of the string, e.g., " 5
/ 2 ".
The program will check that the
string is a legal expression of the above form and report if it is not. If it is legal, compute the value of the
expression and print out the value in base 2, base 8, base 10, and base 16.
I have put an executable version of
my program in file calculator in my handout directory, so that
you can see what I want the program to do.
Details:
1.
Declare a function that will take the input string as an argument and
that will do the following:
·
Find the
operator. You can use strpbrk to do
this, except you must be careful to skip past a '+' or '-' sign if the first
number contains 1.
·
Store the
operator in a local variable that will get returned by this function.
·
Find the first
number. Do this in three steps:
o Store a null character over the operator.
o Copy the string up to the null character into
an operand string variable.
o Call a function that will remove white space
at the beginning and end of this string.
·
Check to make
sure that the first number is indeed a legal base 10 number. If not, return a 0.
·
Find the second
number, in two steps:
o Copy the string after the operator into a
second operand string variable.
o Call the function to remove white space.
·
Check to make
sure that the second number is indeed a legal base 10 number. If not, return a 0.
2.
Assuming you have a legal expression, you should now call a function to
convert the first operand string to an integer, and call it again to convert
the second operand string to an integer.
You can then use the operator to decide what operation to perform on the
two integers.
3.
Then produce the 4 character strings representing the result in the
different bases, printing each string after the function call that does the
conversion.
What to submit. Your program should be in a file called hw9.c. Submit this homework by the due date with the command submit hw9 hw9.c