Loading... ```C++ #include <iostream> #include <string> #include <cstdlib> using namespace std; class Complex { double real,imag; public: Complex( double r=0, double i=0):real(r),imag(i){ }; friend ostream & operator<<( ostream & os,const Complex & c); friend istream & operator>>( istream & is,Complex & c); }; ostream & operator<<( ostream & os,const Complex & c) { os << c.real << "+" << c.imag << "i"; //以"a+bi"的形式输出 return os; } istream & operator>>( istream & is,Complex & c) { string s; is >> s; //将"a+bi"作为字符串读入, "a+bi" 中间不能有空格 int pos = s.find("+",0); string sTmp = s.substr(0,pos); //分离出代表实部的字符串 c.real = atof(sTmp.c_str());//atof库函数能将const char*指针指向的内容转换成 float sTmp = s.substr(pos+1, s.length()-pos-2); //分离出代表虚部的字符串 c.imag = atof(sTmp.c_str()); return is; } int main() { Complex c; int n; cin >> c >> n; cout << c << "," << n; return 0; } ``` 最后修改:2022 年 03 月 06 日 05 : 53 PM © 允许规范转载 赞赏 如果觉得我的文章对你有用,请随意赞赏 赞赏作者 支付宝微信
5 条评论
主题紧扣时代脉搏,具有现实意义。
每次看到你的文章,我都觉得时间过得好快。 http://www.55baobei.com/ad3o5ZkkwQ.html
你的文章内容非常专业,让人佩服。 http://www.55baobei.com/07l7H4XHbB.html
你的文章让我感受到了快乐,每天都要来看一看。 http://www.55baobei.com/sIh42U2FnP.html
文章的确不错啊https://www.cscnn.com/