( 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 Plus Mylab Programming With Pearson Etext -- Access Card Package (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)
- Consider the following expression in C: a/b > 0 && b/a > 0.What will be the result of evaluating this expression when a is zero? What will be the result when b is zero? Would it make sense to try to design a language in which this expression is guaranteed to evaluate to false when either a or b (but not both) is zero? Explain your answerarrow_forwardConsider the following expression in C: a/b > 0 && b/a > 0. What will be the result of evaluating this expression when a is zero? What will be the result when b is zero? Would it make sense to try to design a language in which this expression is guaranteed to evaluate to false when either a or b (but not both) is zero? Explain your answer.arrow_forwardWhat are the major threats of using the internet? How do you use it? How do children use it? How canwe secure it? Provide four references with your answer. Two of the refernces can be from an article and the other two from websites.arrow_forward
- Assume that a string of name & surname is saved in S. The alphabetical characters in S can be in lowercase and/or uppercase letters. Name and surname are assumed to be separated by a space character and the string ends with a full stop "." character. Write an assembly language program that will copy the name to NAME in lowercase and the surname to SNAME in uppercase letters. Assume that name and/or surname cannot exceed 20 characters. The program should be general and work with every possible string with name & surname. However, you can consider the data segment definition given below in your program. .DATA S DB 'Mahmoud Obaid." NAME DB 20 DUP(?) SNAME DB 20 DUP(?) Hint: Uppercase characters are ordered between 'A' (41H) and 'Z' (5AH) and lowercase characters are ordered between 'a' (61H) and 'z' (7AH) in the in the ASCII Code table. For lowercase letters, bit 5 (d5) of the ASCII code is 1 where for uppercase letters it is 0. For example, Letter 'h' Binary ASCII 01101000 68H 'H'…arrow_forwardWhat did you find most interesting or surprising about the scientist Lavoiser?arrow_forward1. Complete the routing table for R2 as per the table shown below when implementing RIP routing Protocol? (14 marks) 195.2.4.0 130.10.0.0 195.2.4.1 m1 130.10.0.2 mo R2 R3 130.10.0.1 195.2.5.1 195.2.5.0 195.2.5.2 195.2.6.1 195.2.6.0 m2 130.11.0.0 130.11.0.2 205.5.5.0 205.5.5.1 R4 130.11.0.1 205.5.6.1 205.5.6.0arrow_forward
- Analyze the charts and introduce each charts by describing each. Identify the patterns in the given data. And determine how are the data points are related. Refer to the raw data (table):arrow_forward3A) Generate a hash table for the following values: 11, 9, 6, 28, 19, 46, 34, 14. Assume the table size is 9 and the primary hash function is h(k) = k % 9. i) Hash table using quadratic probing ii) Hash table with a secondary hash function of h2(k) = 7- (k%7) 3B) Demonstrate with a suitable example, any three possible ways to remove the keys and yet maintaining the properties of a B-Tree. 3C) Differentiate between Greedy and Dynamic Programming.arrow_forwardWhat are the charts (with their title name) that could be use to illustrate the data? Please give picture examples.arrow_forward
- A design for a synchronous divide-by-six Gray counter isrequired which meets the following specification.The system has 2 inputs, PAUSE and SKIP:• While PAUSE and SKIP are not asserted (logic 0), thecounter continually loops through the Gray coded binarysequence {0002, 0012, 0112, 0102, 1102, 1112}.• If PAUSE is asserted (logic 1) when the counter is onnumber 0102, it stays here until it becomes unasserted (atwhich point it continues counting as before).• While SKIP is asserted (logic 1), the counter misses outodd numbers, i.e. it loops through the sequence {0002,0112, 1102}.The system has 4 outputs, BIT3, BIT2, BIT1, and WAITING:• BIT3, BIT2, and BIT1 are unconditional outputsrepresenting the current number, where BIT3 is the mostsignificant-bit and BIT1 is the least-significant-bit.• An active-high conditional output WAITING should beasserted (logic 1) whenever the counter is paused at 0102.(a) Draw an ASM chart for a synchronous system to providethe functionality described above.(b)…arrow_forwardS A B D FL I C J E G H T K L Figure 1: Search tree 1. Uninformed search algorithms (6 points) Based on the search tree in Figure 1, provide the trace to find a path from the start node S to a goal node T for the following three uninformed search algorithms. When a node has multiple successors, use the left-to-right convention. a. Depth first search (2 points) b. Breadth first search (2 points) c. Iterative deepening search (2 points)arrow_forwardWe want to get an idea of how many tickets we have and what our issues are. Print the ticket ID number, ticket description, ticket priority, ticket status, and, if the information is available, employee first name assigned to it for our records. Include all tickets regardless of whether they have been assigned to an employee or not. Sort it alphabetically by ticket status, and then numerically by ticket ID, with the lower ticket IDs on top.arrow_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