C++ How To Program Plus Mylab Programming With Pearson Etext -- Access Card Package (10th Edition)
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
    Case Study Instructions: Offshore Wind Energy in the North Sea For this case study, students will analyze the institutional challenges and social rules surrounding offshore wind energy development in the Dutch North Sea, as explored in the case study from the textbook (pages 44-46). Additional resources in this week’s learning module will provide further perspectives on the impact of wind energy on different stakeholders.   Students should refer to the syllabus for specific guidelines regarding length, format, and content requirements.   Reflection Questions to Consider: What are the key institutional challenges in implementing offshore wind energy in the North Sea? How do formal and informal social rules shape the use of this shared space? What conflicts arise between different stakeholders (e.g., fishermen, naval transport, military, and wind energy developers)? How can policymakers balance economic, environmental, and social considerations when allocating space for wind energy?…
    Alphabetic Telephone Number TranslatorMany companies use telephone numbers like 555-GET-FOOD so the number is easier for their customers to remember. On a standard telephone, the alphabetic letters are mapped to numbers in the following fashion: A, B, and C = 2 D, E, and F = 3 G, H, and I = 4 J, K, and L = 5 M, N, and O = 6 P, Q, R, and S = 7 T, U, and V = 8 W, X, Y, and Z = 9 Write a program that asks the user to enter a 10-character telephone number in the format XXX-XXX-XXXX. The application should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. For example, if the user enters 555-GET-FOOD, the application should display 555-438-3663.
    I would like help to resolve the following case

    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
    C++ Programming: From Problem Analysis to Program...
    Computer Science
    ISBN:9781337102087
    Author:D. S. Malik
    Publisher:Cengage Learning
    Text book image
    C++ for Engineers and Scientists
    Computer Science
    ISBN:9781133187844
    Author:Bronson, Gary J.
    Publisher:Course Technology Ptr
    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