Implement a counter.cpp for counter.h /*****************************************
ID: 3655125 • Letter: I
Question
Implement a counter.cpp for counter.h
/**************************************************************************************************************************
Also include the initializations for ZERO etc. at the very bottom of this file
**************************************************************************************************************************/
#include <iostream>
#include <string>
using namespace std;
// A counter object represents a counting number 0, 1, 2 ...
// The number is stored as a sequence of decimal digits in "reverse order" using an array of char.
// Each array element stores the digit's numeric value (0..9), NOT the ASCII value of the corresponding character.
// If the number increases beyond what the current array will hold then we'll dynamically resize the array.
// Thus counter values can grow into thousands or even millions of digits.
class counter {
friend ostream &operator<<(ostream &, const counter &);
// stream insertion operator
friend istream &operator>>(istream & in, counter &c) ;
// stream extraction operator. This should read arbitrarily long numbers.
private:
char * digits; // to hold the digits with digits[i] representing the 10^i's place.
int arraySize; // size of digits array
int numDigits; // number of digits in the number, e.g 5 has one digit but 45 has two. ZERO should have zero digits
void addDigit(char digit, int position, char & carry);
// optional helper function for operator+=
// sets digits[position] to (digits[position] + digit) % 10
// and carry to (digits[position] + digit) / 10
static char toDigit(char c);
// optional helper function for setValue(string)
// return the numeric value of c (e.g. '3' --> 3) or -1 if c < '0' or c > '9'
int convert(int n , int startPosition);
// optional helper function for setValue(int).
// Algorithm:
// if n == 0 return startPosition
// otherwise set digits[startPosition] to n and return convert(n/10, startPosition + 1)
static int sizeOf(int n);
// optional helper function for the constructor and setValue(int).
// Return the number of digits in n
// Algorithm: return 1 if n == 0 otherwise return int(log(n*1.0)/log(10.0)) + 1;
// you may want to define more private helper functions
public:
static const int sizeIncrement = 2; // increase array size by 2 when the value excedes current limit (increment is small for testing purposes)
static const counter ZERO, ONE, TWO, MAXINT; // useful values. See initializations below.
counter (int value = 0);
// default constructor. Allocate the digits array using sizeOf(value), initialize arraySize.
// Then call setValue(value).
counter (string s);
// constructor. Allocate the digits array using s.length(), initialize arraySize.
// Then call setValue(s).
counter (const counter & c);
// copy constructor. Make an exact copy of c.
~counter ();
// destructor. Deallocate the digits array
void reset() ;
// zero out digits array and set numDigits = 0
void resize(int newSize);
// if newSize > arraySize create a new digits array, copy data to the new array and deallocate the old array.
// if newSize <= arraySize then don't do anything
counter & setValue(int n) ;
// set the value of this counter to n.
// Algorithm:
// call resize(sizeOf(n))
// set numDigits to convert(n, 0);
counter & setValue(const string & s);
// call resize(s.length())
// set the counter's value to the number represented by s
// ignore any non-digit characters in s, e.g. "-1@7" --> 17.
void print(ostream & out, bool moreInfo = false) const;
// print the value to output stream. If moreInfo is true then also print numDigits and arraySize
// Be sure that ZERO prints correctly.
// your operator<<() function should call print().
int getArraySize() const;
// return arraySize
int getNumDigits() const;
// return numDigits
bool isZero() const ;
// return true iff *this is equal to ZERO
counter & operator=(const counter & c);
// assignment operator
counter & operator=(int n);
// assignment operator
int compare(const counter & c) const;
// return 1 iff *this > c, -1 iff *this < c, 0 if *this == c
// The overloaded relational operators should call compare()
int toInt() const ;
// return the integer corresponding to this counter
// if *this > MAXINT print an error message and return 0
counter& operator++() ;
// pre-increment
counter operator++(int n) ;
// post-increment
bool operator> (const counter & c) const ;
// greater than
bool operator< (const counter & c) const ;
// less than
bool operator>= (const counter & c) const ;
// greather than or equal
bool operator<= (const counter & c) const ;
// less than or equal
bool operator== (const counter & c) const ;
// equal
bool operator!= (const counter & c) const ;
// not equal
counter& operator--() ; // pre-decrement
counter operator--(int n) ; // post-decrement
counter& operator+=(const counter & c) ;
// set *this to the sum of *this and c
counter operator+(const counter & c);
// return the sum of *this and c
counter& operator-=(const counter & c) ;
// set *this to ZERO if counter > *this, otherwise to the difference
counter operator-(const counter & c) ;
// return ZERO if counter > *this, otherwise return the difference
counter& operator*=(const counter & c) ;
// set *this to the product of *this and c
counter operator*(const counter & c) ;
// return the product of *this and c
counter & mod(const counter & c, counter & div);
// This function should "do the work" for the four functions below.
counter & operator/=(const counter & c);
// set *this to div of *this and c
counter operator/(const counter & c);
// return the div of *this and c
counter & operator%=(const counter & c);
// set *this to the remainder of *this divided by c
counter operator%(const counter & c);
// return the remainder of *this divided by c
};
// Put these in your implementation
/*
const counter counter::ZERO;
const counter counter::ONE(1);
const counter counter::TWO(2);
const counter counter::MAXINT(2147483647);
*/
Explanation / Answer
Think Traugott can answer this?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.