Showing posts with label Operator Overloading. Show all posts
Showing posts with label Operator Overloading. Show all posts

Operator Overloading -1 (Digit to Word) - C++

Program:
#include<iostream>
#include<string.h>
using namespace std;
class num
{
public:
    char str[50],temp[50];
    void cnvrt(int t)
    {   
    switch(t){   
        case 0:
            strcpy(str,"zero");
            break;
        case 1:
            strcpy(str,"one");
            break;
        case 2:
            strcpy(str,"two");
            break;
        case 3:
            strcpy(str,"three");
            break;
        case 4:
            strcpy(str,"four");
            break;
        case 5:
            strcpy(str,"five");
            break;
        case 6:
            strcpy(str,"six");
            break;
        case 7:
            strcpy(str,"seven");
            break;
        case 8:
            strcpy(str,"eight");
            break;
        case 9:
            strcpy(str,"nine");
            break;
            }
                         
    }
    num operator+(num stin)
    {
        num temp;
        strcpy(temp.str,str);
        strcat(temp.str,stin.str);
        return temp;
    }
};
int main()
{   
    num a[15],op;
    int i=0,j,k=0,l,n,r,ar[15];
    cout<<"Enter the number: ";
    cin>>n;
    while(n!=0)
    {
        r=n%10;
        ar[k++]=r;
        i++;
        n=n/10;
    }
    for(j=0,l=(k-1);j<i,l>=0;j++,l--)
    {
        a[j].cnvrt(ar[l]);
    }
    strcpy(op.str," ");
    for(j=0;j<i;j++)
    {
        op=op+a[j];
    }
    cout<<"\n"<<op.str<<endl;
    return 1;
}

Output:
nn@ubuntu:~$ g++ n2.cpp
nn@ubuntu:~$ ./a.out
Enter the number: 1234567890

 onetwothreefourfivesixseveneightninezero
nn@ubuntu:~$

Operator Overloading -2 - (Digit to Word) - C++

 Program:
#include<iostream>
#include<string.h>
using namespace std;
class strin
{
    char str[50];
public:
    strin()
    {
        strcpy(str," ");
    }
    strin(char* mystr)
    {
        strcpy(str,mystr);
    }
    void operator+(strin s)
    {
        strcpy(str,s.str);
        cout<<str;
    }
};

main()
{
    strin str1,str2;
    int n,n1,dg,y=0,lastzero=0;
    cout<<"\t\tOPERATOR OVERLOADING -II (12 --> ONE TWO)\nEnter the number: ";
    cin>>n;n1=n;
    while(n>0)
    {
        dg=n%10;
       
        y=y*10+dg;
        n=n/10;
    }
    while(n1%10==0)
    {
        lastzero++;
        n1/=10;
        if(lastzero>20)
            {
                lastzero=1;
                break;
            }
    }
   
    while(y>0)
    {
        dg=y%10;
        switch(dg)
        {
            case 1: str2=(char*)" one";
                str1 + str2;
                break;
            case 2 :str2=(char*)" two";
                str1 + str2;
                break;
            case 3: str2=(char*)" three";
                str1 + str2;
                break;
            case 4 :str2=(char*)" four";
                str1 + str2;
                break;
            case 5: str2=(char*)" five";
                str1 + str2;
                break;
            case 6 :str2=(char*)" six";
                str1 + str2;
                break;
            case 7: str2=(char*)" seven";
                str1 + str2;
                break;
            case 8 :str2=(char*)" eight";
                str1 + str2;
                break;
            case 9: str2=(char*)" nine";
                str1 + str2;
                break;
            case 0 :str2=(char*)" zero";
            str1 + str2;
                break;
            default:
                cout<<"\n\nError.......exiting...";
                break;break;
        }
    y=y/10;
    }
    while(lastzero!=0)
    {
        cout<<" zero";
        lastzero--;
    }
    cout<<endl;
}


Output:
nn@ubuntu:~$ g++ op.cpp
nn@ubuntu:~$ ./a.out
        OPERATOR OVERLOADING -II (12 --> ONE TWO)
Enter the number: 1234567890
 one two three four five six seven eight nine zero
nn@ubuntu:~$
Related Posts Plugin for WordPress, Blogger...