| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 639 人关注过本帖
标题:[分享]计算器
只看楼主 加入收藏
魔鬼之子
Rank: 1
来 自:地狱之都
等 级:新手上路
帖 子:100
专家分:0
注 册:2007-9-22
结帖率:71.43%
收藏
 问题点数:0 回复次数:2 
[分享]计算器

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class computer extends Frame implements ActionListener{
Button[] button_number,button_calculater;
Button button_pointer;
Panel panel;
TextField field_number;
JLabel label_dialog;
Dialog dialog;
String[] str={"sqr","Del","CE","C","/","*","-","+","="};
String text_number="",text_string,text_calculater,string_calculater,string_number1,string_number2;
char text_char,judge_char;
int j=0;
boolean flag=true,sqr=true,pow=true;
Stack<String> operate=new Stack();
Stack<String> number=new Stack();
public computer(){
super("计算器");
this.setSize(300,200);
this.setLocation(200,150);
this.setLayout(new java.awt.BorderLayout());
this.setResizable(false);
this.addWindowListener(new WinClose());


dialog=new Dialog(this,"提示",true);
label_dialog=new JLabel("",JLabel.CENTER);
button_number=new Button[10];
button_calculater=new Button[10];
panel=new Panel(new java.awt.GridLayout(5,4,1,1));
field_number=new TextField("",10);

button_pointer=new Button("·");
button_pointer.addActionListener(this);
for(int i=0;i<10;i++){
button_number[i]=new Button(i+"");
button_number[i].addActionListener(this);
}
for(int i=0;i<9;i++){
button_calculater[i]=new Button(str[i]);
button_calculater[i].addActionListener(new calculater());
}

this.add("North",field_number);
this.add("Center",panel);
for(int i=0;i<4;i++)
panel.add(button_calculater[i]);
for(int i=0;i<3;i++)
panel.add(button_number[i+7]);
panel.add(button_calculater[4]);
for(int i=0;i<3;i++)
panel.add(button_number[i+4]);
panel.add(button_calculater[5]);
for(int i=0;i<3;i++)
panel.add(button_number[i+1]);
panel.add(button_calculater[6]);
panel.add(button_number[0]);
panel.add(button_pointer);
panel.add(button_calculater[7]);
panel.add(button_calculater[8]);
dialog.add(label_dialog);
dialog.setVisible(false);
dialog.setSize(300,100);
dialog.addWindowListener(new WinClose());

this.setVisible(true);
}
public static void main(String[] args){
new computer();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==button_pointer){
text_string=field_number.getText();
text_char=text_string.charAt(text_string.length()-1);
if(text_char=='+'||text_char=='-'||text_char=='*'||text_char=='/')
field_number.setText(field_number.getText());
else
field_number.setText(field_number.getText()+".");

}
else{
if(field_number.getText().equals("0"))
field_number.setText("");
field_number.setText(field_number.getText()+e.getActionCommand());
}
}
class WinClose extends WindowAdapter{
public void windowClosing(WindowEvent e){
if(e.getSource()==dialog){
dialog.setVisible(false);
field_number.setText("");
}
else
System.exit(0);
}
}
class calculater implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
if(e.getSource()==button_calculater[1]){
text_string=field_number.getText();
field_number.setText(text_string.substring(0,text_string.length()-1));
}
}
catch(StringIndexOutOfBoundsException b){}
if(e.getSource()==button_calculater[2])
field_number.setText("0");
for(int i=0;i<4;i++)
if(e.getSource()==button_calculater[i+4]){
if(field_number.getText().equals("0"))
field_number.setText("");
field_number.setText(field_number.getText()+e.getActionCommand());
}
if(e.getSource()==button_calculater[8]){
text_string=field_number.getText();
text_string+="#";
operate.push("#");
for(j=0;j<text_string.length();j++){
text_char=text_string.charAt(j);
if(text_char!='#'&&text_char!='+'&&text_char!='-'&&text_char!='*'&&text_char!='/')
text_number+=String.valueOf(text_char);
else{
text_calculater=String.valueOf(text_char);
if(flag) number.push(text_number);
text_number="";
if(operate.peek().equals("+")&&(text_calculater.equals("+")||text_calculater.equals("-")))
judge_char='>';
else if(operate.peek().equals("-")&&(text_calculater.equals("+")||text_calculater.equals("-")))
judge_char='>';
else if(operate.peek().equals("+")&&(text_calculater.equals("*")||text_calculater.equals("/")))
judge_char='<';
else if(operate.peek().equals("-")&&(text_calculater.equals("*")||text_calculater.equals("/")))
judge_char='<';
else if(operate.peek().equals("#"))
judge_char='<';
else
judge_char='>';
switch(judge_char){
case'<':operate.push(text_calculater);flag=true;break;
case'>':new operate();break;
}
}
}
field_number.setText(number.pop());
}
if(e.getSource()==button_calculater[0]){
text_string=field_number.getText();
for(int i=0;i<text_string.length();i++){
text_char=text_string.charAt(i);
if(text_char=='+'||text_char=='-'||text_char=='*'||text_char=='/'){
label_dialog.setText("所输内容不能为表达式或负数,请重新输入");
dialog.setLocation(300+120,200+100);
dialog.setVisible(true);
sqr=false;
break;
}
}
if(sqr) field_number.setText(""+(Math.sqrt(Double.parseDouble(text_string))));
}
if(e.getSource()==button_calculater[3]){
text_string=field_number.getText();
for(int i=0;i<text_string.length();i++){
text_char=text_string.charAt(i);
if(text_char=='+'||text_char=='-'||text_char=='*'||text_char=='/'){
label_dialog.setText("所输内容不能为表达式,请重新输入");
dialog.setLocation(300+120,200+100);
dialog.setVisible(true);
pow=false;
break;
}
}
if(pow) field_number.setText(""+(Math.pow(Double.parseDouble(text_string),2)));
}
}
class operate{
public operate(){
string_calculater=operate.pop();
string_number2=number.pop();
string_number1=number.pop();
if(string_calculater.equals("+"))
number.push(""+(Double.parseDouble(string_number1)+Double.parseDouble(string_number2)));
else if(string_calculater.equals("-"))
number.push(""+(Double.parseDouble(string_number1)-Double.parseDouble(string_number2)));
else if(string_calculater.equals("*"))
number.push(""+(Double.parseDouble(string_number1)*Double.parseDouble(string_number2)));
else
number.push(""+(Double.parseDouble(string_number1)/Double.parseDouble(string_number2)));
j--;
flag=false;
}
}
}
}

可进行表达式的运算
缺点:可连续输入小数点,不能输入负数。

搜索更多相关主题的帖子: 计算器 import string java 
2007-11-08 11:53
wshyj18
Rank: 1
等 级:新手上路
帖 子:124
专家分:0
注 册:2007-5-14
收藏
得分:0 
    一个计算器就要这么多代码,看来我要加强写代码了

2007-11-08 21:12
作弊
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2007-11-3
收藏
得分:0 

boolean pointOn = false
如果已经输入过了 pointOn = true

再次输入的时候用if(!pointOn) 检查

负数同理输入345 -> num 之后输入 - 那 negative = true
num = 0-345

2007-11-09 09:18
快速回复:[分享]计算器
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.045058 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved