C++ Program to convert any numeric entry into Words
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream.h>
char * itoenglish(long n, int st);
void doit(long n, int st);
/*GETS THE NUMBER THAT IS TO BE CHANGED TO NUMBERS*/
void main()
{
long num = 0;
clrscr();
while( num >= 0 )
{
cout<<"Enter any number ( a negative number to quit )"<<endl;
cin>>num;
doit( num, 0 );
};
}
/*TURNS THE DIGIT TO WORDS AND ADDS THE VALUE TO IT SUCH AS HUNDRED, THOUSAND*/
void doit(long n, int st)
{
long z, showed;
char str[100], out[100];
if (n != 0)
{
strcpy(out, "");
showed = 0;
if ((n / 1000) != 0)
{
z = n / 1000;
n = n - z * 1000;
sprintf(str, "%s thousand ", itoenglish(z, 0));
strcat(out, str);
showed = 1000;
}
if ((n / 100) != 0)
{
z = n / 100;
n = n - z * 100;
sprintf(str, "%s hundred ", itoenglish(z, 0));
strcat(out, str);
showed = 100;
}
if (n != 0)
{
z = n;
if (showed == 100) strcat(out, "and");
sprintf(str, "%s", itoenglish(z, st));
strcat(out, str);
showed = 10;
}
printf("%s", out);
}
else if ( n == 0 )
printf("zero");
printf("\n");
}
/*CHANGES THE DIGITS TO WORDS*/
char * itoenglish(long n, int st)
{
char str[100];
char th[2][3];
strcpy(th[0], "");
strcpy(th[1], "TH");
if (n == 0)
strcpy(str, th[st]);
else if (n == 1)
{
if (st == 0)
strcpy(str, " one");
}
else if (n == 2)
{
if (st == 0)
strcpy(str, " two");
}
else if (n == 3)
{
if (st == 0)
strcpy(str, " three");
}
else if (n == 4)
sprintf(str, " four%s", th[st]);
else if (n == 5)
{
if (st == 0)
strcpy(str, " five");
}
else if (n == 6)
sprintf(str, " six%s", th[st]);
else if (n == 7)
sprintf(str, " seven%s", th[st]);
else if (n == 8)
sprintf(str, " eight%s", th[st]);
else if (n == 9)
sprintf(str, " nine%s", th[st]);
else if (n == 10)
sprintf(str, " ten%s", th[st]);
else if (n == 11)
sprintf(str, " eleven%s", th[st]);
else if (n == 12)
sprintf(str, " twelve%s", th[st]);
else if (n == 13)
sprintf(str, " thirteen%s", th[st]);
else if (n == 14)
sprintf(str, " fourteen%s", th[st]);
else if (n == 15)
sprintf(str, " fifteen%s", th[st]);
else if (n >= 16 && n <= 17)
sprintf(str, " %steen%s", itoenglish(n - 10, 0), th[st]);
else if (n == 18)
sprintf(str, " eighteen%s", th[st]);
else if (n == 19)
sprintf(str, " nineteen%s", th[st]);
else if (n >= 20 && n <= 29)
sprintf(str, " twenty%s", itoenglish(n - 20, st));
else if (n >= 30 && n <= 39)
sprintf(str, " thirty%s", itoenglish(n - 30, st));
else if (n >= 40 && n <= 49)
sprintf(str, " fourty%s", itoenglish(n - 40, st));
else if (n >= 50 && n <= 59)
sprintf(str, " fifty%s", itoenglish(n - 50, st));
else if (n >= 60 && n <= 79)
sprintf(str, " %sty%s", itoenglish(n / 10, 0),
itoenglish(n - (n / 10) * 10, st));
else if (n >= 80 && n <= 89)
sprintf(str, " eighty%s", itoenglish(n - 80, st));
else if (n >= 90 && n <= 99)
sprintf(str, " %sty%s", itoenglish(n / 10, 0),
itoenglish(n - (n / 10) * 10, st));
else
strcpy(str, "");
if (strstr(str, "tTH") != NULL)
strcpy(strstr(str, "tTH"), "TH");
return(strdup(str));
}
0 comments:
Post a Comment