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

📄 change.java

📁 逆波兰试的翻译程序
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;

import java.util.*;
public class Change extends JPanel{
JTextField mekey;
JTextField youkey;
JButton button;



Change()
	{
	
	mekey = new JTextField(20);
	button = new JButton("确定");
	
	button.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent evt){
			String text=mekey.getText();
			laoding(text);
		}
	}
);
	youkey = new JTextField(20);
	JPanel panel = new JPanel();
	panel.setLayout(new FlowLayout(FlowLayout.RIGHT,30,20));
	panel.add(mekey);
	panel.add(button);
	this.setLayout(new BorderLayout());
	
	this.add(panel,BorderLayout.NORTH);
	this.add(youkey,BorderLayout.SOUTH);
	
	}



/*void laoding(String s)
	{ 
	int i=0;
	int j=0;
	char ch;
	char ch2[]=new char[100];
	char []ch1=new char[100];
	Stack R=new Stack();
	
	R.push("$");
	R.push("$");
	
	ch1=s.toCharArray();
	ch=ch1[i];
	System.out.println(s);
	System.out.println(s.indexOf(1));
	System.out.println(ch1[0]);
	while(ch!='\0'&&i<s.length()-4)
		{
		if(ch==' ') ch=ch1[++i];
		else if (ch=='(')
					{
					R.push(String.valueOf(ch));
					ch=ch1[++i];
					}
		else if (ch==')')
					{while((String)R.peek()!="(")
						ch2[j]=((String)R.pop()).charAt(0);
						R.pop();
						ch=ch1[++i];
					}
		else if (ch=='+'||ch=='-'||ch=='*'||ch=='/')
				{
					char w=((String)R.peek()).charAt(0);
					while (Precedence(w)>=Precedence(ch))
							{ch2[j++]=w;
							R.pop();
								w=((String)R.peek()).charAt(0);
							}
						R.push(String.valueOf(ch));
						ch=ch1[++i];

					}
		else 
			{ch2[j++]=ch;
		ch=ch1[++i];}
		}
		ch=((String)R.pop()).charAt(0);
		while(ch!='$'){
			if(ch=='(') 
			{
				System.out.println("fjdsal");
			}
			else {
				ch2[j++]=ch;
				ch=((String)R.pop()).charAt(0);
			}
		}
			ch2[j++]='\0';
			for(i=0;i<j;i++)
		
				System.out.println(ch2[i]);



}
*/





	

void laoding(String s)
	{ 
	int i=0;
	int j=0;
	char ch;
	char w;
	char chstr[]=new char[100];
	Stack R=new Stack();
	R.push(new Character('$'));
	R.push(new Character('$'));
	R.push(new Character('$'));
	ch=s.charAt(i);
	System.out.println(ch+""+s.length());
	while((i<s.length()-1)&&ch!='\0')
		{
			if(ch==' ') 
					{//System.out.println(ch+"fjdskj");
						i++;
						ch=s.charAt(i);
					}
			else if(ch=='(')
					{	//System.out.println(ch+"fdkj");
						R.push(new Character(ch));
						i++;
						ch=s.charAt(i);
					}
			else if (ch==')')
					{	//System.out.println(ch+"fjds");
						while(R.peek()!=new Character('('))
						chstr[j++]= ((Character)R.pop()).charValue();
						R.pop();
						i++;
						ch=s.charAt(i);
					}
			else if (ch=='+'||ch=='-'||ch=='*'||ch=='/')
					{
					//System.out.println(ch+"898-");
					//System.out.println(R.peek());
					//System.out.println(((Character)R.peek()).charValue());
					w=((Character)R.peek()).charValue();
					//System.out.println(((Character)R.peek()).charValue());
					while (Precedence(w)>=Precedence(ch))
							{chstr[j++]=w;
							R.pop();
							w=((Character)R.peek()).charValue();
							}
						R.push(new Character(ch));
						i++;
						ch=s.charAt(i);

					}
			else 
					{
					if(Character.isLetter(ch))
						{
						chstr[j++]=ch;
						i++;
						ch=s.charAt(i);
						System.out.println(ch);
						
						}
					else
						{System.out.println("error");
						break;}
					}
		}
		//System.out.println(ch);
		ch=((Character)R.pop()).charValue();
		while(ch!='$'){
					if(ch=='(') 
						{	
							System.out.println("error");
						}
					else 
						{
						chstr[j++]=ch;
						ch=((Character)R.pop()).charValue();
						}
		}
			
			for(i=0;i<j;i++)
		
				System.out.print(chstr[i]);
				System.out.println(chstr);
				youkey.setText(String.valueOf(chstr).trim());
				
	
	
			

}
	

int Precedence(char op)
		{
	
	switch(op){
		case '+':
			case'-':
			return 1;
		case '*':
			case '/':
			return 2;
		case '(':
			case'$':
			default:
			return 0;
			}
		}


	public static void main(String arg[]){
		JFrame frame = new JFrame("词法分析器");
		frame.addWindowListener(new closewindow());
		frame.getContentPane().add(new Change());
		frame.pack();
		frame.show();
	}
	}



class closewindow extends WindowAdapter{
	public void windowClosing(WindowEvent e){
		System.exit(0);
	}
}




		

	
	

⌨️ 快捷键说明

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