Алгоритм для взятия номера и вывода его английского слова

Я хочу сделать программу на C, которая попросит пользователя ввести номер, а затем напечатает этот номер на английском языке.

Например:

if(INPUT == 1) then print ONE if(INPUT == 2) then print TWO 

и так далее. Это может быть сделано с использованием коммутационного футляра, а если иное, но это делает код длинным. Для нескольких чисел это нормально, но если нам нужно написать до 100, то это будет длительным.

Есть ли короткий алгоритм или идея для этого?

Вы можете использовать ниже, но это печатает только до тысяч. Я сделал это, чтобы решить определенную проблему программирования. Вот почему я не выходил за пределы тысячи. Но его не трудно расширить для большего числа. Кроме того, эта программа может быть оптимизирована или сделана более понятной.

 #include  #include  void print(int num) { char digit [21][10] = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; char tens [11][10] = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; char str[1000] = {0}; int prev=0, div=1000; strcpy(str, ""); while(div) { if ((num / div) % 10 > 0 || (div == 10 && (num%100) > 0)) { if (prev) { strcat(str, "and"); prev = 0; } switch(div) { case 1000: strcat(str, digit[(num / div) % 10]); strcat(str, "thousand"); prev = 1; break; case 100: strcat(str, digit[(num / div) % 10]); strcat(str, "hundred"); prev = 1; break; case 10: if ( (num%100) >= 10 && (num%100) <= 19) strcat(str, digit[num%100]); else { strcat(str, tens[(num%100)/10]); strcat(str, digit[num%10]); } break; } } div /= 10; } printf("%d %s\n", num, str); } int main(int argc, char **argv) { long sum = 0; int count = 0; if (argc <= 1) { fprintf(stderr, "wrong number of arguments\n"); return -1; } print(atoi(argv[1])); return 0; } 

Вы можете использовать это, его можно использовать для преобразования до 99 целых чисел в слова. и это немного просто. посмотри:

 void main() { int n,m,j; clrscr(); printf("Enter any number between 1 to 99 : "); scanf("%d",&n); printf("You entered "); if(n>0&&n<=10) goto one; else if (n>10&&n<20) { m=n%10; goto two; } else if(n>20&&n<100) { j=n/10; n=n%10; goto three; } two: switch(m) { case 1:printf("eleven "); break; case 2:printf("twelve "); break; case 3:printf("thirteen "); break; case 4:printf("fourteen "); break; case 5:printf("fifteen "); break; case 6:printf("sixteen "); break; case 7:printf("seventeen "); break; case 8:printf("eighteen "); break; case 9:printf("nineteen "); break; } three: switch(j) { case 2:printf("twenty "); goto one; case 3:printf("thirty "); goto one; case 4:printf("fourty "); goto one; case 5:printf("fifty "); goto one; case 6:printf("sixty "); goto one; case 7:printf("seventy "); goto one; case 8:printf("eighty "); goto one; case 9:printf("ninety "); goto one; } one: switch(n) { case 1:printf("one "); break; case 2:printf("two "); break; case 3:printf("three "); break; case 4:printf("four "); break; case 5:printf("five "); break; case 6:printf("six "); break; case 7:printf("seven "); break; case 8:printf("eight "); break; case 9:printf("nine "); break; case 10:printf("ten "); break; } getch(); } 

Надеюсь это поможет.

Просто используйте рекурсию. У меня не хватает времени, чтобы проверить его, поэтому этот код может быть неисправен, но вы можете легко его расширить.

 public static void convertNum(int number) { String[] digit = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; String[] tens = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }; if (number > 0 && number < 20) System.out.print(digit[number]); else if (number / 1000000 > 0) { convertNum(number / 1000000); System.out.print(" million "); convertNum(number % 1000000); } else if (number / 100000 > 0) { convertNum(number / 100000); System.out.print(" lukh "); convertNum(number % 100000); } else if (number / 1000 > 0) { convertNum(number / 1000); System.out.print(" thousand "); convertNum(number % 1000); } else if (number / 100 > 0) { convertNum(number / 100); System.out.print(" hundred "); convertNum(number % 100); } else if (number / 10 >= 2) { System.out.print(" " + tens[number / 10] + " "); convertNum(number % 10); } } convertNum (9191197); 

