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

📄 homework_4.java

📁 。录入合法的LL(1)文法
💻 JAVA
字号:
import java.awt.*;
import javax.swing.*;
import javax.swing.JTextField;
import java.awt.event.*;

public class homework_4 extends JApplet implements ActionListener
{
	double k;
	int n;
	double a1;//存放第一个运算数
	double a2;//存放第二个运算数
	String s="";//用于存放Button事件接收字符串;
	String s1="";//用于存放TEXT中文本
	String s2="";
	char c;//用于存放运算符。
	
	//建军按扭
	JButton bt1=new JButton("    Backspace    ");
	JButton bt2=new JButton("    CE    ");
	JButton bt3=new JButton("    C      ");
	JButton bt4=new JButton("MC");
	JButton bt5=new JButton("MR");
	JButton bt6=new JButton("MS");
	JButton bt7=new JButton("M+");
	JButton bt8=new JButton("sqrt");
	JButton bt9=new JButton("  %  ");
	JButton bt10=new JButton("0");
	JButton bt11=new JButton("1");
	JButton bt12=new JButton(" 2");
	JButton bt13=new JButton("3");
	JButton bt14=new JButton("4");
	JButton bt15=new JButton(" 5");
	JButton bt16=new JButton("6");
	JButton bt17=new JButton("7");
	JButton bt18=new JButton(" 8");
	JButton bt19=new JButton("9");
	JButton bt20=new JButton("+");
	JButton bt21=new JButton("-");
	JButton bt22=new JButton("*");
	JButton bt23=new JButton("/");
	JButton bt24=new JButton(".");
	JButton bt25=new JButton(" 1/X ");
	JButton bt26=new JButton("   =  ");
	JButton bt27=new JButton("+/-");
	JTextField tf=new JTextField(); 
      //JFrame f=new JFrame();
     Container cp=getContentPane();
	public double vicincy(String s)
	{
	  s1=tf.getText();
	  k=Double.parseDouble(s1);
	  return k;
	}
	
