不用+-*/%实现整数的+-*/%运算
来源:互联网 发布:洞主卖香皂的淘宝店 编辑:程序博客网 时间:2024/05/22 00:12
可能计算机相关专业有教授过相关的知识,这里只是作为一个非计算机专业人员的实现方法,模仿十进制的相关运算方法加以实现:
#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);}
- 不用+-*/%实现整数的+-*/%运算
- 不用比较运算符实现两个整数的大小比较并输出结果
- 不用比较运算符实现两个整数的大小比较并输出结果
- 不用(+)运算符实现两个整数相加
- 不用+ - * / 实现两个整数的相加
- 位运算---不用额外变量交换两个整数的值
- 大整数的运算实现
- 加法的实现-不用加减乘除运算
- 不用 + -× /实现加法运算
- 不用‘/’ 实现除法运算
- 不用+-*/实现加法运算
- 不用‘+’实现加法运算
- 不用判断语句实现两个整数的比较
- java中不用BigInteger实现超大整数的乘法操作
- 不用辅助变量实现两个整数之间值的交换
- java是实现的大整数运算!
- 巧用位运算实现两个整数的互换
- 位运算实现整数的加减乘除
- AO+C++实现文字注记方向为竖直
- 算法导论第十五章习题15.4-5
- Sublime Text 2配置强大的IDE开发环境,运行java
- malloc,calloc,realloc的理解
- Makefile学习之make 的运行
- 不用+-*/%实现整数的+-*/%运算
- html的id的属性innerHTML
- eclipse build 不能生成apk
- 802.11e 无线网络的QoS标准
- xhtml & css 简易学习笔记(二)
- 戏说网络发展简史
- hdu :4359(记忆化搜索DP)
- 《算法竞赛入门经典》习题——Chapter 1
- nyoj 170 网络的可靠性(讨论度数为1时的情况,它们至少应该再连一条线)