Skip to content

Instantly share code, notes, and snippets.

@atlass-dev
Last active May 25, 2023 07:13
Show Gist options
  • Save atlass-dev/ddd926d8e42e15ff629224ef9d5eafe6 to your computer and use it in GitHub Desktop.
Save atlass-dev/ddd926d8e42e15ff629224ef9d5eafe6 to your computer and use it in GitHub Desktop.
package com.company;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
public class Calculator {
private JTextPane textArea;
private boolean resultCalculated;
public static void main(String[] args) {
Calculator gui = new Calculator();
gui.setup();
}
private void setup(){
JFrame frame = new JFrame();
frame.setLayout(null);
JPanel topPanel = new JPanel();
textArea = new JTextPane();
StyledDocument doc = textArea.getStyledDocument();
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, doc.getLength(), center,false);
topPanel.add(textArea);
topPanel.setLayout(null);
textArea.setBounds(10,10,460,50);
topPanel.setBounds(0,0,500,70);
topPanel.setBackground(Color.darkGray);
frame.getContentPane().add(topPanel);
JPanel buttonsPanel = new JPanel();
buttonsPanel.setBackground(Color.GRAY);
buttonsPanel.setBounds(0,70,500,230);
buttonsPanel.setLayout(null);
//y14 x13 слева (25 между 9)
String[][] buttonsValues = {{"1","2","3","+"},
{"4","5","6","-"},
{"7","8","9","*"},
{".","0","C","/"}};
for(int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++){
addButton(buttonsValues[i][j], buttonsPanel, 13 + (100 + 20) * j,10 + (30 + 5) * i); //373 115
}
JButton resultButton = new JButton("=");
resultButton.setBounds(373,150,100,30);
resultButton.addActionListener(new ResultAction());
buttonsPanel.add(resultButton);
frame.getContentPane().add(buttonsPanel);
frame.setSize(500,300);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void addButton(String value, JPanel panel, int x, int y){
JButton button = new JButton(value);
button.setBounds(x,y, 100,30);
button.addActionListener(new InputAction());
panel.add(button);
}
public static double RPNtoAnswer(String rpn){
String operand = new String();
Stack<Double> stack = new Stack<>();
for (int i = 0; i < rpn.length(); i++){
if (getP(rpn.charAt(i)) == ' ') continue;
if (getP(rpn.charAt(i)) == 0){
while(rpn.charAt(i) != ' ' && getP(rpn.charAt(i)) == 0){
operand += rpn.charAt(i++);
if(i == rpn.length()) break;
}
stack.push(Double.parseDouble(operand));
operand = new String();
}
if (getP(rpn.charAt(i)) > 1){
double a = stack.pop(), b = stack.pop();
if(rpn.charAt(i) == '+') stack.push(b+a);
if(rpn.charAt(i) == '-') stack.push(b-a);
if(rpn.charAt(i) == '*') stack.push(b*a);
if(rpn.charAt(i) == '/') stack.push(b/a);
}
}
return stack.pop();
}
public static String ExpressionToRPN(String Expr){
String current = "";
Stack<Character> stack = new Stack<>();
int priority;
for (int i = 0; i < Expr.length();i++){
priority = getP(Expr.charAt(i));
if (priority == 0) current += Expr.charAt(i);
if(priority == 1) stack.push(Expr.charAt(i));
if (priority > 1){
current += ' ';
while(!stack.empty()){
if(getP(stack.peek()) >= priority)current += stack.pop();
else break;
}
stack.push(Expr.charAt(i));
}
if (priority == -1){
current+= ' ';
while(getP(stack.peek()) != 1)current += stack.pop();
stack.pop();
}
}
while(!stack.empty())current += stack.pop();
return current;
}
private static int getP(char token){
if (token == '*' || token == '/') return 3;
else if (token == '+' || token == '-') return 2;
else if (token == '(') return 1;
else if (token == ')')return -1;
else return 0;
}
public class InputAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if (!resultCalculated){
String input = e.getActionCommand();
textArea.setText(textArea.getText() + input);
}
else{
String input = e.getActionCommand();
textArea.setText("");
textArea.setText(textArea.getText() + input);
resultCalculated = false;
}
if (e.getActionCommand().equals("C"))
textArea.setText("");
}
}
public class ResultAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String action = textArea.getText();
textArea.setText(String.valueOf(RPNtoAnswer(ExpressionToRPN(action))));
resultCalculated = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment