除法计算-C++-非*,/,%运算

原理: m = x1*2^y1 + x2*2^y2……

要点:    << >>优先级比+,-低。

顺便插一下优先级图片:


只考虑正数情况下:

int divide(int x,int y)
{
    if(x < y)
        return 0;
    int sum = 0;
    int te = y<<1;
    while(x > te)
    {
        sum++;
        te = te<<1;
    }
    int v = y<<sum;
    return divide(x - v,y) + (1<<sum);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基本的复数类的实现: ```c++ #include <iostream> #include <cmath> class Complex { private: double real; double imag; public: Complex() { real = 0.0; imag = 0.0; } Complex(double r, double i) { real = r; imag = i; } Complex operator+(const Complex& other) const { return Complex(real + other.real, imag + other.imag); } Complex operator-(const Complex& other) const { return Complex(real - other.real, imag - other.imag); } Complex operator*(const Complex& other) const { return Complex(real * other.real - imag * other.imag, real * other.imag + imag * other.real); } Complex operator/(const Complex& other) const { double denominator = other.real * other.real + other.imag * other.imag; return Complex((real * other.real + imag * other.imag) / denominator, (imag * other.real - real * other.imag) / denominator); } friend std::ostream& operator<<(std::ostream& os, const Complex& c) { os << c.real << "+" << c.imag << "i"; return os; } }; int main() { Complex c1(3, 4); Complex c2(2, -1); Complex c3 = c1 + c2; std::cout << c3 << std::endl; c3 = c1 - c2; std::cout << c3 << std::endl; c3 = c1 * c2; std::cout << c3 << std::endl; c3 = c1 / c2; std::cout << c3 << std::endl; return 0; } ``` 该类包含了一个默认构造函数和一个带有实部和虚部参数的构造函数。它还包含四个运算符重载函数,分别实现了加、减、乘、除运算。其中,加、减运算比较简单,直接将实部和虚部分别相加、相减即可。乘运算使用了复数的乘法公式,除运算使用了复数的除法公式。 在主函数中,我们创建了两个复数对象c1和c2,并进行了加、减、乘、除运算,将结果保存在c3对象中,并输出了c3的值。 输出结果如下: ``` 5+3i 1+5i 10+5i -0.4+1.7i ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值