Java實驗考覈--複數的加減乘除

題目:
一、有一個名爲Complex的類,它的UML圖如下,實現該類的相關方法,通過圖形界面來顯示運行結果。

Complex
-realPart:double 實部
-imaginaryPart:double 虛部
+Complex() 默認構造方法
+Complex(double r,double i) 帶參數的構造方法
+getRealPart():double 返回實部
+setRealPart(double d): void 置實部
+getImaginary (): double 返回虛部
+setImaginary (double d):void 設置虛部
+complexAdd(Complex c1, Complex c2): void 複數對象與複數對象相加
+complexMinus(Complex c1, Complex c2) : void 複數對象與複數對象相減
+complexMul(Complex c1, Complex c2) : void 複數對象與複數對象相乘
+complexDiv(Complex c1, Complex c2) : void 複數對象與複數對象相除
+toString():String 以a+bi的形式顯示覆數

public class Complex22 extends Application {
	// 實部
	double realPart;
	// 虛部
	double imaginaryPart;

	// 無參構造函數
	public Complex22() {
		realPart = 0;
		imaginaryPart = 0;
	}

	// 有參數構造函數
	public Complex22(double r, double i) {
		this.realPart = r;
		this.imaginaryPart = i;
	}

	// getter和setter方法
	public double getRealPart() {
		return realPart;
	}

	public void setRealPart(double d) {
		this.realPart = d;
	}

	public double getImaginaryPart() {
		return imaginaryPart;
	}

	public void setImaginaryPart(double d) {
		this.imaginaryPart = d;
	}

	// 複數的加法
	public void Add(Complex22 c1, Complex22 c2, TextArea textArea) {
		double a = c1.getRealPart();
		double b = c1.getImaginaryPart();
		double c = c2.getRealPart();
		double d = c2.getImaginaryPart();
		Complex22 result = new Complex22(a + c, b + d);
		textArea.appendText("(" + c1 + ")" + "+" + "(" + c2 + ")" + "=" + result + "\n");
	}

	// 複數的減法
	public void Minus(Complex22 c1, Complex22 c2, TextArea textArea) {
		double a = c1.getRealPart();
		double b = c1.getImaginaryPart();
		double c = c2.getRealPart();
		double d = c2.getImaginaryPart();
		Complex22 result = new Complex22(a - c, b - d);
		textArea.appendText("(" + c1 + ")" + "-" + "(" + c2 + ")" + "=" + result + "\n");
	}

	// 複數的乘法
	public void Mul(Complex22 c1, Complex22 c2, TextArea textArea) {
		double a = c1.getRealPart();
		double b = c1.getImaginaryPart();
		double c = c2.getRealPart();
		double d = c2.getImaginaryPart();
		Complex22 result = new Complex22((a * c) - (b * d), (a * d) + (b * c));
		textArea.appendText("(" + c1 + ")" + "×" + "(" + c2 + ")" + "=" + result + "\n");
	}

	// 複數的除法
	public void Div(Complex22 c1, Complex22 c2, TextArea textArea) {
		double a = c1.getRealPart();
		double b = c1.getImaginaryPart();
		double c = c2.getRealPart();
		double d = c2.getImaginaryPart();
		Complex22 result = new Complex22((a * c + b * d) / (c * c + d * d), (b * c - a * d) / (c * c + d * d));
		textArea.appendText("(" + c1 + ")" + "÷" + "(" + c2 + ")" + "=" + result + "\n");
	}

	// toString方法
	public String toString() {
		if (realPart == 0 && imaginaryPart == 0) {
			return realPart + "+" + imaginaryPart + "i";
		} else if (realPart == 0) {
			return imaginaryPart + "i";
		} else if (imaginaryPart == 0) {
			return realPart + " ";
		} else if (imaginaryPart < 0) {
			return realPart + "" + imaginaryPart + "i";
		} else {
			return realPart + "+" + imaginaryPart + "i";
		}
	}

