64,682
社区成员
发帖
与我相关
我的任务
分享
template<class _Ty,
class _Elem,
class _Tr> inline
basic_istream<_Elem, _Tr>& operator>>(
basic_istream<_Elem, _Tr>& _Istr, complex<_Ty>& _Right)
{ // extract a complex<_Ty>
typedef complex<_Ty> _Myt;
const ctype<_Elem>& _Ctype_fac = _USE(_Istr.getloc(), ctype<_Elem>);
_Elem _Ch = 0;
long double _Real = 0;
long double _Imag = 0;
if (_Istr >> _Ch && _Ch != _Ctype_fac.widen('('))
{ // no leading '(', treat as real only
_Istr.putback(_Ch);
_Istr >> _Real;
_Imag = 0;
}
else if (_Istr >> _Real >> _Ch && _Ch != _Ctype_fac.widen(','))
if (_Ch == _Ctype_fac.widen(')'))
_Imag = 0; // (real)
else
{ // no trailing ')' after real, treat as bad field
_Istr.putback(_Ch);
_Istr.setstate(ios_base::failbit);
}
else if (_Istr >> _Imag >> _Ch && _Ch != _Ctype_fac.widen(')'))
{ // no imag or trailing ')', treat as bad field
_Istr.putback(_Ch);
_Istr.setstate(ios_base::failbit);
}
if (!_Istr.fail())
{ // store valid result
_Ty _Tyreal((_Ty)_Real), _Tyimag((_Ty)_Imag);
_Right = _Myt(_Tyreal, _Tyimag);
}
return (_Istr);
}
std::ostream& operator << (std::ostream& out, const Complex & z)
{
out << z.real<< '+' << z.img << 'i';
return out;
}
[/quote]这个我也会写啊
重点在重载输入>>
不过谢谢[/quote]
std::istream &operator>> (std::istream &input,Complex &z)
{
input>>z.real>>z.imag;
return input;
}这个我看过了 不是我想要的 参看:http://blog.csdn.net/liyuanbhu/article/details/43982327
std::ostream& operator << (std::ostream& out, const Complex & z)
{
out << z.real<< '+' << z.img << 'i';
return out;
}
这个我看过了 不是我想要的 [quote=引用 2 楼 paschen 的回复:] 参看:http://blog.csdn.net/liyuanbhu/article/details/43982327
std::ostream& operator << (std::ostream& out, const Complex & z)
{
out << z.real<< '+' << z.img << 'i';
return out;
}
[/quote]这个我也会写啊
重点在重载输入>>
不过谢谢
void main()
{
int nA = 0,nB = 0; //nA 和 nB分别是实步和虚部
printf(" Please Input Two Number:\n");
scanf("%d,%d", &nA, &nB);
if(nB > 0)
{
if(nA = 0)
printf("%di", nB);
else
printf("%d+%di", nA, nB);
}
else if(nB == 0)
printf("%d", nA);
else if(nB < 0)
{
if(nA = 0)
printf("%di", nB);
else
printf("%d%di", nA, nB);
}
}
void main() { int nA = 0,nB = 0; //nA 和 nB分别是实步和虚部 printf(" Please Input Two Number:\n"); scanf("%d,%d", &nA, &nB); if(nB > 0) { if(nA = 0) printf("%di", nB); else printf("%d+%di", nA, nB); } else if(nB == 0) printf("%d", nA); else if(nB < 0) { if(nA = 0) printf("%di", nB); else printf("%d%di", nA, nB); } }
参看:http://blog.csdn.net/liyuanbhu/article/details/43982327