⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calcfunc.java

📁 Java 语言实现的计算器
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package calcpackage;/** * * @author Yuyin Li */import javax.swing.*;import java.util.Stack;public class CalcFunc {  private JLabel jLabel = null; private JTextField jTextField = null; private JTextField jLeft = null; private double op1=0,op2=0; public double PI=3.141592655358989323846264; public int m_Left=0; public int m_Bits=0; public double m_Operand=0; public double m_Accum=0; public boolean m_Operable=false; public boolean m_Dot=false; public String Str;  Stack m_Stack=null; Stack o_Stack=null;  private enum Operator { OpNone,OpLeft,OpAdd,OpSub,OpMul,OpDiv,OpRight}; public Operator m_Operator=Operator.OpNone;  public CalcFunc(JLabel jL,JTextField jF,JTextField jTL) {  this.jLabel=jL;  this.jTextField=jF;  this.jLeft=jTL;  m_Stack=new Stack();  o_Stack=new Stack();  Clear(); } public void Input(String str) {  if(str.equals("."))  {   this.m_Dot=true;   this.m_Operable=true;   return;  }  if(str.equals("+"))  {   this.m_Operable=false;   this.m_Operator=Operator.OpAdd;      Run();   return;  }  if(str.equals("-"))  {   this.m_Operable=false;   this.m_Operator=Operator.OpSub;      Run();   return;  }  if(str.equals("*"))  {   this.m_Operable=false;   this.m_Operator=Operator.OpMul;      Run();   return;  }  if(str.equals("/"))  {   this.m_Operable=false;   this.m_Operator=Operator.OpDiv;      Run();   return;  }  if(!this.m_Operable)  {   this.m_Operand=0;   this.m_Dot=false;   this.m_Bits=0;  }  if(this.m_Dot)  {   this.m_Bits++;   this.m_Operand=this.m_Operand+Integer.parseInt(str)/Math.pow(10,this.m_Bits);  }else   this.m_Operand=this.m_Operand*10+Integer.parseInt(str);  this.m_Operable=true;  Disp(); }  public void Disp() {  double lVal=(m_Operable) ? m_Operand:m_Accum;  Str=String.valueOf(lVal);  if(Str.equals("Infinity"))  {   this.jLabel.setText("超过运算范围!");   Str="0.0";  }  jTextField.setText(Str); } public void Sqrt() {  this.m_Operable=false;  this.m_Operand=Math.sqrt(this.m_Operand);  this.m_Accum=this.m_Operand;  Disp();   } public void Sin() {  this.m_Operand=this.m_Operand/180*PI;  this.m_Operand=Math.sin(this.m_Operand);  this.m_Accum=this.m_Operand;  this.m_Operable=false;  Disp(); } public void Cos() {  this.m_Operand=this.m_Operand/180*PI;  this.m_Operand=Math.cos(this.m_Operand);  this.m_Accum=this.m_Operand;  this.m_Operable=false;  Disp();   } public void Tan() {  this.m_Operand=this.m_Operand/180*PI;  this.m_Operand=Math.tan(this.m_Operand);  this.m_Accum=this.m_Operand;  this.m_Operable=false;  Disp();   } public void Left() {  this.m_Left++;  this.jLeft.setText("( "+String.valueOf(this.m_Left));  this.m_Operator=Operator.OpLeft;  this.m_Operable=false;  Run(); } public void Right() {  if(this.m_Left!=0)  {   this.m_Operator=Operator.OpRight;   this.m_Operable=false;   this.m_Left--;   if(this.m_Left!=0)    this.jLeft.setText("( "+String.valueOf(this.m_Left));   else    this.jLeft.setText("");   this.m_Stack.push(this.m_Operand);   Operator Op;   while(this.o_Stack.peek()!=Operator.OpLeft)   {    Op=this.o_Stack.pop();    Compute(Op);   }   this.m_Operand=this.m_Accum=this.m_Stack.peek();   Disp();   this.o_Stack.pop();   this.m_Stack.pop();  } } public void X2() {  this.m_Operable=false;  this.m_Operand=this.m_Operand*this.m_Operand;  this.m_Accum=this.m_Operand;  Disp(); } public void X1() {  this.m_Operable=false;  if(this.m_Operand==0)  {   this.jLabel.setText("除数不能为零!");  }else   this.m_Operand=1/this.m_Operand;  this.m_Accum=this.m_Operand;  Disp();   } public void Clear() {  this.m_Accum=0;  this.m_Left=0;  this.m_Operable=false;  this.m_Operand=0;  this.Str="";  this.jTextField.setText("0.0");  this.m_Dot=false;  this.m_Stack.clear();  this.o_Stack.clear();  this.jLeft.setText("");  this.op1=0;  this.op2=0;  this.m_Operator=Operator.OpNone;  this.jLabel.setText("科学计算器"); } public void As() {  this.m_Operable=false;  this.m_Operand=this.m_Accum=(-1)*this.m_Operand;  Disp(); } public void Run() {  Operator Go;  if(this.o_Stack.empty()||(this.m_Operator==Operator.OpLeft)||this.m_Operator.ordinal()>this.o_Stack.peek().ordinal())  {   if(this.m_Operator==Operator.OpLeft)    this.o_Stack.push(this.m_Operator);   else   {    this.o_Stack.push(this.m_Operator);    this.m_Stack.push(this.m_Operand);   }  }else  {   this.m_Stack.push(this.m_Operand);   while(!this.o_Stack.empty() && this.m_Operator.ordinal()<=this.o_Stack.peek().ordinal())   {    Go=this.o_Stack.pop();    Compute(Go);   }   this.o_Stack.push(this.m_Operator);  }  if(!this.m_Stack.empty())  {   this.m_Operand=this.m_Accum=this.m_Stack.peek();   Disp();  }  if(this.m_Operator==Operator.OpNone)  {   this.o_Stack.clear();   this.m_Stack.clear();  } } public void Compute(Operator Opp) {  int result;  result=GetTwo();  if(result==1)  {   switch(Opp)   {    case OpAdd: this.m_Stack.push(op1+op2);break;    case OpMul:this.m_Stack.push(op1*op2);break;    case OpSub:this.m_Stack.push(op2-op1);break;    case OpDiv:     if(op1==0)      this.jLabel.setText("除数不能为零!");     else      this.m_Stack.push(op2/op1);     break;   }  }else  {   this.m_Stack.clear();   this.o_Stack.clear();  }  this.m_Operable=false; } private int GetTwo() {  if(this.m_Stack.empty()) return 0;   op1=this.m_Stack.pop();  if(this.m_Stack.empty()) return 0;   op2=this.m_Stack.pop();  return 1;   } public void Be() {  this.m_Operable=false;  this.m_Operator=Operator.OpNone;  Run(); }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -