Basically, this problem is supposed to create a class HugeInteger that uses a 40-element array of digits to store integers as large as 40 digits each. It's also supposed to provide member functions input, output, add, subtract, multiply, divide, and modulus. Also, for comparing HugeInteger objects, it's supposed to provide predicate functions (true or false) isEqualTo, isNotEqualTo, isGreaterThan, isLessThan, isGreaterThanOrEqualTo, isLessThanOrEqualTo, and isZero. When I ran the program, an error occurs at the multiplication part. The program executes the add and subtract functions perfectly, but then I get this error message on the multiplication part:
The error was cited in the last line of my header implementation file (the second set of code below). Here's what I have so far:Code:Run-Time check failure #2 - Stack around the variable 'c' was corrupted.
Code:// definition of HugeInteger class #ifndef HugeInteger_H #define HugeInteger_H class HugeInteger { private: char cTheNumber[41]; bool bIsError; void initializeTheNumber(); public: HugeInteger(); int theLength(); void inputHugeInteger(); void outputHugeInteger(); void addHugeIntegers (HugeInteger,HugeInteger); void subtractHugeIntegers (HugeInteger,HugeInteger); void multiplyHugeIntegers (HugeInteger,HugeInteger); HugeInteger multiplySingleDigit (HugeInteger a, int theDigit, int thePosition); HugeInteger divideHugeIntegers (HugeInteger,HugeInteger); HugeInteger modulusHugeIntegers (HugeInteger,HugeInteger); bool isEqualTo (HugeInteger,HugeInteger); bool isNotEqualTo (HugeInteger,HugeInteger); bool isGreaterThan (HugeInteger,HugeInteger); bool isLessThan (HugeInteger,HugeInteger); bool isGreaterThanOrEqualTo (HugeInteger,HugeInteger); bool isLessThanOrEqualTo (HugeInteger,HugeInteger); bool isZero (HugeInteger); }; #endif
Code:// implementation of HugeInteger class #include"HugeInteger.h" #include<iostream> #include<string> // for strlen const iMAX_LENGTH = 40; using namespace std; HugeInteger::HugeInteger() { initializeTheNumber(); bIsError = false; } void HugeInteger::initializeTheNumber() { for (int i = 0; i < iMAX_LENGTH; i++) cTheNumber[i] = '0'; cTheNumber[iMAX_LENGTH] = '\0'; } int HugeInteger::theLength() { for (int i = 0; i < sizeof cTheNumber-2; i++) //the last 0 is significant { //so preserve it if( cTheNumber[i] == '0'); else break; } return i; } void HugeInteger::inputHugeInteger() { //local variables char cInputString [iMAX_LENGTH+1]; //the characters read in from the input stream bool bFailed = false; int i = 0; //get the value from the input stream cin.getline( cInputString, sizeof cInputString ); //throw out anything exceeding the length cin.sync(); cin.clear(); //see if the string entered is an unsigned integer while(i < strlen(cInputString)-1) { if (!isdigit(cInputString[i])) bFailed = true; i++; } //if it's an integer, set the value of iTheNumber if (!bFailed) { i = strlen(cInputString); while(i >= 0) { cTheNumber[iMAX_LENGTH-strlen(cInputString)+i] = cInputString[i]; i--; } } } void HugeInteger::outputHugeInteger() { for (int x = theLength(); x < sizeof cTheNumber; x++) cout << cTheNumber[x]; } void HugeInteger::addHugeIntegers (HugeInteger a,HugeInteger b) { int currentPosition = 0; int curvala = 0; int curvalb = 0; int n = 0; int value = 0; int carry = 0; currentPosition = iMAX_LENGTH-1; while (currentPosition >= 0) { curvalb = b.cTheNumber[currentPosition]-'0'; curvala = a.cTheNumber[currentPosition]-'0'; n = curvala + curvalb + carry; carry = n/10; value = n%10; cTheNumber[currentPosition] = value + '0'; currentPosition--; } if (carry > 0) // an overflow has occurred { initializeTheNumber(); bIsError = true; } } void HugeInteger::subtractHugeIntegers (HugeInteger a,HugeInteger b) { int currentPosition = 0; int curvala = 0; int curvalb = 0; int value = 0; int borrow = 0; currentPosition = iMAX_LENGTH-1; while (currentPosition >= 0) { curvalb = b.cTheNumber[currentPosition]-'0'; curvala = a.cTheNumber[currentPosition]-'0'; value = curvala-curvalb+borrow; if (value < 0) { value += 10; borrow = -1; } else borrow = 0; cTheNumber[currentPosition] = value + '0'; currentPosition--; } if (borrow < 0) // an underflow has occurred { initializeTheNumber(); bIsError = true; } } void HugeInteger::multiplyHugeIntegers (HugeInteger a, HugeInteger b) { int currentPositiona = 0; int currentPositionb = 0; int theDigit = 0; int curvala = 0; int curvalb = 0; int n = 0; int value = 0; int carry = 0; HugeInteger c,d; currentPositionb = iMAX_LENGTH-1; while (currentPositionb >= 0) { theDigit = b.cTheNumber[currentPositionb]-'0'; c = c.multiplySingleDigit(a,theDigit,currentPositionb); d.addHugeIntegers(d,c); currentPositionb--; } for (int i = 0; i < sizeof cTheNumber - 1; i++) cTheNumber[i] = d.cTheNumber[i] ; } HugeInteger HugeInteger::multiplySingleDigit (HugeInteger a, int theDigit, int thePosition) { int currentPosition = 0; int curvala = 0; int curvalb = 0; int n = 0; int value = 0; int carry = 0; HugeInteger c; currentPosition = iMAX_LENGTH-1; while (currentPosition >= 0) { curvalb = theDigit; curvala = a.cTheNumber[currentPosition]-'0'; n = curvala * curvalb + carry; carry = n/10; value = n%10; c.cTheNumber[currentPosition-(iMAX_LENGTH-1-thePosition)] = value + '0'; currentPosition--; } if (carry > 0) // an overflow has occurred { c.initializeTheNumber(); c.bIsError = true; } return c; }Can somebody help me?Code://Program file to test HugeInteger Class #include "HugeInteger.h" #include <iostream> using namespace std; void pause(); int main() { HugeInteger a; HugeInteger b; HugeInteger c; HugeInteger d; cout << "Enter the Huge Integer a: "; a.inputHugeInteger(); cout << "Enter the Huge Integer b: "; b.inputHugeInteger(); c.addHugeIntegers(a,b); cout << "The Huge Integers added: "; c.outputHugeInteger(); cout << '\n'; c.subtractHugeIntegers(a,b); cout << "The Huge Integers subtracted: "; c.outputHugeInteger(); cout << '\n'; c.multiplyHugeIntegers(a,b); cout << "The Huge Integers multiplied: "; c.outputHugeInteger(); cout << '\n'; pause; return 0; }



LinkBack URL
About LinkBacks