	public void init()
	{
		//设置为BoxLayout布局
		Box bBox=Box.createVerticalBox();//设置为水平排列
		//加入组件
		cp.add(bBox);
		Box vBox1=Box.createHorizontalBox();
		vBox1.add(tf);
		bBox.add(vBox1);
		Box vBox2=Box.createHorizontalBox();
		vBox2.add(bt1);
		bt1.addActionListener(this);
		vBox2.add(bt2);
		bt2.addActionListener(this);
		vBox2.add(bt3);
		bt3.addActionListener(this);
		bBox.add(vBox2);
		Box vBox3=Box.createHorizontalBox();
		vBox3.add(bt4);
		bt4.addActionListener(this);
		vBox3.add(bt17);
		bt17.addActionListener(this);
		vBox3.add(bt18);
		bt18.addActionListener(this);
		vBox3.add(bt19);
		bt19.addActionListener(this);
		vBox3.add(bt23);
		bt23.addActionListener(this);
		vBox3.add(bt8);
		bt8.addActionListener(this);
		bBox.add(vBox3);
		Box vBox4=Box.createHorizontalBox();
		vBox4.add(bt5);
		bt5.addActionListener(this);
		vBox4.add(bt14);
		bt14.addActionListener(this);
		vBox4.add(bt15);
		bt15.addActionListener(this);
		vBox4.add(bt16);
		bt16.addActionListener(this);
		vBox4.add(bt22);
		bt22.addActionListener(this);
		vBox4.add(bt9);
		bt9.addActionListener(this);
		bBox.add(vBox4);
		Box vBox5=Box.createHorizontalBox();
		vBox5.add(bt6);
		bt6.addActionListener(this);
		vBox5.add(bt11);
		bt11.addActionListener(this);
		vBox5.add(bt12);
		bt12.addActionListener(this);
		vBox5.add(bt13);
		bt13.addActionListener(this);
		vBox5.add(bt21);
		bt21.addActionListener(this);
		vBox5.add(bt25);
		bt25.addActionListener(this);
		bBox.add(vBox5);
		Box vBox6=Box.createHorizontalBox();
		vBox6.add(bt7);
		bt7.addActionListener(this);
		vBox6.add(bt10);
		bt10.addActionListener(this);
		vBox6.add(bt27);
		bt27.addActionListener(this);
		vBox6.add(bt24);
		bt24.addActionListener(this);
		vBox6.add(bt20);
		bt20.addActionListener(this);
		vBox6.add(bt26);
		bt26.addActionListener(this);
		bBox.add(vBox6);

	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==bt11)
		{
		s=s+"1";
		tf.setText(s);
		}
		else if(e.getSource()==bt12)
		{
		s=s+"2";	
		tf.setText(s);
		}
		else if(e.getSource()==bt13)
		{
		s=s+"3";
		tf.setText(s);
		}
		else if(e.getSource()==bt14)
		{
		s=s+"4";
		tf.setText(s);	
		}
		else if(e.getSource()==bt15)
		{
		s=s+"5";
		tf.setText(s);
		}
		else if(e.getSource()==bt16)
		{
		s=s+"6";
		tf.setText(s);
		}
		else if(e.getSource()==bt17)
		{
		s=s+"7";
		tf.setText(s);
		}
		else if(e.getSource()==bt18)
		{
		s=s+"8";
		tf.setText(s);
		}
		else if(e.getSource()==bt19)
		{
		s=s+"9";
		tf.setText(s);
		}
		else if(e.getSource()==bt10)
		{
			s1=tf.getText();
			if(s1!="")
			{
			s=s+"0";
			tf.setText(s);
			}
		}
		else if(e.getSource()==bt1)//Backspace;
		{
		s1=tf.getText();
		if(s1!="")
		 {n=s1.length();
		 s2=s1.substring(0,n-1);
		 tf.setText(s2);
		 }
		}
		else if(e.getSource()==bt2)//CE
		{
		s="";
		tf.setText(s);
		
		}
		else if(e.getSource()==bt3)//C
		{
		s="";
		tf.setText(s);		
		}
		else if(e.getSource()==bt8)
		{
		 s1=tf.getText();
		 k=vicincy(s1);
		 if(k<0)
		  s1="Input False";
		 else
		  {k=Math.sqrt(k);
		   s1=String.valueOf(k);
		   tf.setText(s1);		   
		  }		
		}
		else if(e.getSource()==bt9)
		{
		  s1=tf.getText();
		  k=vicincy(s1)/100;
		  s1=String.valueOf(k);
		  tf.setText(s1);		  	
		}
		else if(e.getSource()==bt20)
		{s1=tf.getText();
		 a1=vicincy(s1);
		 c='+';
		 s="";
		 tf.setText("");		 
		}
		else if(e.getSource()==bt21)
		{s1=tf.getText();
		 a1=vicincy(s1);
		 c='-';
		 s="";
		 tf.setText("");		 
		}
		else if(e.getSource()==bt22)
		{s1=tf.getText();
		 a1=vicincy(s1);
		 c='*';
		 s="";
		 tf.setText("");		 
		}
		else if(e.getSource()==bt23)
		{s1=tf.getText();
		 a1=vicincy(s1);
		 c='/';
		 s="";
		 tf.setText("");		 
		}
		else if(e.getSource()==bt24)
		{
		 s=s+".";
		tf.setText(s);
		}
		else if(e.getSource()==bt25)
		{
		 s1=tf.getText();
		 k=vicincy(s1);
		 k=1/k;
		 s1=String.valueOf(k);
		 tf.setText(s1);
		}
		else if(e.getSource()==bt26)
		{		
		 s1=tf.getText();
		 a2=vicincy(s1);
		 if(c=='+') k=a1+a2;
		 else if(c=='-') k=a1-a2;
		 else if(c=='*') k=a1*a2;
		 else if(c=='/') k=a1/a2;
		 s1=String.valueOf(k);
		 tf.setText(s1);
		}
	}
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

⌨️ 快捷键说明

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