Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Mathematics ProgramRSS Feeds

Program to implement Add and Subtract functions on Big Number Class

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

A C++ Program to implement Add and Subtract functions on Big Number Class.

Code for Program to implement Add and Subtract functions on Big Number Class in C++ Programming

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

 #define MAX_DIGITS 10
 #define MAX_LENGTH (MAX_DIGITS+5)

 /*************************************************************************///------------------------  Function Prototypes  ------------------------///*************************************************************************/class BigNumber
 {
    private:
       char cSign;
       char sValue[MAX_LENGTH];
       int iLength;

    public:
       BigNumber( );

       void read( );
       void show( );
       void plus(BigNumber);
       void minus(BigNumber);

       constchar* add(char[MAX_LENGTH],char[MAX_LENGTH]);
       constchar* subtract(char[MAX_LENGTH],char[MAX_LENGTH]);
 };

 BigNumber::BigNumber( )
 {
    cSign=' ';
    strset(sValue,NULL);
    iLength=0;
 }

 void BigNumber::read( )
 {
    char sTemp[MAX_LENGTH];

    strset(sTemp,NULL);
    strset(sValue,NULL);

    gets(sTemp);

    iLength=strlen(sTemp);

    if (sTemp[0]=='+' || sTemp[0]=='-')
    {
      cSign=sTemp[0];

      for(int i=1; i<iLength; i++)
     sValue[(i-1)]=sTemp[i];

      sValue[(i-1)]=NULL;
    }

    else
       strcpy(sValue,sTemp);

    iLength=strlen(sValue);

    for(int j=0; j<iLength; j++)
    {
       if (sValue[j] < '0' || sValue[j] > '9')
       {
      cSign=' ';
      strcpy(sValue,"0");
      iLength=1;

      break;
       }
    }
 }

 void BigNumber::show( )
 {
    if (cSign != ' ')
    cout<<cSign;

    cout<<sValue;
 }

 void BigNumber::plus(BigNumber Temp)
 {
    if ((cSign=='+' || cSign==' ') && (Temp.cSign=='+' || Temp.cSign==' '))
    {
       cSign=' ';
       strcpy(sValue,add(sValue,Temp.sValue));
    }

    elseif (cSign=='-' && Temp.cSign=='-')
       strcpy(sValue,add(sValue,Temp.sValue));

    else
    {
       if (iLength>Temp.iLength)
      strcpy(sValue,subtract(sValue,Temp.sValue));

       elseif (iLength<Temp.iLength)
       {
      cSign=Temp.cSign;
      strcpy(sValue,subtract(Temp.sValue,sValue));
       }

       else
       {
      int iFlag=0;

      for (int i=0; i<iLength; i++)
      {
         if (sValue[i]>Temp.sValue[i])
         {
        strcpy(sValue,subtract(sValue,Temp.sValue));
        iFlag=1;
        break;
         }

         elseif (sValue[i]<Temp.sValue[i])
         {
        cSign=Temp.cSign;
        strcpy(sValue,subtract(Temp.sValue,sValue));
        iFlag=1;
        break;
         }
      }

      if (iFlag==0)
      {
         cSign=' ';
         strcpy(sValue,"0");
      }
       }
    }

    iLength=strlen(sValue);
 }

 void BigNumber::minus(BigNumber Temp)
 {
    if ((cSign=='+' || cSign==' ') && (Temp.cSign=='+' || Temp.cSign==' '))
    {
       if (iLength>Temp.iLength)
      strcpy(sValue,subtract(sValue,Temp.sValue));

       elseif (iLength<Temp.iLength)
       {
      cSign='-';
      strcpy(sValue,subtract(Temp.sValue,sValue));
       }

       else
       {
      int iFlag=0;

      for (int i=0; i<iLength; i++)
      {
         if (sValue[i]>Temp.sValue[i])
         {
        strcpy(sValue,subtract(sValue,Temp.sValue));
        iFlag=1;
        break;
         }

         elseif (sValue[i]<Temp.sValue[i])
         {
        cSign='-';
        strcpy(sValue,subtract(Temp.sValue,sValue));
        iFlag=1;
        break;
         }
      }

      if (iFlag==0)
      {
         cSign=' ';
         strcpy(sValue,"0");
      }
       }
    }

    elseif ((cSign=='+' || cSign==' ') && Temp.cSign=='-')
    {
       if (iLength>Temp.iLength)
      strcpy(sValue,add(sValue,Temp.sValue));

       elseif (iLength<Temp.iLength)
       {
      cSign='-';
      strcpy(sValue,add(sValue,Temp.sValue));
       }

       else
       {
      int iFlag=0;

      for (int i=0; i<iLength; i++)
      {
         if (sValue[i]>Temp.sValue[i])
         {
        strcpy(sValue,add(sValue,Temp.sValue));
        iFlag=1;
        break;
         }

         elseif (sValue[i]<Temp.sValue[i])
         {
        cSign='-';
        strcpy(sValue,add(sValue,Temp.sValue));
        iFlag=1;
        break;
         }
      }

      if (iFlag==0)
      {
         cSign=' ';
         strcpy(sValue,add(sValue,Temp.sValue));
      }
       }
    }

    elseif (cSign=='-' && (Temp.cSign=='+' || Temp.cSign==' '))
    {
       if (iLength>Temp.iLength)
      strcpy(sValue,add(sValue,Temp.sValue));

       elseif (iLength<Temp.iLength)
       {
      cSign=Temp.cSign;
      strcpy(sValue,add(sValue,Temp.sValue));
       }

       else
       {
      int iFlag=0;

      for (int i=0; i<iLength; i++)
      {
         if (sValue[i]>Temp.sValue[i])
         {
        strcpy(sValue,add(sValue,Temp.sValue));
        iFlag=1;
        break;
         }

         elseif (sValue[i]<Temp.sValue[i])
         {
        cSign=Temp.cSign;
        strcpy(sValue,add(sValue,Temp.sValue));
        iFlag=1;
        break;
         }
      }

      if (iFlag==0)
      {
         cSign=' ';
         strcpy(sValue,add(sValue,Temp.sValue));
      }
       }
    }

    iLength=strlen(sValue);
 }

 constchar* BigNumber::add(char A[MAX_LENGTH],char B[MAX_LENGTH])
 {
    if (strlen(A) < strlen(B))
    {
       strrev(A);

       while(strlen(A)<strlen(B))
      strcat(A,"0");

       strrev(A);
    }

    if (strlen(B) < strlen(A))
    {
       strrev(B);

       while(strlen(B)<strlen(A))
      strcat(B,"0");

       strrev(B);
    }

    int length=strlen(A);

    int carry=0;
    int sum=0;
    int digit_1=0;
    int digit_2=0;

    char Sum[5]={NULL};
    char Result[MAX_LENGTH]={NULL};

    strset(Result,NULL);
    strrev(A);
    strrev(B);

    for(int i=0; i<length; i++)
    {
       digit_1=(int(B[i])-48);
       digit_2=(int(A[i])-48);

       sum=(digit_1+digit_2+carry);

       if(sum>9)
       {
      sum%=10;
      carry=1;
       }

       else
      carry=0;

       itoa(sum,Sum,10);
       strcat(Result,Sum);
    }

    if(carry)
       strcat(Result,"1");

    if (strlen(Result)>MAX_DIGITS)
       strcpy(Result,"0");

    strrev(Result);

    return Result;
 }

 constchar* BigNumber::subtract(char A[MAX_LENGTH],char B[MAX_LENGTH])
 {
    if (strlen(A) < strlen(B))
    {
       strrev(A);

       while(strlen(A)<strlen(B))
      strcat(A,"0");

       strrev(A);
    }

    if (strlen(B) < strlen(A))
    {
       strrev(B);

       while(strlen(B)<strlen(A))
      strcat(B,"0");

       strrev(B);
    }

    int length=strlen(A);

    int borrow=0;
    int difference=0;
    int digit_1=0;
    int digit_2=0;

    char Difference[5]={NULL};
    char Result[MAX_LENGTH]={NULL};

    strset(Result,NULL);
    strrev(A);
    strrev(B);

    for(int i=0;i<length;i++)
    {
       digit_1=(int(A[i])-48);
       digit_2=(int(B[i])-48);

       difference=(digit_1-digit_2-borrow);

       if(difference<0)
       {
      difference+=10;
      borrow=1;
       }

       else
      borrow=0;

       itoa(difference,Difference,10);
       strcat(Result,Difference);
    }

    while(Result[(strlen(Result)-1)]=='0')
       Result[(strlen(Result)-1)]=NULL;

    strrev(Result);

    return Result;
 }

 /*************************************************************************///------------------------------  main( )  ------------------------------///*************************************************************************/int main( )
 {
    clrscr( );

    BigNumber A, B;

    cout<<"Enter the value of A = ";
    A.read( );

    cout<<"Enter the value of B = ";
    B.read( );

    cout<<"Enter Operation (+ | -) : ";

    char cOperation=getch();

    cout<<endl<<endl<<"A = ";
    A.show( );

    cout<<endl<<"B = ";
    B.show( );

    cout<<endl;

    if (cOperation=='+')
    {
      A.plus(B);

      cout<<endl<<"Sum (A+B) = ";

      A.show( );
    }

    elseif (cOperation=='-')
    {
      A.minus(B);

      cout<<endl<<"Difference (A-B) = ";
      A.show( );
    }

    else
       cout<<endl<<"Invalid Operation!";

    getch( );
    return 0;
 }
  
Share: 



Easy Tutor
Easy Tutor author of Program to implement Add and Subtract functions on Big Number Class 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

 
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!