第三十天
题目:
为复数类Complex重载实现+,-,*,/,<<,>>等运算符,main(void)函数完成对其的测试。@
Complex类结构说明:
Complex类的数据成员包括:
①私有数据成员:实部real(double型),虚部imag(double型)。
Complex类成员函数包括:
①有参构造函数Complex(double, double),其中参数默认值为0。
②重载运算符+以实现两个Complex的加法运算。
③重载运算符-以实现两个Complex的减法运算。
④重载运算符*以实现两个Complex的乘法运算。
⑤重载运算符/以实现两个Complex的除法运算。
⑥重载运算符<<以实现Complex对象的格式输出,输出格式要求:
<实部>+<虚部>i
例如:10.0,10.0+4.7i,10.0-4.7i,-4.7i,0等。
⑦重载运算符>>以实现Complex对象的格式输入,输入格式要求:
<实部>+<虚部>i
例如:10.0,10.0+4.7i,10.0-4.7i,-4.7i,0等。
###裁判测试程序样例:
#include <iostream>
using namespace std;
/*请在这里填写答案*/
int main() //主函数
{
Complex c1(1, 1), c2(-1, -1), c3; //定义复数类的对象
cin>>c1;
cout <<c1<<endl;
cout <<c2<<endl;
c3 = c1 - c2;
cout << c3<<endl;
c3 = c1 + c2;
cout << c3<<endl;
c3=c1 * c2;
cout << c3<<endl;
c3=c1 / c2;
cout << c3<<endl;
return 0;
}
###输入样例:
10.0-4.7i
###输出样例:
10-4.7i
-1-1i
11-3.7i
9-5.7i
-14.7-5.3i
-2.65+7.35i
代码:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
friend Complex operator+(const Complex& c1, const Complex& c2);
friend Complex operator-(const Complex& c1, const Complex& c2);
friend Complex operator\*(const Complex& c1, const Complex& c2);
friend Complex operator/(const Complex& c1, const Complex& c2);
friend istream& operator>>(istream& is, Complex& c);
friend ostream& operator<<(ostream& os, const Complex& c);
};
Complex operator+(const Complex& c1, const Complex& c2) {
return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
Complex operator-(const Complex& c1, const Complex& c2) {
return Complex(c1.real - c2.real, c1.imag - c2.imag);
}
Complex operator*(const Complex& c1, const Complex& c2) {
double r = c1.real \* c2.real - c1.imag \* c2.imag;
double i = c1.real \* c2.imag + c1.imag \* c2.real;
return Complex(r, i);
}
Complex operator/(const Complex& c1, const Complex& c2) {
double denominator = c2.real \* c2.real + c2.imag \* c2.imag;
double r = (c1.real \* c2.real + c1.imag \* c2.imag) / denominator;
double i = (c1.imag \* c2.real - c1.real \* c2.imag) / denominator;
return Complex(r, i);
}
istream& operator>>(istream& is, Complex& c) {
string input;
getline(is, input); // 读取一整行
double r = 0, i = 0;
// 使用字符串流解析输入,支持+-符号和小数点
istringstream iss(input);
char ch;
iss >> r;
iss >> ch;
if (ch == '-') {
iss >> i;
i = -1 \* i;
iss >> ch;
}
else if (ch == '+') {
iss >> i;
iss >> ch;
}
else {
i = 0;
}
if (ch != 'i') is.setstate(ios::failbit); // 如果没有读到i,则报错
c.real = r;
c.imag = i;
return is;
}
ostream& operator<<(ostream& os, const Complex& c) {
if (c.real != 0) os << c.real;
if (c.imag > 0) os << " + ";
if (c.imag != 0) os << c.imag << "i";
if (c.real == 0 && c.imag == 0) os << "0";
return os;
}
int main() {
Complex c1(1, 1), c2(-1, -1), c3;
cin >> c1;
cout << c1 << endl;
cout << c2 << endl;
c3 = c1 - c2;
cout << c3 << endl;
c3 = c1 + c2;
cout << c3 << endl;
c3 = c1 \* c2;
cout << c3 << endl;
c3 = c1 / c2;
cout << c3 << endl;
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署