package BAITHUCHANHSO2;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link];
import [Link];
public class BAI51 extends JFrame implements ActionListener {
JButton bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9, btcong, bttru, btnhan,
btchia, btbang, btkhong, btc, btcham;
JTextField txt1;
Panel pn, pn1, pn2, pn3;
private Stack<String> operatorsStack = new Stack<>();
private Stack<String> operandsStack = new Stack<>();
public BAI51() { // Constructor name corrected
txt1 = new JTextField(15);
[Link]([Link]);
Font font = [Link]();
float size = [Link]() + 5.0f; // Tăng kích thước font lên 5 đơn vị
[Link]([Link](size));
bt1 = new JButton("1");
bt2 = new JButton("2");
bt3 = new JButton("3");
bt4 = new JButton("4");
bt5 = new JButton("5");
bt6 = new JButton("6");
bt7 = new JButton("7");
bt8 = new JButton("8");
bt9 = new JButton("9");
btcong = new JButton("+");
bttru = new JButton("-");
btnhan = new JButton("*");
btchia = new JButton("/");
btkhong = new JButton("0");
btcham = new JButton(".");
btc = new JButton("C");
btbang= new JButton("=");
pn1 = new Panel(new GridLayout(4, 4, 2, 2));
[Link](bt7);
[Link](bt8);
[Link](bt9);
[Link](btchia);
[Link](bt4);
[Link](bt5);
[Link](bt6);
[Link](btnhan);
[Link](bt1);
[Link](bt2);
[Link](bt3);
[Link](bttru);
[Link](btkhong);
[Link](btcham);
[Link](btc);
[Link](btcong);
[Link](pn1, [Link]);
pn2 = new Panel(new GridLayout(1, 1));
[Link](txt1);
[Link](pn2, [Link]); // Corrected to pn2
pn3 = new Panel(new GridLayout(1, 1)); // Syntax error corrected
[Link](btbang);
[Link](pn3, [Link]);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = [Link]();
// Xử lý các nút số
if (source == bt1) {
[Link]([Link]() + "1");
} else if (source == bt2) {
[Link]([Link]() + "2");
} else if (source == bt3) {
[Link]([Link]() + "3");
} else if (source == bt4) {
[Link]([Link]() + "4");
} else if (source == bt5) {
[Link]([Link]() + "5");
} else if (source == bt6) {
[Link]([Link]() + "6");
} else if (source == bt7) {
[Link]([Link]() + "7");
} else if (source == bt8) {
[Link]([Link]() + "8");
} else if (source == bt9) {
[Link]([Link]() + "9");
} else if (source == btkhong) {
[Link]([Link]() + "0");
}
else if (source == btcham) {
[Link]([Link]() + ".");
}
if (source == btcong) {
[Link]([Link]() + "+");
} else if (source == bttru) {
[Link]([Link]() + "-");
} else if (source == btnhan) {
[Link]([Link]() + "*");
} else if (source == btchia) {
[Link]([Link]() + "/");
}
if (source == btc) {
[Link]("");
}
if (source == btbang) {
calculateResult();
}
}
private Double performOperation(Double operand1, Double operand2, String
operator) {
switch (operator) {
case "+":
return operand1 + operand2;
case "-":
return operand1 - operand2;
case "*":
return operand1 * operand2;
case "/":
if (operand2 == 0) {
throw new ArithmeticException("Division by zero!");
}
return operand1 / operand2;
default:
throw new IllegalArgumentException("Invalid operator: " +
operator);
}
}
private void calculateResult() {
String input = [Link]();
// Split the input string into tokens (numbers and operators)
String[] tokens = [Link](" ");
// Stack to store operands
Stack<Double> operandsStack = new Stack<>();
try {
// Process each token
for (String token : tokens) {
// If token is a number, push it to the stack
if (isNumeric(token)) {
[Link]([Link](token));
} else {
// If token is an operator, pop two operands, perform
operation, and push result
Double operand2 = [Link]();
Double operand1 = [Link]();
Double result = performOperation(operand1, operand2, token);
[Link](result);
}
}
// Final result is the top element of the stack
Double finalResult = [Link]();
[Link]([Link](finalResult));
} catch (EmptyStackException e) {
// Handle case of insufficient operands (e.g., pressing "+" without
numbers)
[Link]("Error: Insufficient operands!");
} catch (ArithmeticException e) {
[Link]("Math Error: Division by zero!");
}
}
// Helper method to check if a string is numeric
private boolean isNumeric(String str) {
try {
[Link](str);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public static void main(String[] args) {
new BAI51();
}}