📄 jsq.txt
字号:
/**
* <p>Title: Calculator</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: CUIT</p>
* <p>Calculator.java<p>
* Created on 2004年10月13日, 下午2:35
* @author jacktom
* @version 1.0
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener
{
Operator oper;
String a,result;
int type;
boolean flag1=false;
boolean flag2=false;
boolean judge=true;
int count=0;
JTextField text;
JPanel jpanel[];
JPanel jpanel1;
JButton jbutton[];
String name[]={"0",".","-/+","+","=","1","2","3","-",")","4","5","6","*","(","7","8","9","/","CE"};
//Construct the JFrame
public Calculator()
{
oper=new Operator();
setSize(250,300);
setVisible(true);
//Overridden so we can exit when window is closed
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
Container con=getContentPane();
con.setLayout(new GridLayout(5,5));
text=new JTextField(12);
text.setHorizontalAlignment(JTextField.RIGHT);
jpanel1=new JPanel();
jpanel1.setLayout(new GridLayout(1,1));
jpanel1.add(text);
jpanel=new JPanel[4];
for(int i=0;i<4;i++)
{
jpanel[i]=new JPanel();
jpanel[i].setLayout(new GridLayout(1,5));
}
jbutton=new JButton[name.length];
//add button to panel
for(int j=0;j<name.length;j++)
{
jbutton[j]=new JButton(name[j]);
jbutton[j].addActionListener(this);
jpanel[(int)(j/5)].add(jbutton[j]);
}
con.add(jpanel1);
for(int i=3;i>=0;i--)
{
con.add(jpanel[i]);
}
}
public void actionPerformed(ActionEvent e)
{
for(int i=0;i<10;i++)
{
if(e.getActionCommand().equals(String.valueOf(i)))
if(flag1==false)
{
text.setText(String.valueOf(i));
flag1=true;
}
else
{
text.setText(text.getText()+i);
}
}
if(e.getActionCommand().equals("."))
if(flag2==false&&count==0)
{
text.setText(text.getText()+".");
count++;
flag1=true;
}
if(e.getActionCommand().equals("+")||e.getActionCommand().equals("-")||e.getActionCommand().equals("*")||e.getActionCommand().equals("/"))
{
if(judge)
{
a=text.getText();
oper.EvaluateExpression(a);
}
else
judge=true;
flag1=false;
flag2=false;
count=0;
if(e.getActionCommand().equals("+"))
{
a="+";
oper.EvaluateExpression(a);
}
if(e.getActionCommand().equals("-"))
{
a="-";
oper.EvaluateExpression(a);
}
if(e.getActionCommand().equals("*"))
{
a="*";
oper.EvaluateExpression(a);
}
if(e.getActionCommand().equals("/"))
{
a="/";
oper.EvaluateExpression(a);
}
}
if(e.getActionCommand().equals("="))
{
if(judge)
{
a=text.getText();
oper.EvaluateExpression(a);
}
else
judge=true;
oper.EvaluateExpression("#");
text.setText("");
text.setText(String.valueOf(oper.CalculateResult()));
flag1=false;
flag2=false;
count=0;
}
if(e.getSource()==jbutton[2])
{
text.setText("-"+text.getText());
}
if(e.getActionCommand().equals(")"))
{
a=text.getText();
oper.EvaluateExpression(a);
oper.EvaluateExpression(")");
judge=false;
}
if(e.getActionCommand().equals("CE"))
{
text.setText("");
judge=true;
count=0;
flag1=false;
flag2=false;
oper=new Operator();
}
if(e.getActionCommand().equals("("))
{
oper.EvaluateExpression("(");
}
}
/**
* Main method
*
* @param args String[]
*/
public static void main(String args[])
{
Calculator Cmain=new Calculator();
Cmain.pack();
}
}
/**
* <p>Operator.java<p>
* Description:用栈实现计算
* Created on 2004年10月13日, 下午3:35
* @author jacktom
*/
public class Operator
{
StackY optr; //存放操作符
StackY opnd;//存放操作数
Puzhu p;
boolean Mark;
Operator()
{
p=new Puzhu();
optr=new StackY();
opnd=new StackY();
optr.push("#");
}
public void EvaluateExpression(String s)
{
boolean mark=true;
if(s=="+"||s=="-"||s=="*"||s=="/"||s=="("||s==")"||s=="#")
{
while(mark)
{
switch(p.Precede(optr.peek(),s))
{
case -1:
optr.push(s);
mark=false;
break;
case 0:
optr.pop();
mark=false;
break;
case 1:
String theta=optr.pop();
String a =opnd.pop();
String b =opnd.pop();
if(a.indexOf(".",0)==-1&&b.indexOf(".",0)==-1)
Mark=true;
else
Mark=false;
double c=Double.valueOf(a).doubleValue();
double d=Double.valueOf(b).doubleValue();
double e=p.Operate(c,theta,d);
String f=String.valueOf(e);
if(theta=="/")
Mark=false;
if(Mark)
opnd.push(f.substring(0,f.indexOf(".",0)));
else
opnd.push(f);
break;
}
}
}
else
opnd.push(s);
}
public String CalculateResult()
{
//double result=Double.valueOf(opnd.peek()).doubleValue();
return opnd.peek();
}
}
/**
* Description:判断操作符的优先级并计算结果
* Created on 2004年10月13日, 下午4:00
* @author jacktom
*/
class Puzhu
{
public Puzhu()
{}
public int Precede(String optr1,String optr2)
{
String[] A={"+","-","*","/","(",")","#"};
int[][] B={
{1,1,-1,-1,-1,1,1},
{1,1,-1,-1,-1,1,1},
{1,1,1,1,-1,1,1},
{1,1,1,1,-1,1,1},
{-1,-1,-1,-1,-1,0,2},
{1,1,1,1,2,1,1},
{-1,-1,-1,-1,-1,2,0},
};
int i=0,j=0,k;
while(i<7)
{
if(A[i]==optr1)
{
break;
}
i++;
}
while(j<7)
{
if(A[j]==optr2)
{
break;
}
j++;
}
k=B[i][j];
return k;
}
public double Operate(double a,String oper,double b)
{
double c=0;
if(oper=="+")
c=a+b;
if(oper=="-")
c=b-a;
if(oper=="*")
c=a*b;
if(oper=="/")
c=b/a;
return c;
}
}
/**
* <p>StackY.java<p>
* Description:堆栈的基本操作实现
* Created on 2004年10月13日, 下午3:05
* @author jacktom
*/
public class StackY
{
private int maxSize; // size of stack array
private String[] stackArray;
private int top; // top of stack
public StackY(int s) // constructor
{
maxSize = s; // set array size
stackArray = new String[maxSize]; // create array
top = -1; // no items yet
}
public StackY() // constructor
{
maxSize = 20; // set array size
stackArray = new String[maxSize]; // create array
top = -1; // no items yet
}
public void push(String j) // put item on top of stack
{
top++;
stackArray[top] = j; // increment top, insert item
}
public String pop() // take item from top of stack
{
return stackArray[top--]; // access item, decrement top
}
public String peek() // peek at top of stack
{
return stackArray[top];
}
public boolean isEmpty() // true if stack is empty
{
return (top == 0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -