1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
#include <cstdio>
#include <cstdlib>
#include <cstring>
void GetDigitText(int digit, char text[])
{
switch (digit)
{
case 1:
strcpy(text, "ONE");
break;
case 2:
strcpy(text, "TWO");
break;
case 3:
strcpy(text, "THREE");
break;
case 4:
strcpy(text, "FOUR");
break;
case 5:
strcpy(text, "FIVE");
break;
case 6:
strcpy(text, "SIX");
break;
case 7:
strcpy(text, "SEVEN");
break;
case 8:
strcpy(text, "EIGHT");
break;
case 9:
strcpy(text, "NINE");
break;
default:
strcpy(text, "");
}
}
void GetNumText(float num, char text[])
{
int dollars, cents;
char temp[10];
dollars = (int)num;
cents = int(num * 100) % 100;
int hundreds = dollars / 100;
GetDigitText(hundreds, text);
if (strlen(text) > 0)
strcat(text, " HUNDRED");
if (dollars % 100)
strcat(text, " AND");
int tens = (dollars % 100) / 10;
switch (tens)
{
case 9:
strcat(text, " NINETY");
break;
case 8:
strcat(text, " EIGHTY");
break;
case 7:
strcat(text, " SEVENTY");
break;
case 6:
strcat(text, " SIXTY");
break;
case 5:
strcat(text, " FIFTY");
break;
case 4:
strcat(text, " FORTY");
break;
case 3:
strcat(text, " THIRTY");
break;
case 2:
strcat(text, " TWENTY");
break;
}
int ones;
if (dollars % 100 < 20)
ones = dollars % 100;
else
ones = dollars % 10;
if (ones > 0)
{
if (tens > 1)
strcat(text, "-");
else
strcat(text, " ");
}
GetDigitText(ones, temp);
if (strlen(temp) > 0)
strcat(text, temp);
else
{
switch (ones)
{
case 10:
strcat(text, "TEN");
break;
case 11:
strcat(text, "ELEVEN");
break;
case 12:
strcat(text, "TWELVE");
break;
case 13:
strcat(text, "THIRTEEN");
break;
case 14:
strcat(text, "FOURTEEN");
break;
case 15:
strcat(text, "FIFTEEN");
break;
case 16:
strcat(text, "SIXTEEN");
break;
case 17:
strcat(text, "SEVENTEEN");
break;
case 18:
strcat(text, "EIGHTEEN");
break;
case 19:
strcat(text, "NINETEEN");
break;
}
}
itoa(cents, temp, 10);
strcat(text, " ");
strcat(text, temp);
strcat(text, "/100");
}
int main()
{
float num;
char text[100];
printf("Enter a number: ");
scanf("%f", &num);
while(num > 0)
{
GetNumText(num, text);
printf("%s\nEnter a number: ", text);
scanf("%f", &num);
}
return 0;
}
|