package myPackage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.*;
public class Calculator extends JFrame
{
int j=0,i=0, k=0;double sum=0;double[] a=new double[100];char[] b=new char[100];
public String str1="请输入计算公式(以等号结束):",str2="";
JPanel p1=new JPanel();
JPanel p2=new JPanel(new BorderLayout());
public JTextField f=new JTextField(str1);
public JButton jb1=new JButton(""+1);
public JButton jb2=new JButton(""+2);
public JButton jb3=new JButton(""+3);
public JButton jb4=new JButton(""+4);
public JButton jb5=new JButton(""+5);
public JButton jb6=new JButton(""+6);
public JButton jb7=new JButton(""+7);
public JButton jb8=new JButton(""+8);
public JButton jb9=new JButton(""+9);
public JButton jb0=new JButton(""+0);
public JButton jbAdd=new JButton("+");
public JButton jbSub=new JButton("-");
public JButton jbMul=new JButton("*");
public JButton jbDiv=new JButton("/");
public JButton jbIs=new JButton("=");
public JButton jbRs=new JButton("Reset");
public Calculator()
{
ActionListener listener=new jbListener();
p1.setLayout(new GridLayout(4,4,10,10));//创建软键盘
p1.add(jb1); jb1.addActionListener(listener);
p1.add(jb2); jb2.addActionListener(listener);
p1.add(jb3); jb3.addActionListener(listener);
p1.add(jb4); jb4.addActionListener(listener);
p1.add(jb5); jb5.addActionListener(listener);
p1.add(jb6); jb6.addActionListener(listener);
p1.add(jb7); jb7.addActionListener(listener);
p1.add(jb8); jb8.addActionListener(listener);
p1.add(jb9); jb9.addActionListener(listener);
p1.add(jb0); jb0.addActionListener(listener);
p1.add(jbAdd);jbAdd.addActionListener(listener);
p1.add(jbSub);jbSub.addActionListener(listener);
p1.add(jbMul);jbMul.addActionListener(listener);
p1.add(jbDiv);jbDiv.addActionListener(listener);
p1.add(jbIs); jbIs.addActionListener(listener);
p1.add(jbRs); jbRs.addActionListener(listener);
p2.add(new JLabel("计算器面板"),BorderLayout.NORTH);
p2.add(p1,BorderLayout.CENTER);
add(p2,BorderLayout.EAST);
add(f,BorderLayout.CENTER);
}
public void doCalculator(){
for(int p=0;p<100;p++)
{
a[p]=0;
}
str2=str2.trim();
for(i=0;i<str2.length();i++)
{
if(str2.charAt(i)<='9'&&str2.charAt(i)>='0')
{
a[j]=a[j]*10+(str2.charAt(i)-'0')+0.0;
}
else
j=j+1;
}
for(i=0;i<str2.length();i++)
{
if(str2.charAt(i)>'9'||str2.charAt(i)<'0')
{
b[k]=str2.charAt(i);
k=k+1;
}
}
//for (i=0;i<=j;i++)
//System.out.println(a[i]);
//for (i=0;i<k;i++)
//System.out.println(b[i]);
//else
//{
for(i=0;i<k;i++)
{
if(b[i]=='-')
{
a[i+1]=0-a[i+1];
b[i]='+';
}
}
if(b[0]=='*')
{
a[1]=a[0]*a[1];
a[0]=0.0;
b[0]='+';
}
else if(b[0]=='/')
{
a[1]=a[0]/a[1];
a[0]=0.0;
b[0]='+';
}
for(i=0;i<k;i++)
{
if(b[i]=='*')
{
a[i+1]=a[i]*a[i+1];
a[i]=0.0;
b[i]='+';
}
if(b[i]=='/')
{
a[i+1]=a[i]/a[i+1];
a[i]=0.0;
b[i]='+';
}
}
for(i=0;i<99;i++)
{
sum+=a[i];
}
//System.out.print(sum);
str2=str2+'='+sum;
f.setText(str2);
}
class jbListener implements ActionListener{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb1)
{ str2+="1"; f.setText(str2); return;}
else if(e.getSource()==jb2)
{ str2+="2"; f.setText(str2); return;}
else if(e.getSource()==jb3)
{ str2+="3"; f.setText(str2); return;}
else if(e.getSource()==jb4)
{ str2+="4"; f.setText(str2);return;}
else if(e.getSource()==jb5)
{ str2+="5"; f.setText(str2); return;}
else if(e.getSource()==jb6)
{ str2+="6"; f.setText(str2); return;}
else if(e.getSource()==jb7)
{ str2+="7"; f.setText(str2); return;}
else if(e.getSource()==jb8)
{ str2+="8"; f.setText(str2); return;}
else if(e.getSource()==jb9)
{ str2+="9"; f.setText(str2); return;}
else if(e.getSource()==jb0)
{ str2+="0"; f.setText(str2); return;}
else if(e.getSource()==jbAdd)
{ str2+="+"; f.setText(str2);return;}
else if(e.getSource()==jbSub)
{ str2+="-"; f.setText(str2); return;}
else if(e.getSource()==jbMul)
{ str2+="*"; f.setText(str2); return;}
else if(e.getSource()==jbDiv)
{ str2+="/"; f.setText(str2); return;}
else if (e.getSource()==jbIs)
{ f.setText(str2); doCalculator();return;}
else
{str2=" ";sum=0;j=0;k=0;f.setText(str2);}
}
}
}