Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Numerical AnalysisRSS Feeds

Program to translate integer numbers given in words into numeric representation

Posted By: Easy Tutor     Category: C++ Programming     Views: 11934

In this problem, you will be given one or more integers in English. Your task is to translate these numbers into their integer representation. The numbers can range from negative 999,999,999 to positive 999,999,999. The following is an exhaustive list of English words that your program must account for:
negative, zero, one, two, three, four, five, six, seven, eight, nine, ten,
eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen,
twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety, hundred,
thousand, million

Input & Output

1- Negative numbers will be preceded by the word negative.
2- The word ``hundred'' is not used when ``thousand'' could be. For example, 1500
is written ``one thousand five hundred'', not ``fifteen hundred''.

The answers are expected to be on separate lines with a newline after each.

Code for Program to translate integer numbers given in words into numeric representation in C++ Programming

 # include <iostream.h>
 # include <fstream.h>
 # include <string.h>
 # include <stdlib.h>
 # include <conio.h>


 const unsigned long translate_string(constchar*);
 const unsigned long get_numeric_value(constchar*);


 int main( )
    {
       clrscr( );

       fstream File("CP-20.txt",ios::in|ios::nocreate);

       if(!File)
      {
         cout<<"\n Unable to open the input file."<<endl;
         cout<<"\n Press any key to exit.";

         getch( );
         exit(EXIT_FAILURE);
      }

       char Data[200]={NULL};

       unsigned long number=0;

       do
      {
         strset(Data,NULL);

         File.getline(Data,190);

         if(strcmp(Data,NULL)==0)
        break;

         number=0;

         char* Ptr=NULL;

         Ptr=strstr(Data,"negative");

         if(Ptr!=NULL)
        {
           cout<<"-";

           char Temp[150]={NULL};

           for(int count_1=0,count_2=9;
                  count_2<strlen(Data);count_1++,count_2++)
              Temp[count_1]=Data[count_2];

           strset(Data,NULL);
           strcpy(Data,Temp);
        }

         Ptr=strstr(Data," million");

         if(Ptr!=NULL)
        {
           char Temp[200]={NULL};

           int n=(strlen(Data)-strlen(Ptr));

           strncpy(Temp,Data,n);

           number+=(1000000*translate_string(Temp));

           strset(Temp,NULL);

           for(int count_3=0,count_4=(n+9);
                  count_4<strlen(Data);count_3++,count_4++)
              Temp[count_3]=Data[count_4];

           strset(Data,NULL);
           strcpy(Data,Temp);
        }

         Ptr=strstr(Data," thousand");

         if(Ptr!=NULL)
        {
           char Temp[200]={NULL};

           int n=(strlen(Data)-strlen(Ptr));

           strncpy(Temp,Data,n);

           number+=(1000*translate_string(Temp));

           strset(Temp,NULL);

           for(int count_5=0,count_6=(n+10);
                  count_6<strlen(Data);count_5++,count_6++)
              Temp[count_5]=Data[count_6];

           strset(Data,NULL);
           strcpy(Data,Temp);
        }

         number+=translate_string(Data);

         cout<<number<<endl;
      }
       while(1);

       File.close( );

       getch( );
       return 0;
    }


 /*************************************************************************///------------------------  translate_string( )  ------------------------///*************************************************************************/const unsigned long translate_string(constchar* Number)
    {
       unsigned longvalue=0;

       char String[100]={NULL};

       strcpy(String,Number);

       char* Ptr=NULL;

       Ptr=strstr(Number," hundred");

       if(Ptr!=NULL)
      {
         char Temp[100]={NULL};

         int n=(strlen(Number)-strlen(Ptr));

         strncpy(Temp,Number,n);

         value=(100*get_numeric_value(Temp));

         strset(String,NULL);

         for(int count_1=0,count_2=(n+9);
                  count_2<strlen(Number);count_1++,count_2++)
        String[count_1]=Number[count_2];
      }

       Ptr=strtok(String," ");

       value+=get_numeric_value(Ptr);

       Ptr=strtok(NULL,"\n");

       if(Ptr!=NULL)
      value+=get_numeric_value(Ptr);

       returnvalue;
    }

 /*************************************************************************///------------------------  get_numeric_value( )  -----------------------///*************************************************************************/const unsigned long get_numeric_value(constchar* Number)
    {
       unsigned longvalue=0;

       if(strcmp(Number,"million")==0)
      value=1000000;

       elseif(strcmp(Number,"thousand")==0)
      value=1000;

       elseif(strcmp(Number,"hundred")==0)
      value=100;

       elseif(strcmp(Number,"ninety")==0)
      value=90;

       elseif(strcmp(Number,"eighty")==0)
      value=80;

       elseif(strcmp(Number,"seventy")==0)
      value=70;

       elseif(strcmp(Number,"sixty")==0)
      value=60;

       elseif(strcmp(Number,"fifty")==0)
      value=50;

       elseif(strcmp(Number,"fourty")==0)
      value=40;

       elseif(strcmp(Number,"thirty")==0)
      value=30;

       elseif(strcmp(Number,"twenty")==0)
      value=20;

       elseif(strcmp(Number,"nineteen")==0)
      value=19;

       elseif(strcmp(Number,"eighteen")==0)
      value=18;

       elseif(strcmp(Number,"seventeen")==0)
      value=17;

       elseif(strcmp(Number,"sixteen")==0)
      value=16;

       elseif(strcmp(Number,"fifteen")==0)
      value=15;

       elseif(strcmp(Number,"fourteen")==0)
      value=14;

       elseif(strcmp(Number,"thirteen")==0)
      value=13;

       elseif(strcmp(Number,"twelve")==0)
      value=12;

       elseif(strcmp(Number,"eleven")==0)
      value=11;

       elseif(strcmp(Number,"ten")==0)
      value=10;

       elseif(strcmp(Number,"nine")==0)
      value=9;

       elseif(strcmp(Number,"eight")==0)
      value=8;

       elseif(strcmp(Number,"seven")==0)
      value=7;

       elseif(strcmp(Number,"six")==0)
      value=6;

       elseif(strcmp(Number,"five")==0)
      value=5;

       elseif(strcmp(Number,"four")==0)
      value=4;

       elseif(strcmp(Number,"three")==0)
      value=3;

       elseif(strcmp(Number,"two")==0)
      value=2;

       elseif(strcmp(Number,"one")==0)
      value=1;

       elseif(strcmp(Number,"zero")==0)
      value=0;

       returnvalue;
    }
  
Share: 



Easy Tutor
Easy Tutor author of Program to translate integer numbers given in words into numeric representation is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!