可能计算机相关专业有教授过相关的知识,这里只是作为一个非计算机专业人员的实现方法,模仿十进制的相关运算方法加以实现:
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
struct divide_modulo_zero_exception
{
static const char msg[64];
};
const char divide_modulo_zero_exception::msg[] =
"exception: attempt to divide/modulo integer by 0.";
class Integer
{
public:
explicit Integer(int value = 0);
Integer(const Integer &);
Integer & operator = (const Integer &);
~Integer();
public:
const Integer & operator + () const;
Integer operator - () const;
Integer operator + (const Integer &) const;
Integer operator - (const Integer &) const;
Integer operator * (const Integer &) const;
Integer operator / (const Integer &) const;
Integer operator % (const Integer &) const;
ostream & operator << (ostream & os) const;
private:
int add(int, int) const;
int sub(int, int) const;
int mul(int, int) const;
int div(int, int) const;
int mod(int, int) const;
void div_mod(int, int, int &, int &) const;
private:
int m_value;
};
Integer::Integer(int value)
: m_value(value)
{
}
Integer::Integer(const Integer & other)
: m_value(other.m_value)
{
}
Integer & Integer::operator = (const Integer & other)
{
if (&other != this)
{
m_value = other.m_value;
}
return(*this);
}
Integer::~Integer()
{
}
const Integer & Integer::operator + () const
{
return(*this);
}
Integer Integer::operator - () const
{
return(Integer(add(~m_value, 1)));
}
Integer Integer::operator + (const Integer & other) const
{
return(Integer(add(m_value, other.m_value)));
}
Integer Integer::operator - (const Integer & other) const
{
return(Integer(sub(m_value, other.m_value)));
}
Integer Integer::operator * (const Integer & other) const
{
return(Integer(mul(m_value, other.m_value)));
}
Integer Integer::operator / (const Integer & other) const
{
return(Integer(div(m_value, other.m_value)));
}
Integer Integer::operator % (const Integer & other) const
{
return(Integer(mod(m_value, other.m_value)));
}
int Integer::add(int a, int b) const
{
/*
return(a + b);
*/
while (0 != a && 0 != b)
{
int x = (a ^ b);
int y = (a & b) << 1;
a = x;
b = y;
}
return(0 != a ? a : b);
}
int Integer::sub(int a, int b) const
{
/*
return(a - b);
*/
return(add(a, add(~b, 1)));
}
int Integer::mul(int a, int b) const
{
/*
return(a * b);
*/
bool s = false;
if (a < 0)
{
a = add(~a, 1);
s = !s;
}
if (b < 0)
{
b = add(~b, 1);
s = !s;
}
int x = 0;
int y = a;
while (0 != b)
{
if (0 != (b & 1))
{
x = add(x, y);
}
b >>= 1;
y <<= 1;
}
if (s)
{
x = add(~x, 1);
}
return(x);
}
void Integer::div_mod(int a, int b, int & d, int & m) const
{
if (0 == b)
{
throw divide_modulo_zero_exception();
}
int s = 0;
if (a < 0)
{
a = add(~a, 1);
s |= 1;
}
if (b < 0)
{
b = add(~b, 1);
s |= 2;
}
d = 0;
m = 0;
int x = (1 << sub(sizeof(int) << 3, 2));
while (0 != x)
{
m <<= 1;
if (a & x)
{
m |= 1;
}
d <<= 1;
if (m >= b)
{
d |= 1;
m = sub(m, b);
}
x >>= 1;
}
if (1 == s || 2 == s)
{
d = add(~d, 1);
}
if (0 != (s & 1))
{
m = add(~m, 1);
}
}
int Integer::div(int a, int b) const
{
/*
return(a / b);
*/
int d = 0;
int m = 0;
div_mod(a, b, d, m);
return(d);
}
int Integer::mod(int a, int b) const
{
/*
return(a % b);
*/
int d = 0;
int m = 0;
div_mod(a, b, d, m);
return(m);
}
ostream & Integer::operator << (ostream & os) const
{
return(os << m_value);
}
ostream & operator << (ostream & os, const Integer & number)
{
return(number << os);
}
void test(const Integer & a, const Integer & b)
{
Integer c;
cout << "--------------------" << endl;
c = a + b;
cout << a << " + " << b << " = " << c << endl;
c = a - b;
cout << a << " - " << b << " = " << c << endl;
c = a * b;
cout << a << " * " << b << " = " << c << endl;
c = a / b;
cout << a << " / " << b << " = " << c << endl;
c = a % b;
cout << a << " % " << b << " = " << c << endl;
cout << "--------------------" << endl;
}
int main(int argc, char * argv[])
{
Integer i(55);
Integer j;
Integer k;
cout << "--------------------" << endl;
cout << "i = " << i << endl;
j = +i;
cout << "j = +i = " << j << endl;
k = -i;
cout << "k = -i = " << k << endl;
cout << "--------------------" << endl;
Integer result;
Integer x(19);
Integer y(28);
test(x, y);
test(y, x);
int a[] = { 5, 5, -5, -5 };
int b[] = { 3, -3, 3, -3 };
for (int i = 0; i < 4; ++i)
{
x = Integer(a[i]);
y = Integer(b[i]);
test(x, y);
test(y, x);
}
try
{
Integer(4) % Integer(0);
}
catch (const divide_modulo_zero_exception & e)
{
cout << e.msg << endl;
return(1);
}
return(0);
}