+92 332 4229 857 99ProjectIdeas@Gmail.com

Money Conversion-digits into words (Vb.net)


// MoneyConversion.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "math.h"

using namespace std;

int ConvertToDecimal(int num)
{
            if(num==48)
                        return 0;
            if(num==49)
                        return 1;
            if(num==50)
                        return 2;
            if(num==51)
                        return 3;
            if(num==52)
                        return 4;
            if(num==53)
                        return 5;
            if(num==54)
                        return 6;
            if(num==55)
                        return 7;
            if(num==56)
                        return 8;
            if(num==57)
                        return 9;
return -1;
}
class numToWords
{
private:
public:
            char words[50];
            int num;
            numToWords();
            void convertToWords(char*);
};
numToWords objArr[7];

numToWords::numToWords()
{
            strcpy(objArr[0].words,"");
            strcpy(objArr[1].words,"hundred");
            strcpy(objArr[2].words,"thousand");
            strcpy(objArr[3].words,"lac");
            strcpy(objArr[4].words,"crore");
            strcpy(objArr[5].words,"arab");
            strcpy(objArr[6].words,"kharab");
}
void numToWords::convertToWords(char*moneyInRs)
{
            int moneyLen=strlen(moneyInRs);
            int rem=0;
            int sum=0,i=0,j=0,l=0;
            int arr[8];
            int temp[10];
           
            for(rem=moneyLen;rem>0;)
            {
                        if(sum>=11)
                        {
                                    j++;
                                    rem--;
                        }
                        else if(sum<11)
                        {
                                    if(i!=1)
                                    {
                                                if(rem<2)
                                                            arr[i++]=rem;
                                                else if(rem>=2)
                                                            arr[i++]=2;
                                               
                                                rem-=2;
                                                sum+=2;
                                    }
                                    else if(i==1)
                                    {
                                                arr[i++]=1;
                                                rem--;
                                                sum++;
                                    }
                        }
            }

            if(sum>=11&j!=0)
                        arr[i++]=j;
           
                        moneyLen--;

                        for(int X=0;X<i;X++)
                        {
                                    if(X!=1&X<6)
                                    {
                                                int a=0;
                                                for(int j=0;j<arr[X];j++)
                                                {
                                                   temp[a++]=ConvertToDecimal(moneyInRs[moneyLen--]);
                                                }
                                                if(a==1)
                                                            objArr[l++].num=temp[0];
                                                if(a==2)
                                                            objArr[l++].num=(temp[1]*10)+temp[0];
                                    }
                                    else if(X==1)
                                    {
                                      objArr[l++].num=ConvertToDecimal(moneyInRs[moneyLen--]);
                                    }
                                    else if(X==6)
                                    {
                                      int a=0;
                                      for(int j=0;j<arr[X];j++)
                                      {
                                         temp[a++]=ConvertToDecimal(moneyInRs[moneyLen--]);
                                      }
                                                int sum=0;
                                                int maxPow=a-1;
                                                   for(int y=a-1;y>=0;y--)
                                                   {
                                                       sum+=temp[y]*pow(10.0,maxPow--);
                                                   }
                                                objArr[l++].num=sum;
                                    }
                        }
                       
                        for(int i=l-1;i>-1;i--)
                                    cout<<objArr[i].num<<" "<<objArr[i].words<<" ";
}

int main()
{
            numToWords obj;
            obj.convertToWords("1");cout<<endl;
            obj.convertToWords("12");cout<<endl;
            obj.convertToWords("123");cout<<endl;
            obj.convertToWords("1234");cout<<endl;
            obj.convertToWords("12345");cout<<endl;
            obj.convertToWords("123456");cout<<endl;
            obj.convertToWords("1234567");cout<<endl;
            obj.convertToWords("12345678");cout<<endl;
            obj.convertToWords("123456789");cout<<endl;
            obj.convertToWords("1234567891");cout<<endl;
            obj.convertToWords("12345678912");cout<<endl;
            obj.convertToWords("123456789123");cout<<endl;
            obj.convertToWords("1234567891234");cout<<endl;
            obj.convertToWords("12345678912345");cout<<endl;

            obj.convertToWords("1319872109");cout<<endl;

            _getche();
            return 0;
}

0 comments: