package com.doucity.xiaoxiao.matrix.m;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import com.doucity.xiaoxiao.matrix.v.Matrix;
public class Operation implements ActionListener {
Matrix m = null;
public void actionPerformed(ActionEvent e) {
m.getFileC().setText("xiao_xiao.txt");
//分割符
//String
//小数位数
String text_bit = m.getText_bit().getText();
//文件A
String fileA = m.getFileA().getText();
//规格
String fileA_type_0 = m.getFileA_type_0().getText();
String fileA_type_1 = m.getFileA_type_1().getText();
//文件B
String fileB = m.getFileB().getText();
//规格
String fileB_type_0 = m.getFileB_type_0().getText();
String fileB_type_1 = m.getFileB_type_1().getText();
//参数判断
if (!check()) {
return;
}
//根据不同的操作会对规格做不同的判断
String buttonSource = ((JButton) e.getSource()).getText();
if (buttonSource.equals("A+B")) {
if (!fileA_type_0.equals(fileB_type_0) || !fileA_type_1.equals(fileB_type_1)) {
JOptionPane.showMessageDialog(null,
"规格填写错误,做矩阵加法时,矩阵A的行(列)必须等于矩阵B的行(列)", "提示",
JOptionPane.ERROR_MESSAGE);
return ;
}
} else if (buttonSource.equals("A-B")) {
if (!fileA_type_0.equals(fileB_type_0) || !fileA_type_1.equals(fileB_type_1)) {
JOptionPane.showMessageDialog(null,
"规格填写错误,做矩阵减法时,矩阵A的行(列)必须等于矩阵B的行(列)", "提示",
JOptionPane.ERROR_MESSAGE);
return ;
}
} else if (buttonSource.equals("A*B")) {
if (!fileA_type_1.equals(fileB_type_0)) {
JOptionPane.showMessageDialog(null,
"规格填写错误,做矩阵乘法时,矩阵A的列必须等于矩阵B的行", "提示",
JOptionPane.ERROR_MESSAGE);
return ;
}
}
//读取文件A,并且做简单的验证
double[][] a = null;
try {
a = new ReadMatix().read(fileA, "\t", Integer.parseInt(fileA_type_0), Integer.parseInt(fileA_type_1));
} catch (Exception e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(null,
"读取文件A时出错,错误为:" + e1.getMessage(), "提示",
JOptionPane.ERROR_MESSAGE);
return ;
}
if (a == null) {
JOptionPane.showMessageDialog(null,
"读取文件A时出错", "提示",
JOptionPane.ERROR_MESSAGE);
return;
}
double[][] b = null;
try {
b = new ReadMatix().read(fileB, "\t", Integer.parseInt(fileB_type_0), Integer.parseInt(fileB_type_1));
} catch (Exception e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(null,
"读取文件B时出错,错误为:" + e1.getMessage(), "提示",
JOptionPane.ERROR_MESSAGE);
return ;
}
if (b == null) {
JOptionPane.showMessageDialog(null,
"读取文件B时出错", "提示",
JOptionPane.ERROR_MESSAGE);
return;
}
String fileC = fileB.replace("\\", "/");
fileC = fileC.substring(0, fileC.lastIndexOf("/")) + "/xiao_xiao.txt";
m.getFileC().setText(fileC);
m.getFileC().setToolTipText(fileC);
try {
//根据不同的按钮操作调用不同的操作
if (buttonSource.equals("A+B")) {
new OperationsMatix().plus(fileC, "\t",text_bit,a, b);
} else if (buttonSource.equals("A-B")) {
new OperationsMatix().sub(fileC, "\t",text_bit,a, b);
} else if (buttonSource.equals("A*B")) {
new OperationsMatix().multi(fileC, "\t",text_bit,a, b);
}
JOptionPane.showMessageDialog(null,
"计算完成,结果保存到文件中:\n" + fileC, "提示",
JOptionPane.ERROR_MESSAGE);
return;
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
JOptionPane.showMessageDialog(null,
"计算过程中出错,错误为:" + e1.getLocalizedMessage(), "提示",
JOptionPane.ERROR_MESSAGE);
return;
}
}
public Operation() {
}
public Operation(Matrix m) {
this.m = m;
}
public boolean check() {
//文件A
String fileA = m.getFileA().getText();
//文件A规格
String fileA_type_0 = m.getFileA_type_0().getText();
String fileA_type_1 = m.getFileA_type_1().getText();
if (fileA == null || fileA.equals("")) {
JOptionPane.showMessageDialog(null,
"请选择矩阵A", "提示",
JOptionPane.ERROR_MESSAGE);
return false;
}
if (fileA_type_0 == null || fileA_type_0.equals("")) {
JOptionPane.showMessageDialog(null,
"请填写矩阵A的规格", "提示",
JOptionPane.ERROR_MESSAGE);
return false;
}
if (!isNumber_1_9(fileA_type_0)) {
JOptionPane.showMessageDialog(null,
"矩阵A的规格必须是大于0的整数", "提示",
JOptionPane.ERROR_MESSAGE);
return false;
}
if (fileA_type_1 == null || fileA_type_1.equals("")) {
JOptionPane.showMessageDialog(null,
"请填写矩阵A的规格", "提示",
JOptionPane.ERROR_MESSAGE);
return false;
}
if (!isNumber_1_9(fileA_type_1)) {
JOptionPane.showMessageDialog(null,
"矩阵A的规格必须是大于0的整数", "提示",
JOptionPane.ERROR_MESSAGE);
return false;
}
//文件B
String fileB = m.getFileB().getText();
//文件B规格
String fileB_type_0 = m.getFileB_type_0().getText();
String fileB_type_1 = m.getFileB_type_1().getText();
if (fileB == null || fileB.equals("")) {
JOptionPane.showMessageDialog(null,
"请选择矩阵B", "提示",
JOptionPane.ERROR_MESSAGE);
return false;
}
if (fileB_type_0 == null || fileB_type_0.equals("")) {
JOptionPane.showMessageDialog(null,
"请填写矩阵B的规格", "提示",
JOptionPane.ERROR_MESSAGE);
return false;
}
if (!isNumber_1_9(fileB_type_0)) {
JOptionPane.showMessageDialog(null,
"矩阵B的规格必须是大于0的整数", "提示",
JOptionPane.ERROR_MESSAGE);
return false;
}
if (fileB_type_1 == null || fileB_type_1.equals("")) {
JOptionPane.showMessageDialog(null,
"请填写矩阵B的规格", "提示",
JOptionPane.ERROR_MESSAGE);
return false;
}
if (!isNumber_1_9(fileB_type_1)) {
JOptionPane.showMessageDialog(null,
"矩阵B的规格必须是大于0的整数", "提示",
JOptionPane.ERROR_MESSAGE);
return false;
}
//小数位数
String text_bit = m.getText_bit().getText();
if (text_bit == null || text_bit.equals("")) {
JOptionPane.showMessageDialog(null,
"请填写结果文件的小数位数", "提示",
JOptionPane.ERROR_MESSAGE);
return false;
}
if (!isNumber_0_9(text_bit)) {
JOptionPane.showMessageDialog(null,
"结果文件的小数位数必须是大于等于0的整数", "提示",
JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}
/*
* 大于0的整数
*/
public boolean isNumber_1_9(String str) {
Pattern pattern = Pattern.compile("[1-9][0-9]{0,}");
Matcher matcher = pattern.matcher((CharSequence)str);
boolean result = matcher.matches();
return result;
}
/*
* 大于等于0的整数
*/
public boolean isNumber_0_9(String str) {
Pattern pattern = Pattern.compile("[0-9]{1,}");
Matcher matcher = pattern.matcher((CharSequence)str);
boolean result = matcher.matches();
return result;
}
}