0

Here's my code:
bigInt.cpp

/
//  bigInt.cpp

#include "bigInt.h"

bigInt::bigInt() {

  int i;

   for(i = 0; i < SIZE; i++)   //Sets all the values of digits to 0.
  digits[i] = 0;
  sign = true;
  numDigits = 1;

  error1 = true;
  overflowFlag = false;

}

bigInt::bigInt(const char temp []) {

  int i;
  int tempSize = strlen(temp);
  numDigits = 1;
  error1 = true;
  sign = true;

  if(tempSize > 30)
    error1 = false;

  if(temp[0] == '-') { //If there's a negative symbol set the sign to false
      sign = false;

    if(tempSize > 31) //Add an extra space so the - doesn't count
      error1 = false;

  for(i = 0; i < SIZE; i++){
    if(i < tempSize - 1)
  if ( !(temp[tempSize-i - 1] >= 48 && temp[tempSize-i - 1] <= 58)){ //Check for errors
  error1 = false;
  }
  else {
  digits[i] = temp[tempSize-i-1] - '0';
  if(digits[i]!=0)
    numDigits = i+1;

  }
    else
  digits[i] = 0;
  }
  }
  else{    

  for(i = 0; i < SIZE; i++){
    if(i < tempSize)
  if ( !(temp[tempSize-i-1] >= 48 && temp[tempSize-i-1] <= 58)){ //Check for errors
  error1 = false;
  }
  else {
  digits[i] = temp[tempSize-i-1] - '0';
  if(digits[i]!=0)
    numDigits = i+1;

  }
    else
  digits[i] = 0;
  }

  }

}

bigInt::bigInt(const string temp) {
  int i;
  int tempSize = temp.length();
  numDigits = 1;
  error1 = true;
  sign = true;

if(tempSize > 30)
    error1 = false;

  if(temp[0] == '-') {//If there's a negative symbol set the sign to false
      sign = false;

    if(tempSize > 31)//Add an extra space so the - doesn't count
      error1 = false;

  for(i = 0; i < SIZE; i++){
    if(i < tempSize - 1)
  if ( !(temp[tempSize-i - 1] >= 48 && temp[tempSize-i - 1] <= 58)){ //Check for errors
  error1 = false;
  }
  else {
  digits[i] = temp[tempSize-i-1] - '0';
  if(digits[i]!=0)
    numDigits = i+1;

  }
    else
  digits[i] = 0;
  }
  }
  else{    

  for(i = 0; i < SIZE; i++){
    if(i < tempSize)
  if ( !(temp[tempSize-i-1] >=48 && temp[tempSize-i-1] <= 58) || temp[tempSize-i-1] == '-'  ){//Check for errors
  error1 = false;
    if(sign == false)
    error1 = true;
  }

  else {
  digits[i] = temp[tempSize-i-1] - 48;

  if(digits[i]!=0)
    numDigits = i+1;
  }
    else
  digits[i] = 0;
  }

  }

}

int bigInt::length() const {

    return numDigits; //Returns the number of digits
}

bool bigInt::overflow() const {

return overflowFlag; //Overflow check
}

bool bigInt::error() const {

return !error1;//Error checking
}

bool bigInt::negative() const {

return !sign;//Sign checking
}

string bigInt::toString() const {

  string output = "";
if(sign == false) //If there's a negative symbol it will be outputted
output = "-";

  for(int i = numDigits - 1; i >= 0; i--) {
    if ((i) % 3 == 2 && i < numDigits - 1)
  output += ','; //Add commas
    output += digits[i] + '0';
  }

return output;

}

int bigInt::compare(const bigInt& x) const {

for(int i = SIZE - 1; i >= 0; i--){

     if(sign == false && x.sign == true)
      return -1;
      else if(x.sign == false && sign == true)
      return 1;
        else if(sign == false && x.sign == false){
        if(digits[i] > x.digits[i])
      return -1;
      else if(digits[i] < x.digits[i])
        return 1;
        }
        else if(digits[i] < x.digits[i])
          return -1;
          else if(digits[i] > x.digits[i])
             return 1;

}
    return 0;
}

bigInt bigInt::add(const bigInt& x) const {

  int carry = 0;
  int i;
  bigInt output;

  for(i = 0; i < SIZE; i++) {
    int value;

    if(sign == false && x.sign == false)
  output.sign = false;

    value = digits[i] + x.digits[i] + carry;
    carry = 0;
    carry = value / 10;
    output.digits[i] = value % 10;

    if(value != 0)
  output.numDigits = i + 1;
  }
  if (output.numDigits == SIZE && carry){
    output.overflowFlag = true;
    output.numDigits = 1;
    for (i = 0; i < SIZE; i++) {
        if(output.digits[i] != 0)
  output.numDigits=i+1;
  }
  }
  return output;

}

