用java实现复数的加减乘除运算(改进第1次)
前两天为了赶速度,自己很快实现了如题java老师布置的作业,并在csdn上写了博客,记录学习过程,原博客链接为:http://blog.csdn.net/u010043538/article/details/12765527。但是很快有无私的网友指出了其中一个显然的错误:没有考虑除数为0的情况。虽然java初学,不过错就是错,没有啥理由。自己以后写老师作业,会尽可能深进去,把已经自学过的内容体现出来,而不是跟老师上课的内容来写代码。在此基础上,尽可能保证快速解决问题。
此次改进修正了错误:没有考虑除数为0的情况。并对程序的封装性做了一定程度的改进。
package Four;
/**
* @author Kun Sun
* @Date: 2013.10.17
*/
public class Complex { // 复数类
double real; // 实部
double image; // 虚部
Complex(double real,double image){ // 带参数的构造方法
this.real = real;
this.image = image;
}
public Complex() {
// TODO Auto-generated constructor stub
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getImage() {
return image;
}
public void setImage(double image) {
this.image = image;
}
Complex add(Complex a){ // 复数相加
double real2 = a.getReal();
double image2 = a.getImage();
double newReal = real + real2;
double newImage = image + image2;
Complex result = new Complex(newReal,newImage);
return result;
}
Complex sub(Complex a){ // 复数相减
double real2 = a.getReal();
double image2 = a.getImage();
double newReal = real - real2;
double newImage = image - image2;
Complex result = new Complex(newReal,newImage);
return result;
}
Complex mul(Complex a){ // 复数相乘
double real2 = a.getReal();
double image2 = a.getImage();
double newReal = real*real2 - image*image2;
double newImage = image*real2 + real*image2;
Complex result = new Complex(newReal,newImage);
return result;
}
Complex div(Complex a){ // 复数相除
double real2 = a.getReal();
double image2 = a.getImage();
double newReal = (real*real2 + image*image2)/(real2*real2 + image2*image2);
double newImage = (image*real2 - real*image2)/(real2*real2 + image2*image2);
Complex result = new Complex(newReal,newImage);
return result;
}
public void print(){ // 用于格式化输出
if(image > 0){
System.out.println(real + " + " + image + "i");
}else if(image < 0){
System.out.println(real + "" + image + "i");
}else{
System.out.println(real);
}
}
// 封装了具体运算,主要为对输入进行转换,对输出封装
public static void talk(String data1_1,String data1_2,String data2_1,String data2_2,String operation){
// 以下语句为格式转换
double dat1_1 = Double.parseDouble(data1_1); // 把string型实部转为double型
double dat1_2 = Double.parseDouble(data1_2.substring(0,data1_2.length()-1));// 把去除"i"的string型虚部转为double型
double dat2_1 = Double.parseDouble(data2_1);// 把string型实部转为double型
double dat2_2 = Double.parseDouble(data2_2.substring(0,data2_2.length()-1));// 把去除"i"的string型虚部转为double型
Complex num1 = new Complex(dat1_1,dat1_2);
Complex num2 = new Complex(dat2_1,dat2_2);
Complex result;
int a,b;
if(operation.equals("+")){ // 两数相加
result = num1.add(num2);
result.print();
}
if(operation.equals("-")){ // 两数相减
result = num1.sub(num2);
result.print();
}
if(operation.equals("*")){ // 两数相乘
result = num1.mul(num2);
result.print();
}
if(operation.equals("/")){ // 两数相除
if(num2.real * num2.real + num2.image*num2.image != 0){
result = num1.div(num2);
result.print();
}else{
System.out.println("运算出错,除数运算中除数不能为0,请用户重新输入!");
}
}
}
}
package Four;
import java.util.Scanner;
/**
* @author Kun Sun
* @Date: 2013.10.17
*/
public class MainClass { // 用于测试复数类
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Complex complex = new Complex();
// 以下测试中的talk()中的解释为
// talk(string data1_1,string data1_2,string data2_1,string data2_2,string operation);
// data1_1,data1_2依次为第一个复数的实部和虚部
// data2_1,data2_2依次为第二个复数的实部和虚部
// operation为两个复数运算的操作符,每种测试都做加减乘除
System.out.println("测试1:-------------------------"); // 一般测试
complex.talk("5", "4i", "3", "6i", "+");
complex.talk("5", "4i", "3", "6i", "-");
complex.talk("5", "4i", "3", "6i", "*");
complex.talk("5", "4i", "3", "6i", "/");
System.out.println("\n测试2:-------------------------"); // 包含负数测试
complex.talk("-2","4i", "5", "-8i","+");
complex.talk("-2","4i", "5", "-8i","-");
complex.talk("-2","4i", "5", "-8i","*");
complex.talk("-2","4i", "5", "-8i","/");
System.out.println("\n测试3:-------------------------"); // 包含小数测试
complex.talk("-1.2","3.1i","4.3","-6.5i","+");
complex.talk("-1.2","3.1i","4.3","-6.5i","-");
complex.talk("-1.2","3.1i","4.3","-6.5i","*");
complex.talk("-1.2","3.1i","4.3","-6.5i","/");
System.out.println("\n测试4:-------------------------"); // 包含除数为0测试
complex.talk("5", "4i", "0", "0i", "+");
complex.talk("5", "4i", "0", "0i", "-");
complex.talk("5", "4i", "0", "0i", "*");
complex.talk("5", "4i", "0", "0i", "/");
}
}