7.7 Returning multiple values |
Subroutines and functions often need to return more than one value, even simple ones. For example, a return value and error conditions. "C" uses pass by value for its function calls. This means that to get more than one value returned you need to pass the address of where you want the return value if you have more then one.
In this lab I want you to go quite a bit further than you did in lab 7.4: 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) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If there is a "-
" 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 by 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 *radix, int *characters_read);
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 -123
This output should be in (about) the following format:
Your name CIS265 Louis Taber Lab: 7.7: 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 Total characters read 35
Test 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 2147483647 Decimal max -2147483647 Almost decimal min (sum now zero) 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, no numbers
It is available by anonymous FTP at
ftp://lt.tucson.az.us/pub/c/return.dat.
Extra credit: Handle INT_MIN
(-2147483648) correctly.
Turn in a copy of your program and its output when used with the above test data marked with:
Please turn your lab in to me, Louis Taber, during class, or slide it under the door of Santa Rita Building room A-115 (my office).
7.7 Returning multiple values |