bigInt bigInt::sub(const bigInt& x) const {

  int value;
  int i;

  bigInt output;
  bigInt temp;
  cout << "X num digits = " << x.numDigits << endl;
  cout << "Num digits = " << numDigits << endl;
  for(i = 0; i < SIZE; i++) {

    if(sign == false  && x.sign != false){
  value = digits[i] +  x.digits[i];
  output.sign = false;}
  else if(x.sign == false && sign != false)
  value = digits[i] +  x.digits[i];

 

   else
    if(digits[i] < x.digits[i]){

  value = (digits[i] + 10) - x.digits[i];
  output.digits[i] = value;
  output.digits[i + 1] = digits[i + 1] -1;

  }

    else if(digits[i] >= x.digits[i]){

  value = digits[i] - x.digits[i];
  cout << "value" << value << endl;
  output.digits[i] = value;}

    if(value != 0)
  output.numDigits = i + 1;

  }
  if (output.numDigits == SIZE){
    output.overflowFlag = true;
    output.numDigits = 1;
    for (i = 0; i < SIZE; i++) {
        if(output.digits[i] != 0)
  output.numDigits=i+1;
  }
  }
  return output;
}

bigInt.h

#include <iostream>
#include <cstring>
#include <cmath>
#define SIZE 30
using namespace std;

class bigInt {

public:
   bigInt();
   bigInt(const char[]);
   bigInt(const string);
   int length() const;
   bool overflow() const;
   bool error() const;
   bool negative() const;
   string toString() const;
   int compare(const bigInt&) const;
   bigInt add(const bigInt&) const;
   bigInt sub(const bigInt&) const;

   private:
  int digits[SIZE];
  bool sign; //True for positive, false for negative.
  bool error1;
  int numDigits;
  bool overflowFlag;

   };

main.cpp

#include "bigInt.h"
#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{

  bigInt a;
  bigInt b;
  bigInt c;

  // Default Constructor & toString() Tests
  cout << "a = " << a.toString() << endl;
  cout << "b = " << b.toString() << endl;
  cout << "c = " << b.toString() << endl;
  cout << endl;

  string userInput;

  // Constructor Test (string w/ error checking)
  cout << "Input a big int >" << endl;
  cin >> userInput;
  a = bigInt(userInput);

  while(a.error()) {
    cout << "Invalid input!" << endl;
    cout << "Input a big int >" << endl;
    cin >> userInput;
    a = bigInt(userInput);
  }
  cout << endl;

  // Constructor Test (char[] w/ error checking)
  cout << "Input a big int >" << endl;
  cin >> userInput;
  b = bigInt( userInput.c_str() );

  while(b.error()) {
    cout << "Invalid input!" << endl;
    cout << "Input a big int >" << endl;
    cin >> userInput;
    b = bigInt( userInput.c_str() );
  }
  cout << endl;

  // Length Test
  cout << setw((int)max(a.toString().length(), b.toString().length()));
  cout << a.toString() << " has " << a.length() << " digits" << endl;
  cout << setw((int)max(a.toString().length(), b.toString().length()));
  cout << b.toString() << " has "<< b.length() << " digits" <<endl;
  cout << endl;

  // +/- Test
  cout << setw((int)max(a.toString().length(),b.toString().length()));
  cout << a.toString() << " is " << (a.negative()?"NEGATIVE":"POSITIVE") <<endl;
  cout << setw((int)max(a.toString().length(),b.toString().length()));
  cout << b.toString() << " is " << (b.negative()?"NEGATIVE":"POSITIVE") <<endl;
  cout << endl;

  // Comparison Test
  if (a.compare(b) == 0)
    cout << a.toString() << " == " << b.toString() << endl;
  else if (a.compare(b) < 0)
    cout << a.toString() << " < " << b.toString() << endl;
  else
    cout << a.toString() << " > " << b.toString() << endl;
  cout << endl;

  // Addition Test (w/ Overflow check)
  c = a.add(b);
  cout <<" " << setw(41) << a.toString() << endl;
  cout <<"+" << setw(41) << b.toString() << endl;
  cout << setfill('-') << setw(42) << "" << endl;
  cout << setfill(' ');
  cout << setw(42) << c.toString();
  cout << (c.overflow()?" [OVERFLOW]":"") << endl;
  cout << endl;
  cout << endl;

  // Subtraction Test (w/ Overflow check)
  c = a.sub(b);
  cout <<" " << setw(41) << a.toString() << endl;
  cout <<"-" << setw(41) << b.toString() << endl;  
  cout << setfill('-') << setw(42) << "" << endl;
  cout << setfill(' ');
  cout << setw(42) << c.toString();
  cout << (c.overflow()?" [OVERFLOW]":"") << endl;
  cout << endl;

  return 0;
}

I'm having problems subtracting. My subtracting function is in the bigInt.cpp file at the very bottom. I have it so it should take away 1 from the left digit when the digit to the right is smaller than the one on the bottom but it doesn't seem to be working. The +10 works, but the borrowing part isn't.

1
Contributor
1
Reply
5
Views
5 Years
Discussion Span
Last Post by pwnerboy
0

When i do 258 and 209, it goes to the sub function and I end up getting 59 instead of 49. For some the 5 isn't getting subtracted

Edited by pwnerboy: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.