|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 kkk222 于 2019-6-8 12:07 编辑
- #include <iostream>
- #include <cstring>
- using namespace std;
- class Complex
- {
- private:
- double r, i;
- public:
- void Print()
- {
- cout << r << "+" << i << "i" << endl;
- }
- Complex operator =(const string& s);
- };
- Complex Complex::operator =(const string& s)
- {
- Complex temp;
- int pos = s.find("+",0);
- string sTmp = s.substr(0, pos);
- temp.r = atof(sTmp.c_str());
- sTmp = s.substr(pos + 1, s.length() - pos - 2);
- temp.i = atof(sTmp.c_str());
- cout << temp.r << "+" << temp.i << "i" << endl;
- return temp;
- }
- int main()
- {
- Complex a;
- a = "3+4i"; a.Print();
- a = "5+6i"; a.Print();
- return 0;
- }
程序的目的是把字符串中“+”前面的数赋值给对象的实部,“+”后面到“i”前面的数赋值给虚部
程序的结果:
对象a的实部和虚部怎么都是指数?
- #include <iostream>
- #include <cstring>
- #include <cstdlib>
- #include <string>
- using namespace std;
- class Complex
- {
- private:
- double r, i;
- public:
- void Print()
- {
- cout << r << "+" << i << "i" << endl;
- }
- Complex & operator =(const string& s);
- };
- Complex & Complex::operator =(const string& s)
- {
- Complex temp;
- int pos = s.find("+",0);
- string sTmp = s.substr(0, pos);
- temp.r = atof(sTmp.c_str());
- sTmp = s.substr(pos + 1, s.length() - pos - 2);
- temp.i = atof(sTmp.c_str());
- cout << temp.r << "+" << temp.i << "i" << endl;
- this->r = temp.r;
- this->i = temp.i;
- return *this;
- }
- int main()
- {
- Complex a;
- a = "3+4i"; a.Print();
- a = "5+6i"; a.Print();
- return 0;
- }
复制代码
返回引用
鸣人不说暗话,我要最佳答案!!!
|
|