java 实现逆波兰表达式_怎样用JAVA写出逆波兰表达式求值部分的源代码(提供代码框架)...

怎样用JAVA写出逆波兰表达式求值部分的源代码(提供代码框架)

关注:76  答案:2  mip版

解决时间 2021-02-01 15:29

e6cb1a03ad541b3098697807b7bf1798.png

提问者等妳¬硪唯一鍀执念

2021-02-01 09:19

逆波兰表达式 6 2 5+ * 2 8 7+ * /其中只用到(+ - * /这4中运算符号)

DoubleStack API:

================

Constructor:

------------

DoubleStack (int maxLength)

Mutators:

---------

void push(double elt) - pushes elt onto the top of the stack

double pop() - removes and returns the topmost element of the stack

Instance Methods:

------------------

String toString() - returns a String that formats the contents of the stack */

public class DoubleStack {

// you will need to add instance variables

public DoubleStack (int maxLength) {

// you need to fill in this constructor

}

public void push(double elt) {

// you need to fill this in /* }

public double pop() {

// you need to fill this in

}

public String toString() {

// you need to fill this in

}

public class A2Program {

// you need to fill in the instance variables

// in particular you must define the legal arithmetic operators

// using a symbolic constant

public void start() {

double value;

for(String input = getExpression(); input.length() > 0; input = getExpression()) {

value = evaluate(input);

System.out.println("answer = " + value);

}

}

private String getExpression() {

// fill in this

}

private double evaluate(String input) {

// fill in this

}

// you may want to write some of your own instance methods to help

// in evaluating the arithmetic expressions

}

那个··

只需要从逆波兰表达式到结果的算法··

然后那个expression only can get from user.

然后那个提供的代码里面··

只可以加instance variables /instance methods``其他的variable/parametre 不可以改的

还有··主要目的是把stack运用道题目中

多谢多谢··

最佳答案

e6cb1a03ad541b3098697807b7bf1798.png

二级知识专家瑾色如弦

2021-02-01 10:19

下面的代码是用来计算表达式的,看看是不是你要的

public class OPNode {

char op;// 运算符号

int level;// 优先级

//设置优先级

public OPNode(String op) {

this.op = op.charAt(0);

if (op.equals("+") || op.equals("-")) {

this.level = 1;

} else if (op.equals("*") || op.equals("/")) {

this.level = 2;

} else if (op.equals("(")) {

this.level = -3;

} else {

this.level = -1;

}

}

}

//主类

import java.util.Deque;

import java.util.LinkedList;

import java.util.List;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class OPText {

public static void main(String[] args) {

String expression = "2+2+(8-2)/3";// 要计算的表达式

List list = new LinkedList();

//正则式

Pattern entryOfExpression = Pattern

.compile("[0-9]+(\\.[0-9]+)?|\\(|\\)|\\+|-|\\*|/");

Deque stack = new LinkedList();//栈

Matcher m = entryOfExpression.matcher(expression);

while (m.find()) {

//提取语素

String nodeString = expression.substring(m.start(), m.end());

if (nodeString.matches("[0-9].*")) {

list.add(Double.valueOf(nodeString));//如果是数字直接送入列表

} else {

OPNode opn = new OPNode(nodeString);//如果是运算符

int peekLevel = (stack.peek() == null) ? 0 : ((OPNode) stack

.peek()).level;

if (opn.level >=peekLevel) {

stack.push(opn);//新的运算符比旧的优先级别高则入栈

} else {

if (opn.level == -1) {

OPNode temp = (OPNode) stack.pop();

while (temp.level != -3) {//如果为"("则一直出栈一直到")"

list.add(temp);

System.out.println(nodeString);

temp = (OPNode) stack.pop();

}

} else if (opn.level == -3) {

stack.push(opn);

} else {//如果新运算符比栈顶运算符底则一直出栈

OPNode temp = (OPNode) stack.pop();

while (temp.level > opn.level) {

list.add(temp);

if (stack.isEmpty()) {

break;

}

temp = (OPNode) stack.pop();

}

stack.push(opn);

}

}

}

}

OPNode temp = null;

while (!stack.isEmpty()) {

temp = (OPNode) stack.pop();

list.add(temp);

}//后续表达式计算

stack.clear();

for (Object o : list) {

if (o instanceof Double) {

stack.push(o);//为数字入栈

} else {

double op2 = ((Double) stack.pop()).doubleValue();

double op1 = ((Double) stack.pop()).doubleValue();

switch (((OPNode) o).op) {

case '+':

stack.push(op1 + op2);

break;

case '-':

stack.push(op1 - op2);

break;

case '*':

stack.push(op1 * op2);

break;

case '/':

stack.push(op1 / op2);

break;

}

}

}

System.out.println("结果为:" + stack.pop());

}

}

呃,太晚了,没心思去改了

明天再说

全部回答

e6cb1a03ad541b3098697807b7bf1798.png

1楼娇而不傲的猫

2021-02-01 10:49

你好!

好复杂 没有学过JAVA语言 ~~~~~~

如果对你有帮助,望采纳。

我要举报

如以上问答内容为低俗/色情/暴力/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!

→点此我要举报以上信息!←

推荐资讯

大家都在看

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值