高分求 用JAVA编写计算器界面(只要界面)代码!
发布网友
发布时间:2022-06-03 05:49
我来回答
共1个回答
热心网友
时间:2023-10-12 18:23
package importantpg;
import java.awt.*;
import java.awt.event.*;
public class jisuanqi
{
TextArea tf=new TextArea(3,50 );
Button lb=new Button("clear");
Panel p1=new Panel();
Panel p2=new Panel();
public jisuanqi()
{ Frame f=new Frame("计算器");
Panel p=new Panel();
f.setBackground(Color.white);
f.setSize(500, 1500);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {System.exit(1);}
});
Button[] b=new Button[18]; b[10]=new Button("-"); b[15]=new Button("(");
b[11]=new Button("+"); b[12]=new Button("*"); b[16]=new Button(")");
b[13]=new Button("/"); b[14]=new Button("="); b[17]=new Button("退格");
for(int i=0;i<=9;++i)
{
b[i]=new Button(""+i);
}
p.setLayout(new GridLayout(6,3));
avent aa=new avent();
lb.addActionListener(aa);
for(int i=0;i<18;++i)
{
p.add(b[i]);b[i].addActionListener(aa);
}
p1.setLayout(new BorderLayout());
p1.add(lb,"North");p1.add(tf,"South");f.add(p1, "South");
f.add(p,"Center"); f.pack();
} //构造方法
public static void main(String args[])
{
new jisuanqi();
}
class avent implements ActionListener //事件体
{
public void actionPerformed(ActionEvent e)
{
String s=null; String in="";
s=e.getActionCommand();
if (s!="="&&s!=null&&s!="clear"&&s!="退格")
{
tf.append(s);
in+=s; s=null;
}
else if(s=="clear")
{
tf.replaceRange("",0,1000);
}else if(s=="=")
{
tf.append("="+jisuan(tf.getText()));
}
else if(s=="退格") {}
}///////////////////////////////////////////////事件处理方法
}
///////////////////////////////////////计算过程//////////////
static String jisuan(String s)
{
String hs= "";
char[] ch = new char[s.length()];
int top = -1;
for (int i = 0; i < s.length(); i++)
{
char rsc = s.charAt(i);
switch(rsc)
{
case ' ' : break;
case '+' : //操作符
case '-' :
while (top >= 0) //栈不为空时
{
char pc = ch[top--]; //pop Operator
if (pc == '(')
{
ch[++top] = pc; //push Operator
break;
}
else {hs = hs + pc;}
}
ch[++top] = rsc; //push Operator
hs += " ";break;
case '*' : //操作符
case '/' :
while (top >= 0) //栈不为空时
{
char c = ch[top--]; //pop Operator
if (c == '(')
{
ch[++top] = c; //push Operator
break;
}
else
{
if (c == '+' || c == '-')
{
ch[++top] = c; //push Operator
break;
}
else
{
hs = hs + c;
}
}
}
ch[++top] = rsc; //push Operator
hs += " ";
break;
case '(' : //操作符
ch[++top] = rsc;
hs+= " ";
break;
case ')' : //操作符
while (top >= 0) //栈不为空时
{
char c = ch[top--]; //pop Operator
if (c == '(')
{
break;
}
else
{
hs= hs + c;
}
}
hs += " ";
break;
default : //操作数
hs= hs + rsc;
break;
}//switch
}/////////////////////////////for
while (top >= 0)
{
hs= hs + ch[top--]; //pop Operator
}
//System.out.println("hs="+hs); //后缀
//////////////////////////////////////////////后缀////////////////////////////////////
//后缀表达式计算
double[] result = new double[hs.length()];
double x, y;
top = - 1;
String st = "";
for (int i = 0; i < hs.length(); i++)
{
char c = hs.charAt(i);
if ((c >= '0' && c <= '9') || c == '.')
{
st += c;
}
if ((c == ' ' && st != "") || i == hs.length() - 1)
{
result[++top] = Double.parseDouble(st) ; //push Operands
st = "";
}
if (c == '+' || c == '-' || c == '*' || c == '/')
{
if ((st != ""))
{
result[++top] =Double.parseDouble(st) ; //push Operands
st = "";
}
y = result[top--];
x = result[top--];
switch (c)
{
case '+' :
result[++top] = x+y;break;//push 中间结果再次入栈
case '-' :
result[++top] = x-y;break;
case '*' :
result[++top] = x*y;break;
case '/' :
result[++top] = x/y;break;
default :break;
}
}////////////////////if
} ///////////////////////for
return String.valueOf(result[top]); //pop 最终结果
} /////////////////////////jisuan
}