Question

In: Computer Science

C++ PROGRAM Programming Exercise 11 in Chapter 8explains how to add large integers using arrays. However,...

C++ PROGRAM

Programming Exercise 11 in Chapter 8explains how to add large integers using arrays. However, in that exercise, the program could add only integers of, at most, 20 digits. This chapter explains how to work with dynamic integers. Design a class named largeIntegers such that an object of this class can store an integer of any number of digits. Add operations to add, subtract, multiply, and compare integers stored in two objects. Also add constructors to properly initialize objects and functions to set, retrieve, and print the values of objects. Write a program to test your class.

Solutions

Expert Solution

/* Program for adding large integers using array*/
//header files

#include <iostream>

#include <cctype>

//string library

#include <cstring>



using namespace std;

#define DIGITS 40

class large_integers                    //defining class as large integers

{

public:

large_integers();

void Inputvalue();

void Outputvalue();

large_integers operator+( large_integers );

large_integers operator-( large_integers );

large_integers operator*( large_integers );

int operator==( large_integers);

private:

int integervalue[ DIGITS ];

int lenvalue;

};

void large_integers::Outputvalue()

{

int i1;

for (i1=lenvalue-1;i1 >= 0; i1-- )

cout<<integervalue[i1];

}

void large_integers::Inputvalue()

{

string in;

int i1,j1,k1;

cout<<"Enter a number("<<DIGITS<<" DIGITS maximum):";

cin>>in;

for(i1=0;in[i1]!='\0';i1++);

lenvalue=i1;

k1=0;

for(j1=i1-1;j1>=0;j1--)

    integervalue[j1]=in[k1++]-48;

}

large_integers::large_integers( )

{

for ( int i1 = 0; i1 < DIGITS; i1++ )

      integervalue[ i1 ] = 0;

lenvalue=DIGITS-1;

}

int large_integers::operator==( large_integers oper2 )

{

int i;

if(lenvalue<oper2.lenvalue)

      return -1;

if(oper2.lenvalue<lenvalue)

      return 1;

for(i=lenvalue-1;i>=0;i--)

      if(integervalue[i]<oper2.integervalue[i])

           return -1;

       else if(oper2.integervalue[i]<integervalue[i])

             return 1;

return 0;

}

large_integers large_integers::operator+( large_integers oper2 )

{large_integers temp_value;

int carry_value = 0;

int c1,i;

if(lenvalue>oper2.lenvalue)

    c1=lenvalue;

else

    c1=oper2.lenvalue;

for ( i=0; i<c1; i++ )

     {temp_value.integervalue[ i ] =integervalue[ i ] + oper2.integervalue[ i ] + carry_value;

     if ( temp_value.integervalue[ i ] > 39 )

         {temp_value.integervalue[ i ] %= 40;

          carry_value = 1;

         }

     else

          carry_value = 0;

     }

if(carry_value==1)

    {temp_value.lenvalue=c1+1;

    if(temp_value.lenvalue>=DIGITS)

          cout<<"**overflow** \n";

    else

         temp_value.integervalue[i]=carry_value;

  

    }

else   

   temp_value.lenvalue=c1;

return temp_value;

}

large_integers large_integers::operator-( large_integers oper2 )

{

large_integers temp_value;

int c;

if(lenvalue>oper2.lenvalue)

    c=lenvalue;

else

    c=oper2.lenvalue;

int borrow_value = 0;

for( int i = c;i >= 0;i--)

if(borrow_value==0)

    {if(integervalue[i]>=oper2.integervalue[i])

           temp_value.integervalue[i]=integervalue[i]-oper2.integervalue[i];

    else

          {borrow_value=1;

          temp_value.integervalue[i]=integervalue[i]+40-oper2.integervalue[i];

          }

    }

else

    {borrow_value=0;

    if(integervalue[i]-1>=oper2.integervalue[i])

           temp_value.integervalue[i]=integervalue[i]-1-oper2.integervalue[i];

      else

          {borrow_value=1;

          temp_value.integervalue[i]=integervalue[i]-1+40-oper2.integervalue[i];

          }

      }

temp_value.lenvalue=c;

return temp_value;

}  

large_integers large_integers::operator*( large_integers oper2 )

    {

      large_integers temp_value;

      int i,j,k,tmp_value,m1=0;

       for (int i=0; i<oper2.lenvalue; i++)

        { k=i;

        for (j=0; j< lenvalue; j++)

           {

             tmp_value = integervalue[ j ] * oper2.integervalue[i];

            temp_value.integervalue[k] =temp_value.integervalue[k]+tmp_value;

            temp_value.integervalue[k+1] =temp_value.integervalue[k+1]+ temp_value.integervalue[k]/10;

            temp_value.integervalue[k] %=10;

            k++;

            if(k>m1)

                m1=k;

                }

        }

        temp_value.lenvalue=m1;

         if(temp_value.lenvalue>DIGITS)

          cout<<"**overflow**\n";

        return temp_value;

    }

using namespace std;

int main()

