public class Xushu {
public static void main(String[] args) {
System.out.println(Data.add(new Data(5,3),new Data(8,3)))
}
}
class Data {
private double real// 表示实部
private double vir// 表示虚部
public Data(double real, double vir) {
this.real = real
this.vir = vir
}
static Data add(Data a,Data b) {
b.real += a.real// 实部相友败加
b.vir += a.vir// 虚部相加
return b
}
public String toString() {
return real+"+"+vir+"i"
}
}
//好知颤减乘除和这个类似,你根据他猛扮们的特性和逻辑在Data类里编写就可以了
public class Plus {public static void main(String[] args) {
String fristParam = args[0]
String secondParam = args[1]
String result = plus(fristParam, secondParam)
System.out.println(result)
}
private static String plus(String _sFristParam, String _sSecondParam) {
String sResult = null
if (isVirtual(_sFristParam) || isVirtual(_sSecondParam)) {
sResult = virualPlus(_sFristParam, _sSecondParam)
}
else {
sResult = normalPlus(_sFristParam, _sSecondParam)
}
return sResult
}
private static String normalPlus(String _sFristParam, String _sSecondParam) {
if (_sFristParam == null || _sSecondParam == null) {
return "对不起输入有错,请重新输入"
}
int nFristParam = Integer.parseInt(_sFristParam)
int nSecondParam = Integer.parseInt(_sSecondParam)
int nResult = nFristParam + nSecondParam
String sResult = String.valueOf(nResult)
return sResult
}
private static String virualPlus(String _sFristParam, String _sSecondParam) {
String sFirstActual = getActual(_sFristParam)
String sFirstVirtual = getVirtual(_sFristParam)
String sSecondActual = getActual(_sSecondParam)
String sSecondVirtual = getVirtual(_sSecondParam)
String sResult = null
int nFirstActual = 0
int nFirstVirtual = 0
int nSecondActual = 0
int nSecondVirtual = 0
int nVirtual = 0
int nActual = 0
if (sFirstActual == null || sFirstVirtual == null || sSecondActual == null ||
sSecondVirtual == null) {
return "对不起输入的虚数有错,请重新输入"
}
nFirstActual = Integer.parseInt(sFirstActual)
nFirstVirtual = Integer.parseInt(sFirstVirtual)
nSecondActual = Integer.parseInt(sSecondActual)
nSecondVirtual = Integer.parseInt(sSecondVirtual)
nVirtual = nFirstVirtual + nSecondVirtual
nActual = nFirstActual + nSecondActual
String sVirtual = String.valueOf(nVirtual)
String sActual = String.valueOf(nActual)
sResult = sActual + "+" + sVirtual + "i"
return sResult
}
private static String getVirtual(String _sParam) {
String[] members = _sParam.split("\\+")
String sResult = (members[1] != null) ? members[1].substring(0, 1) : null
return sResult
}
private static String getActual(String _sParam) {
String[] members = _sParam.split("\\+")
String sResult = (members[0] != null) ? members[0] : null
return sResult
}
private static boolean isVirtual(String _sParam) {
return (_sParam.indexOf("i") != -1) ? true : false
}
}
自己改一下吧,基本上没问题
package tutorialimport java.awt.*
import java.awt.event.*
import javax.swing.*
import com.sun.java_cup.internal.internal_error
import com.sun.jndi.url.dns.dnsURLContext
public class Test_1 extends JFrame {
private double realPartDou1, imaginarypartDou1, realPartDou2,
imaginarypartDou2
private String realPart1, imaginarypart1, realPart2, imaginarypart2
public Test_1() {
init()
}
public void init() {
initRealPart1()
initimaginarypart1()
System.out.println("您输入第一个复数是:" + realPartDou1
+( imaginarypartDou1>0?"+"+imaginarypartDou1:"-"+(-imaginarypartDou1) )+ "i")
initRealPart2()
initimaginarypart2()
System.out
.println("您输入第二个的复数是:" + realPartDou2 +( imaginarypartDou2 >0 ? "+"
+ imaginarypartDou2
: "-" + (0 - imaginarypartDou2)) + "型薯i"卜中者)
System.out.println("相培芦加之后的结果是:"
+ add(realPartDou1, realPartDou2)
+ (add(imaginarypartDou1, imaginarypartDou2) >0 ? "+"
+ add(imaginarypartDou1, imaginarypartDou2) : "-"
+ (0 - add(imaginarypartDou1, imaginarypartDou2)))
+ "i")
////5+6i 4+3i
double real=add(cheng(realPartDou1, realPartDou2), -cheng(imaginarypartDou1, imaginarypartDou2))
double imaginary=add(cheng(realPartDou1, imaginarypartDou2), cheng(realPartDou2, imaginarypartDou1))
System.out.println("相乘之后的结果是:"+real+(imaginary>0?"+"+imaginary:"-"+(-imaginary)))
}
public void initRealPart1() {
realPart1 = JOptionPane.showInputDialog("请键入第 1 个实数部分:(必须是数字)")
try {
realPartDou1 = Double.parseDouble(realPart1)
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "键入非法数字,请重新填写", "Message",
JOptionPane.INFORMATION_MESSAGE)
initRealPart1()
}
}
public void initimaginarypart1() {
imaginarypart1 = JOptionPane.showInputDialog("请键入第 1 个虚数部分:(必须是数字)")
try {
imaginarypartDou1 = Double.parseDouble(imaginarypart1)
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "键入非法数字,请重新填写", "Message",
JOptionPane.INFORMATION_MESSAGE)
initimaginarypart1()
}
}
public void initRealPart2() {
realPart2 = JOptionPane.showInputDialog("请键入第 2 个实数部分:(必须是数字)")
try {
realPartDou2 = Double.parseDouble(realPart2)
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "键入非法数字,请重新填写", "Message",
JOptionPane.INFORMATION_MESSAGE)
initRealPart2()
}
}
public void initimaginarypart2() {
imaginarypart2 = JOptionPane.showInputDialog("请键入第 2 个虚数部分:(必须是数字)")
try {
imaginarypartDou2 = Double.parseDouble(imaginarypart2)
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "键入非法数字,请重新填写", "Message",
JOptionPane.INFORMATION_MESSAGE)
initimaginarypart2()
}
}
public double add(double frist, double second) {
double result = frist + second
return result
}
public double cheng(double first,double second) {
double result=first*second
return result
}
public static void main(String[] args) {
Test_1 test_1 = new Test_1()
test_1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
}
}
//===========================
结果演示:
您输入第一个复数是:5.0-5.0i
您输入第二个的复数是:9.0-4.0i
相加之后的结果是:14.0-9.0i
相乘之后的结果是:25.0-65.0
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)