运用JAVA中大数类实现大数的四则运算

运用JAVA中大数类实现大数的四则运算,第1张

import javamathBigInteger;

public class BigIntegerGet {

public String getAdd(String Str1,String Str2){

String Str3=new String();

BigInteger BigInt1=new BigInteger(Str1);

BigInteger BigInt2=new BigInteger(Str2);

BigInt1=BigInt1add(BigInt2);

Str3=BigInt1toString();

return Str3;

}

public String getSubtract(String Str1,String Str2){

String Str3=new String();

BigInteger BigInt1=new BigInteger(Str1);

BigInteger BigInt2=new BigInteger(Str2);

BigInt1=BigInt1subtract(BigInt2);

Str3=BigInt1toString();

return Str3;

}

public String getMultiply(String Str1,String Str2){

String Str3=new String();

BigInteger BigInt1=new BigInteger(Str1);

BigInteger BigInt2=new BigInteger(Str2);

BigInt1=BigInt1multiply(BigInt2);

Str3=BigInt1toString();

return Str3;

}

public String getDivide(String Str1,String Str2){

String Str3=new String();

BigInteger BigInt1=new BigInteger(Str1);

BigInteger BigInt2=new BigInteger(Str2);

BigInt1=BigInt1divide(BigInt2);

Str3=BigInt1toString();

return Str3;

}

}

这里计算要用到栈!

要是学过基础的数据结构你应该知道怎么计算!

其实挺挺复杂的!

栈读取每一个数值和 *** 作符

根据不同的优先级进行计算!

给你推荐一个地址!看一下!

>

代码如下,我测试过了。。。

public class Test {

public static double compute(double data1, double data2, char operator){

double result = 0;//保存运算结果

switch(operator){

case '+' : result = data1+data2;break;

case '-' : result = data1-data2;break;

case '' : result = data1data2;break;

case '/' : result = data1/data2;break;

}

return result;

}

public static void main(String args[]){

Systemoutprintln(compute(5,2,'+'));

Systemoutprintln(compute(5,2,'-'));

Systemoutprintln(compute(5,2,''));

Systemoutprintln(compute(5,2,'/'));

}

}

运行结果:

70

30

100

25

希望对你有帮助。。。。仍有问题可以HI我。。。。

计算器的最好的算法将中缀表达式转换为后缀表达式,楼主可以搜索以下相关资料 ,

也可以 char[] c = xStringtoCharArray(); 然后通过 asc 码来比较,不果逻辑不好控制

import javaxscriptInvocable;

import javaxscriptScriptEngine;

import javaxscriptScriptEngineManager;

public class ScriptTest {

public static void main(String[] args) throws Exception {

String s = "(7-5)6/5";

// 验证数据合不合法。

if (sreplaceAll("[\\d+-\\\\/()]","")equals("")) {

ScriptEngineManager manager = new ScriptEngineManager();

ScriptEngine engine = managergetEngineByName("JavaScript");

engineeval("function t() {return " + s + ";}");

Invocable invocable = (Invocable) engine;

Object o = invocableinvokeFunction("t");

Systemoutprintln(o);

}

}

}

JDK16以上可以使用。利用脚本很容易的。

以上就是关于运用JAVA中大数类实现大数的四则运算全部的内容,包括:运用JAVA中大数类实现大数的四则运算、java实现四则运算、java编程, 用SWITCH写一个两个整数四则运算的程序 急求 各位大侠们帮帮忙吧!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9867286.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存