{

int c;

large_integers n1,n2,result;

n1.Inputvalue();

n2.Inputvalue();

n1.Outputvalue();

cout <<" + ";   //for addition

n2.Outputvalue();

result=n1+n2;

cout<< " = " ;

result.Outputvalue();

cout << "\n\n";

n1.Outputvalue();

cout <<" - ";     //for substraction

n2.Outputvalue();

result=n1-n2;

cout<< " = " ;

result.Outputvalue();

cout << "\n\n";

n1.Outputvalue();

cout <<" * ";     //for multiplication

n2.Outputvalue();

result=n1*n2;

cout<< " = " ;   //for comparing which one is less or equal or more than the other

result.Outputvalue();

cout << "\n\n";

c=n1==n2;

n1.Outputvalue();

switch (c)

{ case -1: cout<<" is less than ";

            break;

   case 0: cout<<" is equal to ";

            break;

    case 1: cout<<" is more than ";

            break;

    }

n2.Outputvalue();

cout << "\n\n";               

}

OUTPUT :-  

  

--------------------------------------------------Thank You --------------------------------------------------------------------------------------------


Related Solutions

Write a C++ program to do the following USING ARRAYS 1) input 15 integers (input validation...
Write a C++ program to do the following USING ARRAYS 1) input 15 integers (input validation , all numbers must be between 0 and 100) 2) find the largest number 3) Find the Smallest Number 4) Find the Sum of all numbers in the Array Display: as per the user's section (menu using switch or if else ) Largest Number Smallest Number Sum of all numbers the original Array DO NOT USE METHODS OR FUNCTIONS Submit: Source code (C++) output...
using c++ 10. Sorting Orders Write a program that uses two identical arrays of eight integers....
using c++ 10. Sorting Orders Write a program that uses two identical arrays of eight integers. It should display the contents of the first array, then call a function to sort it using an ascending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using an ascending order selection sort, modified to print out the...
Modified from Chapter 07 Programming Exercise 5 Original Exercise: Rock, Paper, Scissors Modification Programming Exercise 11...
Modified from Chapter 07 Programming Exercise 5 Original Exercise: Rock, Paper, Scissors Modification Programming Exercise 11 in Chapter 6 asked you to design a program that plays the Rock, Paper, Scissors game. In the program, the user enters one of the three strings—"rock", "paper", or "scissors"—at the keyboard. Add input validation (with a case-insensitive comparison) to make sure the user enters one of those strings only. Modifications: Allow the user to input "r", "p", "s" or the full strings "Rock",...
In C++, write a program that uses two identical arrays of ten randomly ordered integers. It...
In C++, write a program that uses two identical arrays of ten randomly ordered integers. It should display the contents of the first array, then call a function to sort it using the most efficient descending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using descending order selection sort, modified to print out the...
Write program for the exercise 11(Sales Analysis) as explained at the end of the chapter. 11....
Write program for the exercise 11(Sales Analysis) as explained at the end of the chapter. 11. sales Analysis The file SalesData.txt, in this chapter’s source code folder, contains the dollar amount of sales that a retail store made each day for a number of weeks. Each line in the file contains seven numbers, which are the sales numbers for one week. The numbers are separated by a comma. The following line is an example from the file: 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36 Write a...
Lab # 4 Multidimensional Arrays Please write in JAVA. Programming Exercise 8.5 (Algebra: add two matrices)...
Lab # 4 Multidimensional Arrays Please write in JAVA. Programming Exercise 8.5 (Algebra: add two matrices) Write a method to add two matrices. The header of the method is as follows: public static double[][] addMatrix(double[][] a, double[][] b In order to be added, the two matrices must have the same dimensions and the same or compatible types of elements. Let c be the resulting matrix. Each element cij is aij + bij. For example, for two 3 * 3 matrices...
C++ Programming: Programming Design and Data Structures Chapter 13 Ex 2 Redo Programming Exercise 1 by...
C++ Programming: Programming Design and Data Structures Chapter 13 Ex 2 Redo Programming Exercise 1 by overloading the operators as nonmembers of the class rectangleType. The header and implementation file from Exercise 1 have been provided. Write a test program that tests various operations on the class rectangleType. I need a main.cpp file Given: **************rectangleType.cpp******************** #include <iostream> #include <cassert> #include "rectangleType.h" using namespace std; void rectangleType::setDimension(double l, double w) { if (l >= 0) length = l; else length =...
Programming Exercise 11-2 QUESTION: In this chapter, the class dateType was designed to implement the date...
Programming Exercise 11-2 QUESTION: In this chapter, the class dateType was designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the member variables. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and year are checked before storing the date into the member variables. Add a member function, isLeapYear, to check...
Chapter 8 Programming exercise 6 "Days of each month" Original Exercise: Design a program that displays...
Chapter 8 Programming exercise 6 "Days of each month" Original Exercise: Design a program that displays the number of days in each month. The program’s output should be similar to this: January has 31 days. February has 28 days. March has 31 days. April has 30 days. May has 31 days. June has 30 days. July has 31 days. August has 31 days. September has 30 days. October has 31 days. November has 30 days. December has 31 days. The...
how to create BANKACCOUNT program using Arrays in JAVA.
how to create BANKACCOUNT program using Arrays in JAVA.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT