#include "library.h" // PROTOTYPES, allow functions to be used before their definitions void print_0_to_19(const int d); // absolutely tested and perfect, speaks any number 0 .. 19 void print_multiple_of_10(const int d); // perfect, says any number 10, 20, 30, ..., up to 90 void print_20_to_99(const int n); // perfect for any number 20, 21, 22, ..., 99 void print_100_to_999(const int n); // perfect for any number 100 ... 999 /* The various versions of say_number that we went through are all in this file. All but the final one has been turned into a comment */ /* void say_number(const int n) // this function is guaranteed to work perfectly // for any int 0 ... 999. // say_number(x) is guaranteed to write the number x perfectly // in english regardless of how or where it is used, so long as // 0<=x and x<1000. { if (n < 20) print_0_to_19(n); else if (n < 100) print_20_to_99(n); else if (n < 1000) print_100_to_999(n); } */ /* // because we are guaranteed that say_number works for 0 ... 999, // we know that this code will work for 1000 ... 999999 say_number(n / 1000); print(" thousand"); const int last3 = n % 1000; if (last3 > 0) { print(" "); if (last3 < 100) print("and "); say_number(last3); } // therefore this: void say_number(const int n) // this function is guaranteed to work perfectly // for any int 0 ... 999999. // say_number(x) is guaranteed to write the number x perfectly // in english regardless of how or where it is used, so long as // 0<=x and x<1000000. { if (n < 20) print_0_to_19(n); else if (n < 100) print_20_to_99(n); else if (n < 1000) print_100_to_999(n); else if (n < 1000000) { say_number(n / 1000); print(" thousand"); const int last3 = n % 1000; if (last3 > 0) { print(" "); if (last3 < 100) print("and "); say_number(last3); } } } */ /* // and by the same reasoning ... void say_number(const int n) // this function is guaranteed to work perfectly // for any int 0 ... 999999999. // say_number(x) is guaranteed to write the number x perfectly // in english regardless of how or where it is used, so long as // 0<=x and x<1000000000. { if (n < 20) print_0_to_19(n); else if (n < 100) print_20_to_99(n); else if (n < 1000) print_100_to_999(n); else if (n < 1000000) { say_number(n / 1000); print(" thousand"); const int last3 = n % 1000; if (last3 > 0) { print(" "); if (last3 < 100) print("and "); say_number(last3); } } else if (n < 1000000000) { say_number(n / 1000000); print(" million"); const int last6 = n % 1000000; if (last6 > 0) { print(" "); if (last6 < 100) print("and "); say_number(last6); } } } */ /* // and of course void say_number(const int n) // this function is guaranteed to work perfectly // for any positive int. // ints only go up to about 2000000000. { if (n < 20) print_0_to_19(n); else if (n < 100) print_20_to_99(n); else if (n < 1000) print_100_to_999(n); else if (n < 1000000) { say_number(n / 1000); print(" thousand"); const int last3 = n % 1000; if (last3 > 0) { print(" "); if (last3 < 100) print("and "); say_number(last3); } } else if (n < 1000000000) { say_number(n / 1000000); print(" million"); const int last6 = n % 1000000; if (last6 > 0) { print(" "); if (last6 < 100) print("and "); say_number(last6); } } else { say_number(n / 1000000000); print(" billion"); const int last9 = n % 1000000000; if (last9 > 0) { print(" "); if (last9 < 100) print("and "); say_number(last9); } } } */ // and finally void say_number(const int n) // this function is guaranteed to work perfectly // for any int. { if (n < 0) { print("negative "); say_number(- n); } else if (n < 20) print_0_to_19(n); else if (n < 100) print_20_to_99(n); else if (n < 1000) print_100_to_999(n); else if (n < 1000000) { say_number(n / 1000); print(" thousand"); const int last3 = n % 1000; if (last3 > 0) { print(" "); if (last3 < 100) print("and "); say_number(last3); } } else if (n < 1000000000) { say_number(n / 1000000); print(" million"); const int last6 = n % 1000000; if (last6 > 0) { print(" "); if (last6 < 100) print("and "); say_number(last6); } } else { say_number(n / 1000000000); print(" billion"); const int last9 = n % 1000000000; if (last9 > 0) { print(" "); if (last9 < 100) print("and "); say_number(last9); } } } void print_100_to_999(const int n) { const int hundreds = n / 100, remainder = n % 100; say_number(hundreds); print(" hundred"); if (remainder != 0) { print(" and "); say_number(remainder); } } void print_20_to_99(const int n) // example: if n is 47 // then n/10*10 is 40 // and n%10 is 7 { print_multiple_of_10(n/10*10); if (n % 10 != 0) { print(" "); print_0_to_19(n%10); } } void test(const int n) { print("testing "); print(n); print(": '"); say_number(n); print("'\n");; } void main() { test(27); test(138); test(509); test(999); test(12); test(4); test(40); while (true) { print("enter a test number (negative to stop): "); const int t = read_int(); if (t < 0) break; test(t); } print("Test over\n"); } void print_0_to_19(const int d) { if (d == 0) print("zero"); else if (d == 1) print("one"); else if (d == 2) print("two"); else if (d == 3) print("three"); else if (d == 4) print("four"); else if (d == 5) print("five"); else if (d == 6) print("six"); else if (d == 7) print("seven"); else if (d == 8) print("eight"); else if (d == 9) print("nine"); else if (d == 10) print("ten"); else if (d == 11) print("eleven"); else if (d == 12) print("twelve"); else if (d == 13) print("thirteen"); else if (d == 14) print("fourteen"); else if (d == 15) print("fifteen"); else if (d == 16) print("sixteen"); else if (d == 17) print("seventeen"); else if (d == 18) print("eighteen"); else if (d == 19) print("nineteen"); else print("\n**** ERROR ****\n"); } void print_multiple_of_10(const int d) { if (d == 10) print("ten"); else if (d == 20) print("twenty"); else if (d == 30) print("thirty"); else if (d == 40) print("forty"); else if (d == 50) print("fifty"); else if (d == 60) print("sixty"); else if (d == 70) print("seventy"); else if (d == 80) print("eighty"); else if (d == 90) print("ninety"); else print("\n**** ERROR ****\n"); }