问题描述
- java语言想要运算器程序可以运行出正负数及小数点运算
-
public class Calculator
{ private CalcEngine engine;
private UserGUI gui;
public Calculator()
{ engine=new CalcEngine();
gui=new UserGUI(engine); }public void show()
{ gui.setVisible(true); }
public static void main(String a[])
{ Calculator calc=new Calculator();
calc.show(); }
}
class CalcEngine
{ private double number;double result;
final double pi=3.1415926535;
char flag;
boolean isNumber;
public CalcEngine()
{ number=0;
result=0;
flag=' ';
isNumber=true;
}
public double getDisplayValue()
{ return number; }
public void numberPressed(double number)
{ if(isNumber&&this.number!=0)
{ double str1=Double.toDouble(number);
double str2=Double.toDouble(this.number);
this.number=Double.parseDouble(str2+str1);
}
else
{ this.number=number; }
isNumber=true;
}
public void plus()
{ equals();
this.flag='+'; }
public void minus()
{ equals();
this.flag='-'; }
public void mul()
{ equals();
this.flag='*'; }
public void div()
{ equals();
this.flag='/'; }
public void genhao()
{ equals();
this.flag='√'; }
public void quyu()
{ equals();
this.flag='%'; }
public void sinzhi()
{ equals();
this.flag='s'; }
public void dian()
{ equals();
this.flag='.'; }
public double equals()
{ switch(flag)
{ case '+': result=result+number; break;
case '-': result=result-number; break;
case '*': result=result*number; break;
case '/': result=result/number; break;
case '√': result=Math.sqrt( number); break;
case '%': result=result%number; break;
case 's': result= Math.sin(number*pi/180);
break;
case '.': number=(double) number; break; //这里有错
case ' ': result= number; break;
}
return result;
flag=' ';
isNumber=false;
}
public void clear()
{ number=0; result=0; }
public String getTitle()
{ return " calclation example"; }
}