CPP-Object-Oriented/complex-test.cpp

42 lines
1.7 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "complex.h"
#include <iostream>
using namespace std;
// 重载<<表示输出,输出复数的实部和虚部
ostream &
operator<<(ostream &os, const complex &x) // 第一个参数os虽然它是引用类型但也经常被声明为const引用类型。这是因为os作为输出流对象其状态如输出位置可能会在运算符重载过程中被修改所以通常来说不应该将其声明为const。如果将os声明为const引用类型则意味着不允许修改输出流的状态这可能导致编译错误或者输出结果不正确。
{
return os << '(' << real(x) << ',' << imag(x) << ')';
}
int main()
{
complex c1(2, 1);
complex c2(4, 0);
cout << "c1 = " << c1 << endl;
cout << "c2 = " << c2 << endl;
cout << "c1 + c2 = " << c1 + c2 << endl;
cout << "c1 - c2 = " << c1 - c2 << endl;
cout << "c1 * c2 = " << c1 * c2 << endl;
cout << "c1 / 2 = " << c1 / 2 << endl;
cout << "conj(c1) = " << conj(c1) << endl; // 返回复数的共轭复数
cout << "norm(c1) = " << norm(c1) << endl; // 返回复数的模的平方
cout << "polar(10,4) = " << polar(10, 4) << endl; // 极坐标(10,4)对应的复数
cout << "(c1 += c2) = " << (c1 += c2) << endl;
cout << "(c1 == c2) = " << (c1 == c2) << endl; // 结果为0表示 false因为c1和c2不相等
cout << "(c1 != c2) = " << (c1 != c2) << endl; // 结果为1表示 true因为c1和c2不相等
cout << "+c2 = " << +c2 << endl;
cout << "-c2 = " << -c2 << endl;
cout << "(c2 - 2) = " << (c2 - 2) << endl;
cout << "(5 + c2) = " << (5 + c2) << endl;
return 0; // 返回0表示程序运行成功; 返回值通常用来表示错误代码,如返回1表示程序运行失败
}