Expressions.java
public class Expressions {
char data[]; //原始数组
String d[]; //转换后的数组
int n;
double result;
/**
* @param args
*/
public Expressions(char []a) //以表达式及其数组长度初始化
{
n=a.length;
data=new char[n+2];
d=new String[n*2+1];
for(int i=0;i<n*3/2+1;i++)
{
d[i]="";
}
for(int i=0;i<n;i++)
{
data[i]=a[i];
}
data[n]='#';
}
boolean isNeg(int i)
{
if(i==0 && data[i]=='-') return true;
else if(i!=0 && data[i]=='-' && data[i-1]=='(') return true;
else return false;
}
int jiecheng(int a)
{
if(a<0)return -1;
else if(a==0)return 1;
else {
int s=1;
for(int i=1;i<=a;i++)
s*=i;
return s;
}
}
double toDouble(String s)
{
int len=s.length();
double m=0;
double n=0;
int poi=s.indexOf('.');
if(poi==-1)
for(int i=0;i<len;i++)
{
int temp=s.charAt(i)-'0';
m=temp+10*m;
}
else
{
for(int i=0;i<poi;i++)
{
double temp=s.charAt(i)-'0';
m=temp+10*m;
}
for(int i=len-1;i>poi;i--)
{
double temp=s.charAt(i)-'0';
n=n*0.1+temp;
}
n=n*0.1;
}
double r=m+n;
return r;
}
String transform()//求出后缀表达式
{
int p=0;
for(int w=0;w<n;w++)
{if(data[w]=='(') p++;
if(data[w]==')') p--;
if(p<0) {return" 输入错误:括号不匹配";}
}
if(p!=0){return" 输入错误:括号不匹配";}
if(data[0]=='.')
{
return" 输入错误:一个小数的小数点前缺少数字";
}
charStack s=new charStack();
s.push('#');
int j=0;
for(int i=0;i<n+1;i++)
{
//System.out.println("使用元素为:"+data[i]);
if(i==0&&data[0]>='0'&&data[0]<='9') //第0位是数
{
d[j]+=data[0];
j++;
}
else if(data[i]>='0'&&data[i]<='9'&&((data[i-1]<'0'||data[i-1]>'9')&&data[i-1]!='.'))
{ //多位数字第一位或单位数除了第一位是数的可能
d[j]+=data[i];
//System.out.println(' ');//cout<<data[i];
j++;
}
else if(data[i]>='0'&&data[i]<='9'||i!=0&&data[i-1]=='.')//多位数
{
if(j!=0) d[j-1]=d[j-1]+data[i];
//cout<<data[i];
}
else if(data[i]=='.')//小数点
{
for(int u=0;u<d[j-1].length();u++) //检查是否有过多的小数点
if(d[j-1].charAt(u)=='.')
{
return" 输入错误:一个小数中出现多个小数点";
}
if(data[i+1]<'0'||data[i+1]>'9')
{
return" 输入错误:一个小数的小数点后缺少数字";
}
if(data[i-1]<'0'||data[i-1]>'9')
{
return" 输入错误:一个小数的小数点前缺少数字";
}
d[j-1]=d[j-1]+data[i];//cout<<data[i];
}
else if(isNeg(i))//负数
{
//cout<<isNeg(i)<<endl;
d[j]="0";j++;//cout<<" 0";
if(data[i+1]=='.')
{return" 出现错误:错误类型为出现 /"-.x/"结构";}
d[j]+=data[i+1];
j++;
//cout<<' '<<data[i+1];
i=i+2;
while(data[i]>='0'&&data[i]<='9'||data[i]=='.')
{
d[j-1]=d[j-1]+data[i];
i++;
}
i--;
d[j]+='-';j++;//cout<<" -";
}
else if(data[i]=='('||data[i]==')'||data[i]=='+'||data[i]=='-'||data[i]=='√'
||data[i]=='*'||data[i]=='/'||data[i]=='#'&&i==n||data[i]=='^'||data[i]=='!') //是运算符
{
if((data[i]=='+'||data[i]=='-'||data[i]=='*'||data[i]=='/'||data[i]=='^'||data[i]=='√')
&&(data[i+1]=='+'||data[i+1]=='-'||data[i+1]=='*'||data[i+1]=='/'||data[i+1]=='^'||data[i+1]=='!'))
{
return" 输入错误:两个运算符不能连续使用";
}
if(data[i]==')'&&data[i-1]=='(')
{
return" 括号不匹配";
}
int c=compare(data[i],s.Top()); //比较优先级
if(c==1) s.push(data[i]); //大于
else if(c==-1) //小于
{
while(compare(data[i],s.Top())==-1)
{
char m;
m=s.pop();
d[j]+=m;
//cout<<' ';
//cout<<m;
j++;
};
if(compare(data[i],s.Top())==1){s.push(data[i]);}
else if(s.Top()=='('&&data[i]==')'){s.pop();}
else if(s.Top()=='#'&&data[i]=='#'){s.pop();break;}
}
else if(s.Top()=='('&&data[i]==')')
{s.pop();}
else if(s.Top()=='#'&&data[i]=='#')break;
//else cout<<"表达式有误"<<endl;
}
else
{return" 输入错误:出现非法字符"+data[i];}
}
String houzhui="";
System.out.println("后缀表达式为:");
for(int i=0;d[i]!="";i++)
{
houzhui+=d[i];
System.out.println(i+" "+d[i]);
}
return "h后缀表达式为"+houzhui;
}
int compare(char a,char b) //比较优先级,返回1则为a大于b
{
//char b=c.charAt(0);
switch(a)
{
case '+':
case '-':
{
if(b=='+'||b=='-'||b=='*'||b=='/'||b==')'||b=='^'||b=='!'||b=='√') return -1;
if(b=='('||b=='#') return 1;
}
case '*':
case '/':
{
if(b=='+'||b=='-'||b=='#'||b=='(') return 1;
if(b==')'||b=='*'||b=='/'||b=='^'||b=='!'||b=='√') return -1;
}
case '(':
{
return 1;
}
case ')':
{
if(b=='+'||b=='-'||b=='*'||b=='/'||b=='#'||b=='^'||b=='!'||b=='√') return -1;
if(b=='(') return 0;
}
case '^':
{
if(b=='+'||b=='-'||b=='#'||b=='('||b=='*'||b=='/') return 1;
if(b==')'||b=='!'||b=='√') return -1;
}
case '!':
{
if(b=='+'||b=='-'||b=='#'||b=='('||b=='*'||b=='/'||b=='^'||b=='√') return 1;
if(b==')') return -1;
}
case '√':
{
if(b=='+'||b=='-'||b=='#'||b=='('||b=='*'||b=='/'||b=='^'||b=='√') return 1;
if(b==')') return -1;
}
case'#':
{if(b=='#')return 0;
else return-1;
}
default: System.out.println("出现非法运算符");
}
return 0;
}
String calculate()//根据后缀表达式计算
{
int c=0;
for(;d[c]!="";c++);
//cout<<c<<endl;
doubleStack q=new doubleStack();
double r;
for(int i=0;i<c;i++)
{
if(d[i].charAt(0)>='0'&&d[i].charAt(0)<='9')
{
//cout<<d[i][0]<<endl;
double qdouble=toDouble(d[i]);
//cout<<"入栈"<<qdouble<<endl;
q.push(qdouble);
}
else
{
double m,n;
m=q.pop();
if(d[i].charAt(0)!='√'&&q.isEmpty()) {return" 无法计算:操作数不匹配";}//解决"1+"问题
switch(d[i].charAt(0))
{
case'+':
{n=q.pop();
r=m+n;
q.push(r);
break;}
case'-':
{n=q.pop();
r=n-m;
q.push(r);
break;}
case'*':
{n=q.pop();
r=n*m;
q.push(r);
break;}
case'/':
{n=q.pop();
if(m==0)
{return" 无法计算:除数不能为零/n";}
r=n/m;
q.push(r);
break;}
case'^':
{ n=q.pop();
r=Math.pow(n, m);
q.push(r);
break;}
case'!':
{
if(m-(int)m!=0||m<0) return "无法计算:小数和负数不能阶乘";
else {
r=jiecheng((int)m);
q.push(r);}
break;}
case'√'://开平方
{
if(m<0) return "无法计算:负数不能开平方";
else {
r=Math.sqrt(m);
q.push(r);}
break;}
}
}
}
r=q.pop();
result=r;
//System.out.println("/n最后计算结果为:"+r);
String s=Double.toString(r);
return s;
}
String run()//其中所有错误的返回字符串都以空格开头,若以h开头则为可以写出后缀表达式但不能计算
{
String hou=transform();
if(hou.charAt(0)=='h')
{
String res=calculate();
if(res.charAt(0)==' ')
return ' '+hou.substring(1,hou.length())+"/n但是"+res;
else return res;
}
return hou;
//System.out.println("运算正常!!");}
//else System.out.println("此错误表达式无法生成后缀表达式!!!");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
char []a=new char[6];
a[0]='√';a[1]='4';a[2]='9';a[3]='-';a[4]='1';a[5]='0';
Expressions e=new Expressions(a);
System.out.println("最终结果为:"+e.run());
//System.out.println("成功!!!");
}
}
Jisuanqi.java
package program;
import java.awt.BorderLayout;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import javax.swing.JTextField;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileInputStream;
import javax.swing.JButton;
public class Jisuanqi extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel Content = null;
private JTextField expression = null;
private JPanel jPanel = null;
private JButton b1 = null;
private JButton b2 = null;
private JButton b3 = null;
private JButton b4 = null;
private JButton b5 = null;
private JButton b6 = null;
private JButton b7 = null;
private JButton b8 = null;
private JButton b9 = null;
private JButton b0 = null;
private JButton be1 = null;
private JButton be2 = null;
private JButton be3 = null;
private JButton be4 = null;
private JButton be5 = null;
private JButton be6 = null;
private JButton be7 = null;
private JButton be8 = null;
private JButton be9 = null;
private JButton be10 = null;
private JButton 计算 = null;
private JButton 清屏 = null;
/**
* This method initializes Content
*
* @return javax.swing.JPanel
*/
private JPanel getContent() {
if (Content == null) {
Content = new JPanel();
Content.setLayout(null);
Content.add(getExpression(), null);
Content.add(getJPanel(), null);
}
return Content;
}
/**
* This method initializes expression
*
* @return javax.swing.JTextField
*/
private JTextField getExpression() {
if (expression == null) {
expression = new JTextField();
expression.setBounds(new Rectangle(17, 9, 348, 57));
}
return expression;
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.setBounds(new Rectangle(12, 72, 355, 319));
jPanel.add(getB1(), null);
jPanel.add(getB2(), null);
jPanel.add(getB3(), null);
jPanel.add(getB4(), null);
jPanel.add(getB5(), null);
jPanel.add(getB6(), null);
jPanel.add(getB7(), null);
jPanel.add(getB8(), null);
jPanel.add(getB9(), null);
jPanel.add(getB0(), null);
jPanel.add(getBe1(), null);
jPanel.add(getBe2(), null);
jPanel.add(getBe3(), null);
jPanel.add(getBe4(), null);
jPanel.add(getBe5(), null);
jPanel.add(getBe6(), null);
jPanel.add(getBe7(), null);
jPanel.add(getBe8(), null);
jPanel.add(getBe9(), null);
jPanel.add(getBe10(), null);
jPanel.add(get计算(), null);
jPanel.add(get清屏(), null);
}
return jPanel;
}
/**
* This method initializes b1
*
* @return javax.swing.JButton
*/
private JButton getB1() {
if (b1 == null) {
b1 = new JButton();
b1.setText("1");
b1.setBounds(new Rectangle(0, 0, 85, 49));
b1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(expression.getText()+'1'); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return b1;
}
/**
* This method initializes b2
*
* @return javax.swing.JButton
*/
private JButton getB2() {
if (b2 == null) {
b2 = new JButton();
b2.setText("2");
b2.setBounds(new Rectangle(89, 0, 85, 49));
b2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(expression.getText()+'2'); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return b2;
}
/**
* This method initializes b3
*
* @return javax.swing.JButton
*/
private JButton getB3() {
if (b3 == null) {
b3 = new JButton();
b3.setText("3");
b3.setBounds(new Rectangle(178, 0, 85, 49));
b3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(expression.getText()+'3'); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return b3;
}
/**
* This method initializes b4
*
* @return javax.swing.JButton
*/
private JButton getB4() {
if (b4 == null) {
b4 = new JButton();
b4.setText("4");
b4.setBounds(new Rectangle(267, 0, 85, 49));
b4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(expression.getText()+'4'); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return b4;
}
/**
* This method initializes b5
*
* @return javax.swing.JButton
*/
private JButton getB5() {
if (b5 == null) {
b5 = new JButton();
b5.setText("5");
b5.setBounds(new Rectangle(0, 53, 85, 49));
b5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(expression.getText()+'5'); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return b5;
}
/**
* This method initializes b6
*
* @return javax.swing.JButton
*/
private JButton getB6() {
if (b6 == null) {
b6 = new JButton();
b6.setText("6");
b6.setBounds(new Rectangle(89, 53, 85, 49));
b6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(expression.getText()+'6'); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return b6;
}
/**
* This method initializes b7
*
* @return javax.swing.JButton
*/
private JButton getB7() {
if (b7 == null) {
b7 = new JButton();
b7.setText("7");
b7.setBounds(new Rectangle(178, 53, 85, 49));
b7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(expression.getText()+'7'); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return b7;
}
/**
* This method initializes b8
*
* @return javax.swing.JButton
*/
private JButton getB8() {
if (b8 == null) {
b8 = new JButton();
b8.setText("8");
b8.setBounds(new Rectangle(267, 53, 85, 49));
b8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(expression.getText()+'8'); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return b8;
}
/**
* This method initializes b9
*
* @return javax.swing.JButton
*/
private JButton getB9() {
if (b9 == null) {
b9 = new JButton();
b9.setText("9");
b9.setBounds(new Rectangle(0, 106, 85, 49));
b9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(expression.getText()+'9'); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return b9;
}
/**
* This method initializes b0
*
* @return javax.swing.JButton
*/
private JButton getB0() {
if (b0 == null) {
b0 = new JButton();
b0.setText("0");
b0.setBounds(new Rectangle(89, 106, 85, 49));
b0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(expression.getText()+'0'); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return b0;
}
/**
* This method initializes be1
*
* @return javax.swing.JButton
*/
private JButton getBe1() {
if (be1 == null) {
be1 = new JButton();
be1.setText("(");
be1.setBounds(new Rectangle(178, 106, 85, 49));
be1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(expression.getText()+'('); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return be1;
}
/**
* This method initializes be2
*
* @return javax.swing.JButton
*/
private JButton getBe2() {
if (be2 == null) {
be2 = new JButton();
be2.setText(")");
be2.setBounds(new Rectangle(267, 106, 85, 49));
be2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(expression.getText()+')'); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return be2;
}
/**
* This method initializes be3
*
* @return javax.swing.JButton
*/
private JButton getBe3() {
if (be3 == null) {
be3 = new JButton();
be3.setText("+");
be3.setBounds(new Rectangle(0, 159, 85, 49));
be3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(expression.getText()+'+'); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return be3;
}
/**
* This method initializes be4
*
* @return javax.swing.JButton
*/
private JButton getBe4() {
if (be4 == null) {
be4 = new JButton();
be4.setText("-");
be4.setBounds(new Rectangle(89, 159, 85, 49));
be4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(expression.getText()+'-'); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return be4;
}
/**
* This method initializes be5
*
* @return javax.swing.JButton
*/
private JButton getBe5() {
if (be5 == null) {
be5 = new JButton();
be5.setText("*");
be5.setBounds(new Rectangle(178, 159, 85, 49));
be5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(expression.getText()+'*'); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return be5;
}
/**
* This method initializes be6
*
* @return javax.swing.JButton
*/
private JButton getBe6() {
if (be6 == null) {
be6 = new JButton();
be6.setText("/");
be6.setBounds(new Rectangle(267, 159, 85, 49));
be6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(expression.getText()+'/'); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return be6;
}
/**
* This method initializes be7
*
* @return javax.swing.JButton
*/
private JButton getBe7() {
if (be7 == null) {
be7 = new JButton();
be7.setText(".");
be7.setBounds(new Rectangle(0, 212, 85, 49));
be7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(expression.getText()+'.'); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return be7;
}
/**
* This method initializes be8
*
* @return javax.swing.JButton
*/
private JButton getBe8() {
if (be8 == null) {
be8 = new JButton();
be8.setText("幂运算^");
be8.setBounds(new Rectangle(89, 212, 85, 49));
be8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(expression.getText()+"^"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return be8;
}
/**
* This method initializes be9
*
* @return javax.swing.JButton
*/
private JButton getBe9() {
if (be9 == null) {
be9 = new JButton();
be9.setText("阶乘 !");
be9.setBounds(new Rectangle(178, 212, 85, 49));
be9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(expression.getText()+'!'); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return be9;
}
/**
* This method initializes be10
*
* @return javax.swing.JButton
*/
private JButton getBe10() {
if (be10 == null) {
be10 = new JButton();
be10.setText("开平方");
be10.setBounds(new Rectangle(267, 212, 85, 49));
be10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(expression.getText()+"√"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return be10;
}
/**
* This method initializes 计算
*
* @return javax.swing.JButton
*/
private JButton get计算() {
if (计算 == null) {
计算 = new JButton();
计算.setText("计算");
计算.setBounds(new Rectangle(49, 267, 102, 49));
计算.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String ex=expression.getText();
if(ex.equals(""))
JOptionPane.showMessageDialog(null,"表达式不能为空","Stop!!!!",2);
else
{
char[] c=new char[ex.length()];
for(int i=0;i<ex.length();i++)
c[i]=ex.charAt(i);
Expressions s=new Expressions(c);
String result=s.run();
if(result.charAt(0)==' ')
{
JOptionPane.showMessageDialog(null,result,"Stop!!!!",2);
expression.setText("");
}
else expression.setText(expression.getText()+" = "+result);
}}});
}
return 计算;
}
/**
* This method initializes 清屏
*
* @return javax.swing.JButton
*/
private JButton get清屏() {
if (清屏 == null) {
清屏 = new JButton();
清屏.setText("清屏");
清屏.setBounds(new Rectangle(215, 267, 98, 49));
清屏.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
expression.setText(""); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return 清屏;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null,"打开计算器","Warning!!",2);
new Jisuanqi();
}
/**
* This is the default constructor
*/
public Jisuanqi() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(387, 442);
this.setContentPane(getContent());
this.setTitle("精品计算器");
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
ConfirmWindow confirm=new ConfirmWindow();
confirm.setVisible(true);
}
});
this.setVisible(true);
}
class ConfirmWindow //退出确认框
extends JFrame implements ActionListener
{
public ConfirmWindow()
{setLocation(500,300);
setSize(200,100);
setTitle("提示");
JLabel l=new JLabel("确认退出?");
JPanel p=new JPanel();
JButton yes =new JButton("是");
yes.addActionListener(this);
JButton no =new JButton("否");
no.addActionListener(this);
p.add(yes);
p.add(no);
add(l,BorderLayout.CENTER);
add(p,BorderLayout.SOUTH);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{dispose();}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("是"))
{
try{
Thread.sleep(700);}
catch(Exception m){}
System.exit(0);
}
else this.dispose();
}
}
} // @jve:decl-index=0:visual-constraint="175,48"