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

📄 newpro.java

📁 我做的不好呀
💻 JAVA
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class Newpro extends JFrame implements ActionListener
{
	public static void main(String[] args) {
		Newpro frame=new Newpro();
		
	}
	JButton bu1;
	JTextArea ta1;
	JTextArea ta2;
	Scrollbar sb1;
	String Keyword[]={"char","short","int","unsigned","long",
				"float","double","struct","union","void ",
				"enum","signed","const","volatile","typedef",
				"auto","register","static","extem","break",
				"extem ","break","case ","continue","default",
				"do","while","else","for","goto","if",
				"return","switch","while","sizeof"};
	String D[]={"E","e","T","t","F"};
 String O[]={"i","+","*","(",")","$"} ;
	 String DO[][]= {{"Te","Q","Q","Te","Q","Q"},
					 {"Q","+Te","Q","Q","&","&"},
					 {"Ft","Q","Q","Ft","Q","Q"},
					 {"Q","&","*Ft","Q","&","&"},
					 {"i","Q","Q","(E)","Q","Q"}};
	
	
	public Newpro(){
		super("新建的文件集");
		Container C = getContentPane();
		C.setLayout(new BorderLayout());
		bu1= new JButton("确定");
		bu1.setLocation(150,50);
		bu1.addActionListener(this);
		C.add("North",bu1);
		ta1=new JTextArea();
		ta1.setSize(300,250);
		ta1.setLocation(20,50);
		C.add("Center",ta1);
		JPanel pan1 = new JPanel();
		pan1.setLayout(new BorderLayout());
	
		C.add("East",pan1);
		
		ta2=new JTextArea();
		ta2.setSize(300,250);
		ta2.setLocation(20,50);
		sb1=new Scrollbar(Scrollbar.VERTICAL, 1, 1, 0, 25);
		
		ta1.add(sb1);
		sb1.setMinimum(0);
		pan1.add("Center",ta2);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(600,500);
	
		setVisible(true);

}


public void actionPerformed(ActionEvent e) {
	char buffer[]= new char[2048];
	FileReader rd1;
	String str1="";
	ta1.setText("");
	try{
		rd1 = new FileReader("432.txt");
		int len1 = rd1.read(buffer);
		rd1.close();
		str1=new String(buffer,0,len1);
		ta1.setText(str1);
		accidence();
	}catch(IOException e1)
	{return 
		;
	}
	}


int yugaochao(String s)				// 保留字
	{	    
		int key;
		int i;
		for(i=0;i<35;i++)
		{
			if(s.equals(Keyword[i]))
			{	key=5;
				return  key;
				}
          }
         key=8;
		return key;
	}




 void accidence()
	{
	 int i=0;
	 int j;
	 int key;
	 int num1;
	String str2 = new String();
	String str3 = new String();
	String str4 = new String();
	str2=ta1.getText();
	str4 = str2;
	ta2.setText("");
	StringTokenizer tk1=new StringTokenizer(str2,"' ','(',')','\n','\r','+','*'");
	num1=0;
	while (tk1.hasMoreTokens())
	{
		i++;
		str3 = tk1.nextToken();
		j=analyze(str3);
		str4=str4.replaceAll(str3,"i");
		key=j;
		if((key==2)||(key==4)||(key==7)||(key==8))
			;
		else num1=1;
		


	if(key==0)
		ta2.setText(ta2.getText()+"\n"+str3+"   是"+"这什么都不是");
	else if(key==1)
		ta2.setText(ta2.getText()+"\n"+str3+"	是"+"求余");
	else if (key==2)
		ta2.setText(ta2.getText()+"\n"+str3+"是"+"这是数字(整数)");
	else if(key==3)
		ta2.setText(ta2.getText()+"\n"+str3+"	是"+"这是分隔符");
	else if (key==4)
		ta2.setText(ta2.getText()+"\n"+str3+"	是"+"数字(浮点数)	");
	else if (key==5)
		ta2.setText(ta2.getText()+"\n"+str3+"	是"+"这是保留字");
	else if (key==6)
		ta2.setText(ta2.getText()+"\n"+str3+"	是"+"????????");
	else if (key==7)
		ta2.setText(ta2.getText()+"\n"+str3+"	是"+"数字呢!(科学记数发的数值)");
	else if (key==8)
		ta2.setText(ta2.getText()+"\n"+str3+"   是"+"标志符");
	else if (key==9)
		ta2.setText(ta2.getText()+"\n"+str3+"   是"+"特殊字符!");
	else if (key==10)
		ta2.setText(ta2.getText()+"\n"+str3+"   是"+"减号");
	else if (key==11)
		ta2.setText(ta2.getText()+"\n"+str3+"   是"+"加号");
	else if (key==12)
		ta2.setText(ta2.getText()+"\n"+str3+"   是"+"乘号");
	else if (key==13)
		ta2.setText(ta2.getText()+"\n"+str3+"   是"+"除号");
	else if (key==14)
		ta2.setText(ta2.getText()+"\n"+str3+"   是"+"附值符号");
	else if (key==15)
		ta2.setText(ta2.getText()+"\n"+str3+"   是"+"等号");
	else if(key==16)
		ta2.setText(ta2.getText()+"\n"+str3+"   是"+"小于");
	else if (key==17)
		{ta2.setText(ta2.getText()+"\n"+str3+"  是"+"小于等于");
			}
	else if (key==18)
		{ta2.setText(ta2.getText()+"\n"+str3+"  是"+"大于");
		}
	else if (key==19)
		{ta2.setText(ta2.getText()+"\n"+str3+"  是"+"大于等于");
		}
	else if (key==20)
		{ta2.setText(ta2.getText()+"\n"+str3+"  是"+"逻辑与");
		}
	else if (key==21)
		{ta2.setText(ta2.getText()+"\n"+str3+"  是"+"逻辑或");
		}
	else if (key==22)
		{ta2.setText(ta2.getText()+"\n"+str3+"  是"+"逻辑非");
			}
	else if (key==101)
		ta2.setText(ta2.getText()+"\n"+str3+"   是"+"非法的字符!");
	else 
		ta2.setText(ta2.getText()+"\n"+str3+"   是"+"这是错误的,请重新输入一下!");
		
	}
	if(num1==0)
	syntax(str4);
	else ta1.setText(ta1.getText()+"\n\n"+"读入的文件含有非法的字符");
	}

int analyze(String s)
	{	
	char ch;
	int STATE ;
	int key = 101;
	int i = 0;
	int Accept = 100;
	int Erro = 101;
	int j = s.length();
	STATE = 0;
	if(j==0)
			return 0;
	while(STATE!=Accept){
		if(((i==j)&&((STATE==2)||(STATE==4)||(STATE==5)||(STATE==7)||(STATE==8)||(STATE==9)||(STATE==10)))||STATE==Erro)
		{
			key=STATE;
			if(STATE==8)
				{key=yugaochao(s);
				}
			STATE=Accept;
			return key;
		}
	ch=s.charAt(i);
	i++;
	switch(STATE)
		{
			case 0:
				if(ch=='-'||ch=='+')
				{    STATE=1;
					if((i==j)&&(ch=='-'))
					return 10;
					if((i==j)&&(ch=='+'))
					return 11;
				}
				else if (Character.isDigit(ch))
					{STATE=2;
					}
			    else if (Character.isLetter(ch))
					STATE=8;
				else if (ch=='*'&&i==j)
					return 12;
				else if(((ch==' ')||(ch=='\t')||(ch=='\n'))&&(i!=j))
					STATE=0;
				else if (ch=='/'&&i==j)
					return 13;
				else if (ch=='='&&i==j)
					return 14;
				else if(ch=='='&&i!=j)
					STATE= 9;
				else if (ch=='%'&&i==j)
					{return 1;	
					}
				else if (ch=='<'&&i==j)
					{return 16;
					}
				else if (ch=='<'&&i!=j)
					{STATE= 10;	
					}
				else if (ch=='>'&&i==j)
					{return 18;
					}
				else if (ch=='>'&&i!=j)
					{STATE =11;
					}
				else if((ch=='&')&&(i!=j))
					STATE=12;
				else if ((ch=='|')&&(i!=j))
					{STATE=13;
						}
				else if (ch=='!'&&i==j)
					{return 22;
					}		
				else if (((ch==';')||(ch ==' ')||(ch==','))&&i==j)
					{return 3;
					}
				else 
					STATE=Erro;break;
			case 1:
				if(Character.isDigit(ch))
					STATE=2;
				else 
					STATE=Erro;break;
			case 2:
				if(Character.isDigit(ch))
					STATE=2;
				else if (ch=='E'||ch=='e')
					{STATE=5;
					}
				else if(ch=='.')
					{	STATE=3;
						if(i==j)
						STATE=Erro;
						}
				else 
					STATE=Erro;
					break;
			case 3:
				if(Character.isDigit(ch))
					STATE=4;
				else 
					STATE=Erro;
					break;
			case 4:
				if(Character.isDigit(ch))
					STATE=4;
				else if (ch=='E'||ch=='e')
					{	STATE=5;
					}
				else 
					STATE=Erro;
					break;
			case 5:
				if (ch=='+'||ch=='-')
					{STATE=6;
					}
				else if(Character.isDigit(ch))
					STATE=7;
				else 
					STATE=Erro;
				break;
			case 6:
				if(Character.isDigit(ch))
					STATE=7;
				else 
					STATE=Erro;
					break;
			case 7:
				if(Character.isDigit(ch))
					STATE=7;
				else 
					STATE=Erro;
					break;
			case 8:
				if(Character.isLetter(ch)||Character.isDigit(ch))
					STATE=8;
				else
					STATE=Erro;
					break;
			case 9:
				if(ch=='='&&i==j)
					return 15;
				else 
					STATE=Erro;
					break;
			case 10:
				if(ch=='='&&i==j)
						return 17;
				else STATE =Erro;
					break;
			case 11:
				if(ch=='='&&i==j)
					return 19;
				else STATE = Erro;
					break;
			case 12:
				if(ch=='&'&&i==j)
					return 20;
				else 
					STATE=Erro;
			case 13:
				if(ch=='|'&&i==j)
					return 21;
				else STATE=Erro;
			case 101:
					i=j;
				break;
			default:
					STATE=Erro;
				}
		}
				return key;
	}






void syntax(String yugaochao)
	{
						  
		
		int i=0;
		int j=yugaochao.length();
		String  X;
		int keydo,keyto,key;
		String a;
		Stack Xo=new Stack();
		Stack To=new Stack();
		Xo.push("$");
		Xo.push("$");
		Xo.push("E");
		To.push("$");
		To.push("$");
		keydo=0;
keyto=0;
		for(i=j-1;i>=0;i--)
		 {  String go=yugaochao.substring(i,i+1);
			
		
			To.push(go);
		 }
		ta1.setText(ta1.getText()+"\n"+"    "+Xo+"                   "+To+"       ");
		ta2.setText(ta2.getText()+"\n\n"+"化简的结果"+"\n"+yugaochao);
		do{
		X=(String)Xo.peek();

		a=(String)To.peek();

		if(X.equals("i")||X.equals("+")||X.equals("*")||X.equals("(")||X.equals(")")||X.equals("$"))
			{
			
			
			if(X.equals(a))
				{Xo.pop();
					To.pop();
					a=(String)To.peek();
					ta1.setText(ta1.getText()+"\n"+"    "+Xo+"                   "+To+"       ");
					//System.out.println(X);
					
				}
			else {
				System.out.println("error");
				break;
				}
			}
	else {
			for(i=0;i<5;i++)
				{
				if(X.equals(D[i]))
					{	keydo=i;
					}
		
				}
			for(i=0;i<6;i++)
				{if(a.equals(O[i]))
					{keyto=i;
		
					}
				}
			if(!DO[keydo][keyto].equals("Q"))
				{
				Xo.pop();
				key=DO[keydo][keyto].length();
				if(!X.equals("&"))
					{for(i=key;i>0;--i)
				{	Xo.push(DO[keydo][keyto].substring(i-1,i));
					}
					ta1.setText(ta1.getText()+"\n"+"    "+Xo+"              "+To+"               "+X+"------>"+DO[keydo][keyto]);
					}
					
							
						}
			else 
				{
				ta1.setText(ta1.getText()+"\n"+"匹配错误");
				System.out.println("匹配错误");
			break;
			}
			}
		}while(!X.equals("$"));		
	}


}

⌨️ 快捷键说明

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