Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Data File StructureRSS Feeds

Program to implement Add and Subtract function on Big Number Class

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

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

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

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

 /**************************************************************************///--------------------------  Class Definition  --------------------------///**************************************************************************///---------------------------------  List  ------------------------------//class List
 {
    public:
       int totalSize;
       int currSize;
       int* array;

    public:
       List( );
       List(int totalSize);
       ~List( );

       void Insert(intvalue);
       void InsertInSortedOrder(intvalue);
       int Search(intvalue);
       void Delete(int index);
       void Print( );
       void PrintInReverseOrder( );
       int Length( );
       void SortAscending( );
       void ReplaceAnElement(int index, intvalue);
 };

 /**************************************************************************///-------------------------  Function Definitions  -----------------------///**************************************************************************///--------------------------------  List( )  -----------------------------//

 List::List( )
 {
    this->totalSize = 0;
    this->currSize = 0;
    this->array = NULL;
 }

 //--------------------------------  List( )  -----------------------------//

 List::List(int totalSize)
 {
    this->totalSize = totalSize;
    this->currSize = 0;
    this->array = newint[totalSize];

    if (!this->array)
    {
    cout<<"Unable to allocate Memory to the Array.";
    exit(1);
    }
 }

 //-------------------------------  ~List( )  -----------------------------//

 List::~List( )
 {
    delete[] this->array;
 }

 //------------------------------  Insert( )  -----------------------------//void List::Insert(intvalue)
 {
    if (this->currSize < this->totalSize)
    {
       this->array[currSize] = value;

       this->currSize ++;
    }

    else
       cout<<"\nError: Cannot Insert ... Size exceeds"<<endl;
 }

 //----------------------  InsertInSortedOrder( )  -----------------------//void List::InsertInSortedOrder(intvalue)
 {
    if (this->currSize < this->totalSize)
    {
       for (int i = 0; i < this->currSize; i ++)
       {
       if (this->array[i] > value)
           break;
       }

       for (int j = this->currSize; j > i; j --)
       this->array[j] = this->array[j-1];

       this->array[i] = value;
       this->currSize ++;
    }

    else
       cout<<"\nError: Cannot Insert ... Size exceeds"<<endl;
 }

 //------------------------------  Search( )  -----------------------------//int List::Search(intvalue)
 {
    if (this->currSize == 0)
       return -1;

    else
    {
       for (int i = 0; i < this->currSize; i ++)
       {
       if (this->array[i] == value)
        return i;
       }

       return -1;
    }
 }

 //------------------------------  Delete( )  -----------------------------//void List::Delete(intvalue)
 {
    if (this->currSize == 0)
       cout <<"\nError: Cannot Delete. Array is Empty"<<endl;

    else
    {
       int index = this->Search(value);

       if (index != -1)
       {
       for (int i = index; i < this->currSize; i ++)
          this->array[i] = this->array[i+1];

       this->array[i] = NULL;

       this->currSize --;
       }
    }
 }

 //-------------------------------  Print( )  -----------------------------//void List::Print( )
 {
    if (this->currSize == 0)
       cout<<"\n:: Array is Empty."<<endl;

    else
    {
       cout << "Array = ";

       for (int i = 0; i < this->currSize; i ++)
       cout << this->array[i] << " ";

       cout<<endl;
    }
 }

 //--------------------------  PrintInReverseOrder( )  --------------------//void List::PrintInReverseOrder( )
 {
    if (this->currSize == 0)
       cout<<"\n:: Array is Empty."<<endl;

    else
    {
       cout << "Array = ";

       for (int i = (this->currSize - 1); i >= 0 ; i --)
       cout << this->array[i] << " ";

       cout<<endl;
    }
 }

 //------------------------------  Length( )  -----------------------------//int List::Length( )
 {
    returnthis->currSize;
 }

 //--------------------------  ReplaceAnElement( )  ----------------------//void List::ReplaceAnElement(int index, intvalue)
 {
    if (index < this->currSize)
       this->array[index] = value;

    else
       cout<<"\nError: Invalid Element Index"<<endl;
 }


 //--------------------------  SortAscending( )  -------------------------//void List::SortAscending( )
 {
    if (this->currSize <= 1)
       return;

    else
    {
       for (int i = 0; i < this->currSize; i ++)
       {
       for (int j = 0; j < (this->currSize - 1); j ++)
       {
           if (this->array[j] > this->array[j+1])
           {
          int temp = this->array[j];

          this->array[j] = this->array[j+1];
          this->array[j+1] = temp;
           }
       }
       }
    }
 }


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

    List Obj(10);

    cout<<"Insert( ) Demonstration"<<endl;
    cout<<"***********************"<<endl;

    cout<<"Insert(5)"<<endl;
    cout<<"Insert(3)"<<endl;
    cout<<"Insert(9)"<<endl;
    cout<<"Insert(12)"<<endl;
    cout<<"Insert(1)"<<endl;
    cout<<"Insert(7)"<<endl;

    Obj.Insert(5);
    Obj.Insert(3);
    Obj.Insert(9);
    Obj.Insert(12);
    Obj.Insert(1);
    Obj.Insert(7);


    cout<<"\n\nPrint( ) Demonstration"<<endl;
    cout<<"**********************"<<endl;

    Obj.Print( );


    cout<<"\n\nPrintInReverseOrder( ) Demonstration"<<endl;
    cout<<"************************************"<<endl;

    Obj.PrintInReverseOrder( );


    cout<<"\n\nLength( ) Demonstration"<<endl;
    cout<<"***********************"<<endl;

    cout<<"Array Length = " << Obj.Length( )<<endl;


    cout<<"\n\nReplaceAnElement( ) Demonstration"<<endl;
    cout<<"*********************************"<<endl;

    cout<<"ReplaceAnElement(0,6)"<<endl;
    cout<<"ReplaceAnElement(2,8)"<<endl<<endl;

    Obj.ReplaceAnElement(0,6);
    Obj.ReplaceAnElement(2,8);

    Obj.Print( );


    cout<<"\n\nSortAscending( ) Demonstration"<<endl;
    cout<<"******************************"<<endl;

    Obj.SortAscending( );
    Obj.Print( );


    cout<<"\n\nInsertInSortedOrder( ) Demonstration"<<endl;
    cout<<"************************************"<<endl;

    cout<<"InsertInSortedOrder(5)"<<endl;
    cout<<"InsertInSortedOrder(0)"<<endl<<endl;

    Obj.InsertInSortedOrder(5);
    Obj.InsertInSortedOrder(0);

    Obj.Print( );

    getch( );

    cout<<"\n\nSearch( ) Demonstration"<<endl;
    cout<<"***********************"<<endl;

    cout << "Search(8)"<<endl;
    int index = Obj.Search(8);

    if (index == -1)
       cout<<"Search Result = Element Not Found"<<endl;

    else
       cout<<"Search Result = Element Found At Arra["<<index<<"]"<<endl;


    cout <<"\nSearch(5)"<<endl;

    index = Obj.Search(5);

    if (index == -1)
       cout<<"Search Result = Element Not Found"<<endl;

    else
       cout<<"Search Result = Element Found At Arra["<<index<<"]"<<endl;


    cout<<"\n\nDelete( ) Demonstration"<<endl;
    cout<<"***********************"<<endl;

    cout << "Delete(8)"<<endl;
    Obj.Delete(8);

    cout << "Delete(5)"<<endl<<endl;
    Obj.Delete(5);

    Obj.Print( );

    getch( );
    return 0;
 }
  
Share: 



Easy Tutor
Easy Tutor author of Program to implement Add and Subtract function 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!