package calculator;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class Controller {
static View view = null;
Model model = new Model();
public static void main(String[] args) {
view = new View();
view.initPanel();
}
// �����ֽ����������
public void controlNumbers(JButton button, final String number) {
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// �������������01��002������
String temp = view.getJt1().getText();
if ("0".equals(temp)) {
view.getJt1().setText(number);
} else {
view.getJt1().setText(temp + number);
}
}
});
}
// �Բ������п���
public void controlOperators(JButton button, final String operator) {
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String temp = view.getJt1().getText();
// ����ʽ��ʾ���ı�����(��(�����)
showText(temp, operator);
// ��Ӧ���ְ�ť�¼�
buttonClicked(operator, temp);
}
private void showText(String temp, String operator) {
String str = "";
// ������1+2��-1+2����ʽ����ʱ�ٰ����������������1+2�Ľ����ִ�к��������
if (temp.matches(".+[\\+|\\-|\\*|\\/].+") && !".".equals(operator)) {
action(temp);
view.getJt1().setText(view.getJt1().getText() + operator);
} else if (temp.charAt(temp.length() - 1) == '+'// ����ʽΪ����1+ʱ�ٰ�-������ʽ���1-
|| temp.charAt(temp.length() - 1) == '-'
|| temp.charAt(temp.length() - 1) == '*'
|| temp.charAt(temp.length() - 1) == '/' || temp.charAt(temp.length() - 1) == '.') {
// ��ԭ��ʽ�д���.�ţ��������ٶ�.�Ž�������
if (".".equals(operator) && temp.contains(".")) {
view.getJt1().setText(temp);
} else {
str = temp.substring(0, temp.length() - 1) + operator;
view.getJt1().setText(str);
}
} else {
if (".".equals(operator) && temp.contains(".")) {
view.getJt1().setText(temp);
} else {
view.getJt1().setText(temp + operator);
}
}
}
// �ж��û���ִ�еIJ�����������Ӧ���¼���Ӧ
private void buttonClicked(String operator, String temp) {
// ��Ӧ=��ť
if ("=".equals(operator)) {
responseEqual(temp);
}
// ��ӦClear��ť
if ("Clear".equals(operator)) {
responseClear(temp);
}
// ��ӦBackSpace��ť
if ("BackSpace".equals(operator)) {
responseBackSpace(temp);
}
// ��Ӧ1/X��ť
if ("1/X".equals(operator)) {
responseDaoShu(temp);
}
// ��ӦSqrt��ť
if ("Sqrt".equals(operator)) {
responseSqrt(temp);
}
}
// ��ʾ�����Ľ��
private void responseClear(String temp) {
view.getJt1().setText("0");
}
// ��ʾ������
private void responseEqual(String temp) {
if (!(temp.matches(".+[\\+|\\-|\\*|\\/].+"))) {
view.getJt1().setText(temp);
} else {
action(temp);
}
}
// ��ʾ�˸��Ľ��
private void responseBackSpace(String temp) {
if (temp.length() == 1) {
view.getJt1().setText("0");
} else {
view.getJt1().setText(temp.substring(0, temp.length() - 1));
}
}
// ��ʾ��Ľ��
private void responseDaoShu(String temp) {
if ((temp.matches(".+[\\+|\\-|\\*|\\/].+"))) {
action(temp);
view.getJt1().setText(
String.valueOf(model.DaoShu((Double.valueOf(view.getJt1().getText())))));
} else if (temp.matches(".*[\\+|\\-|\\*|\\/\\.]")) {
view.getJt1().setText(temp);
} else {
view.getJt1().setText(String.valueOf(model.DaoShu(Double.valueOf(temp))));
}
}
// ��ʾ������Ľ��
private void responseSqrt(String temp) {
if ((temp.matches(".+[\\+|\\-|\\*|\\/].+"))) {
action(temp);
view.getJt1()
.setText(String.valueOf(model.sqrt(Double.valueOf(view.getJt1().getText()))));
} else if (temp.matches(".*[\\+|\\-|\\*|\\/\\.]")) {
view.getJt1().setText(temp);
} else {
view.getJt1().setText(String.valueOf(model.sqrt(Double.valueOf(temp))));
}
}
// �Ӽ�˳��ȷ��
private void action(String temp) {
String source = "";
String target = "";
double result = 0;
loop: for (int j = temp.length() - 1; j >= 0; j--) {
for (int i = 0; i < view.getOperators().length; i++) {
if (temp.charAt(j) == view.getOperators()[i].toCharArray()[0]) {
if (temp.matches("-.*")) {// ������-1-1����ʽ(��Դ������Ϊ����ʱ)���������
source = "-"
+ temp.substring(1, temp.length()).substring(
0,
temp.substring(1, temp.length()).indexOf(
view.getOperators()[i]));
target = temp.substring(1, temp.length())
.substring(
temp.substring(1, temp.length()).indexOf(view.getOperators()[i]) + 1,
temp.substring(1, temp.length()).length());
} else {// ��Դ������Ϊ�Ǹ�����ʽ���д���
source = temp.substring(0, temp.indexOf(view.getOperators()[i]));
target = temp.substring(temp.indexOf(view.getOperators()[i]) + 1, temp
.length());
}
switch (view.getOperators()[i].toCharArray()[0]) {
case '+':
result = model.plus(Double.valueOf(source), Double.valueOf(target));
break;
case '-':
result = model.minus(Double.valueOf(source), Double.valueOf(target));
break;
case '*':
result = model.multiply(Double.valueOf(source), Double.valueOf(target));
break;
case '/':
result = model.divide(Double.valueOf(source), Double.valueOf(target));
break;
default:
break;
}
view.getJt1().setText(String.valueOf(result));
break loop;
}
}
}
}
});
}
}