我已经为这个问题工作了几天而且我最后都在工作。代码适用于0到9之间的输入数字。但是当输入大于9时,例如28 72 * 13 + 20 67 * +,它不会打印正确的答案。我不知道要编写代码来实现什么。 我不能改变主要功能。我只能在static int evaluatePostfix(char [] izraz,int n)
内部进行更改注意:我修好了。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.NoSuchElementException;
interface Stack<E> {
// The elements of the Stack are any kind of objects
// Access methods:
public boolean isEmpty ();
// Returns true only if the stack is empty.
public E peek ();
public void clear ();
// Clears the stack.
public void push (E x);
// Adds x on the top of the stack.
public E pop ();
// Removes and returns the element on the top.
}
class ArrayStack<E> implements Stack<E> {
private E[] elems;
private int depth;
@SuppressWarnings("unchecked")
public ArrayStack (int maxDepth) {
// Creating new empty stack
elems = (E[]) new Object[maxDepth];
depth = 0;
}
public boolean isEmpty () {
// Returns true only if the stack is empty.
return (depth == 0);
}
public E peek () {
// Returns the element on the top od the stack.
if (depth == 0)
throw new NoSuchElementException();
return elems[depth-1];
}
public void clear () {
// Clears the stack.
for (int i = 0; i < depth; i++) elems[i] = null;
depth = 0;
}
public void push (E x) {
// Adds x on the top of the stack.
elems[depth++] = x;
}
public E pop ()
{
// Removes and returns the element on the top.
if (depth == 0)
throw new NoSuchElementException();
E topmost = elems[--depth];
elems[depth] = null;
return topmost;
}
}
public class PostFixEvaluation
{
static int evaluate(int op1, int op2, char ch)
{
switch (ch) {
case '*': return op2 * op1;
case '/': return op2 / op1;
case '+': return op2 + op1;
case '-': return op2 - op1;
default : return 0;
}
}
static int evaluatePostfix(char [] izraz, int n)
{
ArrayStack<String> e = new ArrayStack<String>(n);
char ch=0;
int op1,op2,result=0;
int i=0;
while(i<n)
{
if(izraz[i]=='+' || izraz[i]=='-' || izraz[i]=='/' || izraz[i]=='*')
{
ch=izraz[i];
op1 =Integer.parseInt(e.pop());
op2 =Integer.parseInt(e.pop());
result=evaluate(op1,op2,ch);
e.push(Integer.toString(result));
}
else
{
final StringBuilder number= new StringBuilder();
if(Character.isDigit(izraz[i]))
{
while (izraz[i] != ' ')
{
number.append(izraz[i]);
i++;
}
e.push(number.toString());
continue;
}
}
i++;
}
return result;
}
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String expression = br.readLine();
char exp[] = expression.toCharArray();
int rez = evaluatePostfix(exp, exp.length);
System.out.println(rez);
br.close();
}
}
答案 0 :(得分:0)
而不是Character,请尝试使用以下字符串:
替换此代码:
ArrayStack<Character> e = new ArrayStack(n);
char ch=0,res=0,a,b;
int op1,op2,result=0,c=0;
int z;
for(int i=0; i<n; i++)
{
if(Character.isDigit(izraz[i]))
{
ch=izraz[i];
e.push(ch);
}
if(izraz[i]=='+' || izraz[i]=='-' || izraz[i]=='/' || izraz[i]=='*')
{
ch=izraz[i];
op1 =e.pop()-'0';
//System.out.print(op1+" ");
op2 =e.pop()-'0';
使用:
ArrayStack<String> e = new ArrayStack<String>(n);
char ch=0;
int op1,op2,result=0;
for(int i=0; i<n; i++)
{
if(Character.isDigit(izraz[i]))
{
final StringBuilder number= new StringBuilder();
while (izraz[i] != ' ') {
number.append(izraz[i]);
i++;
}
e.push(number.toString());
continue;
}
if(izraz[i]=='+' || izraz[i]=='-' || izraz[i]=='/' || izraz[i]=='*')
{
ch=izraz[i];
op1 =Integer.parseInt(e.pop());
//System.out.print(op1+" ");
op2 =Integer.parseInt(e.pop());