fork(1) download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4.  
  5. const char* units[] = {"", "один", "два", "три", "четыре", "пять", "шесть", "семь", "восемь", "девять",
  6. "десять", "одиннадцать", "двенадцать", "тринадцать", "четырнадцать", "пятнадцать",
  7. "шестнадцать", "семнадцать", "восемнадцать", "девятнадцать"};
  8. const char* tens[] = {"", "", "двадцать", "тридцать", "сорок", "пятьдесят", "шестьдесят", "семьдесят",
  9. "восемьдесят", "девяносто"};
  10. const char* hundreds[] = {"", "сто", "двести", "триста", "четыреста", "пятьсот", "шестьсот", "семьсот",
  11. "восемьсот", "девятьсот"};
  12.  
  13. void get_rubles_form(int n, char* result) {
  14. if (n % 100 >= 11 && n % 100 <= 19) strcpy(result, "рублей ");
  15. else switch(n % 10) {
  16. case 1: strcpy(result, "рубль "); break;
  17. case 2: case 3: case 4: strcpy(result, "рубля "); break;
  18. default: strcpy(result, "рублей ");
  19. }
  20. }
  21.  
  22. void convert_three_digits(int num, int is_thousands, char* result) {
  23. result[0] = '\0';
  24. if (num == 0) return;
  25.  
  26. int hundred = num / 100;
  27. int rem = num % 100;
  28.  
  29. if (hundred > 0) {
  30. strcat(result, hundreds[hundred]);
  31. strcat(result, " ");
  32. }
  33. if (rem < 20) {
  34. if (rem > 0) {
  35. const char* word = units[rem];
  36. if (is_thousands) {
  37. if (rem == 1) word = "одна";
  38. else if (rem == 2) word = "две";
  39. }
  40. strcat(result, word);
  41. strcat(result, " ");
  42. }
  43. } else {
  44. strcat(result, tens[rem / 10]);
  45. strcat(result, " ");
  46. int unit = rem % 10;
  47. if (unit > 0) {
  48. const char* word = units[unit];
  49. if (is_thousands && unit == 1) word = "одна";
  50. else if (is_thousands && unit == 2) word = "две";
  51. strcat(result, word);
  52. strcat(result, " ");
  53. }
  54. }
  55. }
  56.  
  57. void number_to_words(double amount, char* output) {
  58. output[0] = '\0';
  59. long rubles = (long)amount;
  60. int kopecks = (int)round((amount - rubles) * 100);
  61.  
  62. if (rubles == 0) strcat(output, "ноль рублей ");
  63. else {
  64. char part[100];
  65. int millions = rubles / 1000000;
  66. rubles %= 1000000;
  67. int thousands = rubles / 1000;
  68. rubles %= 1000;
  69.  
  70. if (millions > 0) {
  71. convert_three_digits(millions, 0, part);
  72. strcat(output, part);
  73. if (millions % 100 >= 11 && millions % 100 <= 14) strcat(output, "миллионов ");
  74. else switch(millions % 10) {
  75. case 1: strcat(output, "миллион "); break;
  76. case 2: case 3: case 4: strcat(output, "миллиона "); break;
  77. default: strcat(output, "миллионов ");
  78. }
  79. }
  80. if (thousands > 0) {
  81. convert_three_digits(thousands, 1, part);
  82. strcat(output, part);
  83. if (thousands % 100 >= 11 && thousands % 100 <= 14) strcat(output, "тысяч ");
  84. else switch(thousands % 10) {
  85. case 1: strcat(output, "тысяча "); break;
  86. case 2: case 3: case 4: strcat(output, "тысячи "); break;
  87. default: strcat(output, "тысяч ");
  88. }
  89. }
  90. if (rubles > 0) {
  91. convert_three_digits(rubles, 0, part);
  92. strcat(output, part);
  93. char rub_form[20];
  94. get_rubles_form(rubles, rub_form);
  95. strcat(output, rub_form);
  96. }
  97. }
  98.  
  99. char kop_str[3];
  100. sprintf(kop_str, "%02d", kopecks);
  101. strcat(output, kop_str);
  102. strcat(output, " копеек.");
  103.  
  104. // Удаление двойных пробелов
  105. char* pos;
  106. while ((pos = strstr(output, " ")) != NULL)
  107. memmove(pos, pos + 1, strlen(pos));
  108. }
  109.  
  110. int main() {
  111. double amount;
  112. printf("Введите сумму: ");
  113. scanf("%lf", &amount);
  114. char result[500];
  115. number_to_words(amount, result);
  116. printf("%s\n", result);
  117. return 0;
  118. }
  119.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Введите сумму: ноль рублей 00 копеек.