6.6 Returning multiple values |
In this lab I want you to go quite a bit further than you did in lab 6.3: Numbers/Character Processing. I want you to write a program that returns (as its return value) the next number received as characters into its standard input.
The signed numbers can be in several formats:
Binary | 0b1010101 | Must start with a 0b and only 0 & 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Octal | 01234567 | Must start with a zero and only 0-7 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Decimal | 123456789 | Must start with 1-9 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Hexadecimal | 0xhhhhhhh | h is one of 0-9 & A-F (Uppercase) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-
" directly prior to the number it is
a negative number.
0bA
and 0xG
can be considered a
zero binary and zero hexadecimal number respectively.
Numbers are terminated my any character that is not part of
their set of characters. For example, an octal number is terminated by
a 8
or a 9
and a binary number would be terminated by a
2
. This terminating character should be "put back" with a
call to ungetchar()
so it can be used in the next number.
For example, 0123456789
will first return a octal number
of 12345678 and then a decimal value of 8910.
A hexadecimal number can be terminated by any lower case letter.
Spaces, new lines, tabs, G-Z
, and a-z
terminate all numbers.
Any special character or non-printing character also terminate all numbers.
The function prototype should look something like this:
int getnumber(int *error, int *radix, int *characters_read);
error
should return the following values:
0 | Found a valid number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-1 | Numeric overflow | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-2 | End of file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If an overflow occurs, the return value needs to be set to zero.
radix
should return the following values:
0 | No number found | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2 | Binary number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
8 | Octal number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
10 | Decimal number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
16 | Hexadecimal number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
characters_read
need to have the total number of character
read from the input minus any put back.
At the start of your program print out:
Write a program that process an input data doing the following:
Given the following example input.
abcde 0b0101 -012 123 0x05A -123This output should be in (about) the following format:
Your name CIS265 Louis Taber Lab: 6.6: Multiple return values Binary 5 Octal -10 Decimal 123 Hexadecimal 90 Decimal -123 EOF reached **************** Binary total 5 Octal total -10 Decimal total 0 Hexadecimal total 90 Grand total 85 Number of overflows 0 Total characters read 35Test your program with the following sample data set.
This is the test data for the multiple returns lab. If you have sixteen bit integers, create your own file. 0b11111 037 31 0x1F Four thirty ones -0b11111 -037 -31 -0x1F Four negative thirty ones 0b11111111111111111111111111111111 Binary overflow 077777777777 Octal overflow 2147483648 Decimal overflow 0x80000000 Hexadecimal overflow 2147483647 Decimal max -2147483648 Decimal min (sum now minus one) Run together numbers. 0b12 (Binary one, decimal two) 078 (Octal seven, decimal eight) 0xA0b10 (Hexadecimal One Hundred sixty, decimal ten) This is the last line
It is available by anonymous ftp at
ftp://lt.tucson.az.us/pub/c/return.dat.
Turn in a copy of your program and its output when used with the above test data marked with:
6.6 Returning multiple values |