📄 yufa.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class yufa extends JFrame implements ActionListener
{
int m;
int right;
int t=0;
private JTextArea ta1;
private JTextArea ta2;
private JButton jb=new JButton("语法分析");
private JButton jb1=new JButton("清空文本区");
private JLabel jl1=new JLabel("输入表达式:");
private JLabel jl2=new JLabel("分析结果:");
char[] c=new char[50];
char exp[]=new char[50];
int result;
public yufa()
{
super("语法分析器");
this.setSize(600,448);
this.setLocation(200,150);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(null);
JPanel jp =new JPanel();
JPanel jp1 =new JPanel();
jp.setSize(300, 400);
jp1.setSize(300, 400);
jp.setLocation(0, 0);
jp1.setLocation(300, 0);
jp.setLayout(null);
jp1.setLayout(null);
ta1=new JTextArea(20,30);
ta2=new JTextArea(20,30);
ta2.setEditable(false);
JScrollPane scrol1=new JScrollPane(ta1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JScrollPane scrol2=new JScrollPane(ta2,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrol1.setSize(260, 310);
scrol1.setLocation(18, 40);
scrol2.setSize(260, 310);
scrol2.setLocation(18, 40);
jp.add(scrol1);
jp1.add(scrol2);
jb.setBounds(80, 370, 100, 25);
jb1.setBounds(80, 370, 100, 25);
jl1.setFont(new Font("楷书",Font.BOLD,12));
jl1.setSize(140, 25);
jl1.setLocation(0, 0);
jl2.setFont(new Font("楷书",Font.BOLD,12));
jl2.setSize(140, 25);
jl2.setLocation(0, 0);
jp.add(jb1);
jp.add(jl1);
jp1.add(jb);
jp1.add(jl2);
this.add(jp);
this.add(jp1);
jb.addActionListener(this);
jb1.addActionListener(this);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb1)
{
int a1=JOptionPane.showConfirmDialog(null, "确定清空吗?","提示!",JOptionPane.YES_NO_OPTION);
if( a1==JOptionPane.YES_OPTION)
{
ta1.setText("");
ta2.setText("");
result=-1;
}
}
if(e.getSource()==jb)
{
int n=0;
right=1;
m=0;
String jg;
String a=ta1.getText();
char[] b=new char[a.length()];
for(int i=0;i<a.length();i++)
{
b[i]=a.charAt(i);
c[i]=a.charAt(i);
n=i;
}
for(int d=n+1;d<50;d++)
c[d]='#';
while(m<a.length()&&c[m]!='#'&&right==1)
{
if(Character.isDigit(c[m])||c[m]=='+'||c[m]=='-'||c[m]=='/'||c[m]=='*'||c[m]=='('||c[m]==')'||c[m]=='#')
{
e(c[m]);
}
else
{
ta2.setText("输入不正确,请重新输入!");
return;
}
}
if(c[m]=='#'&&right==1&&result!=-65535)
{
ta2.setText("OK"+"\n");
jg=String.valueOf(result);
ta2.setText(ta2.getText()+jg);
}
else
if(result!=-65535)
ta2.setText("Error"+"\n");
}
}
int e(char i)
{
result = t (c[m]);
if(result==-1)
return -1;
while (c[m] == '+' || c[m] == '-')
{
if(c[m] == '+')
{
m++;
result += t (c[m]);
}
else
if(c[m] == '-')
{
m++;
result -= t (c[m]);
}
}
return result;
}
int t(char i)
{
result = f (c[m]);
while (c[m] == '*' || c[m] == '/')
{
if(c[m] == '*')
{
m++;
result *= f (c[m]);
}
else
if(c[m] == '/')
{
m++;
if(c[m]=='0')
{
c[m]='#';
ta2.setText("除数不能为0,请重新输入!");
return -65535;
}
else
result /= f (c[m]);
}
}
return result;
}
int f(char i)
{
if(c[m]=='(')
{
m++;
t=e(c[m]);
if(c[m]==')')
m++;
else
{
right=0;
return right;
}
}
else if(Character.isDigit(c[m]))
{
int n=0;
char[] b=new char[5];
b[0]=c[m];
n=1;
m++;
while(Character.isDigit(c[m]))
{
b[n]=c[m];
m++;
n++;
}
for(;n<5;n++)
b[n]='#';
t=(int)b[0]-48;
n=1;
while(n<5)
{
if(Character.isDigit(b[n]))
{
t=t*10+((int)b[n]-48);
n++;
}
else
n=5;
}
}
else
{
right=0;
return right;
}
return t;
}
public static void main(String args[])
{
new yufa();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -