Question

In: Computer Science

c++ I have my code mostly finished but when printing it is printing incorrectly so 62345...

c++

I have my code mostly finished but when printing it is printing incorrectly so 62345 should == |0|0|0|0|0|0|0|0|6|2|3|4|5| but it is printing as == 5|4|3|2|6|0|0|0|0|0|0

is it with my

bigint::bigint(const char c[])  or the output operator<< and with the output should i have something like a while loop to check for leading zeros and print them before any addition.

-----------------------------------------------------------------------------------------------------------------------

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;
  friend std::ostream&  operator<< (std::ostream&, const bigint&); //output operator
  friend std::istream& operator>> (std::istream&,  bigint&); //input operator
  bool operator== (const bigint&) const; //Compares two bigints
  bigint operator+(const bigint&) const; //addition operator
  int operator[] (int) const; //subscript operator
private:
  int j_[CAPACITY];
};
 

#endif /* BIGINT_HPP */

---------------------------------------------------------------------------------------------------------------------------

bigint.cpp

#include <iostream>
#include "bigint.hpp"

bigint::bigint(){ // default constructor initializing bigint to zero
  for (int i=0; i<CAPACITY; ++i)
    j_[i] = 0;
}

bigint::bigint(int b): bigint(){  //Initializing a Bigint to an int value
  for(int i=0; i < CAPACITY; ++i){
    j_[i]=b % 10;
    b /=10;
  }
}


bigint::bigint(const char c[]) : bigint(){ //initializing a bigint to a constant char
  //gets size of c array
  int size = 0;
  for (int i = 1; c[i] != '\0'; ++i) {
    ++size;
  }

  //puts c array into bigint
  for (int i = 0; i < CAPACITY; ++i) {
    if (i <= size) {
      j_[i] = c[size - i] - '0';
    } else {
      j_[i] = 0;
    }
  }
}



void bigint::debugPrint(std::ostream& out) const{  // It simply prints out every element of your bigint array starting from the highend and printing a "|" between each value
  int i;
 for(i=0;i<CAPACITY-1; ++i){
   out<<j_[i]<<'|';
 }
 out<<j_[i];
}

bool bigint::operator==(const bigint& rhs) const{  //compare if two bigints are equal. It returns a bool - true if equal and false other wise
  for (int i=0; i<CAPACITY; ++i){
    if (j_[i] != rhs.j_[i])
      return false;
  }
  return true;
}

int bigint::operator[] (int i) const { //subscript operator
  return j_[i];
}


bigint bigint::operator+ (const bigint& rhs) const { //Addition operator
  bigint result;  
  int number = 0;
  bool carry = false;
  for (int i=0; i<CAPACITY; ++i){
    number = 0;
    if (carry) {
    carry = false;
    ++number;
    }
  number = j_[i] + rhs.j_[i] + number;
  if (number >= 10){
    carry = true;
    number %= 10;
  }
  result.j_[i] = number;
  }
  return result; 
}
  

std::ostream& operator<<(std::ostream& out, const bigint& L){ //Overload output operator. takes a stream and bigint as input and write the bigint to the stream. prints atmost 80 digits per line
  int i;
  for (i=0; i<CAPACITY; ++i){
    out<<L.j_[i]<<'|';
    if ((i+1) % 80 ==0)
      out<<std::endl;
  }
  return out;
}

std::istream& operator>>(std::istream& in, bigint& rhs){ //input operator
  char ch, temp [CAPACITY];
  for (int j = 0; j<CAPACITY; j++){
    temp[j]=0;
  }
  in >> ch;
  for (int i=0; i<CAPACITY && ch!=';'; ++i){
    temp[i]=ch;
    in >> ch;
  }

  rhs=bigint(temp);
  return in;
}

---------------------------------------------------------------------------------------------------

main.cpp

        #include 
#include 
#include 
#include "bigint.hpp"

int main() {
  std::ifstream in("data1-1.txt");    // Define stream for input
  if(!in) {                           // Make sure it opened correctly.
    std::cerr << "Could not open data1-1.txt, exiting." << std::endl;
    exit(1);
  }
  bigint (lhs);
  bigint (rhs);
  bigint (result);

  while(!in.eof() && in >> lhs){
    if (!in.eof()) in >> rhs;
        //prints bigints
    std::cout << "lhs = " << lhs << std::endl << std::endl
              << "rhs = " << rhs << std::endl << std::endl;

        //adds bigints and print result
    result = lhs + rhs;
    std::cout << "result = " << result << std::endl << std::endl;
   
  }
    in.close();
  return 0;
}

--------------------------------------------------------------------------------------------------------------------------

data1-1.txt:

10000000000000000000000000000000000345;
299793000000
00000000000000000000067;
4208574289572473098273498723475;
28375039287459832728745982734509872340985729384750928734590827098752938723;
99999999;  99999999;

Solutions

Expert Solution

// 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;

friend std::ostream& operator<< (std::ostream&, const bigint&); //output operator

friend std::istream& operator>> (std::istream&, bigint&); //input operator

bool operator== (const bigint&) const; //Compares two bigints

bigint operator+(const bigint&) const; //addition operator

int operator[] (int) const; //subscript operator

private:

int j_[CAPACITY];

};

#endif /* BIGINT_HPP_ */

//end of bigint.hpp

// bigint.cpp

#include <iostream>

#include "bigint.hpp"

bigint::bigint(){ // default constructor initializing bigint to zero

for (int i=0; i<CAPACITY; ++i)

    j_[i] = 0;

}

bigint::bigint(int b): bigint()

{ //Initializing a Bigint to an int value

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

    j_[i]=b % 10;

    b /=10;

}

}

bigint::bigint(const char c[]) : bigint()

{ //initializing a bigint to a constant char

//gets size of c array

int size = 0;

for (int i = 1; c[i] != '\0'; ++i) {

    ++size;

}

//puts c array into bigint

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

    if (i <= size) {

      j_[i] = c[size - i] - '0';

    } else {

      j_[i] = 0;

    }

}

}

void bigint::debugPrint(std::ostream& out) const{ // It simply prints out every element of your bigint array starting from the highend and printing a "|" between each value

int i;

for(i=0;i<CAPACITY-1; ++i){

   out<<j_[i]<<'|';

}

out<<j_[i];

}

bool bigint::operator==(const bigint& rhs) const{ //compare if two bigints are equal. It returns a bool - true if equal and false other wise

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

    if (j_[i] != rhs.j_[i])

      return false;

}

return true;

}

int bigint::operator[] (int i) const { //subscript operator

return j_[i];

}

bigint bigint::operator+ (const bigint& rhs) const { //Addition operator

bigint result;

int number = 0;

bool carry = false;

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

    number = 0;

    if (carry) {

    carry = false;

    ++number;

    }

number = j_[i] + rhs.j_[i] + number;

if (number >= 10){

    carry = true;

    number %= 10;

}

result.j_[i] = number;

}

return result;

}

std::ostream& operator<<(std::ostream& out, const bigint& L){ //Overload output operator. takes a stream and bigint as input and write the bigint to the stream. prints atmost 80 digits per line

int i;

// loop from end, since index 0 stores the least significant digit and CAPACITY-1 stores the most significant digit

out<<std::endl;

for (i=CAPACITY-1; i>=0; --i){

    out<<L.j_[i]<<'|';

    if (i % 80 ==0)

      out<<std::endl;

}

return out;

}

std::istream& operator>>(std::istream& in, bigint& rhs){ //input operator

char ch, temp [CAPACITY];

for (int j = 0; j<CAPACITY; j++){

    temp[j]=0;

}

in >> ch;

for (int i=0; i<CAPACITY && ch!=';'; ++i){

    temp[i]=ch;

    in >> ch;

}

rhs=bigint(temp);

return in;

}

//end of bigint.cpp

// main.cpp

#include <iostream>

#include <fstream>

#include <cstdlib>

#include "bigint.hpp"

int main() {

std::ifstream in("data1-1.txt");    // Define stream for input

if(!in) {                           // Make sure it opened correctly.

    std::cerr << "Could not open data1-1.txt, exiting." << std::endl;

    exit(1);

}

bigint (lhs);

bigint (rhs);

bigint (result);

while(!in.eof() && in >> lhs){

    if (!in.eof()) in >> rhs;

        //prints bigints

    std::cout << "lhs = " << lhs << std::endl << std::endl

              << "rhs = " << rhs << std::endl << std::endl;

        //adds bigints and print result

    result = lhs + rhs;

    std::cout << "result = " << result << std::endl << std::endl;

}

    in.close();

return 0;

}

