⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 downtoup.java

📁 编程原理
💻 JAVA
字号:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;

class stacks{
	public char[] sstack=new char[20];

//	public String one=new String();
//	public String two=new String();
	private int[][] xyz ={{1,2,1,1,1,1,0},
	                      {0,3,3,3,3,0,3},
	                      {1,3,3,3,1,1,3},
	                      {1,3,1,3,1,1,3},
	                      {1,3,3,3,1,1,3},
	                      {0,3,3,3,3,0,3},
	                      {1,0,1,1,1,1,2}};

	public stacks()
  	{int i;
  	 for(i=0;i<20;i++)
     sstack[i]=' ';
    }
	public void push(int x,char y)
	{sstack[x]=y;
	}
public boolean isVt(int x)
	{ if(Character.isUpperCase(sstack[x])==true) return false;
	  else return true;
	}
	public int priority(int x,char y)
	{int x1,y1;
	 x1=((int)sstack[x])%10;
	 y1=((int)y)%10;
	 return xyz[x1][y1];
	 }
	}


class dialogclass extends JFrame
{private JTextArea text=new JTextArea("CONTEXT:",20,20);
 private JDialog fh=new JDialog (this,"帮助内容");
 private String s1="Program is designed to test Statement of a given Grammar.";
 private String s2="It can also hint the point where mistake happens!";
private char ch='\012';
 private String sb;
 public dialogclass()
	{text.setBackground(Color.gray);
	 text.setForeground(Color.yellow);
	 text.setEditable(false);
	 fh.add(text);
	 s1=s1.concat(Character.toString(ch));
	 s1=s1.concat(s2);
	 sb=text.getText().concat(Character.toString(ch));
	 sb=sb.concat(s1);
	 text.setText(sb);
	 fh.setSize(350,100);
	 fh.setVisible(true);
		fh.addWindowListener 
(new WindowAdapter () 
{ 
public void windowClosing (WindowEvent e) 
{ 
fh.setVisible(false);
} 
}); 
}
}


class claimclass extends JFrame
{private JTextArea text=new JTextArea("CONTEXT:",20,20);
 private JDialog fh=new JDialog (this,"声明内容----By Liuyi");
 private String s1="Program designed by Liuyi Computer Science 0403!";
 private String s2="You can't attribute to the program without the permission!";
private char ch='\012';
 private String sb;
 public claimclass()
	{text.setBackground(Color.gray);
	 text.setForeground(Color.yellow);
	 text.setEditable(false);
	 fh.add(text);
	 s1=s1.concat(Character.toString(ch));
	 s1=s1.concat(s2);
	 sb=text.getText().concat(Character.toString(ch));
	 sb=sb.concat(s1);
	 text.setText(sb);
	 fh.setSize(350,100);
	 fh.setVisible(true);
		fh.addWindowListener 
(new WindowAdapter () 
{ 
public void windowClosing (WindowEvent e) 

{ 
fh.setVisible(false);
} 

}); 
}
}