У меня возникли проблемы с мыслью о хорошем способе автоматизировать это и все еще сделать это коротким. Если вы знаете конечную точку (т.е. хотите идти 1-100), вы можете сделать что-то вроде этого:

 char* numberArray[101] = {'Zero', 'One', 'Two' ... , 'One Hundred'}; 

И затем, когда вы получите ввод, просто используйте номер для доступа к этому индексу массива, и он выплюнет ваш ответ:

 int input; cin >> input; // input = 5 cout << numberArray[input]; // outputs: Five 

Я прошу прощения, если мой синтаксис ошибочен, я так давно делал PHP и javaScript. Я не помню синтаксиса C ...

То, что вам нужно, – это рекурсивная функция, которая называет себя после единиц, десятков, хундретов и тысяч цифр.

Напр.

 num_to_string(num = 344384) { if( haslakh()) num_to_string(3);print("lakh"); if( hasthou()) num_to_string(44);print("thousand"); if( hashundrer()) num_to_string(38);print("hundred"); num_to_string(4); if( num is from 1 to 9 ) print one..nine; if( num if from 10 to 90 ) print ten to ninty; } 

Я был бы удивлен, если бы человечество не реализовало его тысячами (извините: 1000).

Поэтому я проверил Github и нашел хотя бы несколько .

Я не внес ни одного из них.

Это то, что я написал, это очень легко расширяемое до любого размера. Я не очищал некоторые вещи, которые мог, но логика работает очень хорошо

import java.util.Arrays; import java.util.Scanner;

открытый class NumReader {

 static final String[] units = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; static final String[] tens = {"", null, "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; static final String[] teens = {"ten", "eleven", "twelve", "thrirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; static final String hundredSuffix = "hundred"; static final String[] suffixes = {"", "thousand", "million", "billion"}; static boolean isValid(int num) { return (num <= 1000000000 && num >= 0); } static String numToString(int inpNum) { return numToString(inpNum, String.valueOf(inpNum).toCharArray()); } static String numToString(int inpNum, char[] digits) { return numToString(inpNum, digits, false); } static String numToString(int inpNum, char[] digits, boolean firstCall) { StringBuilder b = new StringBuilder(); if (inpNum == 0 && firstCall) { return "zero"; } else if (inpNum < 10) { return units[inpNum]; } else if (inpNum < 20) { return teens[inpNum - 10]; } else if (inpNum < 100) { b.append(tens[digits[0] - '0']).append(" ").append(units[digits[1] - '0']); return b.toString(); } else if (digits.length == 3) { String sub = new String(Arrays.copyOfRange(digits, 1, 3)); b.append(units[digits[0] - '0']).append(" ") .append(hundredSuffix); sub = numToString(Integer.parseInt(sub), Arrays.copyOfRange(digits, 1, 3)); if (sub.equals("")) { b.append(sub); } else { b.append(" and ").append(sub); } return b.toString(); } else if (digits.length > 3) { int numSuffixes = digits.length / 3; int initCut = digits.length % 3; int i; String sub, opt = ""; for (i = 0; i < numSuffixes; i++) { int end = digits.length - 3 * i; sub = new String(Arrays.copyOfRange(digits, end - 3, end)); sub = numToString(Integer.parseInt(sub)); opt = (sub.equals("")) ? opt : (sub + " " + suffixes[i] + " " + opt); } if (initCut != 0) { sub = new String(Arrays.copyOfRange(digits, 0, initCut)); opt = numToString(Integer.parseInt(sub)) + " " + suffixes[i] + " " + opt; } return opt; } return ""; } public static void main(String[] args) { Scanner s = new Scanner(System.in); int num = s.nextInt(); if (isValid(num)) { System.out.println(numToString(num, String.valueOf(num).toCharArray(), true)); } else { System.out.println("Not a valid input, num <= 1000000000"); } } 

}

Я хочу улучшить ответы калян. Есть 2 ошибки:

  • Между словом нет места. Например: 100 получит результат одиннадцать (без пробела)
  • В английских сто и тысячах требуется множественная форма с s. Например, 200 -> два сотен (это нужно «в результате»)

Измените такой код, чтобы устранить эти ошибки:

 void print(int num) { char digit [21][10] = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; char tens [11][10] = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; char str[1000] = {0}; int prev=0, div=1000; strcpy(str, ""); while(div) { if ((num / div) % 10 > 0 || (div == 10 && (num%100) > 0)) { if (prev) { strcat(str, " and"); prev = 0; } switch(div) { case 1000: if (strlen(str) > 0 && str[strlen(str) - 1] != ' ') strcat(str, " "); strcat(str, digit[(num / div) % 10]); if (((num / div) % 10) > 1) strcat(str, " thousands"); else strcat(str, " thousand"); prev = 1; break; case 100: if (strlen(str) > 0 && str[strlen(str) - 1] != ' ') strcat(str, " "); strcat(str, digit[(num / div) % 10]); if (((num / div) % 10) > 1) strcat(str, " hundreds"); else strcat(str, " hundred"); prev = 1; break; case 10: if ( (num%100) >= 10 && (num%100) <= 19) { if (strlen(str) > 0 && str[strlen(str) - 1] != ' ') strcat(str, " "); strcat(str, digit[num%100]); } else { if (strlen(str) > 0 && str[strlen(str) - 1] != ' ') strcat(str, " "); strcat(str, tens[(num%100)/10]); if (strlen(str) > 0 && str[strlen(str) - 1] != ' ') strcat(str, " "); strcat(str, digit[num%10]); } break; } } div /= 10; } printf("%d %s\n", num, str); } 

Вот еще один способ. Не уверен в эффективности эффективности против памяти и скорости, но легко добавить код для обработки большего количества цифр.

 /* File : num_to_words_int.c * * Descr: Prints the equivalent number in words. '1' to 'one', etc. * This version takes the input and converts it to a numeric vs. * a string value. 345 vs. "345". Then uses modulus division to * count the number of digits. The count represents the places; * ie count=5 ==> 10,000 ,, 1,000 ,, 100 ,, 10 ,, 1 are the * words that will be needed. * * 300 => count=3 ==>three hundred * 30 => count=2 ==>thirtey * 3 => count=1 ==>three * 13 => count=2 ==>thirteen * 3456 => count=4 ==>three thousand four hundred fifty six * * [345], [34 5], [3 4], [3, [0] * * Debugging Option: *  num_to_words_int.exe number option_mask * * 001 - print init remainder array * 010 - print filled remainder array * 100 - print count, index, remainder value * * Author: Gene Bradshaw * Date: 09-16-2016 */ #include #include #include #include void main(int argc, char *argv[]) { const int HUNDREDS=0, THOUSANDS=1, MILLIONS=2, BILLIONS=3; int i, count, total, remainder[12]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; long number=0, orignum=0; char *ones[] = {"zero","one","two","three","four","five","six","seven","eight","nine"}; char *teens[] = {"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}; char *tens[] = {"ten","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}; char *places[] = {"hundred","thousand","million","billion"}; // place values; 10's, 100'2, etc. // ERRORS ARGUMENTS if(argc < 2) { printf("\na number is required as input!\n\n"); exit(1); } else if(argc > 3) { printf("\nonly one number and optionally a flag are required as input!\n\n"); exit(1); } else printf("\n"); // CONVERT TO WORDS if(!(number = atol(argv[1]))) // zero { printf("number: %d\n%s\n\n", number, ones[0]); exit(0); } // Debugging if(argv[2] && (atoi(argv[2]) & 0x01)) { for(i=11; i>-1; i--) printf("%d %d, ", i, remainder[i]); printf("\n\n"); } // GET DIGITS if(number < 0) // Remeber if negative, then make positive { orignum = number; number *= -1; } count=0; do{ remainder[count++] = number%10; number/=10; }while(number); // int type var converts to '0' when # < 0 // ERROR DIGIT COUNT if (count > 12) { printf("\ntoo many digits; up to 12 digits supported!\n\n"); exit(1); } // Debugging if(argv[2] && (atoi(argv[2]) & 0x02)) { for(i=11; i>-1 ; i--) printf("%d %d, ", i, remainder[i]); printf("\n\n"); } // DISPLAY REMAINDERS printf("number: "); // This if for displaying the reverse remainder[]. if (orignum < 0) printf("-"); for(i=count-1; i>-1; i--) { if(!(i%3) && i) printf("%d,", remainder[i]); else printf("%d", remainder[i]); } printf("\n"); // FIND AND PRINT WORDS total = count; i = count-1; // counting rules if(orignum < 0) printf("negative "); while(count) { if(argv[2] && (atoi(argv[2]) & 0x04)) // Debugging printf("\nC: %d, i: %d and R: %d\n", count, i, remainder[i]); switch(count) { case 1: // print if not teens or 0 if(remainder[i+1] != 1 && remainder[i]) printf("%s ", ones[remainder[i]]); break; case 2: // teens when 2nd digit is a '1' if(remainder[i] == 1) printf("%s ", teens[remainder[i-1]]); // ones when 1st digit is not a '0' else if(remainder[i]) printf("%s ", tens[remainder[i]]); break; case 3: // d if(remainder[i]){ printf("%s ", ones[remainder[i]]); printf("%s ", places[count-3]); } break; case 4: // k if(remainder[i]) printf("%s ", ones[remainder[i]]); if(remainder[i] || (total > 4 && total < 7)) printf("%s ", places[count-3]); break; // 10,000 100,000 1,000,000 // ten tho hun tho one million case 5: // 10 k case 8: // 10 M case 11: // 10 B if(remainder[i]){ printf("%s ", tens[remainder[i]]); } break; case 6: // 100 k case 9: // 100 M case 12: // 100 B if(remainder[i]){ printf("%s ", ones[remainder[i]]); printf("%s ", places[HUNDREDS]); } break; case 7: // M if(remainder[i]) printf("%s ", ones[remainder[i]]); if(remainder[i] || (total > 7 && total < 10)) printf("%s ", places[MILLIONS]); break; case 10: // B if(remainder[i]) printf("%s ", ones[remainder[i]]); if(remainder[i] || (total > 10 && total < 13)) printf("%s ", places[BILLIONS]); break; // Add cases to increase digit count supported //case 13: //T /*- add code here -*/ break; default: break; } count--; i--; } printf("\n\n"); } в /* File : num_to_words_int.c * * Descr: Prints the equivalent number in words. '1' to 'one', etc. * This version takes the input and converts it to a numeric vs. * a string value. 345 vs. "345". Then uses modulus division to * count the number of digits. The count represents the places; * ie count=5 ==> 10,000 ,, 1,000 ,, 100 ,, 10 ,, 1 are the * words that will be needed. * * 300 => count=3 ==>three hundred * 30 => count=2 ==>thirtey * 3 => count=1 ==>three * 13 => count=2 ==>thirteen * 3456 => count=4 ==>three thousand four hundred fifty six * * [345], [34 5], [3 4], [3, [0] * * Debugging Option: *  num_to_words_int.exe number option_mask * * 001 - print init remainder array * 010 - print filled remainder array * 100 - print count, index, remainder value * * Author: Gene Bradshaw * Date: 09-16-2016 */ #include #include #include #include void main(int argc, char *argv[]) { const int HUNDREDS=0, THOUSANDS=1, MILLIONS=2, BILLIONS=3; int i, count, total, remainder[12]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; long number=0, orignum=0; char *ones[] = {"zero","one","two","three","four","five","six","seven","eight","nine"}; char *teens[] = {"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}; char *tens[] = {"ten","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}; char *places[] = {"hundred","thousand","million","billion"}; // place values; 10's, 100'2, etc. // ERRORS ARGUMENTS if(argc < 2) { printf("\na number is required as input!\n\n"); exit(1); } else if(argc > 3) { printf("\nonly one number and optionally a flag are required as input!\n\n"); exit(1); } else printf("\n"); // CONVERT TO WORDS if(!(number = atol(argv[1]))) // zero { printf("number: %d\n%s\n\n", number, ones[0]); exit(0); } // Debugging if(argv[2] && (atoi(argv[2]) & 0x01)) { for(i=11; i>-1; i--) printf("%d %d, ", i, remainder[i]); printf("\n\n"); } // GET DIGITS if(number < 0) // Remeber if negative, then make positive { orignum = number; number *= -1; } count=0; do{ remainder[count++] = number%10; number/=10; }while(number); // int type var converts to '0' when # < 0 // ERROR DIGIT COUNT if (count > 12) { printf("\ntoo many digits; up to 12 digits supported!\n\n"); exit(1); } // Debugging if(argv[2] && (atoi(argv[2]) & 0x02)) { for(i=11; i>-1 ; i--) printf("%d %d, ", i, remainder[i]); printf("\n\n"); } // DISPLAY REMAINDERS printf("number: "); // This if for displaying the reverse remainder[]. if (orignum < 0) printf("-"); for(i=count-1; i>-1; i--) { if(!(i%3) && i) printf("%d,", remainder[i]); else printf("%d", remainder[i]); } printf("\n"); // FIND AND PRINT WORDS total = count; i = count-1; // counting rules if(orignum < 0) printf("negative "); while(count) { if(argv[2] && (atoi(argv[2]) & 0x04)) // Debugging printf("\nC: %d, i: %d and R: %d\n", count, i, remainder[i]); switch(count) { case 1: // print if not teens or 0 if(remainder[i+1] != 1 && remainder[i]) printf("%s ", ones[remainder[i]]); break; case 2: // teens when 2nd digit is a '1' if(remainder[i] == 1) printf("%s ", teens[remainder[i-1]]); // ones when 1st digit is not a '0' else if(remainder[i]) printf("%s ", tens[remainder[i]]); break; case 3: // d if(remainder[i]){ printf("%s ", ones[remainder[i]]); printf("%s ", places[count-3]); } break; case 4: // k if(remainder[i]) printf("%s ", ones[remainder[i]]); if(remainder[i] || (total > 4 && total < 7)) printf("%s ", places[count-3]); break; // 10,000 100,000 1,000,000 // ten tho hun tho one million case 5: // 10 k case 8: // 10 M case 11: // 10 B if(remainder[i]){ printf("%s ", tens[remainder[i]]); } break; case 6: // 100 k case 9: // 100 M case 12: // 100 B if(remainder[i]){ printf("%s ", ones[remainder[i]]); printf("%s ", places[HUNDREDS]); } break; case 7: // M if(remainder[i]) printf("%s ", ones[remainder[i]]); if(remainder[i] || (total > 7 && total < 10)) printf("%s ", places[MILLIONS]); break; case 10: // B if(remainder[i]) printf("%s ", ones[remainder[i]]); if(remainder[i] || (total > 10 && total < 13)) printf("%s ", places[BILLIONS]); break; // Add cases to increase digit count supported //case 13: //T /*- add code here -*/ break; default: break; } count--; i--; } printf("\n\n"); } в /* File : num_to_words_int.c * * Descr: Prints the equivalent number in words. '1' to 'one', etc. * This version takes the input and converts it to a numeric vs. * a string value. 345 vs. "345". Then uses modulus division to * count the number of digits. The count represents the places; * ie count=5 ==> 10,000 ,, 1,000 ,, 100 ,, 10 ,, 1 are the * words that will be needed. * * 300 => count=3 ==>three hundred * 30 => count=2 ==>thirtey * 3 => count=1 ==>three * 13 => count=2 ==>thirteen * 3456 => count=4 ==>three thousand four hundred fifty six * * [345], [34 5], [3 4], [3, [0] * * Debugging Option: *  num_to_words_int.exe number option_mask * * 001 - print init remainder array * 010 - print filled remainder array * 100 - print count, index, remainder value * * Author: Gene Bradshaw * Date: 09-16-2016 */ #include #include #include #include void main(int argc, char *argv[]) { const int HUNDREDS=0, THOUSANDS=1, MILLIONS=2, BILLIONS=3; int i, count, total, remainder[12]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; long number=0, orignum=0; char *ones[] = {"zero","one","two","three","four","five","six","seven","eight","nine"}; char *teens[] = {"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}; char *tens[] = {"ten","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}; char *places[] = {"hundred","thousand","million","billion"}; // place values; 10's, 100'2, etc. // ERRORS ARGUMENTS if(argc < 2) { printf("\na number is required as input!\n\n"); exit(1); } else if(argc > 3) { printf("\nonly one number and optionally a flag are required as input!\n\n"); exit(1); } else printf("\n"); // CONVERT TO WORDS if(!(number = atol(argv[1]))) // zero { printf("number: %d\n%s\n\n", number, ones[0]); exit(0); } // Debugging if(argv[2] && (atoi(argv[2]) & 0x01)) { for(i=11; i>-1; i--) printf("%d %d, ", i, remainder[i]); printf("\n\n"); } // GET DIGITS if(number < 0) // Remeber if negative, then make positive { orignum = number; number *= -1; } count=0; do{ remainder[count++] = number%10; number/=10; }while(number); // int type var converts to '0' when # < 0 // ERROR DIGIT COUNT if (count > 12) { printf("\ntoo many digits; up to 12 digits supported!\n\n"); exit(1); } // Debugging if(argv[2] && (atoi(argv[2]) & 0x02)) { for(i=11; i>-1 ; i--) printf("%d %d, ", i, remainder[i]); printf("\n\n"); } // DISPLAY REMAINDERS printf("number: "); // This if for displaying the reverse remainder[]. if (orignum < 0) printf("-"); for(i=count-1; i>-1; i--) { if(!(i%3) && i) printf("%d,", remainder[i]); else printf("%d", remainder[i]); } printf("\n"); // FIND AND PRINT WORDS total = count; i = count-1; // counting rules if(orignum < 0) printf("negative "); while(count) { if(argv[2] && (atoi(argv[2]) & 0x04)) // Debugging printf("\nC: %d, i: %d and R: %d\n", count, i, remainder[i]); switch(count) { case 1: // print if not teens or 0 if(remainder[i+1] != 1 && remainder[i]) printf("%s ", ones[remainder[i]]); break; case 2: // teens when 2nd digit is a '1' if(remainder[i] == 1) printf("%s ", teens[remainder[i-1]]); // ones when 1st digit is not a '0' else if(remainder[i]) printf("%s ", tens[remainder[i]]); break; case 3: // d if(remainder[i]){ printf("%s ", ones[remainder[i]]); printf("%s ", places[count-3]); } break; case 4: // k if(remainder[i]) printf("%s ", ones[remainder[i]]); if(remainder[i] || (total > 4 && total < 7)) printf("%s ", places[count-3]); break; // 10,000 100,000 1,000,000 // ten tho hun tho one million case 5: // 10 k case 8: // 10 M case 11: // 10 B if(remainder[i]){ printf("%s ", tens[remainder[i]]); } break; case 6: // 100 k case 9: // 100 M case 12: // 100 B if(remainder[i]){ printf("%s ", ones[remainder[i]]); printf("%s ", places[HUNDREDS]); } break; case 7: // M if(remainder[i]) printf("%s ", ones[remainder[i]]); if(remainder[i] || (total > 7 && total < 10)) printf("%s ", places[MILLIONS]); break; case 10: // B if(remainder[i]) printf("%s ", ones[remainder[i]]); if(remainder[i] || (total > 10 && total < 13)) printf("%s ", places[BILLIONS]); break; // Add cases to increase digit count supported //case 13: //T /*- add code here -*/ break; default: break; } count--; i--; } printf("\n\n"); } 

Примеры:

  $>./num_to_words.exe -1000000 $>number: -1,000,000 $>negative one million $>./num_to_words.exe 123456789011 $>number: 123,456,789,011 $>one hundred twenty three billion four hundred fifty six million seven hundred eighty nine thousand eleven $>./num_to_words.exe 123456789012 $>number: 123,456,789,012 $>one hundred twenty three billion four hundred fifty six million seven hundred eighty nine thousand twelve $>./num_to_words.exe -123456789012 $>number: -123,456,789,012 $>negative one hundred twenty three billion four hundred fifty six million seven hundred eighty nine thousand twelve $>./num_to_words.exe 0 $>number: 0 $>zero $>./num_to_words.exe 1 $>number: 1 $>one