[c++]整形类Int的各种算数操作

来源:互联网 发布:软件需求整理 编辑:程序博客网 时间:2024/06/08 03:38

对整形类Int实现如下算数操作:

        Int& operator--();Int  operator--(int);Int  operator-(); // -aInt  operator+(); //+aInt  operator+(const Int &I);Int  operator-(const Int &I);Int  operator*(const Int &I);Int  operator/(const Int &I);Int  operator+=(const Int &I); //i += jInt  operator+=(const int &x);Int  operator-=(const Int &I); //i += jInt  operator-=(const int &x);Int  operator*=(const Int &I); //i += jInt  operator*=(const int &x);Int  operator/=(const Int &I); //i += jInt  operator/=(const int &x);Int  operator%(const Int &I); //i += jInt  operator%(const int &x);Int  operator^(const Int &I); //i += jInt  operator^(const int &x);Int  operator|(const Int &I);Int  operator&(const Int &I);bool operator!();Int  operator~();bool operator==(const Int &I);bool operator!=(const Int &I);bool operator>(const Int &I);bool operator<(const Int &I);bool operator>=(const Int &I);bool operator<=(const Int &I);Int operator>>(const Int &I);Int operator<<(const Int &I);Int operator>>=(const Int &I);Int operator<<=(const Int &I);

部分代码比较简单,读者自行完成:

#include<math.h>#include<iostream>using namespace std;//友元函数:class Int; //此处的分号是干什么用的??Int operator+(const int &x,const Int &a);//加法:int值 + 对像Int operator-(const int &x,const Int &a);//减法:int值 - 对像Int operator*(const int &x,const Int &a);//乘法:int值 * 对像Int operator/(const int &x,const Int &a);//除法:int值 / 对像Int operator^(int &x,const Int &a);//异或:int值 ^ 对像Int operator%(const int &x,const Int &a);//取余:int值 % 对象//void operator=(int &x,const Int &a);//等于:int变量 = 对象class Int{//构造函数public:Int(int i = 0):m_i(i){}~Int(){}void show(){cout<<m_i<<endl;}//友元函数:public:friend Int operator+(const int &x,const Int &a);friend Int operator-(const int &x,const Int &a);friend Int operator*(const int &x,const Int &a);friend Int operator/(const int &x,const Int &a);friend Int operator^(int &x,const Int &a);friend Int operator%(const int &x,const Int &a); //friend void operator=(int &x,const Int &a);//运算符重载public://后置++: ++aInt& operator++(){m_i++;return *this;}//前置++:  ++a返回临时对象所以函数的返回类型不能是&Int operator++(int){return Int(m_i++);}//b += a: b = b + a;Int& operator+=(const Int &a){m_i += a.m_i;return *this;}//前置--: --aInt& operator--(){m_i--;return *this;}//后置--: a--;返回临时对象所以函数的返回类型不能是&Int operator--(int){return Int(m_i--);}//b -= a :b = b - aInt& operator-=(const Int &a){m_i -= a.m_i;return *this;}//负号: -aInt& operator-(){m_i = -m_i;return*this;}//正号: +aInt& operator+(){return *this;}//加法+:对象 + 对象Int operator+(const Int &a){return Int(m_i + a.m_i);}//加法:对象 + int值Int operator+(const int &x){return Int(m_i + x);}// 减法:对象 - 对象Int operator-(const Int &a){return Int(m_i - a.m_i);}//减法:对象 - int值Int operator-(const int &x){return Int(m_i - x);}//乘法:对像 * 对像Int operator*(const Int &a){return Int(m_i * a.m_i);}//乘法:对像 * int值Int operator*(const int &x){return Int(m_i * x);}//除法:对象 / 对像Int operator/(const Int &a){return Int(m_i /a.m_i);}//除法:对像 / int值Int operator/(const int &x){return Int(m_i / x);}//取非 !对象Int& operator!(){m_i = !m_i;return *this;}//异或 对象^对象Int operator^(const Int &a){int sum = 0;int i = 0;int tmp1 = m_i;int tmp2 = a.m_i;while(tmp1 || tmp2)//while(m_i != 0 || a.m_i != 0){if(tmp1%2 != tmp2%2){sum = sum + pow(2,i);}i++;tmp1 /= 2;tmp2 /= 2;}return Int(sum);}//异或 对象^int值Int operator^(const int &x){int sum = 0;int i = 0;int tmp1 = m_i;int tmp2 = x;while(tmp1 || tmp2){if(tmp1%2 != tmp2%2){sum = sum + pow(2,i);}i++;tmp1 /= 2;tmp2 /= 2;}return Int(sum);}//取余:对象 % 对象Int operator%(const Int &a){return Int(m_i % a.m_i);}//取余:对象 % 对象Int operator%(const int &x){return Int(m_i % x);}//按位相或:对像 | 对象Int operator|(const Int &a){int sum = 0;int i = 0;int tmp1 = m_i;int tmp2 = a.m_i;while(tmp1 || tmp2){if(tmp1%2 || tmp2%2){sum = sum + pow(2,i);}i++;tmp1 /= 2;tmp2 /= 2;}return Int(sum);}//按位相或:对像 | 对象Int operator&(const Int &a){int sum = 0;int i = 0;int tmp1 = m_i;int tmp2 = a.m_i;while(tmp1 || tmp2){if(tmp1%2 && tmp2%2){sum = sum + pow(2,i);}i++;tmp1 /= 2;tmp2 /= 2;}return Int(sum);}//逻辑右移:对像 >> 对象Int& operator>>(const Int &a){m_i = m_i >> a.m_i;return *this;}Int& operator>>=(const Int &a){m_i = m_i >> a.m_i;return *this;}//逻辑左移:对像 << 对象Int& operator<<(const Int &a){m_i = m_i << a.m_i;return *this;}Int& operator<<=(const Int &a){m_i = m_i << a.m_i;return *this;}//大于bool operator>(const Int &a){return m_i > a.m_i;}private:int m_i;};//友元函数的定义://加法:int值 + 对像Int operator+(const int &x,const Int &a){return Int(x + a.m_i);}//减法:int值 - 对像Int operator-(const int &x,const Int &a){return Int(x - a.m_i);}//乘法:int值 * 对像Int operator*(const int &x,const Int &a){return Int(x * a.m_i);}//除法:int值 / 对像Int operator/(const int &x,const Int &a){return Int(x / a.m_i);}//异或:int值 ^ 对像Int operator^(int &x,const Int &a){int sum = 0;int i = 0;int tmp1 = x;int tmp2 = a.m_i;while(tmp1 || tmp2){if(tmp1%2 != tmp2%2){sum = sum + pow(2,i);}i++;tmp1 /= 2;tmp2 /= 2;}return Int(sum);}Int operator%(const int &x,const Int &a){return Int(x%a.m_i);}/*void operator=(int &x,const Int &a){x = a.m_i;}*/int main(){Int a(1);Int b(2);//Int c = 4;//为何会进入构造函数???Int c = a + b;c.show();c = a - b;c.show();c = a * b;c.show();c = a / b;c.show();a++;a.show();c = a ^ b;c.show();c = a % b;c.show();}


测试结果:



0 0