java计算器求倒数代码 倒数的和计算公式
帮忙用java补齐制作计算器加减乘除等于小数点这几个运算符的代码
import java.awt.*;
创新互联建站-专业网站定制、快速模板网站建设、高性价比武进网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式武进网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖武进地区。费用合理售后完善,十年实体公司更值得信赖。
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.LinkedList; //工具包
import java.text.NumberFormat; //文本包
/**
* java swing计算器
* @author young
*
*/
public class Calculator extends Frame implements ActionListener // 计算器类
{
JTextField result;
NumberButton numberButton[];
OperatorButton operatorButton[];
Button radixpoint, positiveminus, backspace, reciprocal, equal, clear; // 声明成员变量
// 小数点按钮,正负号按钮,退格按钮,求倒数按钮,等号按钮,清零按钮
Panel panel;
String operator[] = { "+", "-", "*", "/" };
LinkedList linklist;
boolean pressequal = false;
public Calculator() // 构造方法
{
super("计算器");
linklist = new LinkedList();
numberButton = new NumberButton[10];
for (int i = 0; i = 9; i++) {
numberButton[i] = new NumberButton(i);
numberButton[i].addActionListener(this);
}
operatorButton = new OperatorButton[4];
for (int i = 0; i 4; i++) {
operatorButton[i] = new OperatorButton(operator[i]);
operatorButton[i].addActionListener(this);
}
radixpoint = new Button(".");
positiveminus = new Button("+/-");
backspace = new Button("CE");
reciprocal = new Button("1/x");
equal = new Button("=");
clear = new Button("C");
radixpoint.setForeground(Color.red);
positiveminus.setForeground(Color.red);
backspace.setForeground(Color.red);
reciprocal.setForeground(Color.red);
equal.setForeground(Color.red);
clear.setForeground(Color.red);
radixpoint.addActionListener(this);
positiveminus.addActionListener(this);
backspace.addActionListener(this);
reciprocal.addActionListener(this);
equal.addActionListener(this);
clear.addActionListener(this);
result = new JTextField(10);
result.setHorizontalAlignment(JTextField.RIGHT);
result.setForeground(Color.black);
result.setBackground(Color.white);
result.setFont(new Font("TimesRoman", Font.PLAIN, 14));
result.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
result.setEditable(false);
panel = new Panel();
panel.setLayout(new GridLayout(4, 5));
panel.add(numberButton[1]);
panel.add(numberButton[2]);
panel.add(numberButton[3]);
panel.add(backspace);
panel.add(clear);
panel.add(numberButton[4]);
panel.add(numberButton[5]);
panel.add(numberButton[6]);
panel.add(operatorButton[0]);
panel.add(operatorButton[2]);
panel.add(numberButton[7]);
panel.add(numberButton[8]);
panel.add(numberButton[9]);
panel.add(operatorButton[1]);
panel.add(operatorButton[3]);
panel.add(numberButton[0]);
panel.add(positiveminus);
panel.add(reciprocal);
panel.add(radixpoint);
panel.add(equal);
add(result, "North");
add(panel, "Center");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setSize(270, 200);
setLocation(300, 230);
setVisible(true);
}
public void actionPerformed(ActionEvent e) // 按钮的单击事件处理方法
{
if (e.getSource() instanceof NumberButton) // 数字按钮
{
NumberButton b = (NumberButton) e.getSource();
if (linklist.size() == 0) {
int number = b.getNumber();
linklist.add("" + number);
result.setText("" + number);
pressequal = false;
}
else if (linklist.size() == 1 pressequal == false) {
int number = b.getNumber();
String num = (String) linklist.getFirst();
String s = num.concat("" + number);
linklist.set(0, s);
result.setText(s);
} else if (linklist.size() == 1 pressequal == true) {
int number = b.getNumber();
linklist.removeFirst();
linklist.add("" + number);
pressequal = false;
result.setText("" + number);
} else if (linklist.size() == 2) {
int number = b.getNumber();
linklist.add("" + number);
result.setText("" + number);
} else if (linklist.size() == 3) {
int number = b.getNumber();
String num = (String) linklist.getLast();
String s = num.concat("" + number);
linklist.set(2, s);
result.setText(s);
}
} else if (e.getSource() instanceof OperatorButton) // 操作按钮
{
OperatorButton b = (OperatorButton) e.getSource();
if (linklist.size() == 1) {
String fuhao = b.getOperator();
linklist.add(fuhao);
} else if (linklist.size() == 2) {
String fuhao = b.getOperator();
linklist.set(1, fuhao);
} else if (linklist.size() == 3) {
String fuhao = b.getOperator();
String number1 = (String) linklist.getFirst();
String number2 = (String) linklist.getLast();
String operator = (String) linklist.get(1);
try {
double n1 = Double.parseDouble(number1);
double n2 = Double.parseDouble(number2);
double n = 0;
if (operator.equals("+")) {
n = n1 + n2;
} else if (operator.equals("-")) {
n = n1 - n2;
} else if (operator.equals("*")) {
n = n1 * n2;
} else if (operator.equals("/")) {
n = n1 / n2;
}
linklist.clear();
linklist.add("" + n);
linklist.add(fuhao);
result.setText("" + n);
} catch (Exception ee) {
}
}
} else if (e.getSource() == equal) // 等号按钮
{
pressequal = true;
if (linklist.size() == 1 || linklist.size() == 2) {
String num = (String) linklist.getFirst();
result.setText("" + num);
} else if (linklist.size() == 3) {
String number1 = (String) linklist.getFirst();
String number2 = (String) linklist.getLast();
String operator = (String) linklist.get(1);
try {
double n1 = Double.parseDouble(number1);
double n2 = Double.parseDouble(number2);
double n = 0;
if (operator.equals("+")) {
n = n1 + n2;
} else if (operator.equals("-")) {
n = n1 - n2;
} else if (operator.equals("*")) {
n = n1 * n2;
} else if (operator.equals("/")) {
n = n1 / n2;
}
result.setText("" + n);
linklist.set(0, "" + n);
linklist.removeLast();
linklist.removeLast();
} catch (Exception ee) {
}
}
} else if (e.getSource() == radixpoint) // 小数点按钮
{
if (linklist.size() == 0) {
pressequal = false;
} else if (linklist.size() == 1) {
String dot = radixpoint.getLabel();
String num = (String) linklist.getFirst();
String s = null;
if (num.indexOf(dot) == -1) {
s = num.concat(dot);
linklist.set(0, s);
} else {
s = num;
}
linklist.set(0, s);
result.setText(s);
}
else if (linklist.size() == 3) {
String dot = radixpoint.getLabel();
String num = (String) linklist.getLast();
String s = null;
if (num.indexOf(dot) == -1) {
s = num.concat(dot);
linklist.set(2, s);
} else {
s = num;
}
result.setText(s);
}
} else if (e.getSource() == backspace) // 退格按钮
{
if (linklist.size() == 1) {
String num = (String) linklist.getFirst();
if (num.length() = 1) {
num = num.substring(0, num.length() - 1);
linklist.set(0, num);
result.setText(num);
} else {
linklist.removeLast();
result.setText("0");
}
} else if (linklist.size() == 3) {
String num = (String) linklist.getLast();
if (num.length() = 1) {
num = num.substring(0, num.length() - 1);
linklist.set(2, num);
result.setText(num);
} else {
linklist.removeLast();
result.setText("0");
}
}
} else if (e.getSource() == positiveminus) // 正负号按钮
{
if (linklist.size() == 1) {
String number1 = (String) linklist.getFirst();
try {
double d = Double.parseDouble(number1);
d = -1 * d;
String str = String.valueOf(d);
linklist.set(0, str);
result.setText(str);
} catch (Exception ee) {
}
} else if (linklist.size() == 3) {
String number2 = (String) linklist.getLast();
try {
double d = Double.parseDouble(number2);
d = -1 * d;
String str = String.valueOf(d);
linklist.set(2, str);
result.setText(str);
} catch (Exception ee) {
}
}
} else if (e.getSource() == reciprocal) // 求倒数按钮
{
if (linklist.size() == 1 || linklist.size() == 2) {
String number1 = (String) linklist.getFirst();
try {
double d = Double.parseDouble(number1);
d = 1.0 / d;
String str = String.valueOf(d);
linklist.set(0, str);
result.setText(str);
} catch (Exception ee) {
}
} else if (linklist.size() == 3) {
String number2 = (String) linklist.getLast();
try {
double d = Double.parseDouble(number2);
d = 1.0 / d;
String str = String.valueOf(d);
linklist.set(0, str);
result.setText(str);
} catch (Exception ee) {
}
}
} else if (e.getSource() == clear) // 清零按钮
{
pressequal = false;
result.setText("0");
linklist.clear();
}
}
public static void main(String args[]) {
new Calculator();
}
}
class NumberButton extends Button // 数字按钮类
{
int number;
public NumberButton(int number) // 构造方法
{
super("" + number);
this.number = number;
setForeground(Color.blue);
}
public int getNumber() {
return number;
}
}
class OperatorButton extends Button // 运算符号按钮类
{
String operator;
public OperatorButton(String operator) // 构造方法
{
super(operator);
this.operator = operator;
setForeground(Color.red);
}
public String getOperator() {
return operator;
}
}
java编写计算器实现退格,倒数,小数点,正负号功能代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class General extends JFrame implements ActionListener
{
String args[]={"退格","总清","清零","/","1","2","3","+","4","5","6","-","7","8","9","*","0","+/-",".","="};
JButton jb[]=new JButton[20];
JPanel p1=new JPanel();
JTextField jtf=new JTextField(15);
double num1,num2,jieguo;
char c,ch;
public General()
{
Container ct=getContentPane();
jtf.addKeyListener(new KA());
jtf.setHorizontalAlignment(JTextField.RIGHT);
ct.add(jtf,"North");
p1.setLayout(new GridLayout(5,4));
for(int i=0;i20;i++)
{
jb[i]=new JButton(args[i]);
jb[i].addActionListener(this);
p1.add(jb[i]);
}
ct.add(p1);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
new General();
}
public void actionPerformed(ActionEvent e)// 监听按键
{
String str=e.getActionCommand().trim();
if("0123456789.".indexOf(str)!=-1)
{
if(jtf.getText().equals("0"))jtf.setText(null);
jtf.setText(jtf.getText()+str);
return;
}
if("+-*/".indexOf(str)!=-1)
{
num1=Double.parseDouble(jtf.getText());
c=str.charAt(0);
jtf.setText(null);
return;
}
else if(str.equals("="))
{
num2=Double.parseDouble(jtf.getText());
switch(c)
{
case '+': jieguo=num1+num2; break;
case '-': jieguo=num1-num2; break;
case '*': jieguo=num1*num2; break;
case '/': jieguo=num1/num2; break;
}
jtf.setText(Double.toString(jieguo));
}
else if(str.equals("+/-"))
{
if(jtf.getText().isEmpty()==false)
{
Double lnum=-(Double.parseDouble(jtf.getText()));
jtf.setText(Double.toString(lnum));
}
else
{
jtf.setText("-");
}
}
else if(str.equals("清空"))
{
jtf.setText(null);
}
else if(str.equals("清零"))
{
jtf.setText("0");
}
else if(str.equals("退格"))
{
jtf.setText(jtf.getText().substring(0,jtf.getText().length()-1));
}
}
class KA extends KeyAdapter // 监听键盘
{
public void keyPressed(KeyEvent e)
{
char ch=e.getKeyChar();
if("+-*/".indexOf(ch)!=-1)
{
num1=Double.parseDouble(jtf.getText());
c=ch;
}
if(ch=='='|ch==e.VK_ENTER)
{
num2=Double.parseDouble(jtf.getText());
switch(c)
{
case '+': jieguo=num1+num2; break;
case '-': jieguo=num1-num2; break;
case '*': jieguo=num1*num2; break;
case '/': jieguo=num1/num2; break;
}
}
}
public void keyReleased(KeyEvent e)
{
char ch=e.getKeyChar();
if("+-*/".indexOf(ch)!=-1)
{
jtf.setText(null);
}
if(ch=='='|e.getKeyCode()==e.VK_ENTER)
{
jtf.setText(Double.toString(jieguo));
}
}
}
}
求一个 JAVA 的 科学计算器 的 源代码最好里面有 sin cos 倒数、平方根、平方、立方高级一点的!
import javax.swing.*;
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();
tf.setHorizontalAlignment(JTextField.RIGHT);//文本框
c.add(tf,"North");
JPanel pn3 = new JPanel(new BorderLayout());
c.add(pn3,"Center");
JPanel pn2 = new JPanel();//功能键界面(清除键和关闭键)
pn2.setLayout(new BorderLayout());
JPanel pn1 = new JPanel();//运算界面
pn1.setLayout(new GridLayout(4,4));
pn3.add(pn2,"North");
pn3.add(pn1);
//设置按钮
JButton b = new JButton("CLEAR");
b.setToolTipText("请按清除键!");//设置清零键
b.setForeground(Color.RED);//设置字体颜色
b.setBackground(Color.YELLOW);//设置背景色
b.addActionListener(this);
pn2.add(b,"Center");
b = new JButton("OFF");
b.setToolTipText("请按退出键!");//设置off键,点击退出应用程序b.addActionListener(this);
b.setForeground(Color.RED);//字体颜色
b.setBackground(Color.ORANGE);//背景色
pn2.add(b,"East");
b = new JButton("1");//add butten 1
b.addActionListener(this);
pn1.add(b);
b = new JButton("2");//add butten 2
b.addActionListener(this);
pn1.add(b);
b = new JButton("3");//add butten 3
b.addActionListener(this);
pn1.add(b);
b = new JButton("+");//add butten +
b.setForeground(Color.BLUE);//设置字体颜色
b.addActionListener(this);
pn1.add(b);
b = new JButton("4");//add butten 4
b.addActionListener(this);
pn1.add(b);
b = new JButton("5");//add butten 5
b.addActionListener(this);
pn1.add(b);
b = new JButton("6");//add button 6
b.addActionListener(this);
pn1.add(b);
b = new JButton("-");//add button -
b.setForeground(Color.BLUE);//设置字体颜色
b.addActionListener(this);
pn1.add(b);
b = new JButton("7");//add button 7
b.addActionListener(this);
pn1.add(b);
b = new JButton("8");//add button 8
b.addActionListener(this);
pn1.add(b);
b = new JButton("9");//add button 9
b.addActionListener(this);
pn1.add(b);
b = new JButton("*");//add button *
b.setForeground(Color.BLUE);//设置字体颜色
b.addActionListener(this);
pn1.add(b);
b = new JButton("0");//add button 0
b.addActionListener(this);
pn1.add(b);
b = new JButton(".");//add button .
b.addActionListener(this);
pn1.add(b);
b = new JButton("=");//add button =
b.setForeground(Color.RED);//设置字体颜色
b.addActionListener(this);
pn1.add(b);
b = new JButton("\\");//add button \
b.setForeground(Color.BLUE);//设置字体颜色
b.addActionListener(this);
pn1.add(b);
jf.setSize(300,300);//设置大小
jf.setVisible(true);//设置为可视
}
//处理按钮按下时的动作,进行相应的处理
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
tf.setText(tf.getText()+command);
if(command.equals("CLEAR")) //清零键 按下时返回初始状态
{
s1=null;
s="";
tf.setText("");//记录输入值的变量清空
}
else if(command.equals("OFF")) System.exit(0);//off键 关闭应用程序
else if(!command.equals("*")!command.equals("\\")
!command.equals("+")!command.equals("-")
!command.equals("="))//判断输入是否为数字
{
if(s1==null)//判断输入是否为第一个
s1 = command;
else s1+=command;
d1 = new Double(s1).doubleValue();//字符串型转换为双精度型,还原输入数字
try
{
if(s.equals("+")) d1 = d1+d2;//加法运算
else if(s.equals("-")) d1 = d2-d1;//减法运算
else if(s.equals("*")) d1 = d1*d2;//乘法运算
else if(s.equals("\\"))d1 = d2/d1;//除法运算
}
catch(Exception ex)
{
tf.setText("Error");//错误显示"Error"
System.out.println(ex.getMessage());
}
}
else if(!command.equals("=")) //判断输入是否为+ - * \
{
s = command;
s1 = null;
d2 = d1;
}
else//输入=时,显示运算结果
{
tf.setText(tf.getText()+d1);
}
}
public static void main(String [] args)
{
new Calculator().init();
}
}
当前题目:java计算器求倒数代码 倒数的和计算公式
新闻来源:http://scyanting.com/article/hpeigd.html