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
    Hands-On Assignments Part II Assignment 1-5: Querying the DoGood Donor Database Review the DoGood Donor data by writing and running SQL statements to perform the following tasks: 1. List each donor who has made a pledge and indicated a single lump sum payment. Include first name, last name, pledge date, and pledge amount. 2. List each donor who has made a pledge and indicated monthly payments over one year. Include first name, last name, pledge date, and pledge amount. Also, display the monthly payment amount. (Equal monthly payments are made for all pledges paid in monthly payments.) 3. Display an unduplicated list of projects (ID and name) that have pledges committed. Don't display all projects defined; list only those that have pledges assigned. 4. Display the number of pledges made by each donor. Include the donor ID, first name, last name, and number of pledges. 5. Display all pledges made before March 8, 2012. Include all column data from the DD PLEDGE table.
    Write a FancyCar class to support basic operations such as drive, add gas, honk horn, and start engine. FancyCar.java is provided with method stubs. Follow each step to gradually complete all methods. Note: This program is designed for incremental development. Complete each step and submit for grading before starting the next step. Only a portion of tests pass after each step but confirm progress. The main() method includes basic method calls. Add statements in main() as methods are completed to support development mode testing. Step 0. Declare private fields for miles driven as shown on the odometer (int), gallons of gas in tank (double), miles per gallon or MPG (double), driving capacity (double), and car model (String). Note the provided final variable indicates the gas tank capacity of 14.0 gallons. Step 1 (2 pts). 1) Complete the default constructor by initializing the odometer to five miles, tank is full of gas, miles per gallon is 24.0, and the model is "Old Clunker". 2)…
    Find the error: daily_sales = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday',                     'Wednesday', 'Thursday', 'Friday',                     'Saturday'] for i in range(7):         daily_sales[i] = float(input('Enter the sales for ' \                                      + day_of_week[i] + ': ')

    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