复数

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class Complex{
private:
    string str;
    float real;
    float imag;
    void split(){
        int len=str.size()-1;
        if(str[len]!='i') {
            real=stof(str);
            imag=0;
        }
        else if(str=="i"){
            real=0;
            imag=1;
        }
        else
        {
            if(str.find('+',0)!=string::npos){
                char *p,*b;
                char s[100];
                strcpy(s,str.c_str());
                p=strtok(s,"+");
                real=stof(p);
                b=strtok(NULL,"+");
                imag=stof(b);
            }
            else if(str.find('-',0)!=string::npos){
                size_t l=str.find_last_of('-');
                if(l==0){
                    real=0;
                    if(str=="-i")
                    {
                        imag=-1;
                    }
                    else{
                        imag=stof(str.substr(0,len));
                    }
 
                }else{
                    real=stof(str.substr(0,l));
                    imag=stof(str.substr(l,len-l));
                }
            }
            else{
                real=0;
                imag=stof(str.substr(0,len));
            }
        }
    }
public:
    Complex(string n){str=n;split();}
    
    string get1(){
        return str;
    }
    
    float get2(){
        return real;
    }
    
    float get3(){
        return imag;
    }
};
int main()
{
    string n;
    cin>>n;
    Complex c(n);
    cout << c.get1() << endl << c.get2() << endl << c.get3() << endl;
    return 0;
}
posted @ 2021-07-01 17:41  Xuuxxi  阅读(42)  评论(0)    收藏  举报