#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Complex {
private:
double r,i;
public:
void Print() {
cout << r << "+" << i << "i" << endl;
}
Complex()
{
}
Complex(double cr,double ci)
{
r=cr;
i=ci;
}
Complex operator=(const string str)
{
int pos=str.find("+",0);
string stemp=str.substr(0,pos);
r=atof(stemp.c_str());
stemp=str.substr(pos+1,str.length()-pos-2);
i=atof(stemp.c_str());
return Complex(r,i);
}
};
int main()
{
Complex a;
a = "3+4i"; a.Print();
a = "5+6i"; a.Print();
return 0;
}
Complex类运算符重载
最新推荐文章于 2023-08-22 22:40:59 发布