( 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:
- Describe precisely how it operates.
- What restrictions does the class have?
- Overload the * multiplication operator.
- Overload the / division operator.
- 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.]
- // Fig. 10.17: HugeInteger.h
- // HugeInteger Class definition.
- #ifndef HugeInteger H
- #define HugeInteger H
- #include
- #include
- #include
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
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 }
- // Fig. 10.19: Fig10_19.cpp
- // HugeInteger test program.
- #include < iostream>
- #include “ HugeInteger.h”
- Using namespace std;
- Int main () {
- HugeInteger n1{7654321};
- HUgeInteger n2{7891234};
- HugeInteger n3{“999999999999999999999999999999999”};
- HUgeInteger n4{“1”};
- HugeInteger n5;
- Cout << “n1 is “ << n1 << “\nn2 is “ << n2
- << “nn3 is “ <<n3 << nn4 is “ <<n4
- << ‘\nn5 is “ << n5 << “\n\n”;
- N5 = n1 + n2 ;
- Cout << n1 << “ + ” << n2 << “ = ” << n5 << “\n\n”;
- Cout << n3 << “ + “ << n4 << “\n= “ << (n3 + n4) << “\n\n;
- n5 = n1 + n2;
- Cout << n1<< “+” << 9<< “ = “ << n5 << “\n\n”;
- n5 = n2 + “10000” ;
- Cout << n2 << “ +” << “10000” << “ + “ << n5 << end ] :
- }
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 |
Want to see the full answer?
Check out a sample textbook solutionChapter 10 Solutions
C++ How to Program (10th Edition)
Additional Engineering Textbook Solutions
SURVEY OF OPERATING SYSTEMS
Electric Circuits. (11th Edition)
Database Concepts (8th Edition)
Modern Database Management
Automotive Technology: Principles, Diagnosis, And Service (6th Edition) (halderman Automotive Series)
Java: An Introduction to Problem Solving and Programming (8th Edition)
- 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 ' \ + days_of_week[i] + ': ')arrow_forwardFind 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(6): daily_sales[i] = float(input('Enter the sales for ' \ + days_of_week[i] + ': '))arrow_forwardWhat are the steps you will follow in order to check the database and fix any problems with it and normalize it? Give two references with your answer.arrow_forward
- What are the steps you will follow in order to check the database and fix any problems with it? Have in mind that you SHOULD normalize it as well. Consider that the database offline is not allowed since people are connected to it and personal data might be bridged and not secured. Provide three refernces with you answer.arrow_forwardShould software manufacturers should be tolerant of the practice of software piracy in third-world countries to allow these countries an opportunity to move more quickly into the information age? Why or why not?arrow_forwardI would like to know about the features of Advanced Threat Protection (ATP), AMD-V, and domain name space (DNS).arrow_forward
- I need help to resolve the following activityarrow_forwardModern life has been impacted immensely by computers. Computers have penetrated every aspect of oursociety, either for better or for worse. From supermarket scanners calculating our shopping transactionswhile keeping store inventory; robots that handle highly specialized tasks or even simple human tasks,computers do much more than just computing. But where did all this technology come from and whereis it heading? Does the future look promising or should we worry about computers taking over theworld? Or are they just a necessary evil? Provide three references with your answer.arrow_forwardWhat are the steps you will follow in order to check the database and fix any problems with it? Have in mind that you SHOULD normalize it as well. Describe in full, consider the following:• Taking the database offline is not allowed since people are connected to it.• Personal data might be bridged and not secured. Provide three refernces with you answerarrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr