#include<bits/stdc++.h> 
using namespace std;
const int N=1e6+7;
const double eps=1e-6;

string s;
double a,b;
complex<double> m(1,1);

void Split(const string& src, const string& separator, vector<string>& dest){
	string str = src;
	string substring;
	string::size_type start = 0, index;
	dest.clear();
	index = str.find_first_of(separator,start);
	do{
		if (index != string::npos){    
			substring = str.substr(start,index-start );
			dest.push_back(substring);
			start =index+separator.size();
			index = str.find(separator,start);
			if (start == string::npos) break;
		}
	}while(index != string::npos);
	substring = str.substr(start);
	dest.push_back(substring);
}
int main( ){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        vector<string>v;
        vector<int>vis;
        cin>>s;
        int len=s.length();
        for(int i=0;i<len;i++){
            if(i==0&&s[i]!='-'){
                vis.push_back(1);
            }else if(i==0&&s[i]=='-'){
                vis.push_back(0);
                s[i]='1';
            }else if(s[i]=='-'){
                vis.push_back(0);
                s[i]='+';
            }
            else if(s[i]=='+')vis.push_back(1);
        }
        Split(s,"+",v);
        double a=0,b=0;
        int le=v.size();
        for(int i=0;i<le;i++){
            if(v[i].find("i")!=string::npos){
                if(v[i].length()==1){
                    if(vis[i])b+=1;
                    else b-=1;
                }else if(v[i].length()==2&&v[i][0]=='1'){
                    if(vis[i])b+=1;
                    else b-=1;
                }else{
                    if(vis[i]) b+=stof(v[i]);
                    else b-=stof(v[i]);
                }
            }else{
                if(vis[i]) a+=stof(v[i]);
                else a-=stof(v[i]);
            }
        }
        if(fabs(a-0.0)<eps&&fabs(b-0.0)<eps)cout<<0<<endl;
        else if(fabs(a-0.0)<eps){
            if(fabs(b-1.0)<eps)cout<<"i"<<endl;
            else if(fabs(b+1.0)<eps)cout<<"-i"<<endl;
            else cout<<b<<"i"<<endl;
        }
        else if(fabs(b-0.0)<eps)cout<<a<<endl;
        else{
            cout<<a;
            if(b>0)cout<<"+";
            if(b<0)cout<<"-";
            if(fabs(b-1.0)<eps){
                cout<<"i"<<endl;
            }else if(fabs(b+1.0)<eps){
                cout<<"i"<<endl;
            }
            else cout<<fabs(b)<<"i"<<endl;
        }
    }
    return 0;
}

0 comments

No comments so far...