幂函数计算器介绍
我有这段Java代码,但我不知道如何在其中引入power、sin、cos或其他函数。我在各个网站上查找过,但没有找到,
public class calculator extends Activity {
private WebView mWebView;
private StringBuilder mMathString;
private ButtonClickListener mClickListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator);
// Create the math string
mMathString = new StringBuilder();
// Enable javascript for the view
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
// Set the listener for all the buttons
mClickListener = new ButtonClickListener();
int idList[] = { R.id.button0, R.id.button1, R.id.button2,
R.id.button3, R.id.button4, R.id.button5, R.id.button6,
R.id.button7, R.id.button8, R.id.button9, R.id.buttonLeftParen,
R.id.buttonRightParen, R.id.buttonPlus, R.id.buttonPlus,
R.id.buttonMinus, R.id.buttonDivide, R.id.buttonTimes,
R.id.buttonDecimal, R.id.buttonBackspace, R.id.buttonClear, R.id.buttonPow };
for(int id : idList) {
View v = findViewById(id);
v.setOnClickListener(mClickListener);
}
}
private void updateWebView() {
StringBuilder builder = new StringBuilder();
builder.append("<html><body>");
builder.append("<script type=\"text/javascript\">document.write('");
builder.append(mMathString.toString());
builder.append("');");
builder.append("document.write('<br />=' + eval(\"");
builder.append(mMathString.toString());
builder.append("\"));</script>");
builder.append("</body></html>");
mWebView.loadData(builder.toString(), "application/xhtml", "UTF-8");
}
private class ButtonClickListener implements OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonBackspace:
if(mMathString.length() > 0)
mMathString.deleteCharAt(mMathString.length()-1);
break;
case R.id.buttonClear:
if(mMathString.length() > 0)
mMathString.delete(0, mMathString.length());
break;
default:
mMathString.append(((Button) v).getText());
}
updateWebView();
}
}
}
有人能说一下如何引入幂函数吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能想查看 Java 的 Math 包。它有很多数学函数,包括您正在寻找的函数。如果导入尚不存在,您可能需要在使用 Math 函数的文件的开头添加
import java.lang.Math;
的导入。例如,
double answer = Math.pow(firstnum,secondnum);
是计算一个数字的另一个数字次幂的函数,或者double answer = Math.sin(anum)
code> 给出变量 anum 中某个值的 sin,等等。当您阅读该类的 javadoc 时,您将看到其他函数,这些函数应该提供您想要的其余“科学”计算器功能。
还要记住,整数和双精度数的存储容量确实有限制,因此,如果有人决定做一些疯狂的事情(例如 100,000^100,000,000),您可能需要考虑如何处理溢出。如果对非常大的数字有足够的精度对您来说很重要,您可能还需要研究诸如 BigDecimal 或 BigInteger。
You will probably want to look at Java's Math package. It has lots of math functions including the ones you are looking for. You may need to add an import of
import java.lang.Math;
at the beginning of the file you use the Math functions in, if the import is not already there.For example,
double answer = Math.pow(firstnum,secondnum);
is the function to raise one number to the power of another, ordouble answer = Math.sin(anum)
to give the sin of a value in the variable anum, and so on.As you read through the javadoc for that class you will see additional functions that should supply the rest of the "scientific" calculator features you want.
Keep in mind also that integers and doubles do have limits to their storage capacities, so you may want to consider how to handle overflow if someone decides to do something crazy like 100,000^100,000,000. If having enough precision for really really large numbers is important to you, you may also want to look into classes such as BigDecimal or BigInteger.
为了回答这个问题,我想你真的想知道,这就是代码如何在不使用 Java 数学包的情况下计算数学函数...
你正在查看的代码将一堆 javascript 打印到 HTML 页面,包括
eval("your math expression")
在 eval 中输入数学表达式,而 eval 行实际上是在进行数学评估,并且它是使用 javascript 而不是 Java 来实际进行数学计算的。http://javascriptsource.com/math-lated/5-function.html有使用 javascript 的 eval 函数执行权力的示例...您必须修改应用程序生成的 javascript 才能以这种方式评估权力。
To answer the question I think you're really wanting to know, which is how does THAT code calculate math functions without using Java's math package...
That code you are looking at prints a bunch of javascript to an HTML page, including
eval("your math expression")
with the entered mathmatical expression within the eval, and that eval line is what is actually doing the mathmatical evaluation, and it is doing so using javascript, not Java to actually do the math.http://javascriptsource.com/math-related/5-function.html has example of doing powers using javascript's eval function...you would have to modify the javascript generated by the app in order to evaluate powers in that way.
您需要使用 java 数学 API。看一下这个教程。
You need to use java Math API. Take a look at this tutorial.
Java 将这些函数作为静态方法内置在
java.lang.Math
类中。您可以像这样计算它们:
幂,使用
Math.pow(double, double)
平方根,使用
Math.sqrt (double)
Sin、cos、tan
Arcsin、arccos、arctan
所有这些函数都是原生的(不是用 Java 编写的),这意味着它们将执行为在您的平台上尽可能好。
Java has these functions built-in in the
java.lang.Math
class as static methods.You can cal them just like this:
Power, using
Math.pow(double, double)
Square Root, using
Math.sqrt(double)
Sin, cos, tan
Arcsin, arccos, arctan
All these functions are Native (not written in Java), which mean that they will perform as good as possible on your platform.