A C++ OOP program to get hugeinteger

By using this program you can input an integer having up to 40 digits. All the operators are overloaded hence Class can be used as normal integer.


 // ==============================
 //        Main.cpp
 // ==============================


#include <iostream>
#include "hugeint.h"
using namespace std;

int main()
{
    hugeinteger ary,ary1,ary2,ary3;


    char* str = new char[40];

    cout<<"enter string : ";
    cin>>str;
    ary.getinput(str);

    cout<<"enter string : ";
    cin>>str;
    ary1.getinput(str);
/*
    cout<<"enter string : ";
    cin>>str;
    ary2.getinput(str);

    cout<<"enter string : ";
    cin>>str;
    ary3.getinput(str);

    cout<<"size of array:"<<ary.getsize()<<endl;
    ary.print();

    cout<<"size of array1:"<<ary1.getsize()<<endl;
    ary1.print();

    cout<<"size of array2:"<<ary2.getsize()<<endl;
    ary2.print();

    cout<<"size of array3:"<<ary3.getsize()<<endl;
    ary3.print();

    cout<<"apend 2 in aray"<<endl;
    ary.shiftAppend('2');
    ary.print();

    cout<<"equal"<<endl;
    cout<<(ary  == ary1)<<endl;

    cout<<"not equal"<<endl;
    cout<<(ary1 != ary2)<<endl;

    cout<<"greater"<<endl;
    cout<<(ary2 >  ary3)<<endl;

    cout<<"greater or equal"<<endl;
    cout<<(ary1 >= ary2)<<endl;

    cout<<"less"<<endl;
    cout<<(ary  <  ary2)<<endl;

    cout<<"less or equal"<<endl;
    cout<<(ary3 <= ary2)<<endl;


    hugeinteger sum,diff,mul,div,imul,mod;

    cout<<"sum"<<endl;
    sum=ary+ary1;
    sum.print();

    cout<<"diff"<<endl;
    diff=ary3-ary2;
    diff.print();

    cout<<"mul"<<endl;
    mul=  ary*ary1;
    mul.print();

    cout<<"imul"<<endl;
    imul=ary1*4;
    imul.print();
    */
    hugeinteger div,mod;
    cout<<"division"<<endl;
    div=ary1/ary;
    div.print();

    cout<<"mod"<<endl;
    mod=ary1/ary;
    mod.print();

    return 0;
}






 // ==============================
 //          HUGEINT.H
 // ==============================


#include<iostream>
using namespace std;
#ifndef HUGEINT_H
#define HUGEIN_H
class hugeinteger
{
private:
    char hugeint[40];
public:

    hugeinteger();
    void getinput(const char* chr);
    int  getsize() const;
    void shiftAppend(char c);
    void print();

    bool operator==(hugeinteger&);
    bool operator!=(hugeinteger&);
    bool operator >(hugeinteger&);
    bool operator <(hugeinteger&);
    bool operator>=(hugeinteger&);
    bool operator<=(hugeinteger&);



    hugeinteger operator+(hugeinteger&);
    hugeinteger operator-(hugeinteger&);
    hugeinteger operator*(hugeinteger&);
    hugeinteger operator/(hugeinteger&);
    hugeinteger operator%(hugeinteger&);
    hugeinteger operator*(int);
};
#endif


 // ==============================
 //          HUGEINT.CPP
 // ==============================
#include"hugeint.h"

hugeinteger::hugeinteger()
{
    for( int i=0 ; i<40 ; i++ )
        hugeint[i]='0';
}

void hugeinteger::getinput(const char* chr)
{
    int size=0;
    for(int i=0 ; !(chr[i]=='\0') ; i++)
        size++;
    int j=0;
    for(int i=(40-size); i<40 ; i++)
    {
        this->hugeint[i]=chr[j];
        j++;
    }
}

int  hugeinteger::getsize() const
{
    int size=0;
    for( ; this->hugeint[size]=='0' ; size++);

    size=40-size;
    return size;
}

 void hugeinteger::shiftAppend(char c)
 {
    char temp = this->hugeint[39];
    this->hugeint[39]=c;

    for(int i=38 ; i >= 0 ; i-- )
    {
        c = this->hugeint[i];
        this->hugeint[i] = temp;
        temp=c;
    }
 }

 void hugeinteger::print()
 {
    int size=0;
    for( ; this->hugeint[size]=='0' ; size++);

    cout<<"Elements Of Array Are:";
      for(int i=size ; i < 40 ; i++ )
        cout<<hugeint[i]<<" ";

    cout<<endl;
 }

 bool hugeinteger::operator==(hugeinteger & ary)
 {
     if (this->getsize() == ary.getsize() )
        for(int i=0 ; i<40 ;i++)
            if(!(this->hugeint[i]==ary.hugeint[i]))
                return false;
     else
        return false;

     return true;
}

bool hugeinteger::operator!=(hugeinteger & ary)
 {
     if (this->getsize() == ary.getsize() )
        for(int i=0 ; i<40 ;i++)
        {
            if(!(this->hugeint[i]==ary.hugeint[i]))
                return true;
        }
     else
        return true;

     return false;
}

