Question

In: Computer Science

Language: C++ I am starting to make a Bigint ADT and i have the hpp file...

Language: C++

I am starting to make a Bigint ADT and i have the hpp file finished now i need to make the methods for the functions.

Problem:
The data type int in C++ is limited to the word size of the CPU architecture (e.g., 32 or 64 bit). Therefore you can only work with signed integers up to 2,147,483,647 (in the case of signed 32 bit). Unsigned 32 bit is still only 10 digits. Maxint for 64 is somewhat larger at 9,223,372,036,854,775,807 but still only 19 digits. Clearly, this causes difficulties for working with very large integer values (say 100 digits). Your job is to develop an ADT (called bigint) that can take any size postive integer. It will work for 100, 200, 500, etc. digit integers.

Representation is a key issue for this assignment. We recommend an array of integers, with each element representing one single digit (0 to 9) of the big number. One could use an array of char, but the memory savings is pretty minimal. Placing the values in the array is the interesting part. The naïve representation makes storing the bigint easy but makes the operations (add and multiply) very difficult to implement. A slightly more clever representation makes storing the big number a little bit harder but makes implementing the operations way easier.

Arrays are typically drawn to be read left to right with the 0 element on the left and the largest on the right. However, arrays are a completely made up concept and are not physical in nature. So you can draw them and think about them anyway you want. For this problem having the right side as the 0 element and the left side as the largest makes much more sense.

Take the example of the number 299,793. We show how it is stored in the array below. The 3 is in the one's position, the 9 in the 10's position and so on. This neatly corresponds to the index of the array. The addition and multiple algorithms given below use this representation.

bigint

Index: n ... 7 6 5 4 3 2 1 0
Place: 10^n's ... 10^7's 10^6's 10^5's 10000's 1000's 100's 10's 1's
Value: 0 ... 0 0 2 9 9 7 9 3

Directions:

  • The capacity of the bigint must be specified by a global constant CAPACITY, use: const int CAPACITY = 400;
  • A default constructor to initialize a bigint to zero.
  • A constructor to initialize a bigint to an int value you provide [0, maxint]. Example: bigint(128).
  • A constructor to initialize a bigint to a const char[] you provide. You can assume what is provided is a valid bigint. Example: bigint("299793").
  • Develop a method called debugPrint that will be helpful for debugging your bigint. Use a method defintition of void debugPrint(std::ostream&) const; It simply prints out every element of your bigint array starting from the highend (e.g., capacity-1) of the bigint to zero. Printing a "|" between each value will also be pretting helpful to help with debugging.
  • Overload output operator<< as a friend or free function, so that takes a stream and bigint as input and writes the bigint to the stream. It will print at most 80 digits per line. No leading zeros are to be printed.
  • Overload operator== to compare if two bigints are equal. It returns a bool - true if equal and false otherwise.
  • using namespace std; is stricly forbiden. As are any global using statements.
  • You can NOT use a pre-defined library or built in class (such as std::vector or std::string) to solve this problem. Use a standard array to solve the problem. You also do not need to solve any part of this problem.

bigint.hpp:

#ifndef BIGINT_HPP
#define BIGINT_HPP

const int CAPACITY = 400;

class bigint {
public:
bigint(); //default constructor
bigint(int);
bigint(const char[]);
void debugPrint(std::ostream&) const;
bool operator<< (const bigint&) const;
bool operator== (const bigint&) const;
private:
int j_[CAPACITY];
int num;
};

bigint.cpp: // start of method file

#include
#include "bigint.hpp"

bigint::bigint(){
for(int i=0;i j_[i]=0;
}
}

bigint::bigint(int){

}

bigint::bigint(const char[]){
}

Solutions

Expert Solution

Please find the explanation int the comments in the functions.

The code is tested;

#include "Header.h"

//since size of zero int is not given just set the num to zero;
bigint::bigint()
{
   num = 0;
}

//given a number in
bigint::bigint(int in)
{
   //declare an array of capacity
   int arr[CAPACITY];
   num = 0;
   while (in)
   {
       //extract the last digit of the in
       arr[num] = in % 10;
       //remove the last digit from in
       in = in / 10;
       //count the number of digits remaining
       num++;
   }

   //the number collected in arr is reverse of the original number so reverse
   // and store in j_
   for (int i = 0; i < num; i++)
   {
       j_[i] = arr[num - i - 1];
   }
}

bigint::bigint(const char n[])
{
   num = 0;
   int i = 0;
   //nzFound stores if we are iterating over leading zeros.
   bool nzFound = false;
   while (n[i] != '\0')
   {
       //as long as iterating over leading zeros, dont store that digit
       if (!nzFound && n[i] == '0')
       {
           i++;
           continue;
       }
       //since finding first non zero digit set the flag
       //so that all upcoming zeros should be stored in j_
       nzFound = true;
       //converting ascii value to integer
       j_[num] = n[i]-'0';
       num++;
       i++;
   }
}

