%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c1%22
ID: 3551111 • Letter: #
Question
%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c1%22%20xml%3Alang%3D%0A%22EN-US%22%3EA%20problem%20that%20occurs%20with%20most%20high-level%20programming%0Alanguages%20is%20that%20standard%20numeric%20types%20%E2%80%93%20int%20%2C%20double%E2%80%A6%20-%20have%20a%0Avery%20limited%20range%20because%20the%20represented%20values%20are%20stored%20using%0Aa%20fixed%20number%20of%20bits.%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c2%22%20xml%3Alang%3D%0A%22EN-US%22%3EIn%20this%20project%2C%20you%20will%20design%20and%20implement%20an%20integer%0Atype%20as%20a%20C%2B%2B%20class%2C%20called%20%3Cb%3ELargeInt%20%2C%3C%2Fb%3E%20that%20will%20allow%20users%0Ato%20store%20and%20manipulate%20very%20large%20integers.%20Your%20implementation%0Awill%20use%20a%20dynamic%20array%20to%20store%20all%20the%20digits%20of%20an%20integer%20and%0Awill%20supply%20basic%20I%2FO%2C%20addition%20and%20equality%20operations.%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c2%22%20xml%3Alang%3D%0A%22EN-US%22%3ESpecifically%3A%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c2%22%20xml%3Alang%3D%0A%22EN-US%22%3E1)%20Your%20class%20should%20have%203%20data%20members%3A%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c2%22%20xml%3Alang%3D%0A%22EN-US%22%3E%20%20a)%20a%20variable%20to%20store%20the%20maximum%20size%20of%20the%20array%0Athat%20stores%20the%20large%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c2%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%0Ainteger%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c2%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20b)%20a%20variable%20to%0Astore%20actual%20length%20of%20the%20large%20integer%26nbsp%3B%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c2%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20c)%20an%20int%20array%0Ato%20hold%20the%20large%20integer%20(this%20is%20obviously%20a%20pointer%2C%20since%0Athe%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c2%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Barray%20will%20be%0Adynamic).%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c2%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c2%22%20xml%3Alang%3D%0A%22EN-US%22%3E2)%20Your%20class%20should%20have%20the%20following%0Afunctions%3A%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c2%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%20c4%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c3%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20a)%20An%20appropriate%20constructor%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%20c5%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c3%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20b)%20A%20function%20to%20overload%20the%20%2B%20operator%0Ato%20support%20addition%20of%20LargeInt%20values%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%20c6%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c3%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3Bc)%20A%20function%20to%20overload%20the%20assignment%20operator%0A%3D%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%20c4%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c3%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20d)%20A%20function%20to%20overload%20the%20comparison%0Aoperator%20%3D%20%3D%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%20c4%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c3%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20e)%20A%20function%20to%20overload%20the%20%26lt%3B%26lt%3B%0Aoperator%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%20c4%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c3%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20f)%26nbsp%3B%20A%20function%20to%20overload%20the%0A%26gt%3B%26gt%3B%20operator%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c2%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c2%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c7%22%20xml%3Alang%3D%0A%22EN-US%22%3EFor%20this%20project%20LargeInt%20values%20can%20only%20be%0Apositive.%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c7%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c2%22%20xml%3Alang%3D%0A%22EN-US%22%3ETest%20your%20class%20with%20the%20program%20on%20the%20next%0Apage.%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20xml%3Alang%3D%22EN-US%22%3ENOTE%3A%0ACheck%20your%20GRADING%20CRITERIA%20FOR%20PROJECTS%20posted%20on%20Blackboard%0Abefore%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%0Aturning%20in%20your%20programs.%3C%2Fspan%3E%3C%2Fp%3E%0AExplanation / Answer
You can provide numbers of your choice and the program will sum the two numbers for you
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
class LargeInt
{
public:
//a variable to store the maximum size of the array that stores the large integer
int capacity;
//a variable to store actual length of the large integer
int size;
//Dynamic array to store the integer
int* A;
void setCapacity(int c){
capacity = c;
A = new int[c];
}
LargeInt(){
capacity = 100;
size = 0;
A = new int[capacity];
}
LargeInt(string number){
size = number.length();
const char* num = new char[size];
num = number.c_str();
capacity = 2*size;
A = new int[capacity];
int i;
for(i=0;i<size;i++){
A[size-i-1]=num[i]-'0';
}
}
LargeInt operator+(LargeInt other){
int carry=0;
int i;
int sum;
LargeInt result;
if(size < other.size){
result.setCapacity(other.size*2);
for(i=0;i<size;i++){
sum = A[i] + other.A[i];
result.A[i] = (sum%10) + carry;
carry = sum/10;
}
for(i=size;i<other.size;i++){
result.A[i] = other.A[i] + carry;
carry = result.A[i]/10;
}
result.size = other.size;
if(carry != 0){
result.size++;
result.A[i] = carry;
}
}
else if(size == other.size){
result.setCapacity(other.size*2);
for(i=0;i<size;i++){
sum = A[i] + other.A[i];
result.A[i] = (sum%10) + carry;
carry = sum/10;
}
result.size = size;
if(carry > 0){
result.A[i] = carry;
result.size++;
}
}
else{
result.setCapacity(size*2);
for(i=0;i<other.size;i++){
sum = A[i] + other.A[i];
result.A[i] = (sum%10) + carry;
carry = sum/10;
}
for(i=other.size;i<size;i++){
result.A[i] = A[i] + carry;
carry = result.A[i]/10;
}
result.size = size;
if(carry != 0){
result.size++;
result.A[i] = carry;
}
}
return result;
}//end of + operator overloading
void operator=(LargeInt other){
capacity = other.capacity;
size = other.size;
A = other.A;
}
bool operator==(LargeInt other){
if(size != other.size) return false;
int i;
for(i=0;i<size;i++){
if(A[i] != other.A[i]) return false;
}
return true;
}
void print(){
char* num = new char[size];
int i;
for(i=0;i<size;i++){
num[size-i-1] = A[i] + '0';
}
string number(num);
if(number.length() > size){
number = number.substr(0,size);
}
cout<<number<<endl;
}
};
int main(){
string number1,number2;
cout<<"Enter first Number : ";
cin>>number1;
cout<<"Enter second number : ";
cin>>number2;
LargeInt A(number1);
cout<<"First Number : ";
A.print();
LargeInt B(number2);
cout<<"Second Number : ";
B.print();
LargeInt C = A + B;
cout<<"Number after adding First and Second Number : "<<endl;
C.print();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.