M+,M-,MR,MC,是计算器的存储键
目前成都创新互联已为数千家的企业提供了网站建设、域名、虚拟主机、网站托管、服务器租用、企业网站设计、镇坪网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
M+键:当屏幕上已经出现计算结果或某个数值后,按下M+键,计算器就把屏幕上的数字存到了存储器中,此时计算中断,可以重新开始按新的数字进行新的运算,如果再次按下M+键,就会把屏幕当前的数和刚才存在存储器中的数相加,再把计算结果存在存储器中。
比如:按下这些键:45×3= M+ 12×3= M+
当按下“45×3=”后,屏幕会出现135,在这时按下“M+”键,就把135存在存储器中了,可以继续按其他数据开始新的运算,当按下“12×3= ”后,屏幕出现的是36,这时再按下“M+”,计算器就在内部运算,把36和135相加,把最后的结果171存在存储器中。
M-键:M-与M+相反,就是把存储器中的数据减去当前屏幕上的数,再把结果存储。
MR键:当存储器中存的有数据时,按下MR键,就可以把存储器中的数据显示在屏幕上
MC键:清除存储器中存的数据,把其中的数归零。
--------------------------------------
这是说明书,可能对你有帮助
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
class JPanelEa1 extends JFrame implements MenuListener,ActionListener
{
JPanel mainp,p1,p2,p3,p4;
JTextField jt1;
JMenuItem mnuCopy;
JMenuItem mnuPaste;
JButton bM;
boolean isDouble = false;// 是否为实数
int opFlag = -1;
static double t1 = 0, t2 = 0, t3 = 0, result = 0;
static int opflag1 = -1, opflag2 = -1, flag = 0, resflag = 1;
int preOp, currentOp = 0;// 标准位
double op1 = 0, op2 = 0;// 操作数
double n3;
StringBuffer buf = new StringBuffer(20);
StringBuffer copyBoard = new StringBuffer(20);// 剪贴板
StringBuffer memory = new StringBuffer(20);// M系列
StringBuffer str = new StringBuffer();
public JPanelEa1()
{
p1=new JPanel();
p1.setLayout(new GridLayout(2,1,10,10));
JMenuBar mnuNotepad=new JMenuBar();
JMenu mnuEdit=new JMenu("编辑(E)");
mnuEdit.setMnemonic(KeyEvent.VK_E);
JMenu mnuCheck=new JMenu("查看(V)");
mnuCheck.setMnemonic(KeyEvent.VK_V);
JMenu mnuHelp=new JMenu("帮助(H)");
mnuCopy=new JMenuItem("复制(C)");
mnuPaste=new JMenuItem("粘贴(P)");
JMenuItem mnuVisit=new JMenuItem("查看帮助(V)");
JMenuItem mnuAbout=new JMenuItem("关于计算器(A)");
JSeparator sep=new JSeparator();
jt1=new JTextField("0.");
jt1.setEnabled(false);
jt1.setHorizontalAlignment(JTextField.RIGHT);
mnuEdit.addMenuListener(this);
mnuCheck.addMenuListener(this);
mnuHelp.addMenuListener(this);
mnuCopy.addActionListener(this);
mnuPaste.addActionListener(this);
mnuVisit.addActionListener(this);
mnuAbout.addActionListener(this);
mnuNotepad.add(mnuEdit);
mnuNotepad.add(mnuCheck);
mnuNotepad.add(mnuHelp);
mnuEdit.add(mnuCopy);
mnuEdit.add(mnuPaste);
mnuEdit.add(sep);
mnuHelp.add(mnuVisit);
mnuHelp.add(sep);
mnuHelp.add(mnuAbout);
p1.add(mnuNotepad);
p1.add(jt1);
p2=new JPanel();
p2.setLayout(new FlowLayout(FlowLayout.CENTER));
bM = new JButton(" ");
bM.setEnabled(false);
p2.add(bM);
Button b25=new Button("Backspace");
b25.addActionListener(this);
Button b26=new Button("CE");
b26.addActionListener(this);
Button b27=new Button("C");
b27.addActionListener(this);
p2.add(b25);
p2.add(b26);
p2.add(b27);
p3=new JPanel();
p3.setLayout(new GridLayout(4,6));
Button button1=new Button("MC");
button1.addActionListener(this);
Button button2=new Button("7");
button2.addActionListener(this);
Button button3=new Button("8");
button3.addActionListener(this);
Button button4=new Button("9");
button4.addActionListener(this);
Button button5=new Button("/");
button5.addActionListener(this);
Button button6=new Button("sqrt");
button6.addActionListener(this);
Button button7=new Button("MR");
button7.addActionListener(this);
Button button8=new Button("4");
button8.addActionListener(this);
Button button9=new Button("5");
button9.addActionListener(this);
Button button10=new Button("6");
button10.addActionListener(this);
Button button11=new Button("*");
button11.addActionListener(this);
Button button12=new Button("%");
button12.addActionListener(this);
Button button13=new Button("MS");
button13.addActionListener(this);
Button button14=new Button("1");
button14.addActionListener(this);
Button button15=new Button("2");
button15.addActionListener(this);
Button button16=new Button("3");
button16.addActionListener(this);
Button button17=new Button("-");
button17.addActionListener(this);
Button button18=new Button("1/x");
button18.addActionListener(this);
Button button19=new Button("M+");
button19.addActionListener(this);
Button button20=new Button("0");
button20.addActionListener(this);
Button button21=new Button("+/-");
button21.addActionListener(this);
Button button22=new Button(".");
button22.addActionListener(this);
Button button23=new Button("+");
button23.addActionListener(this);
Button button24=new Button("=");
button24.addActionListener(this);
p3.add(button1);p3.add(button2);
p3.add(button3);p3.add(button4);
p3.add(button5);p3.add(button6);
p3.add(button7);p3.add(button8);
p3.add(button9);p3.add(button10);
p3.add(button11);p3.add(button12);
p3.add(button13);p3.add(button14);
p3.add(button15);p3.add(button16);
p3.add(button17);p3.add(button18);
p3.add(button19);p3.add(button20);
p3.add(button21);p3.add(button22);
p3.add(button23);p3.add(button24);
setSize(250,150);
mainp=new JPanel();
mainp.setLayout(new GridLayout(3,1));
mainp.add(p1);
mainp.add(p2);
mainp.add(p3);
setContentPane(mainp);
setTitle("计算器");
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String s= e.getActionCommand();
if (s.equals("复制(C)")) {
String temp = jt1.getText().trim();
copyBoard.replace(0, copyBoard.length(), temp);
mnuPaste.setEnabled(true);
} else if (s.equals("粘贴(p)")) {
jt1.setText(copyBoard.toString());
} else if (s.equals("CE")) {
// 如果是CE则清除文本框
jt1.setText("0.");
} else if (s.equals("Backspace")) {
if (!jt1.getText().trim().equals("0.")) {
// 如果文本框中有内容
if (str.length() != 1 str.length() != 0) {
jt1.setText(str.delete(str.length() - 1, str.length())
.toString());
} else {
jt1.setText("0.");
str.setLength(0);
}
}
op2 = Double.parseDouble(jt1.getText().trim());
} else if (s.equals("C")) {
// 如果是C删除当前计算
jt1.setText("0.");
op1 = op2 = 0;
str.replace(0, str.length(), " ");
preOp = currentOp = 0;
} else if (s.equals("MC")) {
// 如果是MC则清除缓冲区
String temp = "";
memory.replace(0, memory.length(), temp);
bM.setText(" ");
} else if (s.equals("MR")) {
// 如果按键为MR则恢复缓冲区的数到文本框
jt1.setText(memory.toString());
} else if (s.equals("MS")) {
// 如果按键为MS则将文本框的数存入缓冲区
String s1 = jt1.getText().trim();
memory.replace(0, memory.length(), s1);
bM.setText("M");
} else if (s.equals("M+")) {
// 如果按键为MS则将文本框值与缓冲区的数相加但不显示结果
String temp1 = jt1.getText().trim();
double dtemp = Double.parseDouble(temp1);
String temp2 = memory.toString();
dtemp += Double.parseDouble(temp2);
temp1 = String.valueOf(dtemp);
memory.replace(0, memory.length(), temp1);
} else if (s.equals("1/x")) {
// 如果按键为1/x则将文本框中的数据为它的倒数
String temp = jt1.getText().trim();
double dtemp = Double.parseDouble(temp);
jt1.setText("" + 1 / dtemp);
} else if (s.equals("sqrt")) {
// 如果按键为sqrt则将文本框中的内容求平方根
String temp = jt1.getText().trim();
double dtemp = Double.parseDouble(temp);
jt1.setText("" + Math.sqrt(dtemp));
} else if (s.equals("+")) {
str.setLength(0);
if (currentOp == 0) {
preOp = currentOp = 1;
op2 = 0;
jt1.setText("" + op1);
} else {
currentOp = preOp;
preOp = 1;
switch (currentOp) {
case 1:
op1 += op2;
jt1.setText("" + op1);
break;
case 2:
op1 -= op2;
jt1.setText("" + op1);
break;
case 3:
op1 *= op2;
jt1.setText("" + op1);
break;
case 4:
op1 /= op2;
jt1.setText("" + op1);
break;
}
}
} else if (s.equals("-")) {
str.setLength(0);
if (currentOp == 0) {
preOp = currentOp = 2;// op1=op2;op2=0;
jt1.setText("" + op1);
} else {
currentOp = preOp;
preOp = 2;
switch (currentOp) {
case 1:
op1 = op1 + op2;
jt1.setText("" + op1);
break;
case 2:
op1 = op1 - op2;
jt1.setText("" + op1);
break;
case 3:
op1 = op1 * op2;
jt1.setText("" + op1);
break;
case 4:
op1 = op1 / op2;
jt1.setText("" + op1);
break;
}
}
} else if (s.equals("*"))// *
{
str.setLength(0);
if (currentOp == 0) {
preOp = currentOp = 3;// op1=op2;op2=1;
jt1.setText("" + op1);// op1=op2;
} else {
currentOp = preOp;
preOp = 3;
switch (currentOp) {
case 1:
op1 = op1 + op2;
jt1.setText("" + op1);
break;
case 2:
op1 = op1 - op2;
jt1.setText("" + op1);
break;
case 3:
op1 = op1 * op2;
jt1.setText("" + op1);
break;
case 4:
op1 = op1 / op2;
jt1.setText("" + op1);
break;
}
}
} else if (s.equals("/"))// /
{
str.setLength(0);
if (currentOp == 0) {
preOp = currentOp = 4;// op2=1;
jt1.setText("" + op1);// op1=op2;
} else {
currentOp = preOp;
preOp = 4;
switch (currentOp) {
case 1:
op1 = op1 + op2;
jt1.setText("" + op1);
break;
case 2:
op1 = op1 - op2;
jt1.setText("" + op1);
break;
case 3:
op1 = op1 * op2;
jt1.setText("" + op1);
break;
case 4:
op1 = op1 / op2;
jt1.setText("" + op1);
break;
}
}
} else if (s.equals("="))// =
{
if (currentOp == 0) {
str.setLength(0);
jt1.setText("" + op2);
} else {
str.setLength(0);
currentOp = preOp;
switch (currentOp) {
case 1:
op1 = op1 + op2;
jt1.setText("" + op1);
break;
case 2:
op1 = op1 - op2;
jt1.setText("" + op1);
break;
case 3:
op1 = op1 * op2;
jt1.setText("" + op1);
break;
case 4:
op1 = op1 / op2;
jt1.setText("" + op1);
break;
}
currentOp = 0;
op2 = 0;
}
} else if (s.equals(".")) {
isDouble = true;
if (jt1.getText().trim().indexOf('.') != -1)
;
else {
if (jt1.getText().trim().equals("0")) {
str.setLength(0);
jt1.setText((str.append("0" + s)).toString());
}
// else
// if(jt1.getText().trim().equals("")){}//如果初时显示为空则不做任何操作
else {
jt1.setText((str.append(s)).toString());
}
}
} else if (s.equals("0"))// 如果选择的是"0"这个数字键
{
if (jt1.getText().trim().equals("0.")) {
} else {
jt1.setText(str.append(s).toString());
op2 = Double.parseDouble(jt1.getText().trim());
}
} else {
jt1.setText(str.append(s).toString());
op2 = Double.parseDouble(jt1.getText().trim());
if (currentOp == 0)
op1 = op2;
}
}
public static void main(String args[])
{
JPanelEa1 g=new JPanelEa1();
}
@Override
public void menuSelected(MenuEvent e) {
// TODO Auto-generated method stub
}
@Override
public void menuDeselected(MenuEvent e) {
// TODO Auto-generated method stub
}
@Override
public void menuCanceled(MenuEvent e) {
// TODO Auto-generated method stub
}
}
如果只是实现的话,java有现成的函数
如果想自己写的话,我觉得用堆栈吧。
public class Calculator {
private int number1;
private int number2;
public Calculator(int number1, int number2) {
this.number1 = number1;
this.number2 = number2;
}
public double add() {
return number1 + number2;
}
public double subtract() {
return number1 - number2;
}
public double multiple() {
return number1 * number2;
}
public double divide() {
if(number2 == 0) {
return 0;
}
return number1 / number2;
}
}
public class TestCalculator {
public static void main(String[] args) {
Calculator calculator = new Calculator(5, 2);
System.out.println(calculator.add());
System.out.println(calculator.subtract());
System.out.println(calculator.multiple());
System.out.println(calculator.divide());
}
}