不定个数的输入数字 并做复数运算

import java.util.Scanner;
class change1
{
public float shang[];
//shang=new float [1005];
float count=0;
int m=0,sum=0;
int sum2=0;
int sum10=0;
float nnf=0.1f;
void change(String a)
{
shang=new float [1005];
for(int t=0;t<a.length();t++)
{
if(a.charAt(t)!=' '&&a.charAt(t)!='.'&&sum!=1&&a.charAt(t)!='-')
{
count=count*10+(a.charAt(t)-'0');
}
if(a.charAt(t)=='.')
{
sum=1;
}
if(a.charAt(t)=='-')
{
sum2=1;
}
//System.out.printf("%f\n",nnf);
if(sum==1&&a.charAt(t)!=' '&&a.charAt(t)!='.'&&a.charAt(t)!='-')
{
count=count+(a.charAt(t)-'0')*nnf;
nnf=nnf/10;
}
if(a.charAt(t)==' '||t==a.length()-1)
{
if(sum2==1)
{
count=-count;
sum2=0;
}
shang[m]=count;
count=0;
sum=0;
m++;
nnf=0.1f;
}

}

//System.out.println();
float first=shang[0],second=shang[1];
//System.out.printf("%.2f\n",count);
for(int n=2;n<m;n=n+3)
{
//System.out.printf("kkk\n");
if(shang[n]==1)
{
first=first+shang[n+1];
second=second+shang[n+2];
}
if(shang[n]==2)
{
first=first-shang[n+1];
second=second-shang[n+2];

}
if(shang[n]==3)
{
float first1=first,second1=second;
first=(first1*shang[n+1])-(second1*shang[n+2]);
second=(first1*shang[n+2])+(second1*shang[n+1]);
}
if(shang[n]==4)
{
float first2=first,second2=second;
if(shang[n+1]==0&&shang[n+2]==0)
{
sum10=10;
System.out.printf("divied by 0!\n");
}
if(shang[n+1]!=0||shang[n+2]!=0)
{
first=(first2*shang[n+1]+second2*shang[n+2])/(shang[n+1]*shang[n+1]+shang[n+2]*shang[n+2]);
second=(second2*shang[n+1]-first2*shang[n+2])/(shang[n+1]*shang[n+1]+shang[n+2]*shang[n+2]);
}
//System.out.printf("%.2f %.2f\n",shang[n+1],shang[n+2]);
}
}
//System.out.printf("%.2f %.2f\n",(shang[n+1]*shang[n+1]+shang[n+2]*shang[n+2]),(first*shang[n+1]+second*shang[n+2]));
if(sum10!=10) {
if(second<0)
System.out.printf("(%.2f%.2fi)\n",first,second);
if(second>=0)
System.out.printf("(%.2f+%.2fi)\n",first,second);
}
}

}
public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner AI=new Scanner(System.in);
String a;
while(AI.hasNext())
{
a=AI.nextLine();
change1 c =new change1();
c.change(a);
}


}

}

posted on 2019-06-18 00:11  AIERSTOM  阅读(175)  评论(0编辑  收藏  举报

导航