Java Postfix Expression Evaluator

时间:2016-04-29 12:19:05

标签: java

我真的需要帮助我的修复后表达式计算器。我真的不知道我编写的代码有什么问题,但是当我运行程序时,它只会打印出顶部的任何数字。例如,如果我输入" 7 2 +",则输出为2.如果我输入" 2 7 +",则输出为7.有人请指点我正确的方向如何解决这个问题?我认为(不确定)问题是我的程序无法正确检测操作数" +"和" *"。但是,我不知道为什么。

文件#1:

import java.io.*;
import java.util.*;

public class ProblemTwo {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a post value expression: ");
        String input = scan.nextLine();
        StringTokenizer st = new StringTokenizer(input);
        Stack hello = new Stack(st.countTokens());

        for (int i = 0; i <= st.countTokens(); i++) {
            String inputToken = st.nextToken();
            if (inputToken.trim().contains("+")) {
                int sum = Integer.parseInt(hello.pop() + Integer.parseInt(hello.pop()));
                System.out.println(sum);
                hello.push(Integer.toString(sum));
            }
            else if (inputToken.trim().contains("*")){
                int product = Integer.parseInt(hello.pop()) * Integer.parseInt(hello.pop());
                hello.push(Integer.toString(product));
            }
            else {
                hello.push(inputToken);
            }
        }
        System.out.println(hello.pop());
    }
}

文件#2:

public class Stack {

        private String[] stackArray;
        private int arraySize;
        private int top;

        public Stack(int capacity) {
            arraySize = capacity;
            stackArray = new String[arraySize];
            top = -1;
        }

        public void push(String i) {
            stackArray[++top] = i;
        }

        public String pop() {
            return stackArray[top--];
        }

        public boolean isEmpty() {
            return top == -1;
        }

        public boolean isFull() {
            return top == arraySize - 1;
        }
    }

3 个答案:

答案 0 :(得分:1)

问题是您在st.countTokens()循环中使用for但是对countTokens() 的每次后续调用都会返回此标记符{{1}的次数计数}方法可以被称为。来自StringTokenizer&#39的doc:

  

计算此令牌化程序的nextToken方法的次数   可以在生成异常之前调用它。目前的立场   没进展。

在开始循环之前使用另一个变量来捕获nextToken,或者更好地使用st.countTokens()来终止循环。像:

st.hasMoreTokens()

同时修改while (st.hasMoreElements()) { // same logic } 方法以返回pop()

答案 1 :(得分:0)

您的代码至少有两个问题: 你在for循环中没有取得所有的输入值,你的堆栈已经破坏了实现。 这是工作版。

文件#1:

bin/kafka-topics.sh --zookeeper 192.168.56.101:2181 --describe

Topic:__consumer_offsets    PartitionCount:50    ReplicationFactor:1    Configs:segment.bytes=104857600,cleanup.policy=compact,compression.type=uncompressed
Topic: __consumer_offsets    Partition: 0    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 1    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 2    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 3    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 4    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 5    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 6    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 7    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 8    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 9    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 10    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 11    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 12    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 13    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 14    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 15    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 16    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 17    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 18    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 19    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 20    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 21    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 22    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 23    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 24    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 25    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 26    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 27    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 28    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 29    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 30    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 31    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 32    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 33    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 34    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 35    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 36    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 37    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 38    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 39    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 40    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 41    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 42    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 43    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 44    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 45    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 46    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 47    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 48    Leader: 0    Replicas: 0    Isr: 0
Topic: __consumer_offsets    Partition: 49    Leader: 0    Replicas: 0    Isr: 0  Error while executing topic command next on empty iterator  java.util.NoSuchElementException: next on empty iterator
at scala.collection.Iterator$$anon$3.next(Iterator.scala:27)
at scala.collection.Iterator$$anon$3.next(Iterator.scala:25)
at scala.collection.IterableLike$class.head(IterableLike.scala:90)
at scala.collection.immutable.Map$EmptyMap$.head(Map.scala:88)
at kafka.admin.TopicCommand$$anonfun$describeTopic$1.apply(TopicCommand.scala:147)
at kafka.admin.TopicCommand$$anonfun$describeTopic$1.apply(TopicCommand.scala:137)
at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:60)
at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:47)
at kafka.admin.TopicCommand$.describeTopic(TopicCommand.scala:137)
at kafka.admin.TopicCommand$.main(TopicCommand.scala:58)
at kafka.admin.TopicCommand.main(TopicCommand.scala)

文件#2:

import java.io.*;
import java.util.*;

public class ProblemTwo {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a post value expression: ");
        String input = scan.nextLine();
        StringTokenizer st = new StringTokenizer(input);
        Stack hello = new Stack(st.countTokens());

        for (int i = 0; i <= st.countTokens()+1; i++) {
            String inputToken = st.nextToken();
            if (inputToken.trim().contains("+")) {
                int sum = Integer.parseInt(hello.pop()) + Integer.parseInt(hello.pop());
                System.out.println(sum);
                hello.push(Integer.toString(sum));
            }
            else if (inputToken.trim().contains("*")){
                int product = Integer.parseInt(hello.pop()) * Integer.parseInt(hello.pop());
                hello.push(Integer.toString(product));
            }
            else {
                hello.push(inputToken);
            }
        }
        System.out.println(hello.pop());
    }
} 

答案 2 :(得分:-1)

import java.util.Stack;
public class Postfix_Eva {
public static void main(String[] args)
{
    String exp="231*+9-";//giving input
    System.out.println(eva(exp));//calling eva method with argument exp(String)
}

static int eva(String e)
{
    Stack stk=new Stack();//creating object for Stack
    for(int i=0;i<e.length();i++)
    {
        char c=e.charAt(i); //retriving each character from String using .charAt() method
        if(Character.isDigit(c)) //checking whether the character is digit or not
        {
            //Casting is not done
            stk.push(c-'0');

        }
        else
        {
            int a=(int) stk.pop(); //casting popped value into int
            int b=(int) stk.pop();
            switch(c)
            {
                case '+': 
                stk.push(a+b); 
                break; 

                case '-': 
                stk.push(a-b); 
                break; 

                case '/': 
                stk.push(a/b); 
                break; 

                case '*': 
                stk.push(a*b); 
                break; 
            }
        }
    }
    return (int) stk.pop();//returning int casted value of stk.pop() 
  }
}