bool hugeinteger::operator >(hugeinteger & ary)
 {
     if (this->getsize() > ary.getsize() )
        return true;
     else
        return false;


}

bool hugeinteger::operator < (hugeinteger & ary)
 {
     if (this->getsize() < ary.getsize() )
        return true;
     else
        return false;


}

bool hugeinteger::operator >=(hugeinteger & ary)
 {
     if (this->getsize() > ary.getsize() )
     {   
        return true;
     }
     else if(this->getsize() == ary.getsize() )
     {
        for(int i=0 ; i<40 ;i++)
            if(!(this->hugeint[i] == ary.hugeint[i]))
                return false;
     }
     else
        return false;

     return true;
}

bool hugeinteger::operator <=(hugeinteger & ary)
 {
     if (this->getsize() < ary.getsize() )
    {   
        return true;
     }
     else if(this->getsize() == ary.getsize() )
     {
        for(int i=0 ; i<40 ;i++)
            if(!(this->hugeint[i] == ary.hugeint[i]))
                return false;
     }
     else
        return false;

     return true;
}

 hugeinteger hugeinteger::operator+(hugeinteger& ary)
 {
     hugeinteger temp;
     int rzlt=0;
     int cary=0;
    for(int i=39 ; i>=0 ; i--)
    {
        rzlt= (this->hugeint[i]-'0')+(ary.hugeint[i]-'0') + cary;
        cary=rzlt/10;
        rzlt=rzlt%10;
        temp.hugeint[i] = rzlt + '0';
    }
    return temp;
 }



 hugeinteger hugeinteger::operator-(hugeinteger& ary)
 {
    hugeinteger temp;
    if(*this >= ary)
    {
        hugeinteger temp1=*this;
        int rzlt=0;
        for(int i=39 ; i >= 39-this->getsize() ; i--)
        {
            if(temp1.hugeint[i] < ary.hugeint[i])
            {
                temp1.hugeint[i-1] -= 1  ;
                temp1.hugeint[i] -= '0'  ;
                temp1.hugeint[i] += 10   ;
                rzlt = temp1.hugeint[i] - (ary.hugeint[i]-'0');
            }
            else
            {
                rzlt = (temp1.hugeint[i]-'0') - (ary.hugeint[i]-'0');
            }

            temp.hugeint[i] = rzlt + '0';
        }
    }
    else
        cout<<"Substraction can`t takeplace\nResult is zero"<<endl;
    return temp;
 }

hugeinteger hugeinteger::operator*(hugeinteger& ary)
{
    hugeinteger tmp;
    for(int j=39 ; j >= (39-ary.getsize()) ; j-- )
    {
        int k=j;
        int rzlt;
        int rzlt1;
        int cary=0;
        int cary1=0;
        for(int i=39 ; i>=(39-this->getsize()) ; i--)
        {
            rzlt= (this->hugeint[i]-'0')*(ary.hugeint[j]-'0')+cary;
            cary = rzlt/10;
            rzlt = rzlt%10;

            rzlt1=((tmp.hugeint[k]-'0')+(rzlt)+cary1);
            cary1=rzlt1/10;
            rzlt1=rzlt1%10;
            tmp.hugeint[k]=(rzlt1+'0');
            k--;
        }
    }
    return tmp;
}

hugeinteger hugeinteger::operator*(int in)
{
    hugeinteger tmp;
        int rzlt;
        int cary=0;
        for(int i=39 ; i>=(39-this->getsize()) ; i--)
        {
            rzlt= (this->hugeint[i]-'0')*in+cary;
            cary = rzlt/10;
            rzlt = rzlt%10;
            tmp.hugeint[i]=(rzlt+'0');
        }

    return tmp;
}


hugeinteger hugeinteger::operator/(hugeinteger& ary)
{
   
    hugeinteger result;
    if( *this >= ary)
    {
        hugeinteger left;
        hugeinteger right=*this;
        int rsize=right.getsize();
        while(rsize > 0 )
        {
            left.shiftAppend(right.hugeint[40-rsize]);
            rsize --;
            int i=0;
            while(left >= ary)
            {
                left = left-ary;
                i++;
            }
            result.shiftAppend(i +'0');
        }
    }
    else
        cout<<"Divisor Is Greater Than Dividend\nReturned Result Is Zero"<<endl;
    return result;
}

hugeinteger hugeinteger::operator%(hugeinteger& ary)
{
   
    hugeinteger left;
    if( *this > ary)
    {
        hugeinteger result;
        hugeinteger right=*this;
        int rsize=right.getsize();
        while(rsize > 0 )
        {
            left.shiftAppend(right.hugeint[40-rsize]);
            rsize --;
            int i=0;
            while(left > ary)
            {
                i++;
                left = left-ary;
            }
            result.shiftAppend(i +'0');
        }
    }
    else
        cout<<"Divisor Is Greater Than Dividend\nReturned Result Is Zero"<<endl;
   
    return left;
}

Comments

  1. try giving first digit 2 and second digit 9 and try subtracting .....wrong output is generated..vice versa.. overloaded operators logic is not right

    ReplyDelete

Post a Comment

Popular posts from this blog

Assembly Program Code For Brick Breaker Game

Polynomial C++ Class