//end of main.cpp

Output:

Input file:

Output:


Related Solutions

(Python) This is my code for printing a roster for a team. When I print to...
(Python) This is my code for printing a roster for a team. When I print to the console, it makes the first player's name show up as number 2, and it says [] (its just blank for 1). How can I fix that so the first player's name is 1, not skipping 1 and going to 2. def file_to_dictionary(rosterFile): myDictionary={} myDict=[]    with open(rosterFile,'r') as f: for line in f:    (num,first,last,position)=line.split() myDictionary[num]= myDict myDict=[first, last, position] print (myDictionary) return...
The code following is what I have so far. It does not meet my requirements. My...
The code following is what I have so far. It does not meet my requirements. My problem is that while this program runs, it doesn't let the user execute the functions of addBook, isInList or compareLists to add, check, or compare. Please assist in correcting this issue. Thank you! Write a C++ program to implement a singly linked list of books. The book details should include the following: title, author, and ISBN. The program should include the following functions: addBook:...
How would I make it so that when I run my code it does not ask...
How would I make it so that when I run my code it does not ask for input (not having to enter after the statement and enter 0 for example) after ROXY (Forever ROXY Enterprises) appears? Like it would tell me the second statement right away along with the Roxy phrase. This is in C++. My code: #include / #include using std::endl; int main() {    void readAndConvert();    unsigned int stockSymbol;    unsigned int confNum;    std::cout << "ROXY...
This is my C language code. I have some problems with the linked list. I can't...
This is my C language code. I have some problems with the linked list. I can't store the current. After current = temp, I don't know how to move to the next node. current = current-> next keeps making current into NULL. #include #include #include #include struct node{int data; struct node *next;}; int main() {     struct node *head, *current, *temp, *trash;     srand(time(0));     int randNumber = rand()%51;     if(randNumber != 49)     {         temp = (struct node*)malloc(sizeof(struct node));         current = (struct node*)malloc(sizeof(struct node));...
In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
hello! So I have this CIS assignment lab but when I try to make the code...
hello! So I have this CIS assignment lab but when I try to make the code I don't really know where to start from. My professor is very hard and he likes to see the outcomes as they are shown in the problem. Please help me! Write a program that can be used as a math helper for an elementary student. The program should display two random integer numbers that are to be added, such as:     247 + 129...
This is the code I have. My problem is my output includes ", 0" at the...
This is the code I have. My problem is my output includes ", 0" at the end and I want to exclude that. // File: main.cpp /*---------- BEGIN - DO NOT EDIT CODE ----------*/ #include <iostream> #include <fstream> #include <sstream> #include <iomanip> using namespace std; using index_t = int; using num_count_t = int; using isConnected_t = bool; using sum_t = int; const int MAX_SIZE = 100; // Global variable to be used to count the recursive calls. int recursiveCount =...
I Have posted my Java code below. Fix the toString, add, and remove implementations so that...
I Have posted my Java code below. Fix the toString, add, and remove implementations so that the following test cases work. Note: I have removed all the unnecessary inherited List implementations. I have them to: throw new UnsupportedException(); For compilation, you could also add //TODO. Test (Main) List list = new SparseList<>(); list.add("0"); list.add("1"); list.add(4, "4"); will result in the following list of size 5: [0, 1, null, null, 4]. list.add(3, "Three"); will result in the following list of size...
Below is my source code for file merging. when i run the code my merged file...
Below is my source code for file merging. when i run the code my merged file is blank and it never shows merging complete prompt. i dont see any errors or why my code would be causing this. i saved both files with male names and female names in the same location my source code is in as a rtf #include #include #include using namespace std; int main() { ifstream inFile1; ifstream inFile2; ofstream outFile1; int mClientNumber, fClientNumber; string mClientName;...
So I have written a code for it but i just have a problem with the...
So I have written a code for it but i just have a problem with the output. For the month with the highest temperature and lowest temperature, my code starts at 0 instead of 1. For example if I input that month 1 had a high of 20 and low of -10, and every other month had much warmer weather than that, it should say "The month with the lowest temperature is 1" but instead it says "The month with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT