Code:
/*
compile and run:
gcc -lm this_program_code.c
./a.out
I tested all of the numbers with this bash command:
for i in `seq -99 100` ; do ./a.out $i ; done
Disclaimer:
I am exempt from using C comments, because I like you.
*/
#include <stdio.h>
#include <math.h>
#define SHORTCUT_BIATCH \
switch(second_digit){\
case 1:\
puts("one");break;\
case 2:\
puts("two");break;\
case 3:\
puts("three");break;\
case 4:\
puts("four");break;\
case 5:\
puts("five");break;\
case 6:\
puts("six");break;\
case 7:\
puts("seven");break;\
case 8:\
puts("eight");break;\
case 9:\
puts("nine");break;\
}
// Let's get this party started
int main(int argc, char **argv){
int number_from_user;
int first_digit, second_digit;
double temporary_number;
// ask for a number -- from command line or command-line input
if(argc == 2)
sscanf(argv[1], "%d", &number_from_user);
else{
printf("Just give me a freakin' number-- ");
fscanf(stdin, "%d", &number_from_user);
}
// is the number outside of the range?
if(number_from_user > 99 || number_from_user < -99){
puts("Error: OUT OF RANGE -- hacker alert!!! lolllly pop");
return 0;
}
printf("%d: ", number_from_user);
// is the number negative?
if(number_from_user < 0){
printf("negative ");
number_from_user = -1 * number_from_user;
}
// get the first digit Xx (the big X)
// (for example, if 34 was the user's input, '3' would be the first digit)
first_digit = (int) floor(number_from_user / 10.0);
//printf("First digit: %d\n", first_digit); //-- debugging bull........
// get the second digit xX (the big X)
second_digit = number_from_user - first_digit * 10;
//printf("Second digit: %d\n", second_digit); // -- debugging crap
//puts("---------------");
// output the number in words
// based on the first and second digits of the numbers they entered.
switch(first_digit){
case 0:
if(second_digit == 0)
puts("zero");
SHORTCUT_BIATCH
break;
case 1:
switch(second_digit){
case 0:
puts("ten");break;
case 1:
puts("eleven");break;
case 2:
puts("twelve");break;
case 3:
puts("thirteen");break;
case 4:
puts("fourteen");break;
case 5:
puts("fiveteen");break;
case 6:
puts("sixteen");break;
case 7:
puts("seventeen");break;
case 8:
puts("eighteen");break;
case 9:
puts("nineteen");break;
}
break;
case 2:
printf("twenty ");
if(second_digit == 0)puts("");
SHORTCUT_BIATCH
break;
case 3:
printf("thirty ");
if(second_digit == 0)puts("");
SHORTCUT_BIATCH
break;
case 4:
printf("fourty ");
if(second_digit == 0)puts("");
SHORTCUT_BIATCH
break;
case 5:
printf("fiftey ");
if(second_digit == 0)puts("");
SHORTCUT_BIATCH
break;
case 6:
printf("sixty ");
if(second_digit == 0)puts("");
SHORTCUT_BIATCH
break;
case 7:
printf("seventy ");
if(second_digit == 0)puts("");
SHORTCUT_BIATCH
break;
case 8:
printf("eighty ");
if(second_digit == 0)puts("");
SHORTCUT_BIATCH
break;
case 9:
printf("ninety ");
if(second_digit == 0)puts("");
SHORTCUT_BIATCH
break;
}
return 0; // successorz :D yay
}