Mylab Programming With Pearson Etext -- Access Code Card -- For C++ How To Program (early Objects Version)
bartleby

Videos

Textbook Question
Book Icon
Chapter 10, Problem 10.9E

( HugeInteger Class) A machine with 32- bit integers can represent integers in the range of approximately-2 billion. This fixed-size restriction is rarely troublesome, but there are applications in which we would like to be able to use a much wider range of integers. This is what C++ was built to do, namely, create powerful new data types. Consider class HugeInteger of Figs. 10.17-10.19, which is similar to the HugeInteger class in Exercise 9.14. Study the class carefully, then respond to the following:

  1. Describe precisely how it operates.
  2. What restrictions does the class have?
  3. Overload the * multiplication operator.
  4. Overload the / division operator.
  5. Overload all the relational and equality operators.

[Note: We do not show an assignment operator or copy constructor for class HugeInteger, because the assignment operator and copy constructor provided by the compiler are capable of copying the entire array data member properly.]

  1. // Fig. 10.17: HugeInteger.h
  2. // HugeInteger Class definition.
  3. #ifndef HugeInteger H
  4. #define HugeInteger H
  5. #include
  6. #include
  7. #include

  • Class HugeInteger {
  • Friend std:: ostream& operator <<(std::ostream&, const HugeInteger&):
  • Public:
  • Static const int digits {40}; // maximum digits in a HugeInteger
  • HugeInteger (long= 0); // conversion/default constructor
  • HugeInteger (Const std::string&); // conversion constructor
  • // addition operator; HugeInteger +HugeInteger
  • HugeInteger operator+(const HugeInteger&) const:
  • // addition operator; HugeInteger + int
  • HugeInteger operator+(int) const;
  • // addition operator;
  • // HugeInteger + string that represents large integer value
  • HugeInteger operator+(const std:: string&) const;
  • Private:
  • Std::array integer{}; //default init to 0s
  • };
  • #endif
  • Fig.10.17 HugeInteger Class Definition. (Part 2of 2)

    1 // Fig. 10.18; HugeInteger.Cpp
    2 // HugeInteger member-function and friend-function definitions.
    3 #include // isdigit function prototype
    4 #include “HugeInteger .h” // HugeInteger Class definition
    5 using namespace;
    6
    7 // default constructors; conversion constructor that converts
    8 // a long integer into a HugeInteger object
    9 HugeInteger ::HugeInteger (long value) {
    10 // place digits of argument into array
    11 for (int j {digits -1} ; value != 0 && j >= 0; j--) {
    12 integer [ j ] = value % 10;
    13 value /= 10;
    14 }
    15 }
    16
    17 // Conversion constructor that converts a character string
    18 // representing a large integer into a HugeInteger object
    19 HugeInteger ::HugeInteger (const string&number) {
    20 // place digits of argument into array
    21 int length { number, size () };
    22
    23 for (int j {digits -length}, k {0}; j < digits; ++j, ++k) {
    24 if (isdigit (number [k])) { // ensure that character is a digit
    25 integer [j] = number [k] − ‘0’;
    26 }
    27 }
    30 // addition operator; HugeInteger + HugeInteger
    31 // HugeInteger HugeInteger :: operator+(const HugeInteger & op2) const {
    32 HugeInteger temp; // temporary result
    33 int carry= 0;
    34
    35 for (int I (digits-1) ; I >=0; i--) {
    36 temp.integer [i] = integer [i] op2.integer [i]
    + carry:

    37
    38 //determine whether to carry a 1
    39 If (temp.integer[i] > 9) {
    40 temp.integer [i] %-10; //reduce to
    41 carry= 0;
    42 }
    43 else { // no carry
    44 carry = 0;
    45 }
    46 }
    47
    48 return temp; // return copy of temporary object
    49 ]
    50
    51 // addition operator; HugeInteger + int
    52 HugeInteger HugeInteger ::operators+(int op2) const {
    53 // convert op2 to a HugeInteger , then invoke
    54 // operator + for two HugeInteger objects
    55 return *this + HugeInteger (op2);
    56 }
    57
    58 //addition operator ;
    59 // HugeInteger + string that represents large integervalue
    60 HugeInteger HugeInteger ::operator+(const string&op2) const {
    61 // convert op2 to a HugeInteger , then invoke
    62 //operator + for two HugeInteger objects
    63 return *this +HugeInteger (op2);
    64 }
    65
    66 // overloaded output operator
    67 ostream & operator <<(ostream & output, const HugeInteger & num ) {
    68 int I ;
    69
    70 // skip leading zeros
    71 for (I =0; (I < HugeInteger ::digits) && (0== num.integer [i] ) ; ++i) { }
    72
    73 if (I ==HugeInteger ::digits) {
    74 output << 0;
    75 }
    76 else {
    77 for (; I < HugeInteger :: digits; ++i) [
    78 output << num.integer [i]
    79 ]
    80 ]
    81
    82 return output
    83 }

    1. // Fig. 10.19: Fig10_19.cpp
    2. // HugeInteger test program.
    3. #include < iostream>
    4. #include “ HugeInteger.h”
    5. Using namespace std;
    6. Int main () {
    7. HugeInteger n1{7654321};
    8. HUgeInteger n2{7891234};
    9. HugeInteger n3{“999999999999999999999999999999999”};
    10. HUgeInteger n4{“1”};
    11. HugeInteger n5;
    12. Cout << “n1 is “ << n1 << “\nn2 is “ << n2
    13. << “nn3 is “ <<n3 << nn4 is “ <<n4
    14. << ‘\nn5 is “ << n5 << “\n\n”;
    15. N5 = n1 + n2 ;
    16. Cout << n1 << “ + ” << n2 << “ = ” << n5 << “\n\n”;
    17. Cout << n3 << “ + “ << n4 << “\n= “ << (n3 + n4) << “\n\n;
    18. n5 = n1 + n2;
    19. Cout << n1<< “+” << 9<< “ = “ << n5 << “\n\n”;
    20. n5 = n2 + “10000” ;
    21. Cout << n2 << “ +” << “10000” << “ + “ << n5 << end ] :
    22. }

      N1 is 7654321
      N2 is 7891234
      N3 is 99999999999999999999999999999
      N4 is 1
      N5 is 0
      765431+7891324=15545555
      99999999999999999999999999999+1
      =100000000000000000000000000000
      7654321+9 = 7654330
      7891234+ 10000 =7901234

    Blurred answer
    Students have asked these similar questions
    State whether each of the following is true or false. If false, explain why g) Definitions can appear anywhere in the body of a function.
    7. using c++, Write a void function that accepts an integer array a and a integer SizeA, and an integer n, and adds n to every element in that array. Be sure to develop an appropriate name for your function.
    - in this exercise, please do not include and use string class. The function is using only array notation and manipulation.- string functions such as strlen is not allowed.- it should not have multiple return statements in the same function- there should be no global variable.- the function should not traverse the arrays more than once (e.g. looping through the array once only) A C++ PROGRAM named "changeCase" that takes an array of characters terminating by NULL character (C-string) and a boolean flag of toUpper. If the toUpper flag is true, it will go through the array and convert all lowercase characters to uppercase. Otherwise, it will convert all uppercase to lowercase. For example, if the array is {'H', 'e', 'l', 'l', 'o', '\0'} and the flag is true, then the array will become{'H', 'E', 'L', 'L', 'O', '\0'}. And if the flag is false, the array will become{'h', 'e', 'l', 'l', 'o', '\0'}

    Additional Engineering Textbook Solutions

    Find more solutions based on key concepts
    Knowledge Booster
    Background pattern image
    Computer Science
    Learn more about
    Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
    Recommended textbooks for you
    Text book image
    C++ for Engineers and Scientists
    Computer Science
    ISBN:9781133187844
    Author:Bronson, Gary J.
    Publisher:Course Technology Ptr
    Text book image
    C++ Programming: From Problem Analysis to Program...
    Computer Science
    ISBN:9781337102087
    Author:D. S. Malik
    Publisher:Cengage Learning
    Algebraic Expressions – Algebra Basics; Author: TabletClass Math;https://www.youtube.com/watch?v=U-7nq7OG18s;License: Standard YouTube License, CC-BY
    Python Tutorial for Beginners 3 - Basic Math, Mathematical Operators and Python Expressions; Author: ProgrammingKnowledge;https://www.youtube.com/watch?v=Os4gZUI1ZlM;License: Standard Youtube License