Complex Number Multiplication

Complex Number Multiplication

question:
题目的意思就是要求我们进行负数乘法
给出了两个字符串,其难点在于字符串的划分得到字符串里面的有用的数字信息。
字符串有两个标识符:+,i
首先利用循环找出+之前的字符长度,形成子串
然后字符串长度减去2再减去第一个字符长度,得到第二个字符长度,在形成第二个子串
利用c语言函数atoi函数得到子串的数值
之后便是运算输出;
但是这样不够简洁,最后采用java语言的split函数可以轻松识别标识符
下面是java代码:
细心运算,可以得到accepted
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To define the addition, subtraction, multiplication, and division operators of complex numbers, we need to first understand the basic arithmetic operations on complex numbers. Addition: (a+bi) + (c+di) = (a+c) + (b+d)i Subtraction: (a+bi) - (c+di) = (a-c) + (b-d)i Multiplication: (a+bi) * (c+di) = (ac-bd) + (ad+bc)i Division: (a+bi) / (c+di) = ((ac+bd)/(c^2 + d^2)) + ((bc-ad)/(c^2 + d^2))i To implement these operations in a program, we can define a class for complex numbers and overload the operators using operator overloading. Here's an example program that implements the required functions: ``` #include <iostream> using namespace std; class Complex { private: double real, imag; public: Complex(double r = 0, double i = 0) : real(r), imag(i) {} // Addition operator overloading Complex operator+(const Complex& c) const { return Complex(real + c.real, imag + c.imag); } // Subtraction operator overloading Complex operator-(const Complex& c) const { return Complex(real - c.real, imag - c.imag); } // Multiplication operator overloading Complex operator*(const Complex& c) const { return Complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real); } // Division operator overloading Complex operator/(const Complex& c) const { double den = c.real * c.real + c.imag * c.imag; return Complex((real * c.real + imag * c.imag) / den, (imag * c.real - real * c.imag) / den); } // Addition operator overloading with integer Complex operator+(const int num) const { return Complex(real + num, imag); } // Subtraction operator overloading with integer Complex operator-(const int num) const { return Complex(real - num, imag); } // Multiplication operator overloading with integer Complex operator*(const int num) const { return Complex(real * num, imag * num); } // Division operator overloading with integer Complex operator/(const int num) const { return Complex(real / num, imag / num); } // Output operator overloading friend ostream& operator<<(ostream& os, const Complex& c) { os << c.real << (c.imag >= 0 ? "+" : "") << c.imag << "i"; return os; } }; int main() { Complex c1(1, 2), c2(3, 4); int num = 5; // Addition, subtraction, multiplication, and division of two complex numbers cout << "c1 + c2 = " << c1 + c2 << endl; cout << "c1 - c2 = " << c1 - c2 << endl; cout << "c1 * c2 = " << c1 * c2 << endl; cout << "c1 / c2 = " << c1 / c2 << endl; // Addition, subtraction, multiplication, and division of complex number c1 and integer num cout << "c1 + num = " << c1 + num << endl; cout << "c1 - num = " << c1 - num << endl; cout << "c1 * num = " << c1 * num << endl; cout << "c1 / num = " << c1 / num << endl; // Addition, subtraction, multiplication, and division of integer num and complex number c1 cout << "num + c1 = " << num + c1 << endl; cout << "num - c1 = " << num - c1 << endl; cout << "num * c1 = " << num * c1 << endl; cout << "num / c1 = " << num / c1 << endl; return 0; } ``` Input format and output format are the same as mentioned in the problem statement.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值