用Java编写一个简单的计算器程序

如题所述

第1个回答  推荐于2017-09-22
import java.awt.*;
import java.awt.event.*;

public class CalcAppDemo extends Frame{
private TextField t_result;
private Panel p_main; //主面板
private Panel p_num; //数字面板
private Panel p_oper; //操作符面板
private Panel p_show; //显示面板
private Button b_num[]; //数字按钮
private Button b_oper[]; //操作符面板

public CalcAppDemo(String title){
setTitle(title);
t_result = new TextField("0.0", 21);
p_main = new Panel();
p_num = new Panel();
p_oper = new Panel();
p_show = new Panel();
p_main.setLayout(new BorderLayout());
p_num.setLayout(new GridLayout(4, 3, 1, 1));
p_oper.setLayout(new GridLayout(4, 2, 1, 1));

b_num = new Button[12];
for(int i=0; i<9; i++)
{
b_num[i] = new Button(new Integer(i+1).toString());
}
b_num[9] = new Button("0");
b_num[10] = new Button("cls");
b_num[11] = new Button(".");
for(int i=0; i<12; i++)
{
p_num.add(b_num[i]);
}

b_oper = new Button[8];
b_oper[0] = new Button("+");
b_oper[1] = new Button("-");
b_oper[2] = new Button("*");
b_oper[3] = new Button("/");
b_oper[4] = new Button("pow");
b_oper[5] = new Button("sqrt");
b_oper[6] = new Button("+/-");
b_oper[7] = new Button("=");
for(int i=0; i<8; i++) //
{
p_oper.add(b_oper[i]);
}

t_result.setEditable(false);
p_show.add(t_result, BorderLayout.NORTH);
p_main.add(p_show, BorderLayout.NORTH);
p_main.add(p_num, BorderLayout.WEST);
p_main.add(p_oper, BorderLayout.EAST);
this.add(p_main, BorderLayout.CENTER);
setSize(400, 400);
setResizable(false);
pack();

this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
ButtonListener b1 = new ButtonListener();
for(int i=0; i<12; i++)
{
b_num[i].addActionListener(b1);
}
for(int i=0; i<8; i++)
{
b_oper[i].addActionListener(b1);
}
}

class ButtonListener implements ActionListener
{
private String lastOp; //存储上一此操作符
private String strVal; //存储数字对应的字符串
private double total; //总数
private double number; //存储新输入的数
private boolean firsttime; //判断是否第一次按下的是操作符按钮
private boolean operatorPressed;//判断是否已经按过操作符按钮

ButtonListener()
{
firsttime = true;
strVal = "";
}

//事件处理器
public void actionPerformed(ActionEvent e)
{
String s = ((Button)e.getSource()).getLabel().trim();

if(Character.isDigit(s.charAt(0)))
{//判断是操作数还是操作符
handleNumber(s);
}
else
{
calculate(s);
}
}

//判断是一元操作符还是二元操作符,并根据操作符类型做计算
void calculate(String op)
{
operatorPressed = true;

if(firsttime&&! isUnary(op))
{
total = getNumberOnDisplay();
firsttime = false;
}
if(isUnary(op))
{
handleUnaryOp(op);
}
else if(lastOp != null)
{
handleBinaryOp(lastOp);
}
if(! isUnary(op))
{
lastOp = op;
}
}

//判断是否一元操作符
boolean isUnary(String s)
{
return s.equals("=")
||s.equals("cls")||s.equals("sqrt")
||s.equals("+/-")||s.equals(".");
}

//处理一元操作符
void handleUnaryOp(String op)
{
if(op.equals("+/-"))
{//
number = negate(getNumberOnDisplay() + "");
t_result.setText("");
t_result.setText(number + "");
return;
}else if(op.equals("."))
{
handleDecPoint();
return;
}else if(op.equals("sqrt"))
{
number = Math.sqrt(getNumberOnDisplay());
t_result.setText("");
t_result.setText(number + "");
return;
}else if(op.equals("="))
{//
if(lastOp!= null && !isUnary(lastOp))
{
handleBinaryOp(lastOp);
}
lastOp = null;
firsttime = true;
return;
}else
{
clear();
}
}

//处理二元运算符
void handleBinaryOp(String op)
{
if(op.equals("+"))
{
total +=number;
}else if(op.equals("-"))
{
total -=number;
}else if(op.equals("*"))
{
total *=number;
}else if(op.equals("/"))
{
try
{
total /=number;
}catch(ArithmeticException ae){}
}else if(op.equals("pow"))
total = Math.pow(total, number);
//t_result.setText("");
lastOp = null;
// strVal = "";
number = 0;
t_result.setText(total + "");
}

//该方法用于处理数字按钮
void handleNumber(String s)
{
if(!operatorPressed)
{
strVal += s;
}else
{
operatorPressed = false;
strVal = s;
}
//
number = new Double(strVal).doubleValue();
t_result.setText("");
t_result.setText(strVal);
}

//该方法用于按下"."按钮
void handleDecPoint()
{
operatorPressed = false;
//
if(strVal.indexOf(".")<0)
{
strVal += ".";
}
t_result.setText("");
t_result.setText(strVal);
}

//该方法用于将一个数求反
double negate(String s)
{
operatorPressed = false;
//如果是一个整数,去掉小数点后面的0
if(number == (int)number)
{
s = s.substring(0,s.indexOf("."));
}

//如果无"-"增加在该数的前面
if(s.indexOf("-")<0)
{
strVal = "-" + s;
}
else
{
strVal = s.substring(1);
}
return new Double(strVal).doubleValue();
}

//将显示框中的值转换成Double
double getNumberOnDisplay()
{
return new Double(t_result.getText()).doubleValue();
}

//清除屏幕并设置所有的标识
void clear()
{
firsttime = true;
lastOp = null;
strVal = "";
total = 0;
number = 0;
t_result.setText("0");
}
}

public static void main(String[] args) {
CalcAppDemo c = new CalcAppDemo("简单的计算器程序");
c.setVisible(true);
}

}本回答被提问者采纳
第2个回答  2011-11-25
那就编啊!