class jframeinit   implements ActionListener
{public JFrame fr=new JFrame("句子识别");
 public JTextArea t=new JTextArea("句型编辑区",10,10);
 public JPanel p=new JPanel();
 public JPanel p1=new JPanel();
 public JPanel p2=new JPanel();
 public JLabel l1=new JLabel("YES",new ImageIcon("a.gif"),JLabel.CENTER);
 public JLabel l2=new JLabel("NO",new ImageIcon("b.gif"),JLabel.CENTER);
 public JTextField t1=new JTextField(10);
 public JTextField t2=new JTextField(10);
 public JMenu m1=new JMenu("运行");
 public JMenu m2=new JMenu("关于");
 public JMenuItem i1=new JMenuItem("分析");
 public JMenuItem i2=new JMenuItem("退出");
 public JMenuItem i3=new JMenuItem("帮助");
 public JMenuItem i4=new JMenuItem("声明");
 public JMenuItem i5=new JMenuItem("查错");
 public JMenuBar mb=new JMenuBar();
 public int errpoint=0;
 public int flag=0;
 public int errkind=0;
 public jframeinit()
 {
  mb.setBorder(BorderFactory.createRaisedBevelBorder());
  mb.setBackground(Color.gray);
 
  fr.setJMenuBar(mb);	
  m1.setBorder(BorderFactory.createRaisedBevelBorder());
  m2.setBorder(BorderFactory.createRaisedBevelBorder());
  m1.setBackground(Color.red);
  m2.setBackground(Color.red);
  m1.setForeground(Color.yellow);
  m2.setForeground(Color.yellow);
  mb.add(m1);
  mb.add(m2);
  i1.addActionListener(this);
  m1.add(i1);
  m1.addSeparator(); 
  i5.addActionListener(this);
  m1.add(i5);
  m1.addSeparator();
  i2.addActionListener(this);
  m1.add(i2);
  
  i3.addActionListener(this);
  m2.add(i3);
  m2.addSeparator();
  i4.addActionListener(this);
  m2.add(i4);
  fr.setLayout(new GridLayout());
  t.setBorder(BorderFactory.createRaisedBevelBorder());
  t.setForeground(Color.white);
  t.setBackground(Color.gray);
  fr.add(t);
  fr.add(p);
  p1.setBackground(Color.orange);
  p2.setBackground(Color.yellow);
  p.setLayout(new GridLayout(2,1));
  p1.setBorder(BorderFactory.createRaisedBevelBorder());
  p2.setBorder(BorderFactory.createRaisedBevelBorder());
  p.add(p1);
  p.add(p2);
  l1.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
  l2.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
  t1.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
  t2.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
  p1.add(l1);
  p1.add(t1);
  p2.add(l2);
  p2.add(t2);
  fr.resize(new Dimension(400, 200));
  fr.setVisible(true);
  fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  
 }
	
public void actionPerformed(ActionEvent a)
{   int j,k=0;
    char aa,Q;
    
    int arrflag=1;
    String s;
               //标识出是否有错 
    stacks sp=new stacks();
	char[] strs;
    String haha="Here"; 
    String kong="  ";   //此处以上各个变量均被多次初始化,TOO BAD
	
	if(a.getActionCommand ().equals("分析"))
	{
	 s=t.getText();
	 strs=s.toCharArray();
     k=1;
	 sp.push(k,'$');//方法
	 
	 do{ aa=strs[arrflag];
	     arrflag++;
	 	 //System.out.println(aa);
	 	if(sp.isVt(k)==true) j=k;
	 	 else j=k-1;
	 	 //System.out.println(j);
	 	 if(sp.priority(j,aa)==0) {flag=1;errpoint=arrflag;errkind=0;
	 	                            break;}
	     

	 while(sp.priority(j,aa)==3)
	 {
	 	do{Q=sp.sstack[j];
	 	   if(sp.isVt(j-1)==true) j=j-1;
	 	   else j=j-2;
	 	   }while(sp.priority(j,Q)!=1);
	 	//下面是归约过程,包括出错处理
	 if((j+1)==k)
	   {//System.out.println(j);
	   	if(sp.sstack[j+1]!='i') {flag=1; errkind=1;
	                             errpoint=arrflag-1;break;}} 	
	 else
	    if((k-(j+1))!=2) {System.out.println(j);
	    	              flag=1;flag=1;errkind=2;
	                      errpoint=arrflag-1;break;}	
	 else 
	    if((sp.sstack[j+1]!=sp.sstack[k])&&(sp.sstack[j+1]!='(')&&(sp.sstack[k]!=')'))  
	                                      {flag=1;errkind=3;
	                                      errpoint=arrflag-1;break;}
	 //剩下是正确归约的地方   	
	 	k=j+1;
	 	sp.push(k,'N');
	 }	 
	     //System.out.println(aa);
	 	 if(flag==1) break;
	 	 //System.out.println("I am not running");
	 	if((sp.priority(j,aa)==1)||(sp.priority(j,aa)==2))
	 	  {k=k+1;
	 	   sp.push(k,aa);}
    }  while(aa!='$');	 
	    
	    if(flag==1) {t2.setText(haha);t1.setText(kong);}
	     else {t1.setText(haha);t2.setText(kong);}
	     }
	     //end of if
	
if(a.getActionCommand ().equals("查错"))	
	{String errmsg;
	 String errmsg1="类";
	 String errmsg2="  ";
	 //System.out.println(flag);
	 if(flag==1) {errmsg=Integer.toString(errkind);
	              errmsg=errmsg.concat(errmsg1);
	              errmsg=errmsg.concat(errmsg2);
	 	          errmsg=errmsg.concat(Integer.toString(errpoint)); 
	 	          t2.setText(errmsg);}
		else t2.setText("NO ERROR");
		flag=0;
		
	}
	
if(a.getActionCommand().equals("退出"))	
{fr.dispose();
}

if(a.getActionCommand().equals("帮助"))
{
new dialogclass();
}	
	
if(a.getActionCommand().equals("声明"))	
	{
	new claimclass();	
	}
	
}//end of actionPerformed	
	




	
}//end of class jframeinit

public class downtoup
{
	public static void main(String args[])
	{  
		new jframeinit();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -