Changes

MyWikiBiz, Author Your Legacy — Saturday November 23, 2024
Jump to navigationJump to search
updating
Line 5: Line 5:  
#include <iomanip>
 
#include <iomanip>
 
using namespace std;
 
using namespace std;
 +
 +
const int MAX = 50;
    
class BigInt
 
class BigInt
Line 12: Line 14:  
//Precondition:None.
 
//Precondition:None.
 
//Postcondition:The two BigInts have been added together.
 
//Postcondition:The two BigInts have been added together.
 +
friend BigInt operator*(const BigInt& num1, const BigInt& num2);
 +
//Purpose:Operator overloaded, * operator will call MultBigInts.
 +
//Precondition:None.
 +
//Postcondition:The two BigInts have been multiplied together.
 
friend ostream& operator<<(ostream& os, const BigInt& num);
 
friend ostream& operator<<(ostream& os, const BigInt& num);
 
//Purpose:Operator overloaded, << operator will call PrintToBigInt.
 
//Purpose:Operator overloaded, << operator will call PrintToBigInt.
Line 20: Line 26:  
//Precondition:BigInts num1 and num2 are valid.
 
//Precondition:BigInts num1 and num2 are valid.
 
//Postcondition:1.Return true if sum is valid.
 
//Postcondition:1.Return true if sum is valid.
 +
// 2.Return false if there is overflow.
 +
friend bool MultBigInts(const BigInt& num1, const BigInt& num2, BigInt& product);
 +
//Purpose:Multiply two integers together into a product.
 +
//Precondition:BigInts num1 and num2 are valid.
 +
//Postcondition:1.Return true if product is valid.
 
// 2.Return false if there is overflow.
 
// 2.Return false if there is overflow.
 
friend void firstIntegerLarger(const BigInt& largest, const BigInt& smallest, BigInt& sum);
 
friend void firstIntegerLarger(const BigInt& largest, const BigInt& smallest, BigInt& sum);
Line 27: Line 38:  
// 2.If there is overflow (numDigits > MAX) it will be indicated in operator+.
 
// 2.If there is overflow (numDigits > MAX) it will be indicated in operator+.
 
public:
 
public:
static const int MAX = 5;
+
//static const int MAX = 5;
 
BigInt();
 
BigInt();
 
//Default Constructor
 
//Default Constructor
Line 49: Line 60:  
//Precondition:BigInt num is valid.
 
//Precondition:BigInt num is valid.
 
//Postcondition:Data is printed on screen matching the way it was entered from the console.
 
//Postcondition:Data is printed on screen matching the way it was entered from the console.
 +
bool ExtractGoodString(const string& input, string& output);
 +
//Purpose:To make sure the goodstring is valid.
 +
//Precondition:StringToBigInt has been called.
 +
//Postcondition:A valid string is returned.
 
private:
 
private:
void StringToBigInt(const string& goodString);
  −
//Purpose:Convert from characters to numeric data
  −
//Precondition:Assume "goodString" is perfect.
  −
//Postcondition:goodString is converted into numeric data and entered into an array.
   
int Digits[MAX];
 
int Digits[MAX];
 
int numDigits;
 
int numDigits;
Line 73: Line 84:  
void main()
 
void main()
 
{
 
{
BigInt num1, num2;//("12345");
+
BigInt num1, num2;
 
bool moreNumbers = true;
 
bool moreNumbers = true;
 
char update = ' ';
 
char update = ' ';
Line 87: Line 98:  
cout<<"The BigInt entered is: ";
 
cout<<"The BigInt entered is: ";
 
num2.PrintBigInt();
 
num2.PrintBigInt();
 +
cout<<"The product is: "<<num1 * num2;
 
cout<<"The sum is: "<<num1 + num2;
 
cout<<"The sum is: "<<num1 + num2;
 
}
 
}
 
catch(OverFlowException ex)
 
catch(OverFlowException ex)
 
{
 
{
cout<<"The sum has caused OVERFLOW"<<endl;
+
cout<<"The sum or product has caused OVERFLOW"<<endl;
 
}
 
}
   −
cout<<"Do you wish to add more numbers? ";
+
cout<<"Do you wish to continue? ";
 
cin>>update;
 
cin>>update;
 +
cout<<"========================="<<endl;
 
cin.ignore(50,'\n');
 
cin.ignore(50,'\n');
 
if(update == 'Y' || update == 'y')
 
if(update == 'Y' || update == 'y')
Line 120: Line 133:  
getline(cin,inputString);
 
getline(cin,inputString);
   −
len = (int)inputString.length(); //Length of the string entered into len
+
len = (int)inputString.length(); //Length of the string entered into len.
    
if(len > MAX)
 
if(len > MAX)
Line 128: Line 141:  
}
 
}
 
else
 
else
{
+
{
 
StringToBigInt(inputString);
 
StringToBigInt(inputString);
 
tooBig = true;
 
tooBig = true;
Line 166: Line 179:  
return true;
 
return true;
 
}
 
}
 +
}
 +
bool MultBigInts(const BigInt& largest, const BigInt& smallest, BigInt& product)
 +
{
 +
int i, j, k;
 +
int remainder;
 +
BigInt temp, temp2;
 +
bool flag;
 +
 +
temp.numDigits = 0;
 +
temp2.numDigits = 0;
 +
product.numDigits = 0;
 +
 +
for(i = 0; i < smallest.numDigits; i++)
 +
{
 +
remainder = 0;
 +
temp2 = product;
 +
 +
if(temp2.numDigits >= MAX)
 +
return false;
 +
 +
for(j = i; j > 0; j--)
 +
{
 +
temp.Digits[j - 1] = 0;
 +
}
 +
 +
for(k = 0; k < largest.numDigits; k++)
 +
{
 +
temp.Digits[k + i] = smallest.Digits[i] * largest.Digits[k] + remainder;
 +
 +
if(temp.Digits[k + i] >= 10)
 +
{
 +
remainder = temp.Digits[k + i] / 10;
 +
temp.Digits[k + i] = temp.Digits[k + i] % 10;
 +
}
 +
else
 +
remainder = 0;
 +
}
 +
 +
if(remainder >= 1)
 +
{
 +
temp.Digits[k + i] = remainder;
 +
k++;
 +
}
 +
 +
temp.numDigits = (k + i);
 +
flag = AddBigInts(temp2,temp,product);
 +
}
 +
return flag;
 
}
 
}
 
void firstIntegerLarger(const BigInt& largest, const BigInt& smallest, BigInt& sum)
 
void firstIntegerLarger(const BigInt& largest, const BigInt& smallest, BigInt& sum)
Line 222: Line 283:  
for(int i = numDigits-1; i >= 0; i--) //Reverse the string and position of numbers
 
for(int i = numDigits-1; i >= 0; i--) //Reverse the string and position of numbers
 
{
 
{
ch = str[i]; //Convert from char to numeric value
+
ch = str[i]; //Convert from char to numeric value
Digits[counter] = ch - '0'; //char c = '2';
+
Digits[counter] = ch - '0'; //char c = '2';
counter++; //int n = c - '0';
+
counter++; //int n = c - '0';
 
}
 
}
 
}
 
}
Line 237: Line 298:  
else
 
else
 
return sum;
 
return sum;
 +
}
 +
BigInt operator*(const BigInt& num1, const BigInt& num2)
 +
{
 +
BigInt product;
 +
if(MultBigInts(num1,num2,product) == false)
 +
{
 +
OverFlowException ex(num1,num2,'*');
 +
throw ex;
 +
}
 +
else
 +
return product;
 
}
 
}
 
ostream& operator<<(ostream& os, const BigInt& num)
 
ostream& operator<<(ostream& os, const BigInt& num)
123

edits

Navigation menu