用JAVA编写的科学计算器源代码
代码首先导入了`Scanner`类,用于从用户处获取输入。然后,在`main`方法中,创建了一个`Scanner`对象,用于读取用户的输入。程序通过一个无限循环来持续接收用户的输入,直到用户输入“quit”为止。在循环中,程序首先提示用户输入一个操作,然后读取用户的输入并将其分割为多个部分。接着,程序...

java怎样写一个简单的计算器
import java.util.Scanner;public class Calculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);System.out.println("Enter first number:");double num1 = scanner.nextDouble();System.out.println("Enter second number:");double num2 = scanner.nex...

求JAVA编写的 简易计算器程序,附上注释
import java.awt.*;import java.awt.event.*;public class Calculator implements ActionListener { String s="",s1;double d1,d2;JFrame jf = new JFrame("小计算器by Graduate") ;JTextField tf = new JTextField();public void init()\/\/实现计算器界面 { Container c=jf.getContentPane();...

java编写程序:创建一个 计算器 的 界面,包含 0-9 个 数字 , A-F个字 ...
import java.awt.*;import javax.swing.*;public class MyCalculator extends JFrame{public MyCalculator() {String ss1 = "0123456789ABCDEF";String[] ss2 = {"+","-","*","\/","√ ̄","aⁿ","CE","="};\/\/有些字符不在GBK中,所以文本使用了UTF-8编码。如果要用GBK编码,...

用java编得计算器程序软件和源代码
java计算器源程序java计算器import java.awt.*; import javax.swing.*; import java.awt.event.*; class calculation extends JFrame {public calculation() \/*构造方法*\/ {super("计数器"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); initTextPanel(); \/*文本框*\/ initControlPanel(); ...

1 简单计算器。请编写一个程序计算表达式:datal op data2的值。其中op...
1,简单计算器代码如下:import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Jsq extends JFrame implements ActionListener{ JTextField txt = new JTextField(15);JPanel p = new JPanel();JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;JButton bAdd,bDif,bMul,b...

用java编写计算器 能够实现简单的加、减、乘、除、乘方、开方运算等...
addComponent(layout, operation, 0, 0, 4, 1);addComponent(layout, numbers[1], 1, 0, 1, 1);addComponent(layout, numbers[2], 1, 1, 1, 1);addComponent(layout, numbers[3], 1, 2, 1, 1);addComponent(layout, numbers[4], 2, 0, 1, 1);addComponent(layout, numbers[5...

求简单JAVA程序代码,能操作的,简单的计算器有计算功能+-*\/就行越...
public static void main(String[] args) { String st = "";计算器 j = new 计算器();boolean t = true;while (t) { System.out.print("请输入计算代码:");BufferedReader br = new BufferedReader(new InputStreamReader(System.in));try { st = br.readLine();} catch (IOException ...

JAVA编写一个简单的计算器,要求用户在界面上输入两个数,能够自动计算出...
输入数字后,敲回车开始自动计算 import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextField;public class SimpleCalculator { private JFrame f ...

JAVA 编写计算器 要代码最简单的
学java的时候自己编的,很简单,能够连续输入计算式后进行计算import java.awt.BorderLayout;import java.awt.Color;import java.awt.FlowLayout;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.text.NumberFormat;import java.util...

相似回答