大佬们,帮我看看这个代码那里错误啊!
#include
#include
#include
using namespace std;
class Complex {
private:
double r, i;
public:
void Print() {
cout << r << "+" << i << "i" << endl;
}
// 在此处补充你的代码
Complex() {};
Complex(const std::string& str)
{
int pos = str.find("+", 0);
string stmp = str.substr(0, pos);//分离出代表实部的字符串
r = atoi(stmp.c_str()); //将字符串转换为整数
stmp = str.substr(pos + 1, str.length() - pos - 2);//分离出代表虚部的字符串
i = atoi(stmp.c_str());
}
};
int main() {
Complex a;
//string str = "3+4i";
//a = str;a.Print();
a = "3+4i"; a.Print();
system("pause");
return 0;
}
#include <iostream>
using namespace std;
class Complex {
private:
double r, i;
public:
void Print() {
cout << r << "+" << i << "i" << endl;
}
// 在此处补充你的代码
Complex& operator =(const std::string& str)
{
int pos = str.find("+", 0);
string stmp = str.substr(0, pos);//分离出代表实部的字符串
this->r = atoi(stmp.c_str()); //将字符串转换为整数
stmp = str.substr(pos + 1, str.length() - pos - 2);//分离出代表虚部的字符串
this->i = atoi(stmp.c_str());
return *this;
}
};
int main() {
Complex a;
//string str = "3+4i";
//a = str;a.Print();
a = "3+4i"; a.Print();
system("pause");
return 0;
}