[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
- [c++]整形类Int的各种算数操作
- 各种Bar的整形
- C语言中的算数转换和整形提升
- 各种整形 int、bigint、smallint 和 tinyint的区别与长度
- c的整形转换和整形提升
- C语言自己实现的 整形转换成字符串函数itoa(int Number,char* Str)
- 字符char转整形int的操作方法
- c/c++ 的 整形编译
- 各种int的范围
- Object-C 文件类的各种操作…
- 算数编码的C语言实现
- C语言:断言,指针的算数运算
- String 类的拷贝构造函数, 赋值操作符重载, 算数操作符重载
- 关于python的整形(int)自动转长整形(long)的问题
- int 整形数所能表示的最大范围
- C++中字符串string和整形int的转化
- 注意java int整形变量的取值范围
- C语言实现单链表的各种操作
- Apache域名自动指向虚拟主机目录
- NGUI的UIScrollview的优化
- java.lang.OutOfMemoryError: Java heap space
- 29-m-Divide Two Integers
- Merge k sorted linked lists
- [c++]整形类Int的各种算数操作
- 变长参数函数说明
- android viewPager实现轮播效果一(本地图片篇)
- LeetCode---(7)Reverse Integer
- 十三周项目二
- LeetCode 之 Merge Two / k Sorted Lists — C/C++ 实现
- VS2008/VS2012/MFC/C++/ACCESS/Redistributable Package/installshield/打包/安装/部署
- ACdream 1099 - 瑶瑶的第K大
- 大端与小端