السلام عليكــم ورحمـة الله وبركاتــه ،،
اخوتي احتاج مساعدتكم لاكمال مشروعي وهو عبارة عن اله حاسبة علمية تقوم بحساب المدخل عن طريق التحويل من infix to postfix
من خلال استخدام stackولغة البرنامج هي لغة جافا انا قمت بكتابة الكود التالي ولكن بقيت لدي مشكلة كيف سأقوم بتفعيل كبسة الpost
ماذ يجب ان اضع في
action performed
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
public class calculator extends JFrame {
private JTextField text = new JTextField(10);
private float data1=0;
private float data2=0;
private JButton btn0 = new JButton("0");
private JButton btn1 = new JButton("1");
private JButton btn2 = new JButton("2");
private JButton btn3 = new JButton("3");
private JButton btn4 = new JButton("4");
private JButton btn5 = new JButton("5");
private JButton btn6 = new JButton("6");
private JButton btn7 = new JButton("7");
private JButton btn8 = new JButton("8");
private JButton btn9 = new JButton("9");
private JButton dot = new JButton(".");
private JButton equal = new JButton("=");
private JButton minus = new JButton("-");
private JButton plus = new JButton("+");
private JButton multiply = new JButton("*");
private JButton divid = new JButton("/");
private JButton bracket1 = new JButton("(");
private JButton bracket2 = new JButton(")");
private JButton pow = new JButton("^");
private JButton post = new JButton("post");
private JButton norm = new JButton("norm");
public calculator(){
JPanel s = new JPanel();
s.setLayout(new BorderLayout());
s.setSize(300,200);
JPanel p = new JPanel();
p.setLayout(new GridLayout(3,4));
p.setSize(300,200);
p.add(btn0);
p.add(btn1);
p.add(btn2);
p.add(btn3);
p.add(btn4);
p.add(btn5);
p.add(btn6);
p.add(btn7);
p.add(btn8);
p.add(btn9);
p.add(dot);
p.add(equal);
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3,3));
p1.setSize(300,200 );
p1.add(post);
p1.add(divid);
p1.add(plus);
p1.add(norm);
p1.add(pow);
p1.add(minus);
p1.add(bracket1);
p1.add(bracket2);
p1.add(multiply);
s.add(p , BorderLayout.WEST);
s.add(p1 , BorderLayout.EAST);
s.add(text , BorderLayout.NORTH);
s.setVisible(true);
getContentPane().add(s);
btn0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.setText(text.getText()+"0");
}
});
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.setText(text.getText()+"1");
}
});
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.setText(text.getText()+"2");
}
});
btn3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.setText(text.getText()+"3");
}
});
btn4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.setText(text.getText()+"4");
}
});
btn5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.setText(text.getText()+"5");
}
});
btn6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.setText(text.getText()+"6");
}
});
btn7.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.setText(text.getText()+"7");
}
});
btn8.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.setText(text.getText()+"8");
}
});
btn9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.setText(text.getText()+"9");
}
});
dot.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.setText(text.getText()+".");
}
});
minus.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.setText(text.getText()+"-");
}
});
/*
minus.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
data1=Float.parseFloat(text.getText());
}
});
*/
plus.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.setText(text.getText()+"+");
}
});
multiply.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.setText(text.getText()+"*");
}
});
divid.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.setText(text.getText()+"/");
}
});
post.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
// ماذا اكتب هنا حتى يتم التحويل واظهار الناتج على الشاشة
}
});
bracket1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.setText(text.getText()+"(");
}
});
bracket2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.setText(text.getText()+")");
}
});
norm.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
// حتى يرجعلي المعادلة الاصلية
}
});
equal.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//حتى يظهر الناتج رقمي بعد حساب ال post
}
});
}
public static void main(String[] args) {
calculator c = new calculator();
c.setSize(500,400);
c.setLayout(new FlowLayout());
c.setVisible(true);
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setTitle("Calculator");
}
}
import javax.swing.*;
private int maxSize;
private char[] stackArray;
private int top;
public Stack(int max) {
maxSize = max;
stackArray = new char[maxSize];
top = -1;
}
public void push(char j) {
stackArray[++top] = j;
}
public char pop() {
return stackArray[top--];
}
public char peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
}
class Stack {private Stack stack;
private String input;
private String output = "";
public InToPost(String in) {
input = in;
int stackSize = input.length();
stack = new Stack(stackSize);
}
public String doTrans(String input) {
for (int j = 0; j < input.length(); j++) {
char ch = input.charAt(j);
switch (ch) {
case '+':
case '-':
gotOper(ch, 1);
break; // (precedence 1)
case '*': // it's * or /
case '/':
gotOper(ch, 2); // go pop operators
break; // (precedence 2)
case '(': // it's a left paren
stack.push(ch); // push it
break;
case ')': // it's a right paren
gotParen(ch); // go pop operators
break;
default: // must be an operand
output = output + ch; // write it to output
break;
}
}
while (stack.isEmpty()) {
output = output + stack.pop();
}
System.out.println(output);
return output; // return postfix
}
public void gotOper(char opThis, int prec1) {
while (!stack.isEmpty()) {
char opTop =stack.pop();
if (opTop == '(') {
stack.push(opTop);
break;
}// it's an operator
else {// precedence of new op
int prec2;
if (opTop == '+' || opTop == '-')
prec2 = 1;
else
prec2 = 2;
if (prec2 < prec1) // if prec of new op less
{ // than prec of old
stack.push(opTop); // save newly-popped op
break;
} else
// prec of new not less
output = output + opTop; // than prec of old
}
}
stack.push(opThis);
}
public void gotParen(char ch){
while (!stack.isEmpty()) {
char chx = stack.pop();
if (chx == '(')
break;
else
output = output + chx;
}
}
}
public class InToPost {اتمنى المساعده منكم ولكم جزيل الشكر والاحترام