#include #include using namespace std;

class Complex{ private: double real; double imag; public: Complex():real(0),imag(0){} //默认构造函数,将实部和虚部初始化为0 Complex(double r, double i):real(r),imag(i){} //带参构造函数,初始化实部和虚部 void Set(double r, double i){ //设置实部和虚部 real = r; imag = i; } void Show(){ //输出实部和虚部 if(real == 0 && imag == 0){ cout<<'0'<<endl; } else if(real == 0){ if(imag > 0){ cout<<imag<<'i'<<endl; } else{ cout<<'-'<<-imag<<'i'<<endl; } } else if(imag == 0){ cout<<real<<endl; } else{ if(imag > 0){ cout<<real<<'+'<<imag<<'i'<<endl; } else{ cout<<real<<'-'<<-imag<<'i'<<endl; } } } };

C++复数类Complex实现:包含实部和虚部,支持构造、设置和显示

原文地址: https://www.cveoy.top/t/topic/oyQj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录