Project Data - تراكيب

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

package postfix.

prefix;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.util.Stack;

public class PostfixPrefix extends JFrame implements ActionListener {


JButton b1 = new JButton("Evaluation PostFix");
JButton b2 = new JButton("Evaluation PreFix");
JButton b3 = new JButton("Delete everything ");
JTextField t1 = new JTextField(10);
JTextField t2 = new JTextField(10);
JLabel l1 = new JLabel("Enter PostFix or PreFix: ");
JLabel l2 = new JLabel("Result: ");
public PostfixPrefix(){
super("PostFix&PreFix");
setLayout(null);
add(l1);l1.setBounds(45,20,150, 50);
add(t1);t1.setBounds(200,35, 150, 20);
add(b1);b1.setBounds(400,35, 150, 20); b1.setForeground(Color.BLUE);
add(b2);b2.setBounds(400,80, 150, 20); b2.setForeground(Color.BLUE);
add(b3); b3.setBounds(400,120, 150, 20); b3.setForeground(Color.RED);
add(l2);l2.setBounds(45,110, 150, 50);
add(t2);t2.setBounds(100, 125, 150, 20);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
t2.setEditable(false);

public void actionPerformed(ActionEvent e) {


try{
if(e.getSource()==b1){
Stack<Integer> stack=new Stack<>();
String x=t1.getText();
for(int i=0;i<x.length();i++) {
char a=x.charAt(i);
if(Character.isDigit(a))
stack.push(a - '0');
else {
int num1 = stack.pop();
int num2 = stack.pop();
switch(a) {
case '+':
stack.push(num2+num1);
break;

case '-':
stack.push(num2- num1);
break;

case '/':
stack.push(num2/num1);
break;

case '*':
stack.push(num2*num1);
break;}
}}
int z= stack.pop();
String c=Integer.toString(z);
t2.setText(c);
t2.setForeground(Color.blue);
}

if(e.getSource()==this.b2){
Stack<Integer> stack=new Stack<>();
String f=t1.getText();
String f=t1.getText();
String exp="";
char ch[] = f.toCharArray();
int v = ch.length;
char result[] = new char[v];
for(int i = 0; i<ch.length; i++) {
result[v-1] = ch[i];
v = v - 1; }
for(int i = 0; i<result.length; i++)
exp= exp+result[i];

for(int i=0;i<exp.length();i++) {
char c=exp.charAt(i);

if(Character.isDigit(c))
stack.push(c - '0');
else {
int num1 = stack.pop();
int num2 = stack.pop();
switch(c){
case '+':
stack.push(num1+num2);
break;
case '-':
stack.push(num1- num2);
break;
case '/':
stack.push(num1/num2);
break;
case '*':
stack.push(num1*num2);
break; }
}}
int g= stack.pop();
String s=Integer.toString(g);
t2.setText(s);
t2.setForeground(Color.blue);
}
}catch(ArithmeticException ae){
t2.setText("you cant divide by 0");
t2.setForeground(Color.RED);
} catch(Exception ae){
t2.setText("some things is wrong");
t2.setForeground(Color.RED);
}
if(e.getSource()==b3){
t1.setText(" ");
t2.setText(" ");
}
}

public static void main(String[] args) {


PostfixPrefix f = new PostfixPrefix();
f.setVisible(true);
f.setSize(600,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
}
}

: ‫اﻋﺪاد اﻟﻄﺎﻟ)ٮﺎت‬
‫ﺷﻬﺪ‬-‫ٮﻼف‬2‫ﻣ‬-‫اﺷواق‬-‫اﺳﻤﺎء‬
1297: ‫ڡﻢ‬:‫ﺷﻌ)ٮﻪ ر‬

You might also like