- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在创建一个计算器应用程序,它可以在屏幕上显示所有计算步骤,如下所示:
3x3x5/7*(3/2)
然后,用户只需单击“输入”按钮即可获得结果。
问题是我总是得到一个整数值,这很明显,因为我在计算中使用了整数值(3、5、7 等)。所以,如果我这样做:1/2,结果将为 0,但我希望答案为 0.5
更具体地说,我的计算器是这样工作的:用户点击按钮 5,它出现在屏幕上(一个 TextView )。然后用户点击按钮/(除),使用附加方法,屏幕现在显示 5/,最后用户点击按钮 9,屏幕显示 5/9。
当用户点击“Enter”时,应用程序从屏幕上获取文本并将其存储在一个字符串变量中,该变量将由 JEXL 库(用于计算方程式)使用,为我提供结果。
问题是,我希望数字在屏幕上显示为整数,所以如果我点击 9,我不希望它在屏幕上显示为 9.0,但还有另一个问题,假设我想输入数字“93”,我先打9,然后出现9.0,然后我打3,出现3.0,给我“数字”9.03.0,它不存在,不能使用.
那么,在哪里可以将字符串表达式中的整数转换为 double ?或者,也许,从整数计算中获得 double 值?
这是我的java文件:
public class Main extends Activity implements OnClickListener {
TextView tv_screen;
TextView tv_set1;
TextView tv_set2;
String screenEvaluation;
Double screenCalculation;
JexlEngine jexl = new JexlEngine();
static final String digits = "0123456789.*/-+^( )√(";
static final String numbers = "0123456789";
static final String operators = "*/-+";
static final String shifteds = "B1";
Boolean shiftPressed = false;
Boolean userIsInTheMiddleOfTypingANumber = false;
Solver solver = new Solver();
DecimalFormat df = new DecimalFormat("@###########");
NumberFormat nf = new DecimalFormat("#.####");
//String buttonPressed;
@Override
protected void onCreate(Bundle savedInstanceState) {
/* REMOVE TITLE BAR */
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
/* REMOVE NOTIFICATION BAR (AKA FULL SCREEN) */
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
df.setMinimumFractionDigits(0);
df.setMinimumIntegerDigits(1);
df.setMaximumIntegerDigits(8);
tv_screen = (TextView) findViewById(R.id.tv_screen);
tv_set1 = (TextView) findViewById(R.id.tv_set1);
tv_set2 = (TextView) findViewById(R.id.tv_set2);
findViewById(R.id.button_1).setOnClickListener(this);
findViewById(R.id.button_2).setOnClickListener(this);
findViewById(R.id.button_3).setOnClickListener(this);
findViewById(R.id.button_4).setOnClickListener(this);
findViewById(R.id.button_5).setOnClickListener(this);
findViewById(R.id.button_6).setOnClickListener(this);
findViewById(R.id.button_7).setOnClickListener(this);
findViewById(R.id.button_8).setOnClickListener(this);
findViewById(R.id.button_9).setOnClickListener(this);
findViewById(R.id.button_0).setOnClickListener(this);
findViewById(R.id.button_multiply).setOnClickListener(this);
findViewById(R.id.button_divide).setOnClickListener(this);
findViewById(R.id.button_minus).setOnClickListener(this);
findViewById(R.id.button_sum).setOnClickListener(this);
findViewById(R.id.button_root).setOnClickListener(this);
findViewById(R.id.button_power).setOnClickListener(this);
findViewById(R.id.button_bracket).setOnClickListener(this);
findViewById(R.id.button_more_less).setOnClickListener(this);
findViewById(R.id.button_dot).setOnClickListener(this);
findViewById(R.id.button_shift).setOnClickListener(this);
findViewById(R.id.button_enter).setOnClickListener(this);
findViewById(R.id.button_clear).setOnClickListener(this);
}
@Override
public void onClick(View v) {
String buttonPressed = ((Button) v).getText().toString();
if (digits.contains(buttonPressed)) {
// digit was pressed
if (userIsInTheMiddleOfTypingANumber) {
if (buttonPressed.equals(".") && tv_screen.getText().toString().contains(".")) {
// ERROR PREVENTION
// Eliminate entering multiple decimals
} else {
if (buttonPressed.equals("( )")) {
buttonPressed = "(";
if (tv_screen.getText().toString().contains("(")) {
buttonPressed = ")";
}
}
if (buttonPressed.equals("√")) {
buttonPressed = "√(";
}
if (operators.contains(buttonPressed)) {
tv_screen.append(".0" + buttonPressed);
} else {
tv_screen.append(buttonPressed);
}
}
} else {
if (buttonPressed.equals(".")) {
// ERROR PREVENTION
// This will avoid error if only the decimal is hit before an operator, by placing a leading zero
// before the decimal
tv_screen.setText(0 + buttonPressed);
} else {
if (buttonPressed.equals("( )")) {
buttonPressed = "(";
if (tv_screen.getText().toString().contains("(")) {
buttonPressed = ")";
}
}
if (buttonPressed.equals("√")) {
buttonPressed = "√(";
}
tv_screen.setText(buttonPressed);
}
userIsInTheMiddleOfTypingANumber = true;
}
} else if (buttonPressed.equals("SHIFT")) {
if (shiftPressed == true) {
shiftPressed = false;
solver.setShift(false);
tv_set1.setText("");
} else {
shiftPressed = true;
solver.setShift(true);
tv_set1.setText("SHIFT");
}
} else if (buttonPressed.equals("ENTER")) {
//solver.performEnterOperation(buttonPressed);
//tv_screen.setText("DONE!");
screenEvaluation = tv_screen.getText().toString();
//screenEvaluation = screenEvaluation.replace("x", "*");
screenEvaluation = screenEvaluation.replace("√(", "SQRT(");
Log.w("TAG", "thickness round:" + screenEvaluation);
Expression e = jexl.createExpression(screenEvaluation);
JexlContext context = new MapContext();
String result = e.evaluate(context).toString();
//Log.w("TAG", "thickness round:" + screenEvaluation);
tv_screen.setText(result);
userIsInTheMiddleOfTypingANumber = false;
} else if (shifteds.contains(buttonPressed) && shiftPressed == true) {
tv_set2.setText(solver.getSetTextView(buttonPressed));
} else if (buttonPressed.equals("CLEAR")) {
userIsInTheMiddleOfTypingANumber = false;
tv_screen.setText("0");
} else {
// operation was pressed
if (userIsInTheMiddleOfTypingANumber) {
//Log.w("TAG", "thickness round:" + yyy);
tv_set2.setText(solver.getSetTextView(buttonPressed));
solver.setOperand(Double.parseDouble(tv_screen.getText().toString()));
userIsInTheMiddleOfTypingANumber = false;
}
//tv_set2.setText(solver.getSetTextView(buttonPressed));
solver.performOperation(buttonPressed);
tv_screen.setText(df.format(solver.getResult()));
}
}
}
还有这个,但目前还没有被使用:
public class Solver {
// 3 + 6 = 9
// 3 & 6 are called the operand.
// The + is called the operator.
// 9 is the result of the operation.
private double mOperand;
private double mWaitingOperand;
private String mWaitingOperator;
private double mCalculatorMemory;
private Boolean shift;
private double pressure;
private double inner_diameter;
private double allowable_stress;
private double weld_factor;
private double y_factor;
private double corrosion;
// operator types
public static final String ADD = "+";
public static final String SUBTRACT = "-";
public static final String MULTIPLY = "x";
public static final String DIVIDE = "/";
public static final String CLEAR = "C" ;
public static final String CLEARMEMORY = "MC";
public static final String ADDTOMEMORY = "M+";
public static final String SUBTRACTFROMMEMORY = "M-";
public static final String RECALLMEMORY = "MR";
public static final String SQUAREROOT = "√";
public static final String SQUARED = "x²";
public static final String INVERT = "1/x";
public static final String TOGGLESIGN = "+/-";
public static final String SINE = "sin";
public static final String COSINE = "cos";
public static final String TANGENT = "tan";
public static final String ARC = "ARC";
public static final String B1 = "B1";
public static final String B2 = "B2";
public static final String B3 = "B3";
public static final String B4 = "B4";
public static final String B5 = "B5";
public static final String B6 = "B6";
// public static final String EQUALS = "=";
// constructor
public Solver() {
// initialize variables upon start
mOperand = 0;
mWaitingOperand = 0;
mWaitingOperator = "";
mCalculatorMemory = 0;
shift = false;
pressure = 0;
inner_diameter = 0;
allowable_stress = 0;
weld_factor = 0;
y_factor = 0;
corrosion = 0;
}
public void setOperand(double operand) {
mOperand = operand;
}
public double getResult() {
return mOperand;
}
// used on screen orientation change
public void setMemory(double calculatorMemory) {
mCalculatorMemory = calculatorMemory;
}
// used on screen orientation change
public double getMemory() {
return mCalculatorMemory;
}
public String toString() {
return Double.toString(mOperand);
}
public void setShift(Boolean shiftState) {
shift = shiftState;
}
public void setPressure(double setPressure) {
pressure = setPressure;
}
public void setInnerDiameter(double setInnerDiameter) {
inner_diameter = setInnerDiameter;
}
public void setAllowableStress(double setAllowableStress) {
allowable_stress = setAllowableStress;
}
public void setWeldFactor(double setWeldFactor) {
weld_factor = setWeldFactor;
}
public void setYFactor(double setYFactor) {
y_factor = setYFactor;
}
public void setCorrosion(double setCorrosion) {
corrosion = setCorrosion;
}
public String getSetTextView(String setText) {
String text;
if (setText.equals(ARC)) {
text = "PIPE";
return text;
} else if (setText.equals(B1)) {
text = "PRESSURE";
return text;
} else if (setText.equals(B2)) {
text = "INNER DIAMETER";
return text;
} else if (setText.equals(B3)) {
text = "ALLOWABLE STRESS";
return text;
} else if (setText.equals(B4)) {
text = "WELD FACTOR";
return text;
} else if (setText.equals(B5)) {
text = "Y FACTOR";
return text;
} else if (setText.equals(B6)) {
text = "CORROSION";
return text;
}
text = "NADA";
return text;
}
public double pipeThickness(double inner_pressure, double allowable_stress, double weld_factor, double inner_diameter, double corrosion, double y_factor) {
double thickness = ( ( inner_pressure * inner_diameter) + ( 2* allowable_stress * weld_factor * corrosion )
+ ( 2 * y_factor * inner_pressure * corrosion ) ) / ( 2* ( ( allowable_stress * weld_factor )
+ ( inner_pressure * y_factor ) - inner_pressure ) );
return thickness;
}
protected void performEnterOperation(String operator) {
if (shift == true) {
if (operator.equals(B1)) {
setPressure(mOperand);
}
}
}
protected double performOperation(String operator) {
/*
* If you are using Java 7, then you can use switch in place of if statements
*
* switch (operator) {
* case CLEARMEMORY:
* calculatorMemory = 0;
* break;
* case ADDTOMEMORY:
* calculatorMemory = calculatorMemory + operand;
* break;
* etc...
* }
*/
if (shift == true) {
if (operator.equals(ARC)) {
getSetTextView(ARC);
mOperand = 25.0;
}
} else {
if (operator.equals(CLEAR)) {
mOperand = 0;
mWaitingOperator = "";
mWaitingOperand = 0;
// mCalculatorMemory = 0;
} else if (operator.equals(CLEARMEMORY)) {
mCalculatorMemory = 0;
} else if (operator.equals(ADDTOMEMORY)) {
mCalculatorMemory = mCalculatorMemory + mOperand;
} else if (operator.equals(SUBTRACTFROMMEMORY)) {
mCalculatorMemory = mCalculatorMemory - mOperand;
} else if (operator.equals(RECALLMEMORY)) {
mOperand = mCalculatorMemory;
} else if (operator.equals(SQUAREROOT)) {
mOperand = Math.sqrt(mOperand);
} else if (operator.equals(SQUARED)) {
mOperand = mOperand * mOperand;
} else if (operator.equals(INVERT)) {
if (mOperand != 0) {
mOperand = 1 / mOperand;
}
} else if (operator.equals(TOGGLESIGN)) {
mOperand = -mOperand;
} else if (operator.equals(SINE)) {
mOperand = Math.sin(Math.toRadians(mOperand)); // Math.toRadians(mOperand) converts result to degrees
} else if (operator.equals(COSINE)) {
mOperand = Math.cos(Math.toRadians(mOperand)); // Math.toRadians(mOperand) converts result to degrees
} else if (operator.equals(TANGENT)) {
mOperand = Math.tan(Math.toRadians(mOperand)); // Math.toRadians(mOperand) converts result to degrees
} else {
performWaitingOperation();
mWaitingOperator = operator;
mWaitingOperand = mOperand;
}
}
return mOperand;
}
protected void performWaitingOperation() {
if (mWaitingOperator.equals(ADD)) {
mOperand = mWaitingOperand + mOperand;
} else if (mWaitingOperator.equals(SUBTRACT)) {
mOperand = mWaitingOperand - mOperand;
} else if (mWaitingOperator.equals(MULTIPLY)) {
mOperand = mWaitingOperand * mOperand;
} else if (mWaitingOperator.equals(DIVIDE)) {
if (mOperand != 0) {
mOperand = mWaitingOperand / mOperand;
}
}
}
}
嗯,我找到的解决方案是这样的:
if (operators.contains(buttonPressed)) {
tempNumber = Double.parseDouble(tv_screen.getText().toString().replace(tempString, ""));
equation = equation + tempNumber.toString() + buttonPressed;
tempString = tv_screen.getText().toString() + buttonPressed;
}
我创建了 3 个变量:Double tempNumber,将从屏幕获取值并将其存储为 double。字符串方程,它将方程存储为具有 Double 值的字符串,因此,“9*3”将是“9.0*3.0”。字符串 tempString,用于“清理”tempNumber 变量,将“x”、“/”等替换为“”(无),因此它只会将数字转换为 double 。
最后,我不是从屏幕 TextView 中获取方程式,而是从方程式变量中获取它:
tempNumber = Double.parseDouble(tv_screen.getText().toString().replace(tempString, ""));
equation = equation + tempNumber.toString();
Expression e = jexl.createExpression(equation);
JexlContext context = new MapContext();
String result = e.evaluate(context).toString();
无论如何,谢谢大家的宝贵时间!
最佳答案
如果你想从一个字符串中解析你可以使用的double
Double.parseDouble(sValue);
您还可以在方程式中使用显式转换,这样您就可以执行以下操作:
(double)inum*(double)inum1*(double)inum2/(double)inum4*((double)inum5/(double)inum6)
然后您的结果应该是 double 而不是整数。
关于java - Android/Java - 将等式中的 int 值转换为 double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20954781/
下面的代码有效,我觉得double(double)和double(*)(double)没有区别,square和 &square,我说得对吗? #include double square(doubl
我知道我的作业很草率,这是我在这门课上的第 4 次作业。任何帮助将不胜感激,谢谢。 double getPrincipal(0); double getRate(0); double getYe
我遇到了那个错误,当我使用类时,我在使用函数指针时遇到了这个错误。我的函数'ope'函数我该如何解决 evaluator::function(){ double (*ope) (dou
问题://故事从哪里开始 Graphics 类型中的方法 drawLine(int, int, int, int) 不适用于参数 (double, double, double, double) g.
我有一张 map> m1 形式的 map .我可以将其复制到 map m2 形式的 map 吗?这样键是相同的,并且 m2 中的值是 get(m1->second) 不使用循环?谢谢! 最佳答案 这样
有没有办法获取vector> 的“.first”和“.second”的连续内存? ?我的意思是: void func(int N, double* x, double* y) { for (i
我正在尝试将自定义 lambda 传递给需要函数指针的函数(更准确地说是 zero 中的 Brent library 函数)。 我的想法是,我将使用参数创建一次 lambda,然后用多个值对其求值 x
这是一个很简单的问题,让我很困惑。 我收到一个源文件的以下错误,但另一个没有: 4 src/Source2.cpp:1466: error: no matching function for cal
struct CalculatorBrain { private var accumulator: Double? func changeSign(operand: Double) -
在我正在进行的项目中,我尝试使用 curlpp库来发出一个简单的 html GET 请求。当我将 cpp 文件传递给 g++ 时,出现以下错误: /usr/local/include/curlpp
不使用double就能获得quadruple精度超过16位的数字吗?如果可能的话,这取决于编译器还是其他?因为我知道有人说他使用double精度,并且具有22位精度。 最佳答案 数据类型double
我正在寻找有关特斯拉 GPU 中硬件如何实现 double 的信息。我读到,两个流处理器正在处理单个 double 值,但我没有找到 nvidia 的任何官方论文。 提前致谢。聚苯硫醚为什么大多数 G
这个问题在这里已经有了答案: Passing capturing lambda as function pointer (10 个答案) 关闭 2 年前。 我有这个错误 error: cannot
情况:我有一个元组列表,其中添加了一个元组: List> list = new List>(); list .Add(new Tuple(2.2, 6.6)); 一切似乎都还好。但是......在 D
我有一个 JList,里面有一堆名字,还有一个包含这些名字值的数组 final Double[] filmcost = { 5.00, 5.50, 7.00, 6.00, 5.00 }; 我想做的是,
我试图找出牛顿法来求方程的根。这个错误出来了,我无法处理。 double fn(double n){ return sin(n)+log(n)-1; } double f1n(double n
我有一个 junit 测试断言两个 Double 对象,具有以下内容: Assert.assertEquals(Double expected, Double result); 这很好,然后我决定将其
我正在尝试引入部分数据文件来填充数组,用户尝试了三次输入正确的数据文件名。我一再遇到这些错误。我知道像 arr 这样的数组只是一个指向内存块的指针。 #include #include #incl
我正在尝试完成复习题(为即将到来的编程决赛),但是,我无法解决这个问题,因为我不断收到错误(标题)。正如预期的那样,我将发布问题和我尝试的解决方案。 问题: 给定以下函数定义:void swap(do
任何人都知道如何实现这一目标。我已经尝试了通常的公式,但我只得到正数 Double.NEGATIVE_INFINITY) return d; } } 这将以相同的概率
我是一名优秀的程序员,十分优秀!