package other;
import java.util.Scanner;
public class Fushu {
double real;
double im;
public Fushu() {
}
public Fushu(double real, double im) {
this.real = real;
this.im = im;
}
public Fushu add(Fushu c) {
Fushu com = new Fushu(0, 0);
com.real = c.real + this.real;
com.im = c.im + this.im;
return com;
}
public Fushu add(double real) {
Fushu com = new Fushu(0, 0);
com.real = this.real + real;
com.im = this.im;
return com;
}
public Fushu add(Fushu com1, Fushu com2) {
Fushu com = new Fushu(0, 0);
com.real = com1.real + com2.real;
com.im = com1.im + com2.im;
return com;
}
public Fushu sub(double real) {
Fushu com = new Fushu(0, 0);
com.real = this.real - real;
com.im = this.im;
return com;
}
public Fushu sub(Fushu c) {
Fushu com = new Fushu(0, 0);
com.real = c.real - this.real;
com.im = c.im - this.im;
return com;
}
public Fushu sub(Fushu com1, Fushu com2) {
Fushu com = new Fushu(0, 0);
com.real = com1.real - com2.real;
com.im = com1.im - com2.im;
return com;
}
public Fushu mul(Fushu com1, Fushu com2) {
Fushu com = new Fushu(0, 0);
com.real = com1.real * com2.real;
com.im = com1.im * com2.im;
com.real = com.real - com.im;
com.im = 0;
return com;
}
public void shownumber(Fushu c) {
if (c.im > 0)
System.out.println(c.real + "+" + c.im + "i");
else if (c.im < 0)
System.out.println(c.real + "" + c.im + "i");
else if (c.im == 0)
System.out.println(c.real);
if (c.real == 0)
System.out.println(c.im + "i");
}
public Fushu innumber(String str) {
String str1;
boolean flag1;
int flag2 = 0;
int flag3 = 0 ;
boolean flag4 = true;
double real = 0, im = 0;
double num[] = new double[2];
String b[] = new String[2];
Fushu com = new Fushu(0, 0);
char a[] = str.toCharArray();
for (int i = 0; i < a.length; i++) {
if ('i' == a[0]) {
real = 0;
im = 1;
flag3=11;
break;
}
else if(a[0]=='-'){
real = 0;
im = -1;
flag3=11;
break;
}
else if (((a[a.length - 2]) < '0') || ((a[a.length - 2]) > '9')) {
if (a[a.length - 2] == '-') {
str1 = str.substring(0, a.length - 2);
b = str1.split("\\-");
real = Double.parseDouble(b[0]);
im = -1;
flag3=11;
break;
}
else if (a[a.length - 2] == '+') {
str1 = str.substring(0, a.length - 1);
b = str1.split("\\+");
real = Double.parseDouble(b[0]);
im = 1;
flag3=11;
break;
}
} else if ((a[i] >= '0' && a[i] <= '9') || a[i] == '+' || a[i] == '-' || a[i] == '.' || a[i] == 'i') {
if (a[i] == '+') {
str1 = str.substring(0, a.length - 1);
b = str1.split("\\+");
flag2 = 1;
} else if (a[i] == '-') {
flag2 = -1;
str1 = str.substring(0, a.length - 1);
if (i == 0) {
flag4 = false;
b[1] = str1;
} else {
b = str1.split("\\-");
flag4 = true;
}
} else if (a[i] == 'i') {
flag3++;
}
}
else {
System.out.println("输入错误11111");
flag1 = false;
break;
}
}
switch (flag3) {
case 0:
b[0] = str;
real = Double.parseDouble(b[0]);
break;
case 1:
switch (flag2) {
case -1:
if (flag4) {
for (int k = 0; k < 2; k++) {
num[k] = Double.parseDouble(b[k]);
}
real = num[0];
im = -num[1];
} else {
real = 0;
im = Double.parseDouble(b[1]);
}
break;
case 1:
for (int k = 0; k < 2; k++) {
num[k] = Double.parseDouble(b[k]);
}
real = num[0];
im = num[1];
break;
case 0:
b[0] = str.substring(0, a.length - 1);
im = Double.parseDouble(b[0]);
real = 0;
break;
default:
System.out.println("特殊异常,请处理!!!!!!!!!!!!!!!!!!");
break;
}
break;
case 11: break;
default:
System.out.println("输入错误22222");
flag1 = false;
break;
}
com.real=real;
com.im=im;
return com;
}
public static void main(String[]args){
//public void window() {
boolean flag=true;
Fushu w1=new Fushu();
Fushu w2=new Fushu();
while(flag){
System.out.println("---------------欢迎使用虚数算法-------------");
System.out.println("----------------选择所选功能---------------");
System.out.println("-------1.加法运算------------2.减法运算-----");
System.out.println("-------3.乘法运算------------4.退出程序-----");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
switch (a) {
case 1:
System.out.println("输入需要相加的两数:");
System.out.println("请输入第一个数:");
String str=sc.next();
w1=w1.innumber(str);
System.out.println("请输入第二个数:");
String str1=sc.next();
w2=w2.innumber(str1);
System.out.println("结果为:");
w1.shownumber(w1.add(w1,w2));
break;
case 2:
System.out.println("输入需要相减的两数:");
System.out.println("请输入第一个数:");
String str21=sc.next();
w1=w1.innumber(str21);
System.out.println("请输入第二个数:");
String str22=sc.next();
w2=w2.innumber(str22);
System.out.println("结果为:");
w1.shownumber(w1.sub(w1,w2));
break;
case 3:
System.out.println("输入需要相乘的两数:");
System.out.println("请输入第一个数(x+/-yi):");
String str31=sc.next();
w1=w1.innumber(str31);
System.out.println("请输入第二个数(x+/-yi):");
String str32=sc.next();
w2=w2.innumber(str32);
System.out.println("结果为:");
w1.shownumber(w1.mul(w1, w2));
break;
case 4: System.out.println("已退出程序!");
flag=false;
break;
default: System.out.println("选择无效,请重新选择!");
break;
}
}
}
}
JAVA学习代码——虚数/复数的计算
最新推荐文章于 2021-03-21 09:41:34 发布