import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
public class hi extends JFrame{
private JTextField inputspace;
private String num="";
private String prev_operation="";
private ArrayList<String>equation=new ArrayList<String>();
public hi() {
setLayout(null);
inputspace=new JTextField();
inputspace.setEditable(false);
inputspace.setBackground(Color.WHITE);
inputspace.setHorizontalAlignment(JTextField.RIGHT);
inputspace.setBounds(8, 10, 270, 70);
JPanel buttonPanel=new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4, 10, 10));
buttonPanel.setBounds(8, 90, 270, 235);
String button_names[]= {"C","/","*","=","7","8","9","+","4","5","6","-","1","2","3","0"};
JButton buttons[]=new JButton[button_names.length];
for(int i=0;i<button_names.length;i++) {
buttons[i]=new JButton(button_names[i]);
buttons[i].setFont(new Font("Arial",Font.BOLD,20));
if(button_names[i]=="C")buttons[i].setBackground(Color.RED);
else if((i>=4&&i<=6)||(i>=8&&i<=10)||(i>=12&&i<=14))buttons[i].setBackground(Color.BLACK);
else buttons[i].setBackground(Color.GRAY);
buttons[i].setForeground(Color.WHITE);
buttons[i].setBorderPainted(false);
buttons[i].addActionListener(new PadActionListener());
buttonPanel.add(buttons[i]);
}
add(inputspace);
add(buttonPanel);
setTitle("계산기");
setVisible(true);
setSize(300, 370);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
class PadActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String operation=e.getActionCommand();
if(operation.equals("C")) {
inputspace.setText("");
}
else if(operation.equals("=")) {
String result=Double.toString(calculate(inputspace.getText()));
inputspace.setText(""+result);
num="";
}
else if(operation.equals("+")||operation.equals("-")||operation.equals("*")||operation.equals("/")) {
if(inputspace.getText().equals("")&&operation.equals("-")) {
inputspace.setText(inputspace.getText()+e.getActionCommand());
}
else if(!inputspace.getText().equals("")&&!prev_operation.equals("+")&&!prev_operation.equals("-")&&!prev_operation.equals("*")&&!prev_operation.equals("/")) {
inputspace.setText(inputspace.getText()+e.getActionCommand());
}
}
else {
inputspace.setText(inputspace.getText()+e.getActionCommand());
}
prev_operation=e.getActionCommand();
}
}
private void fullTextParsing(String inputText) {
equation.clear();
for(int i=0;i<inputText.length();i++) {
char ch=inputText.charAt(i);
if(ch=='-'||ch=='+'||ch=='*'||ch=='/') {
equation.add(num);
num="";
equation.add(ch+"");
}
else {
num=num+ch;
}
}
equation.add(num);
equation.remove("");
}
public double calculate(String inputText) {
fullTextParsing(inputText);
double prev=0;
double current=0;
String mode="";
for(int i=0;i<equation.size();i++) {
String s=equation.get(i);
if(s.equals("+")) {
mode="add";
}
else if(s.equals("-")) {
mode="sub";
}
else if(s.equals("*")) {
mode="mul";
}
else if(s.equals("/")) {
mode="div";
}
else {
if((mode.equals("mul")||mode.equals("div"))&&!s.equals("-")&&!s.equals("*")&&!s.equals("/")) {
Double one=Double.parseDouble(equation.get(i-2));
Double two=Double.parseDouble(equation.get(i));
Double result=0.0;
if(mode.equals("mul")) {
result=one*two;
}
else if(mode.equals("div")) {
result=one/two;
}
equation.add(i+1,Double.toString(result));
for(int j=0;j<3;j++) {
equation.remove(i-2);
}
i-=2;
}
}
}
for(String s:equation) {
if(s.equals("+")) {
mode="add";
}
else if(s.equals("-")) {
mode="sub";
}
else {
current=Double.parseDouble(s);
if(mode.equals("add")) {
prev+=current;
}
else if(mode.equals("sub")) {
prev-=current;
}
else {
prev=current;
}
}
prev=Math.round(prev*100000)/100000.0;
}
return prev;
}
public static void main(String[] args) {
new hi();
}
}



