My Project
|
Defines interface of numconv.c functions (do not modify) More...
Go to the source code of this file.
Functions | |
char | int2char (int radix, int value) |
int | char2int (int radix, char digit) |
void | divRem (int numerator, int divisor, int *quotient, int *remainder) |
int | ascii2int (int radix, int valueOfPrefix) |
void | int2ascii (int radix, int value) |
double | frac2double (int radix) |
This file defines the interface to a C file numconv.c that you will complete. You will learn how to use the C language operators for address-of (&), and dereferencing pointers *).
int ascii2int | ( | int | radix, |
int | valueOfPrefix | ||
) |
Convert a string to an integer value. The string comes from the standard input. You must read one character at a time by using the built-in getchar()
function which simply returns the next character available in the standard input. The end of the string is signaled by a newline character ('\n'). You MUST implement this function recursively. That means you shouldn't have loops. For information on the algorithm, see the Example revisited (page 3) on Sanjay's handout titled "Number Systems" (referenced in the main page). You may assume that the string is legal in the given radix (letters may be uppercase or lowercase). You can also assume that the string represents a non-negative number. Here's an example for how to test this function:
echo "48A6" | ./testConv a2i 12
This ensures that the string "48A6" is available for you to read from the standard input. The first time you call getchar()
, it will return '4'. The second time, it will return '8', and so on. Eventually, it will return '\n' (because the echo
command automatically appends a newline character).
This function should not print anything (you will lose all points for this function if it does). However, you may print stuff for your own debugging purposes as long as you remove any printing statements before you submit.
radix | - the base you are working in (2-36) |
valueOfPrefix | - the integer value of the digits processed so far (the prefix). For example, if the string is "48A6" in base 12 and you're about to read 'A', the prefix is "48" and the integer value of this prefix is 56. When your function is called by the main program (the testConv.c file), this parameter is 0. |
int char2int | ( | int | radix, |
char | digit | ||
) |
Return the value corresponding to a character.
This function should not print anything (you will lose all points for this function if it does). However, you may print stuff for your own debugging purposes as long as you remove any printing statements before you submit.
radix | - the base you are working in (2-36) |
digit | - character '0'..'9' or 'A'..'Z' or 'a'..'z' |
0 - (radix-1)
corresponding to the given digit if the digit is valid in the given radix. Otherwise return -1void divRem | ( | int | numerator, |
int | divisor, | ||
int * | quotient, | ||
int * | remainder | ||
) |
Calculate both the quotient and remainder of a division and return the values via pointers. You may use the C operators for division (/) and modulus (%). However, you might want to write a loop to perform repeated subtraction to help you understand how you would implement this in LC3 assembly language (which has no operators for division or modulus).
This function should not print anything (you will lose all points for this function if it does). However, you may print stuff for your own debugging purposes as long as you remove any printing statements before you submit.
numerator | - non-negative value for numerator |
divisor | - a positive value to divide by |
quotient | - a pointer to the location in which to store the quotient |
remainder | - a pointer to the location in which to store the remainder |
double frac2double | ( | int | radix | ) |
Convert a string representing the fractional part of a number into a double. This function is for extra credit. The string comes from the standard input. You must read one character at a time by using the built-in getchar()
function which simply returns the next character available in the standard input. The end of the string is signaled by a newline character ('\n'). You MUST implement this function recursively. That means you shouldn't have loops. For information on the algorithm, see Section 4 (page 8) on Sanjay's handout titled "Number
Systems" (referenced in the main page). You may assume that the string is legal in the given radix (letters may be uppercase or lowercase). The string that is provided in the standard input will start with 0.
(a zero followed by a radix point). However, you should assume that everything up to (and including) the radix point has been read. Hence, the first time you call getchar()
, you will get the first character after the radix point. Here's an example for how to test this function:
echo "0.B5C" | ./testConv f2d 13
This ensures that the string "B5C" is available for you to read from the standard input. The first time you call getchar()
, it will return 'B'. The second time, it will return '5', and so on. Eventually, it will return '\n' (because the echo
command automatically appends a newline character).
This function should not print anything (you will lose all points for this function if it does). However, you may print stuff for your own debugging purposes as long as you remove any printing statements before you submit.
radix | - the base you are working in (2-36) |
void int2ascii | ( | int | radix, |
int | value | ||
) |
Print a number in the specified radix. Use the C call putchar()
to print a single character obtained using int2char()
. Do not use any other output functions. This function MUST be implemented recursively. Your program can not use arrays or strings in C, even if you know about them. For information on the algorithm, see Section 3 (page 5) on Sanjay's handout titled "Number Systems" (referenced in the main page). See also Section A2 in the "Number Conversion" notes by Fritz (referenced in the main page).
This function should only print the digits of the number. If it prints extra stuff, you will lose all points for this function. Do not print a newline character at the end. You may print stuff for your own debugging purposes as long as you remove any extra printing statements before you submit.
Also, make sure this function does not print leading zeroes (if it does, you will lose all points for this function).
radix | - the base you are working in (2-36) |
value | - the number to convert (it will be non-negative) |
char int2char | ( | int | radix, |
int | value | ||
) |
Return the character corresponding to a value.
This function should not print anything (you will lose all points for this function if it does). However, you may print stuff for your own debugging purposes as long as you remove any printing statements before you submit.
radix | - the base you are working in (2-36) |
value | - a value in the range of 0 - (radix-1) |