In: Computer Science
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;
// 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: