اینم عیدیه ما تمام عملگر های اصلی برای ارقام بزرگ پیاده سازی شده برید اساسی حالشو ببرین البته جا برای تحلیل داره
#include<iostream>
#include<string>
using namespace std;
class INT {
int digits[100];
char sign;
INT& check(); // eliminates a negative zero (new) In Progress...
public:
INT( ); // Completed...
INT(int num); // Completed... convert a int to INT
INT(string s); // In Progress... convert a string to INT
bool operator<(const INT&) const; // Compelted...
bool operator>(const INT&) const; // Compelted...
bool operator==(const INT&) const; // Completed...
bool operator!=(const INT&) const; // Completed...
bool operator <=(const INT&) const; // Completed...
bool operator >=(const INT&) const; // Completed...
operator int() const; // Completed...
operator string() const; // In Progress... INT to string
friend INT operator%(const INT&, const INT&); // Completed...
friend INT operator+(const INT&, const INT&); // Completed...
friend INT operator-(const INT&, const INT&); // Completed...
friend INT operator*(const INT&, const INT&); // Completed...
friend INT operator/(const INT&, const INT&); // Completed...
friend ostream& operator<<(ostream&, const INT&); // Completed...
friend istream& operator>>(istream&, INT&); // Completed...
};
INT::INT( ){
sign = 'p';
for (int a=0; a<100; a++)
digits[a] = 0;
}
INT::INT(int num){
sign = 'p';
num = 0;
}
INT::INT(string s){ //convert a string to INT
int m=99, g, b;
if(s[0] =='-'){b=1; sign='n';}
else {b=0; sign='p';}
for(int a=(s.length()-1); a>=b; a--){
g=s[a]-48;
digits[m]=g;
m--;
}
}
INT::operator int() const{ // In Progress...
int num=0, multiply = 1, newnum = 1;
for ( int a = 99; a > 0; a --){
num += digits[a] * multiply;
multiply *= 10;
}
if ( sign == 'n') return (num * -1);
else return num;
}
INT::operator string() const{
string s;
char digchar;
digchar = (int)digits[99];
cout << digchar << endl;
// cout << digits[99];
cout << endl;
//for(int a = 99; a >= 0; a--)
//{
// s[a] = "digits[a]";
// cout << s[a] << endl;
//}
//cout << s << endl;
//cout << " ^s" << endl;
return s;
}
// Less Than " < " Completed...
bool INT::operator<(const INT& z) const{
if ( operator>(z) == true || operator==(z) == true) return false;
else return true;
}
// Greater Than " > " Completed...
bool INT::operator>(const INT& z)const{
string num1, num2;
int a = 0;
if ( operator==(z) == true) return false;
if(sign == 'p' && z.sign == 'n') return true;
if(sign == 'n' && z.sign == 'p') return false;
if(sign == 'p' && z.sign == 'p'||sign == 'n' && z.sign == 'n'){
bool flag = false;
bool flag2 = false;
bool flag3 = false;
while (a<100 && flag3==false) { // figure out which one is largest
if (digits[a] < z.digits[a]) { num2 = "L"; num1 = "S"; flag3 = true; }
if (digits[a] > z.digits[a]) { num1 = "L"; num2 = "S"; flag3 = true; }
a++;
}
if ( num1 == "S" && num2 == "L" ) return false;
if ( num1 == "L" && num2 == "S" ) return true;
}
}
// Equal to " == " Completed...
bool INT::operator==(const INT& z) const{
if(sign == 'p' && z.sign == 'n') return false;
if(sign == 'n' && z.sign == 'p') return false;
if(sign == 'p' && z.sign == 'p'||sign == 'n' && z.sign == 'n'){
for ( int a = 0; a < 100; a++){
if( digits[a] != z.digits[a]) return false;
}
return true;
}
}
// Not Equal to " != " Completed...
bool INT::operator!=(const INT& z) const{
if ( operator==(z) == true ) return false;
else return true;
}
// Less Than or Equal to " <= " In Progress...
bool INT::operator <=(const INT& z ) const{
if ( operator==(z) == true || operator<(z) == true) return true;
else return false;
}
// Greater Than orEqual to " >= " In Progress...
bool INT::operator >=(const INT& z ) const {
if ( operator==(z) == true || operator>(z) == true) return true;
else return false;
}
// Modulation...Completed...
INT operator%(const INT& x, const INT& y){
INT modX, modY, modZ, prod1, prod2;
modX.sign=x.sign;
modY.sign=y.sign;
modZ.sign= 'p';
prod1.sign= 'p';
prod2.sign= 'p';
for(int a=0; a < 100; a++){
modX.digits[a]= x.digits[a];
modY.digits[a]= y.digits[a];
modZ.digits[a]= 0;
prod1.digits[a] = 0;
prod2.digits[a] = 0;
}
//if(modX.operator>(modY))
// if(modX > modY){
prod1 = operator/(modX, modY);
prod2 = operator*(prod1, modY);
modZ = operator-(modX, prod2);
// }
return modZ;
}
// Addition...Completed...
INT operator+(const INT& x, const INT& y) {
INT result;
if (x.sign == y.sign) {
result.sign = x.sign;
int a, carry = 0, total;
for (a=99; a>=0; a--) {
total=x.digits[a]+y.digits[a]+carry;
if (total > 9) carry = 1;
else carry = 0;
result.digits[a] = total % 10;
}
}
else {
INT larger, smaller;
int a=0;
bool flag=false;
while (a<100 && flag==false) { // figure out which one is largest
if (x.digits[a] < y.digits[a])
{ larger = y; smaller = x; result.sign = y.sign; flag = true; }
if (x.digits[a] > y.digits[a])
{ larger = x; smaller = y; result.sign = x.sign; flag = true; }
a++;
}
if (flag) { // add them unless flag is false (the two were equal)
for (int b=99; b>=0; b--) {
if (smaller.digits[b] > larger.digits[b]) { // need to borrow
int c = b-1;
while (larger.digits[c] == 0) {
larger.digits[c] = 9;
c = c - 1;
}
larger.digits[c] = larger.digits[c] - 1;
larger.digits[b] = larger.digits[b] + 10;
}
result.digits[b] = larger.digits[b] - smaller.digits[b];
}
}
}
return result;
}
// Subtraction...Completed...
INT operator-(const INT& x, const INT& y) {
INT subX, subY;
subX.sign=x.sign;
if(y.sign=='p') subY.sign='n'; // Swaps the sign for the second variable
if(y.sign=='n') subY.sign='p';
for(int a=0; a < 100; a++){subX.digits[a]=x.digits[a]; subY.digits[a]=y.digits[a];}
return operator+(subX, subY);
}
// Multiplication...Completed...
INT operator*(const INT& x, const INT& y){
INT result, array;
int count=0, count2=0, carry = 0, total, num=0, a=0, b=0;
if (x.sign == y.sign)
result.sign = 'p';
else
result.sign = 'n';
for (a=99; a>=0; a--) {
total = (x.digits[a] * y.digits[99]) + carry;
if (total > 9) { carry = total/10; total = total % 10; }
else carry = 0; result.digits[a] = total;
}
for (b=98; b>=0; b--) { count = b; count2 = b;
for (int c=99; c>=0;c--) array.digits[c] = 0; carry = 0;
for (a=99; count>=0; a--, count--) {
while (count2 < a) { array.digits[a] = 0; count2++; }
total = (y.digits[b] * x.digits[a]) + carry;
if (total > 9) { carry = total/10; total = total % 10; }
else carry = 0;
array.digits[count] = total;
}
carry = 0;
for (int c=99; c>=0; c--) { total = result.digits[c] + array.digits[c] + carry;
if (total > 9) { carry = total/10; total = total % 10; }
else carry = 0;
result.digits[c] = total;
}
} // End for
return result;
}
// Division...Completed...
INT operator/(const INT& x, const INT& y)
{ INT result,temp_product,temp_extracted,temp_quotient,t emp_dif,guess,temp_constant;
int x_init_pos=0,y_init_pos=0,sizex,sizey,p=0;
//to find how big x is and y is.
int b;
for( b=0;x.digits[b]==0;b++) x_init_pos++;
for(b=0;y.digits[b]==0;b++) y_init_pos++;
//to determine the sign of the result
sizey=100-y_init_pos;
sizex=100-x_init_pos;
for(int a=0;a<sizey;a++)
temp_extracted.digits[y_init_pos+a]=x.digits[x_init_pos+a];
for(int c=0;c<=sizex-sizey;C++)
{
guess.digits[99]=9;
temp_product=y*guess;
if(temp_product.sign=='n')
temp_product.sign='p';
temp_dif=temp_extracted-temp_product;
if(temp_dif.sign=='n') p=1;
if(p == 1) {
while(p==1){
guess.digits[99]=guess.digits[99]-1;
temp_product=guess*y;
if(temp_product.sign=='n') temp_product.sign='p';
temp_dif=temp_extracted-temp_product;
if(temp_dif.sign=='n') { INT temp_dif; p=1; }
else p=0;
}
}
temp_quotient.digits[100-(sizex-sizey)+c-1]=guess.digits[99];
int e;
for( e = 0; temp_dif.digits[e] == 0 && e < 99; e++ )
{}
INT shifted_value;
if(e==100&&temp_dif.digits[99]==0) {
e=99;
shifted_value.digits[99]=0;
}
for(int shift_value = e-1,m=0;shift_value<99;shift_value++,m++) {
shifted_value.digits[shift_value]=temp_dif.digits[e+m];
}
shifted_value.digits[99]=x.digits[100-(sizex-sizey)+c];
for(int h=0;h<100;h++)
temp_extracted.digits[h]=shifted_value.digits[h];
}
for(int k=0;k<100;k++)
result.digits[k]=result.digits[k]+temp_quotient.digits[k];
if(x.sign!=y.sign)
result.sign = 'n';
return result;
}
// Output Stream...Completed...
ostream& operator<< (ostream& os, const INT& num) {
int a = 0;
if (num.sign == 'n') os << '-';
while (a < 99 && num.digits[a] == 0)
a++;
while (a < 100)
os << num.digits[a++];
return os;
}
// Input Stream...Completed...
istream& operator>> (istream& is, INT& num) {
string number;
is >> number;
if (number.at(0) == '-') num.sign='n';
if (number.at(0) == '+') num.sign='p';
int start = 0;
if ( !isdigit(number.at(0)) ) start = 1;
int loc = 100-(number.length()-start);
for (int a=start; a<number.length(); a++)
num.digits[loC++] = number.at(a) - '0';
return is;
}
using namespace std;
int main( ) {
INT a, b, c;
int num;
string s;
system("cls");
cout << "Enter two large integers and a string : " << endl;
cin >> a >> b >> s;
cout << "Addition : " << endl;
c = a + b; cout << c << endl << endl;
cout << "Subtraction : " << endl;
c = a - b; cout << c << endl << endl;
cout << "Multiplication : " << endl;
c = a * b; cout << c << endl << endl;
cout << "Division : " << endl;
c = a / b; cout << c << endl << endl;
cout << "Modulation : " << endl;
c = a % b; cout << c << endl << endl;
cout << "Comparisons: "<< endl;
if(a == b) cout << "a == b" << endl;
if(a > b) cout << "a > b" << endl;
if(a >= b) cout << "a >= b" << endl;
if(a < b) cout << "a < b" << endl;
if(a <= b) cout << "a <= b" << endl;
if(a != b) cout << "a != b" << endl;
cout <<endl << "Convert from (INT) to (int)" << endl;
cout << "Original INT:" << endl;
cout << a << endl << endl;
cout << "Converted (int)INT:" <<endl;
cout << (int)a << endl << endl;
cout <<" Convert string to INT:"
<< (INT)s <<endl;
cout <<" Convert from (INT) to (string)" << endl;
cout << "Oringinal INT:" << endl;
cout << a << endl << endl;
cout << "Converted (string)INT:" << endl;
//cout << (string)a << endl;
//num = a.operator int();
//num = a;
//cout << (int)a << endl;
system("pause");
return 0;
}