加减乘除的c++实现

算法描述参考:http://www.cnblogs.com/kiven-code/archive/2012/09/15/2686922.html


#include <iostream>
using namespace std;

int Add(int a,int b)//加法
{
	int x,y,icarry=0;//icarry进位标志
	int result=0;

	//从低位到高位 按位加
	for(int n=0;n<32;n++){
		//取a,b的末位
		x=a&1;
		y=b&1;
		
		//求当前位 和 进位标志
		int bit=0;
		bit=(x^y)^icarry;
		icarry=(x&y)|(y&icarry)|(icarry&x);
		if(bit==1)
			result=result|(1<<n);

		//a,b各右移一位
		a=a>>1;
		b=b>>1;
	}
	return result;
}
int Sub(int a,int b)//减法
{
	b=Add(~b,1);//补码
	return Add(a,b);
	//方案二:
	/*	return Add(a,-b);*/
}
int mul(int a,int b)//乘法
{
	int sign=a^b;//sign<0时 结果为负
	int result=0;//保持结果
	//a,b取补码
	a=a<0?Add(~a,1):a;
	b=b<0?Add(~b,1):b;
	
	while(a)
	{
		if(a&0x1)
			result=Add(result,b);
		b=b<<1;
		a=a>>1;
	}
	if(sign<0) result=Add(~result,1);//结果取补码
	return result;
}
int divide(int a, int b)//除法
{
	//对被除数和除数取绝对值
	int dividend = a < 0 ? Add(~a, 1) : a;
	  int divisor = b < 0 ? Add(~b, 1) : b;

		//对被除数和除数的绝对值求商
		int remainder = dividend;
		  int quotient = 0;

			while(remainder >= divisor)
			  {
					remainder = Sub(remainder, divisor);
						quotient = Add(quotient, 1);
			  }

			  //求商的符号
			  if((a ^ b) < 0)
				{
					  quotient = Add(~quotient, 1);
				}

			return quotient;
}

int remainder(int a, int b)//求余
{
	//对被除数和除数取绝对值
	int dividend = a < 0 ? Add(~a, 1) : a;
	  int divisor = b < 0 ? Add(~b, 1) : b;

		//对被除数和除数的绝对值求商
		int remainder = dividend;
		  int quotient = 0;

			while(remainder >= divisor)
			{
					remainder = Sub(remainder, divisor);
						quotient = Add(quotient, 1);
			}

			  //求余的符号
			  if(a < 0)
				{
					  remainder = Add(~remainder, 1);
				}

				return remainder;
}
int main()
{
	int a=666666,b=-333333;
	cout<<"a="<<a<<endl<<"b="<<b<<endl;
	cout<<"a+b="<<Add(a,b)<<endl;
	cout<<"a-b="<<Sub(a,b)<<endl;
	cout<<"a*b="<<mul(a,b)<<endl;
	cout<<"a/b="<<divide(a,b)<<"  余数为:"<<remainder(a,b)<<endl;
	system("pause");
	return 0;
}


  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
复数的加减乘除可以通过运算符重载来实现,下面是一个简单的复数类及其运算符重载实现: ```c++ #include <iostream> class Complex { public: Complex(double real = 0.0, double imag = 0.0) : m_real(real), m_imag(imag) {} Complex operator+(const Complex& other) const { return Complex(m_real + other.m_real, m_imag + other.m_imag); } Complex operator-(const Complex& other) const { return Complex(m_real - other.m_real, m_imag - other.m_imag); } Complex operator*(const Complex& other) const { double real = m_real * other.m_real - m_imag * other.m_imag; double imag = m_real * other.m_imag + m_imag * other.m_real; return Complex(real, imag); } Complex operator/(const Complex& other) const { double denominator = other.m_real * other.m_real + other.m_imag * other.m_imag; double real = (m_real * other.m_real + m_imag * other.m_imag) / denominator; double imag = (m_imag * other.m_real - m_real * other.m_imag) / denominator; return Complex(real, imag); } friend std::ostream& operator<<(std::ostream& out, const Complex& c) { out << c.m_real << " + " << c.m_imag << "i"; return out; } private: double m_real; double m_imag; }; int main() { Complex c1(1, 2); Complex c2(3, 4); Complex c3 = c1 + c2; Complex c4 = c1 - c2; Complex c5 = c1 * c2; Complex c6 = c1 / c2; std::cout << c3 << std::endl; std::cout << c4 << std::endl; std::cout << c5 << std::endl; std::cout << c6 << std::endl; return 0; } ``` 运行结果: ``` 4 + 6i -2 - 2i -5 + 10i 0.44 - 0.08i ``` 注意,这里实现的是复数的基本运算,可能还需要根据具体需求扩展其他功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值