Created
September 14, 2017 18:47
-
-
Save khayyamsaleem/b33924b914940a91b5c4a60c859e600b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "my.h" | |
/** | |
* Prints a number using the length of the string as the base and the contents | |
* as the alphabet. For example, if you called my_num_base(9, "RTFM"), then | |
* R = 0, T = 1, F = 2, M = 3. 9 in base 4 is 21 and so the result is printed | |
* out as "FT". | |
* | |
* If char* is NULL or empty, print an error message and return. | |
* If given unary, repeat alphabet letter the specified number of times. | |
* For negatives print a '-' and then the number. | |
*/ | |
void my_num_base(int num_int, char *chars) | |
{ | |
if (chars == NULL || *chars == '\0') { | |
my_str("Error: invalid alphabet"); | |
return; | |
} | |
long num = num_int; // for min int | |
int base = my_strlen(chars); | |
// negative | |
if (num < 0) { | |
my_char('-'); | |
num = -num; | |
} | |
// unary | |
if (base == 1) { | |
for (int i = 0; i < num; ++i) | |
my_char(chars[0]); | |
return; | |
} | |
// zero | |
if (num == 0) { | |
my_char(chars[0]); | |
return; | |
} | |
// unary is handled above, so MAX_INT in base 2 is 31 bits wide | |
char str[32]; | |
int i = 0; | |
while (num > 0) { | |
str[i++] = chars[num % base]; | |
num /= base; | |
} | |
str[i] = '\0'; | |
my_revstr(str); | |
my_str(str); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment