C++ How to Program (10th Edition)
C++ How to Program (10th Edition)
10th Edition
ISBN: 9780134448237
Author: Paul J. Deitel, Harvey Deitel
Publisher: PEARSON
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
    (In C++) and please try using Occam’s razor law.
    C++ Programming. Topic: Working with pointers and dynamic memory. Indicators. Working with dynamic memory. Dynamic arrays and their use as function parameters. Task : Describe a void function named Swap(x,y) that swaps the values ​​stored in the variables x and (x is a real type parameter and is both input and output). Using this function , for the given variables of real type a, b, c, d, one should sequentially replace the values ​​of the pairs (a, b), (c, d) and (b, c) and let a, b, c, d be new values ​​.
    (Pointers + Dynamic 1D Arrays)   c++ program please use only pointers and dynamic 1D array and please don't use recursion and vector or other

    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.
    Similar questions
    SEE MORE QUESTIONS
    Recommended textbooks for you
    Text book image
    Database System Concepts
    Computer Science
    ISBN:9780078022159
    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
    Publisher:McGraw-Hill Education
    Text book image
    Starting Out with Python (4th Edition)
    Computer Science
    ISBN:9780134444321
    Author:Tony Gaddis
    Publisher:PEARSON
    Text book image
    Digital Fundamentals (11th Edition)
    Computer Science
    ISBN:9780132737968
    Author:Thomas L. Floyd
    Publisher:PEARSON
    Text book image
    C How to Program (8th Edition)
    Computer Science
    ISBN:9780133976892
    Author:Paul J. Deitel, Harvey Deitel
    Publisher:PEARSON
    Text book image
    Database Systems: Design, Implementation, & Manag...
    Computer Science
    ISBN:9781337627900
    Author:Carlos Coronel, Steven Morris
    Publisher:Cengage Learning
    Text book image
    Programmable Logic Controllers
    Computer Science
    ISBN:9780073373843
    Author:Frank D. Petruzella
    Publisher:McGraw-Hill Education
    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