关于复数输入输出的一点见解

我只能说复数这个东西是真的恶心,没话讲
因为大家都知道的,输入格式就很多样,输出也很多样。
只得哇的一声哭出来。
因为我是c++运算符重载要用,所以手打了“一万个if”
大家只看clss Complex里的display和inial就可以
(display输出,inial输入)

	void disply(){
	if (imag==0){
			cout<<real;
		}//虚部为0,直接输出实部
		if (imag==1||imag==-1){
			if (imag==1){
				if (real==0){
				cout<<"i";}
				else 
				cout<<real<<"+"<<"i";
		}//因为会出现n+i或者n—i的情况,所以判断一手
			if (imag==-1){
				if (real==0){
				cout<<"-"<<"i";}//-i的情况单独判断
				else
				cout<<real<<"-"<<"i";
		}
		}
		if (imag<-1||imag>1){
			if (imag<0){
				if (real==0){
					cout<<imag<<"i";}
				else 
				cout<<real<<imag<<"i";	
			}
			if (imag>0){
				if (real==0){
					cout<<imag<<"i";}
				else 
				cout<<real<<"+"<<imag<<"i";
			}
		}//这就是比较标准的输出,例如a+bi这种
	}

当然输出不是最恶心的,输入才是。
我输出大概打了半个小说,而输出用了我一节晚自习。
当然也有我个人因素,但是情况的多样性和操作的复杂性,没得说

Complex inial(){
		string s;
	cin>>s;
	char c[100],real[100],imag[100];
	strcpy(c, s.c_str());//得到便于操作的字符数组
	int len1=s.length();//得到字符串的有效长度
	int sum=0;
	int real1=0,imag1=0;
	for (int i=0;i<len1;i++){
		if (c[i]=='+'||c[i]=='-'||c[i]=='i'){
			sum++;
		}
	}//判断+ - 和i的个数
	if (sum==0){
		real1=atoi(c);
	}	//如果符号是0,那么只可能是实部
	if (sum==1){
		if (c[len1-1]=='i')//最后一位是i的情况
		{
			if (len1==1){
				imag1=1;
			}//长度是1只能是i,只有i没有1i这种操作
			else
			imag1=atoi(c);//长度不为1,只能是虚部,
		}
		else{
			real1=atoi(c);//最后一位不是i,那么只能是负数,直接转化就ok
		}
	}
	if (sum==2){
		if (c[len1-2]=='+'){
			real1=atoi(c);
			imag1=1;//n+i
		}
		if (c[len1-2]=='-'){
			real1=atoi(c);
			imag1=-1;//n-i
		}	
		if (c[0]=='-'){
			if (c[1]=='i')
			imag1=-1;//-i
			else
			imag1=atoi(c);
		}
		if (c[len1-2]!='+'&&c[len1-2]!='-'){//找符号,骚气的操作来了,a+-bi的形式
			int pos=1;//得到子串长度
			for (pos;pos<len1;pos++)
			{
				if (c[pos]=='+'||c[pos]=='-')
				break;
			}
			if (c[pos]=='+'){
				real1=atoi(c);
				string c_other=s.substr(0,pos);
				real1=atoi(c_other.c_str());
				string c_others=s.substr(pos+1,s.length()-pos-2);
				imag1=atoi(c_others.c_str());
			}
			if (c[pos]=='-'){
				real1=atoi(c);
				string c_other=s.substr(0,pos);
				real1=atoi(c_other.c_str());
				string c_others=s.substr(pos+1,s.length()-pos-2);
				imag1=-atoi(c_others.c_str());
			}
			}
		}//我觉得百度都ok的
	if (sum==3){
		if (c[len1-2]=='+'){
			real1=atoi(c);
			imag1=1;
		}
		if (c[len1-2]=='-'){
			real1=atoi(c);
			imag1=-1;
		}	
		if (c[len1-2]!='+'&&c[len1-2]!='-'){
			int pos=1;
			for (pos;pos<len1;pos++)
			{
				if (c[pos]=='+'||c[pos]=='-')
				break;
			}
			if (c[pos]=='+'){
				real1=atoi(c);
				string c_other=s.substr(0,pos);
				real1=atoi(c_other.c_str());
				string c_others=s.substr(pos+1,s.length()-pos-2);
				imag1=atoi(c_others.c_str());
			}
			if (c[pos]=='-'){
				real1=atoi(c);
				string c_other=s.substr(0,pos);
				real1=atoi(c_other.c_str());
				string c_others=s.substr(pos+1,s.length()-pos-2);
				imag1=-atoi(c_others.c_str());//一个道理
			}
		}
	}
		return Complex(real1,imag1);//用了构造函数初始化,大家忽略就ok
	}

总体来说就这样,具体我觉得自己写出虚部是否为0,+-i,然后+-ni
实部为0,+-n,然后排列组合一下
祝大家好运hhh

posted @ 2018-06-12 23:22  egoistor  阅读(757)  评论(0编辑  收藏  举报