Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C++ Please, thank you. Create a .h and .cpp for a class called dbiguint. You should have 3 files at this point: cpp, dbiguint.cpp,

In C++ Please, thank you.

  1. Create a .h and .cpp for a class called dbiguint. You should have 3 files at this point: cpp, dbiguint.cpp, and dbiguint.h. To start, fill them in with the header for *dynamic biguint* above, and the .cpp file for the *regular biguint* from last time. You will need to modify the #include in the .cpp file. In both the header and the .cpp, comment out all the functions except the two constructors. We will uncomment things throughout the lab as we add them. In the .cpp file, modify the two existing constructors from biguint so that they allocate memory for the dynamic array. Do the default constructor first, THEN the string constructor. They should allocate exactly as much memory as is needed for the number of digits, with no leading zeros. Test your functions in your main().
  1. Write a new function size() as described in the header. Modify the definitions for the [] operator, and the non-member operator << from biguint to work for dbiguint. Dont forget to uncomment them in the header file. Test your functions in your main().
  2. Write a member function void reserve(std::size_t new_capacity) that changes variables as necessary to increase the capacity to new_capacity. Note that if new_capacity is smaller than the current capacity, this function should do nothing. Put leading zeros in the extra spots. Dont forget to uncomment it in the header file.
  3. Implement += for dbiguint by changing the one you wrote for biguint. Make sure there are no leading zeros in your final result. Dont forget to uncomment it in the header file. Test your functions in your main().
  4. Add a destructor.

dbiguint.h:

#ifndef DBIGUINT_H #define DBIGUINT_H

#include #include

class dbiguint{ public: // pre: none // post: creates a dynamic bigint value 0 dbiguint();

// pre: s[0], ..., s[s.size()-1] are digits // post: creates a dbiguint whose digits are given in s dbiguint(const std::string & s); /*We aren't implementing this constructor yet // pre: none //NEW // post: copy constructor: creates a new dynamic bigint which is // a copy of given dynamic bigint dbiguint(const dbiguint & b); */

// pre: none //NEW // post: returns dynamically allocated memory to heap ~dbiguint();

// pre: none //NEW // post: makes this dynamic bigint a copy of given dynamic bigint void operator =(const dbiguint & b);

// pre: none //NEW // post: returns the size of the memory block of this dbiguint std::size_t size() const;

// pre: none // post: returns the digit at given pos (0 if does not exist) // pos 0 is the least significant (units) digit unsigned short operator [](std::size_t pos) const;

// pre: none // post: returns 0 if this dbiguint equals given dbiguint // 1 if this dbiguint > given dbiguint // -1 otherwise int compare(const dbiguint & b) const;

// pre: none // post: returns a string containing the digits and sign of this dbiguint std::string toStdString() const;

// pre: none // post: adds/subtracts given dbiguint to this dbiguint void operator +=(const dbiguint & b); void operator -=(const dbiguint & b); void operator *=(const dbiguint & b);

// pre: none // post: if newcapacity_ <= capacity_ then do nothing (cannot shrink) // else allocate a new block with size newcapacity_ // copy existing digits and fill the rest with 0 void reserve(std::size_t newcapacity);

private: unsigned short *data_; std::size_t capacity_;

// INVARIANTS: // data_ points to (has the address of) a dynamic array // of capacity_ digits // data_[0] = least significant (units) digits // data_[k] = digit at position k (or 0 if not used) };

// nonmember functions

dbiguint operator +(const dbiguint & a, const dbiguint & b); dbiguint operator -(const dbiguint & a, const dbiguint & b); dbiguint operator *(const dbiguint & a, const dbiguint & b);

bool operator < (const dbiguint & a, const dbiguint & b); bool operator <= (const dbiguint & a, const dbiguint & b); bool operator == (const dbiguint & a, const dbiguint & b); bool operator != (const dbiguint & a, const dbiguint & b); bool operator >= (const dbiguint & a, const dbiguint & b); bool operator > (const dbiguint & a, const dbiguint & b);

std::ostream & operator << (std::ostream & out, const dbiguint & b); std::istream & operator >> (std::istream & out, dbiguint & b);

#endif // DBIGUINT_H

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

Conceptual Database Design An Entity Relationship Approach

Authors: Carol Batini, Stefano Ceri, Shamkant B. Navathe

1st Edition

0805302441, 978-0805302448

More Books

Students also viewed these Databases questions