Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 后缀评估3_Java - Fatal编程技术网

Java 后缀评估3

Java 后缀评估3,java,Java,我已经为这个问题工作了好几天了,我已经结束了。对于0到9之间的输入数字,代码运行良好。但当输入大于9时,例如2872*13+2067*+它不会打印正确的答案。我不知道如何实现代码工作。 我不能改变主要功能。我只能在静态int evaluatePostfixchar[]izraz,int n的内部进行更改 注意:我修好了 import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.NoS

我已经为这个问题工作了好几天了,我已经结束了。对于0到9之间的输入数字,代码运行良好。但当输入大于9时,例如2872*13+2067*+它不会打印正确的答案。我不知道如何实现代码工作。 我不能改变主要功能。我只能在静态int evaluatePostfixchar[]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();

    }

}

请尝试以下字符串,而不是字符:

替换此代码:

    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';
与:


这个语句在做什么Character.isDigitizaz[i+1]{}?很抱歉,我尝试了一些代码中没有的东西。它给出了这个错误:PostFixEvaluation.java:111:error:method push-in类ArrayStack无法应用于给定的类型;e、 pushnumber.toString;^必需:找到字符:字符串原因:无法通过方法调用转换将实际参数字符串转换为字符非常感谢您我忘记了ArrayStack e=new ArrayStack N;现在我修好了,它工作了。
    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());