( 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
Programming in C
Management Information Systems: Managing The Digital Firm (16th Edition)
Database System Concepts
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Concepts Of Programming Languages
- .“Dangling and wild pointers are known to be problems with pointers”. Justify the given statement with the helpof suitable examplearrow_forwardAnswer question (d) only a. critically explain why you would consider arrays over normal data types.b. from your explanation in “a” above, define and initialize an array called BongoBar of type string and having 4 elements. c. embed your array in “b” above in a full c++ program and display each of the initialized data when your program executes.d. critically explain what will happen if an amateur programmer decides to called BongoBar[5] in the main function in your program in “c” above.arrow_forwardImplement the following in C++: 1) Differentiate a given function with respect to x. The input will consist of only one line, given in the file input.txt. The input will be just a power, exponential, logarithmic, hyperbolic, trigonometric or an inverse trigonometric function of x. The output should be displayed in the terminal.arrow_forward
- (Chapter 10) Here again is the example used in our lecture to show the difference between scoping with blocks and dynamic scoping; fun g x = let val inc = 1; fun f y = y + inc; fun h z = let val inc = 2; in f z end; in h x end; Annotate it as follows: Draw a circle around every block, and number the blocks. Identify each definition of a name. For each definition, describe its scope in terms of your block numbers. For each occurrence of a name (other than a definition of that name), show which definition is used to bind it. Check that this agrees with your scopes.arrow_forward(In java please) ( NO TOY CLASS PLEASE READ INSTRUCTION OR DOWN VOTE PLEASE LOOK AT TWO IMAGES BELOW FIRST ONE IS CURRENCY CLASS NEED THE SECOND IS INSTRUCTIONS ON HOW TO MAKE TEST CLASS. PLEASE NO TOY CLASS OR DOWN VOTE USE CURRENCY CLASS INSTEAD AND UPDATE IT) THIS A LinkNode structure or class which will have two attributes - a data attribute, and a pointer attribute to the next node. The data attribute of the LinkNode should be a reference/pointer of the Currency class of Lab 2. Do not make it an inner class or member structure to the SinglyLinkedList class of #2 below. A SinglyLinkedList class which will be composed of three attributes - a count attribute, a LinkNode pointer/reference attribute named as and pointing to the start of the list and a LinkNode pointer/reference attribute named as and pointing to the end of the list. Since this is a class, make sure all these attributes are private. The class and attribute names for the node and linked list are the words in bold in #1…arrow_forward[C++ Programming] Please help me. Do not copy other solutions. Please write your own progrmming to avoid doing same with others.arrow_forward
- #17a Simple Statistics (not in textbook) Generate a STL array or vector of 1000 floating point numbers in the range 0 to 1. Compute the mean, median, and standard deviation of these. You may use function objects and lambda expressions, and the STL algorithms for_each, accumulate, and sort. You may not use any of the built-in looping constructs while, do-while or for. Also you may not create and use any ordinary c++ functions. Hint: The following code can be used to populate your random array named arr auto arr_store = [](float& v) { v = rand()/float(RAND_MAX); }; for_each(arr.begin(),arr.end(),arr_store); Warning, You may need to know some details about lambda expressions that are not covered in the textbook. These will be covered in the weekly lecture and in a sample program in Sakai resources. Note: The theoretical value of the standard deviation for large N is sqrt(1.0/12) which about 0.289. You should be getting a value fairly close to this.arrow_forward(Subject: Computer Programming C++) Hello dear, l need to answer in a short time, less than an hour.arrow_forward(Enhancing Class Date) Modify the Date class of Figs. 17.13–17.14 to perform error checking on the initializer values for data members month, day and year. Also, provide a member functionnextDay to increment the day by one. Write a program that tests function nextDay in a loop thatprints the date during each iteration to illustrate that nextDay works correctly. Be sure to test thefollowing cases:a) Incrementing into the next month.b) Incrementing into the next year.arrow_forward
- (JAVA) Create a two-dimensional array of type double to contain the three different SD Marks (JD521, PRG521, and IP521) for six different students. A single array of type String must be used to store the student names (Maxwell, Carl, Gerhard, Paul, James, and Cena). Allow a user to enter in a number, ranging from 1 to 6, which will represent the student position in the table MCSD max and present the marks for each respecting module. The program can only stop when the user enter a number greater than 6. Printout the student name including the JD521, PRG521, and IP521 max, the total of marks and the average of all marks. Use a condition statement to decide that the student has passed or not (Pass rate is 70). The programming language is java Student name JD521 PRG521 IPG521 Maxwell 80 65 70 Carl 95 70 65 Gerhard 87 80 73 Paul 65 45 60 James 45 87 65arrow_forwardIn c++ , apply linear data structure to practical problems. ( Drop coding in words )arrow_forwardc++ problem 1. Explain the concept of a partially filled array, what variables are required and why we would want to use one over a regular array.2. What comparison must occur in any function which adds values to a partially filled array.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education