	// 字體
	Font f1 = Font.font("宋體", 19);
	Font f2 = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 18);
	Font f3 = Font.font("宋體", 18);

	// 複數
	HBox h1 = new HBox();
	Label complex1 = new Label("複數1");
	TextField tf1 = new TextField();
	Label complex2 = new Label("複數2");
	TextField tf2 = new TextField();

	// 文本框
	HBox h2 = new HBox();
	TextArea tArea = new TextArea();

	// 按鈕
	HBox h3 = new HBox();
	Button add = new Button("加  法"), sub = new Button("減  法"), mul = new Button("乘  法"), div = new Button("除  法");

	@Override
	public void start(Stage stage) {
		// 創建內部類對象
		ButtonHandler handler = new ButtonHandler();

		// 複數的樣式佈局
		h1.setPadding(new Insets(50, 40, 50, 50));
		h1.setSpacing(10);
		complex1.setFont(f1);
		tf1.setPromptText("a+bi");
		tf1.setPrefSize(200, 20);
		tf1.setFont(f2);
		complex2.setFont(f1);
		tf2.setPromptText("c+di");
		tf2.setPrefSize(200, 20);
		tf2.setFont(f2);
		h1.getChildren().addAll(complex1, tf1, complex2, tf2);

		// 文本框的樣式佈局
		h2.setPadding(new Insets(0, 40, 50, 65));
		tArea.setPrefSize(500, 300);
		tArea.setPromptText("計算結果...");
		tArea.setFont(f3);
		h2.getChildren().add(tArea);

		// 按鈕的樣式佈局
		h3.setPadding(new Insets(0, 50, 50, 100));
		h3.setSpacing(25);
		add.setPrefSize(90, 20);
		add.setFont(f3);
		sub.setPrefSize(90, 20);
		sub.setFont(f3);
		mul.setPrefSize(90, 20);
		mul.setFont(f3);
		div.setPrefSize(90, 20);
		div.setFont(f3);
		h3.getChildren().addAll(add, sub, mul, div);

		// 爲加法按鈕註冊事件處理器
		add.setOnAction(handler);

		// 爲減法按鈕註冊事件處理器
		sub.setOnAction(handler);

		// 爲乘法按鈕註冊事件處理器
		mul.setOnAction(handler);

		// 爲除法按鈕註冊事件處理器
		div.setOnAction(handler);

		// 創建根面板
		BorderPane rootNode = new BorderPane();
		rootNode.setTop(h1);
		rootNode.setCenter(h2);
		rootNode.setBottom(h3);

		// 設置場景和舞臺
		Scene scene = new Scene(rootNode, 630, 630);
		stage.setTitle("複數計算器");
		stage.setScene(scene);
		stage.setResizable(false);// 固定窗口大小
		stage.show();

	}

	// 內部類實現事件處理方法
	public class ButtonHandler implements EventHandler<ActionEvent> {
		@Override
		public void handle(ActionEvent event) {
			// 對象實例化
			Complex22 c1 = new Complex22();
			Complex22 c2 = new Complex22();
			// 複數實部和虛部的提取
			if (tf1.getText().matches("(\\d{1,}\\+\\d{1,})[i]")) { // a+bi
				String[] str = tf1.getText().split("[+i]");
				c1.realPart = Double.parseDouble(str[0]);
				c1.imaginaryPart = Double.parseDouble(str[1]);
			} else if (tf1.getText().matches("(\\d{1,}\\-\\d{1,})[i]")) { // a-bi
				String[] str = tf1.getText().split("[-i]");
				c1.realPart = Double.parseDouble(str[0]);
				double b = Double.parseDouble(str[1]);
				c1.imaginaryPart = -b;
			} else if (tf1.getText().matches("(\\-\\d{1,}\\+\\d{1,})[i]")) { // -a+bi
				String[] str = tf1.getText().split("[+i]");
				c1.realPart = Double.parseDouble(str[0]);
				c1.imaginaryPart = Double.parseDouble(str[1]);
			} else if (tf1.getText().matches("(\\-\\d{1,}\\-\\d{1,})[i]")) {// -a-bi
				String[] str = tf1.getText().split("[-i]");
				double a = Double.parseDouble(str[1]);
				double b = Double.parseDouble(str[2]);
				c1.realPart = -a;
				c1.imaginaryPart = -b;
			} else if (tf1.getText().matches("(\\-?\\d{1,})[i]")) { // bi // //-bi
				String[] str = tf1.getText().split("[i]");
				c1.realPart = 0;
				c1.imaginaryPart = Double.parseDouble(str[0]);
			} else if (tf1.getText().matches("\\-?\\d{1,}\\+[i]")) { // a+i // //-a+i
				String[] str = tf1.getText().split("[+i]");
				c1.realPart = Double.parseDouble(str[0]);
				c1.imaginaryPart = 1;
			} else if (tf1.getText().matches("\\d{1,}\\-[i]")) { // a-i
				String[] str = tf1.getText().split("[-i]");
				c1.realPart = Double.parseDouble(str[0]);
				c1.imaginaryPart = -1;
			} else if (tf1.getText().matches("\\-\\d{1,}-[i]")) { // -a-i
				String[] str = tf1.getText().split("[-i]");
				double a = Double.parseDouble(str[1]);
				c1.realPart = -a;
				c1.imaginaryPart = -1;
			} else if (tf1.getText().matches("[i]")) { // i
				c1.realPart = 0;
				c1.imaginaryPart = 1;
			} else if (tf1.getText().matches("\\-[i]")) { // -i
				c1.realPart = 0;
				c1.imaginaryPart = -1;
			} else if (tf1.getText().matches("\\d{1,}")) { // a
				String[] str = tf1.getText().split("[ ]");
				c1.realPart = Double.parseDouble(str[0]);
				c1.imaginaryPart = 0;
			} else if (tf1.getText().matches("\\-\\d{1,}")) { // -a
				String[] str = tf1.getText().split("[-]");
				double a = Double.parseDouble(str[1]);
				c1.realPart = -a;
				c1.imaginaryPart = 0;
			}else {
				Alert alert =new Alert(AlertType.WARNING);
				alert.titleProperty().set("出錯了");
				alert.headerTextProperty().set("數據格式不對");
				alert.show();
			}
			if (tf2.getText().matches("(\\d{1,}\\+\\d{1,})[i]")) { // a+bi
				String[] str1 = tf2.getText().split("[+i]");
				c2.realPart = Double.parseDouble(str1[0]);
				c2.imaginaryPart = Double.parseDouble(str1[1]);
			} else if (tf2.getText().matches("(\\d{1,}\\-\\d{1,})[i]")) { // a-bi
				String[] str1 = tf2.getText().split("[-i]");
				c2.realPart = Double.parseDouble(str1[0]);
				double b1 = Double.parseDouble(str1[1]);
				c2.imaginaryPart = -b1;
			} else if (tf2.getText().matches("(\\-\\d{1,}\\+\\d{1,})[i]")) { // -a+bi
				String[] str1 = tf2.getText().split("[+i]");
				c2.realPart = Double.parseDouble(str1[0]);
				c2.imaginaryPart = Double.parseDouble(str1[1]);
			} else if (tf2.getText().matches("(\\-\\d{1,}\\-\\d{1,})[i]")) {// -a-bi
				String[] str1 = tf2.getText().split("[-i]");
				double a1 = Double.parseDouble(str1[1]);
				double b1 = Double.parseDouble(str1[2]);
				c2.realPart = -a1;
				c2.imaginaryPart = -b1;
			} else if (tf2.getText().matches("(\\-?\\d{1,})[i]")) { // bi // //-bi
				String[] str1 = tf2.getText().split("[i]");
				c2.realPart = 0;
				c2.imaginaryPart = Double.parseDouble(str1[0]);
			} else if (tf2.getText().matches("\\-?\\d{1,}\\+[i]")) { // a+i // //-a+i
				String[] str1 = tf2.getText().split("[+i]");
				c2.realPart = Double.parseDouble(str1[0]);
				c2.imaginaryPart = 1;
			} else if (tf2.getText().matches("\\d{1,}\\-[i]")) { // a-i
				String[] str1 = tf2.getText().split("[-i]");
				c2.realPart = Double.parseDouble(str1[0]);
				c2.imaginaryPart = -1;
			} else if (tf2.getText().matches("\\-\\d{1,}-[i]")) { // -a-i
				String[] str1 = tf2.getText().split("[-i]");
				double a1 = Double.parseDouble(str1[1]);
				c2.realPart = -a1;
				c2.imaginaryPart = -1;
			} else if (tf2.getText().matches("[i]")) { // i
				c2.realPart = 0;
				c2.imaginaryPart = 1;
			} else if (tf2.getText().matches("\\-[i]")) { // -i
				c2.realPart = 0;
				c2.imaginaryPart = -1;
			} else if (tf2.getText().matches("\\d{1,}")) { // a
				String[] str1 = tf1.getText().split("[ ]");
				c2.realPart = Double.parseDouble(str1[0]);
				c2.imaginaryPart = 0;
			} else if (tf2.getText().matches("\\-\\d{1,}")) { // -a
				String[] str1 = tf2.getText().split("[-]");
				double a1 = Double.parseDouble(str1[1]);
				c2.realPart = -a1;
				c2.imaginaryPart = 0;
			}else {
				Alert alert =new Alert(AlertType.WARNING);
				alert.titleProperty().set("出錯了");
				alert.headerTextProperty().set("數據格式不對");
				alert.show();
			}

			if ((Button) (event.getSource()) == add) { // 加法
				try {
					Complex22 c = new Complex22();
					c.Add(c1, c2, tArea);
				} catch (Exception e) {
					// TODO: handle exception

				}
			} else if (event.getSource() == sub) { // 減法
				try {
					Complex22 c = new Complex22();
					c.Minus(c1, c2, tArea);
				} catch (Exception e) {
					// TODO: handle exception

				}

			} else if (event.getSource() == mul) { // 乘法
				try {
					Complex22 c = new Complex22();
					c.Mul(c1, c2, tArea);
				} catch (Exception e) {
					// TODO: handle exception

				}
			} else if (event.getSource() == div) { // 除法
				try {
					Complex22 c = new Complex22();
					c.Div(c1, c2, tArea);
				} catch (Exception e) {
					// TODO: handle exception

				}
			}
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Application.launch(args);
	}
}

缺點:只能夠實現以上幾種a+bi形式複數的計算,如果實部和虛部之間加一些空格,或者是“3+3”、“6i+6i”的形式,會跳出警告框。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章