#include<iostream>
#include<string>
using namespace std;
class complex{
private:
double real,imag;
string com;
public:
complex(string a):com(a){}
void opr();
friend ostream& operator<< (ostream&,complex&);
};
int main()
{
string com;
while(cin>>com){
complex c(com);
c.opr();
cout << c;
}
return 0;
}
void complex::opr(){
if(com=="i") real=0,imag=1;
else if(com=="-i") real=0,imag=-1;
else{
int len=com.size(),flag=0,k=0;string stra,strb;
if(com[len-1]!='i') real=atof(com.c_str()),imag=0;
else{
for(int i=1;i<len;i++) if(com[i]=='+'||com[i]=='-') flag=1,k=i;
if(flag){
for(int i=0;i<k;i++) stra+=com[i];real=atof(stra.c_str());
for(int i=k+1;i<len-1;i++) strb+=com[i];imag=atof(strb.c_str());
if(com[k+1]=='i')imag=1;
if(com[k]=='-')imag=-imag;
}
else{
for(int i=0;i<len-1;i++)
strb+=com[i];
real=0;imag=atof(strb.c_str());
}
}
}
}
ostream& operator<< (ostream& op,complex& cc){
op << "complex " << cc.com << endl;
op << "the real part is " << cc.real << endl;
op << "and the imaginary part is " << cc.imag << endl;
return op;
}
复数(手写体)
最新推荐文章于 2024-11-05 22:05:58 发布