Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#ifndef HW3_BIGUINT_H #define HW3_BIGUINT_H #include #include using namespace std; class BigUInt { private: // Pointer to array. Each element of array contains a digit. //

#ifndef HW3_BIGUINT_H

#define HW3_BIGUINT_H

#include

#include

using namespace std;

class BigUInt {

private:

// Pointer to array. Each element of array contains a digit.

// Array length is equal to the number of digits of the BigUInt.

// data[0] contains the least significant digit.

// For example, if the BigUInt represents the number 385,

// then data[0] is 5, data[1] is 8, data[2] is 3, and length is 3.

unsigned char* data;

// The length of the array data.

int length;

public:

BigUInt();

BigUInt(unsigned int n);

~BigUInt();

void Print();

void TimesTenPow(unsigned int p);

BigUInt& operator+=(const BigUInt& rhs);

friend ostream& operator<<(ostream& os, const BigUInt& b);

// (a) (2 points) Write copy constructor such that this->data is

// pointing to different location as b.data.

BigUInt(const BigUInt& b);

// (b) (2 points) Write assignment operator such that this->data is

// pointing to different location as b.data.

BigUInt& operator=(const BigUInt& b);

// (c) (3 points) Write the += operator such that new value is equal

// to old value + i.

BigUInt& operator+=(unsigned int i);

// (d) (2 points) Implement SetDigit. This function sets the digit at

// position i. It is set to d. Check that the input i is valid.

// Check also that the input d is valid. If either input is invalid,

// this function returns without doing anything.

void SetDigit(int i, int d);

// (e) (3 points) Return a string representing this unsigned integer.

// For example, if the integer is 4892, then return a string "4892".

string ToString();

friend bool operator>(const BigUInt& lhs, const BigUInt& rhs);

};

ostream& operator<<(ostream& os, const BigUInt& b);

// (f) (4 points) Non-member friend function. Returns true of the value of

// lhs is greater than rhs, returns false otherwise.

bool operator>(const BigUInt& lhs, const BigUInt& rhs);

// (g) (4 points) Write a non-member function + that takes a BigUInt and an

// unsigned int, and returns that BigUInt that represents their sum.

BigUInt operator+(const BigUInt& b, unsigned int i);

#endif // HW3_BIGUINT_H

//----------------------------------------------------------------------------------------------------------------

//

//

//

//

//

//

#include

#include "hw3.biguint.h"

using namespace std;

/*** PART 1: CODE FROM HOMEWORK 2. DO NOT MODIFY ***/

BigUInt::BigUInt() {

data = new unsigned char[1];

data[0] = 0;

length = 1;

}

BigUInt::BigUInt(unsigned int n) {

// First pass: Figure out how many digits there are.

int num_digits = 1;

int d = n;

while (d > 10) {

d /= 10;

num_digits++;

}

// Second pass: Create and fill the array of digits.

data = new unsigned char[num_digits];

int pos = 0;

data[pos] = 0;

while (n > 0) {

data[pos] = n % 10;

n /= 10;

pos++;

}

length = num_digits;

}

BigUInt::~BigUInt() {

delete [] data;

}

void BigUInt::Print() {

for (int i = length - 1; i >= 0; i--) {

cout << (int) data[i];

}

cout << endl;

}

ostream& operator<<(ostream&os, const BigUInt& b) {

for (int i = b.length - 1; i >= 0; i--) {

os << (int) b.data[i];

}

os << endl;

return os;

}

void BigUInt::TimesTenPow(unsigned int p) {

int new_length = length + p;

unsigned char* old = data;

data = new unsigned char[new_length];

for (int i = 0; i < p; i++) {

data[i] = 0;

}

for (int i = 0; i < length; i++) {

data[i+p] = old[i];

}

length = new_length;

}

BigUInt& BigUInt::operator+=(const BigUInt& rhs) {

// Part 1: If the other number is bigger, allocate more space.

if (rhs.length > length) {

unsigned char* old = data;

data = new unsigned char[rhs.length];

for (int i = 0; i < length; i++) {

data[i] = old[i];

}

for (int i = length; i < rhs.length; i++) {

data[i] = 0;

}

length = rhs.length;

delete [] old;

}

// Part 2: Sum the two numbers.

unsigned char carry = 0;

for (int i = 0; i < rhs.length; i++) {

data[i] += rhs.data[i] + carry;

if (data[i] >= 10) {

carry = 1;

data[i] -= 10;

} else {

carry = 0;

}

}

if (carry > 0) {

for (int i = rhs.length; i < length; i++) {

data[i] += carry;

if (data[i] >= 10) {

carry = 1;

data[i] -= 10;

} else {

carry = 0;

}

}

}

// Part 3: If last carry is 1, then allocate one more digit.

if (carry > 0) {

unsigned char* old = data;

data = new unsigned char[length+1];

for (int i = 0; i < length; i++) {

data[i] = old[i];

}

data[length] = 1;

length++;

delete [] old;

}

return *this;

}

BigUInt::BigUInt(const BigUInt& b) {

length = b.length;

data = new unsigned char[length];

}

BigUInt& BigUInt::operator=(const BigUInt& b) {

if (length > 0) delete [] data;

length = b.length;

data = new unsigned char[length];

return *this;

}

/*** PART II: WRITE YOUR HOMEWORK 3 CODE BELOW THIS LINE ***/

//

//

//

//

//

//

//----------------------------------------------------------------------------------------------------------------

#include

#include "hw3.biguint.h"

using namespace std;

void func(BigUInt b) {

b.SetDigit(3, 5);

b.SetDigit(4, 7);

cout << b << endl;

}

int main() {

BigUInt b1(88408721);

func(b1);

cout << b1 << endl;

string s = b1.ToString();

cout << s << endl;

BigUInt b2(872);

b2 += 134908571;

cout << b2 << endl;

BigUInt b3(645050);

BigUInt b4(10050385);

b4 = b3;

b4.SetDigit(0, 9);

cout << b3 << " " << b4 << endl;

BigUInt b5(3459950);

BigUInt b6(8385);

if (b5 > b6) cout << "5 greater" << endl;

if (b6 > b5) cout << "6 greater" << endl;

BigUInt b7(3453450);

BigUInt b8(3458385);

if (b7 > b8) cout << "7 greater" << endl;

if (b8 > b7) cout << "8 greater" << endl;

BigUInt b10(3459950);

BigUInt b11(3459950);

if (b10 > b11) cout << "10 greater" << endl;

if (b11 > b10) cout << "11 greater" << endl;

BigUInt b12(99832395);

cout << b12 << endl;

b12.TimesTenPow(7);

cout << b12 << endl;

BigUInt b13(10);

b13 = b12 + 170814972;

cout << b13 << endl;

return 0;

}

/*

Result:

88475721

88408721

88408721

134909443

645050 645059

5 greater

8 greater

99832395

998323950000000

998324120814972

*/

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Fred R. McFadden, Jeffrey Slater, Mary B. Prescott

5th Edition

0805360549, 978-0805360547

More Books

Students also viewed these Databases questions

Question

Discuss the nature of the major federal labor relations laws.

Answered: 1 week ago