java的算术表达式程序,java计算数学表达式

import java.util.EmptyStackException;

import java.util.Stack;

public class CaculateFunction {

private static String[] TrnsInToSufix(String IFX)// PFX放后缀表达式,IFX为中缀表达式

{

String PFX[] = new String[IFX.length()];

StringBuffer numBuffer = new StringBuffer();// 用来保存一个数的

Stack s = new Stack();// 放操作符

String a;

s.push("=");// 第一个为等号

int i = 0, j = 0;

char ch;

for (i = 0; i < IFX.length();) {

ch = IFX.charAt(i);

switch (ch) {

case '0':

case '1':

case '2':

case '3':

case '4':

case '5':

case '6':

case '7':

case '8':

case '9':

while (Character.isDigit(ch) || ch == '.')// 拼数

{

numBuffer.append(ch); // 追加字符

ch = IFX.charAt(++i);

}

PFX[j++] = numBuffer.toString();// break;

numBuffer = new StringBuffer(); // 清空已获取的运算数字

continue; // 这里要重新循环,因为i已经增加过了

case '(':

s.push("(");

break;

case ')':

while (s.peek() != "(")

PFX[j++] = s.pop();

break;

case '+':

case '-':

while (s.size() > 1 && s.peek() != "(")

PFX[j++] = s.pop();

a = String.valueOf(ch);

s.push(a);

break;

case '*':

case '/':

while (s.size() > 1 && (s.peek() == "*") || s.peek() == "/"

|| s.peek() == "s" || s.peek() == "c"

|| s.peek() == "t" || s.peek() == "^"

|| s.peek() == "√")

// 优先级比较,与栈顶比较,

PFX[j++] = s.pop();// 当前操作符优先级大于等于栈顶的弹出栈顶

a = String.valueOf(ch);

s.push(a);

break;

case 's':

case 'c':

case 't':// 三角函数

while (s.size() > 1

&& (s.peek() == "s" || s.peek() == "c"

|| s.peek() == "t" || s.peek() == "^" || s

.peek() == "√"))

// 优先级比较,与栈顶,大于等于的弹出

PFX[j++] = s.pop();

a = String.valueOf(ch);

s.push(a);

break;

case '^':// 幂

case '√':// 开方

while (s.size() > 1 && (s.peek() == "^" || s.peek() == "√"))

PFX[j++] = s.pop();

a = String.valueOf(ch);

s.push(a);

break;

}

i++;

}

while (s.size() > 1)

PFX[j++] = s.pop();

PFX[j] = "=";

return PFX;

}

public static String Evaluate(String IFX)// 后缀表达式求值

{

String PFX[] = null;

try {

PFX = TrnsInToSufix(IFX);

} catch (EmptyStackException e) {

return "syntax error";

}

int i = 0;

double x1, x2, n;

String str;

Stack s = new Stack();

while (PFX[i] != "=") {

str = PFX[i];

switch (str.charAt(0)) {

case '0':

case '1':

case '2':

case '3':

case '4':

case '5':

case '6':

case '7':

case '8':

case '9':

s.push(str);

break;

case '+':

x1 = Double.parseDouble(s.pop());

x2 = Double.parseDouble(s.pop());

n = x1 + x2;

s.push(String.valueOf(n));

break;

case '-':

x1 = Double.parseDouble(s.pop());

x2 = Double.parseDouble(s.pop());

n = x2 - x1;

s.push(String.valueOf(n));

break;

case '*':

x1 = Double.parseDouble(s.pop());

x2 = Double.parseDouble(s.pop());

n = x1 * x2;

s.push(String.valueOf(n));

break;

case '/':

x1 = Double.parseDouble(s.pop());

x2 = Double.parseDouble(s.pop());

n = x2 / x1;

s.push(String.valueOf(n));

break;

case 's':

x1 = Double.parseDouble(s.pop());

n = Math.sin(x1 * Math.PI / 180);

s.push(String.valueOf(n));

break;

case 'c':

x1 = Double.parseDouble(s.pop());

n = Math.cos(x1 * Math.PI / 180);

s.push(String.valueOf(n));

break;

case 't':

x1 = Double.parseDouble(s.pop());

n = Math.tan(x1 * Math.PI / 180);

s.push(String.valueOf(n));

break;

case '√':

x1 = Double.parseDouble(s.pop());

n = Math.sqrt(x1);

s.push(String.valueOf(n));

break;// 开方

case '^':

x1 = Double.parseDouble(s.pop());

x2 = Double.parseDouble(s.pop());

n = Math.pow(x2, x1);

s.push(String.valueOf(n));

break;

}

i++;

}

String result = s.pop();

return result;

}

public static void main(String args[]) {

System.out.println(Evaluate("(31 + 21) * 51 - (21 + 33) / 2 = "));

}

}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com

特别注意:本站所有转载文章言论不代表本站观点!

本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值