void bigint::debugPrint(std::ostream& o) const
{
   int i;
   for (i = 0; i < num-1; i++)
   {
       o << j_[i] << '|';
   }
   o << j_[i];
}

bool bigint::operator== (const bigint &b) const
{
   if (num != b.num)
       return false;
   for (int i = 0; i < num; i++)
   {
       if (j_[i] != b.j_[i])
           return false;
   }
   return true;
}

bool operator<<(std::ostream o, const bigint &b)
{
   int i;
   for (i = 0; i < b.num; i++)
   {
       o << b.j_[i] << '|';
       //print every 80th char to be \n thus not printing more than 80 digits per line.
       if ( (i + 1) % 80 == 0)
           o << '\n';
   }
   return true;
}


Related Solutions

For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
C programming language. **I am aware that I am only supposed to ask one question so...
C programming language. **I am aware that I am only supposed to ask one question so if you cant do all of this could you please do part 2? thank you! This lab, along with your TA, will help you navigate through applying iterative statements in C. Once again we will take a modular approach to designing solutions to the problem below. As part of the lab you will need to decide which C selection structure and iterative structure is...
C programming language. **I am aware that I am only supposed to ask one question so...
C programming language. **I am aware that I am only supposed to ask one question so if you cant do all of this could you please do part 3? thank you! This lab, along with your TA, will help you navigate through applying iterative statements in C. Once again we will take a modular approach to designing solutions to the problem below. As part of the lab you will need to decide which C selection structure and iterative structure is...
I am trying to make a program in Python that opens a file and tells you...
I am trying to make a program in Python that opens a file and tells you how many lines, vowels, consonants, and digits there are in the file. I got the print out of lines, digits, and consonants, but the if statement I made with the vowels list isn't recognizing them. How can I make the vowels go through the if statement with the list? Am I using the wrong operator? Here is my program #Assignment Ch7-1 #This program takes...
IN PROGRAMMING LANGUAGE C -I am trying to alphbetize a string in descending or to EX...
IN PROGRAMMING LANGUAGE C -I am trying to alphbetize a string in descending or to EX INPUT: B C D A OUTPUT: D C B A #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]) {         int MAX = 100000;         int i =0;         int k =0;         int j =0;         char array[MAX];         char split[] = " ,.-!?()0123456789";         int n = 0;         char second[MAX];         printf("Please enter in a String: ");...
I am building a game in C programming language where I need to add objects of...
I am building a game in C programming language where I need to add objects of various length into a game board. The game board is 8X8 and we must account for the boundaries for the board and not go over them with our objects. The boards upper left corner is at 0x0 and we must return 1 if it fits and -1 if it does not fit. I have the following 2 functions to start with: ```int add_object_vert(int r,...
A C PROGRAM *Edit/Update I do not need the file loading script, but I am not...
A C PROGRAM *Edit/Update I do not need the file loading script, but I am not against it being included in the answer* I must read off of an excel file (comma separated) to input five different things for a book, all comma separated, there are 360 books in the file. The book title, author, ISBN number, number of pages, and finally the year it was published. Now some of the ISBN or Pg numbers may be missing and will...
(LANGUAGE: C++) 1)     For this lab, you will fill in what’s missing to make the attached file...
(LANGUAGE: C++) 1)     For this lab, you will fill in what’s missing to make the attached file cust_leads.cpp work 2)     The Lead class is for a very limited amount of information about a potential customer ·       Include string fields for name and email ·       A constructor that takes values for these two fields ·       Include a getter for the email field ·       Overload the == operator ·       2 Lead’s are equal if the names are the same (the emails don’t need to match) ·       the first few lines...
Hello this is for C++ language. I am currently stuck on creating my api for Day...
Hello this is for C++ language. I am currently stuck on creating my api for Day Trading Stocks. as follows I need an api for *//Function Signature * * parameter: * * Return Value: ** *// Write the following function taking in an integer vector (vector &prices) consisting of all prices, in chronological order, for an hypothetical instrument. Your function recommends the maximum profit an investor can make by placing AT MOST one buy and one sell order in the...
Hey, I have a code that I am working on to make a garden plot calculator,...
Hey, I have a code that I am working on to make a garden plot calculator, however I am reaching an error that I cannot seem to get past. Please help. import math # GarednPlot contains all the utility functions class GardenPlot: # constructor function: Welcomes the user and sets default values def __init__(self): print("Welcome!") self.length = 0 self.radius = 0 self.depth = 0 # Sets the length of the GardenPlot and returns that length def setLength(self): self.length = float(input("Enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT