检查两个相邻的字符串标记是否相等

时间:2014-01-10 05:27:36

标签: java arraylist stack stringtokenizer

我已经将代码粘贴在下面,这是一个非常基本的反向抛光记谱计算器。它可以从文本文件中获取输入,对其进行标记,并将元素添加到ArrayList中,完成计算并反馈答案。我设法通过确保以运营商结束来处理太少的运营商。现在我需要找出如何处理太多的运算符,并且基本上方程具有太多运算的唯一方法是它们中的两个是否彼此相邻。那么,我的问题是如何检查彼此之间是否有两个/ * + -

package project5;
// Import
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.List;
import java.util.ArrayList;

public class Project5 extends IOException {
    public static void main(String[] args) throws FileNotFoundException, IOException{
        // Open and read input file
        String fileName = "input.txt";

        BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
        String line = bufferedReader.readLine();

        while(line != null)
        {
            System.out.println(compute(line)); // Compute and display answer

            line = bufferedReader.readLine();
        }        
        bufferedReader.close();
    }
    public static String compute(String input) {
        // Make new arraylist
        List<String> processedList = new ArrayList<>();
        // make sure there's content in the input file
        if (!input.isEmpty()) {
            StringTokenizer st = new StringTokenizer(input);
            while (st.hasMoreTokens()) {
                processedList.add(st.nextToken());
            }
        } else {
            return "Error, no input!"; // else return "Error, no input"
        }
        // Make a new stack
        Stack<String> tempList = new Stack<>();
        // Make a new iterator
        Iterator<String> iter = processedList.iterator();
        // Make sure there are enough operators
        if(processedList.get(processedList.size()-1).equals("+") ||
            processedList.get(processedList.size()-1).equals("-") ||
            processedList.get(processedList.size()-1).equals("/") ||
            processedList.get(processedList.size()-1).equals("*")) {
            // Continue while there's more calculations
            while (iter.hasNext()) {
                String temp = iter.next();
                // Check if content is correctly formatted
                if (temp.matches("[0-9]*")) {
                    tempList.push(temp);
                } else if (temp.matches("[*-/+]")) {
                    // perform appropriate operations
                    if (temp.equals("*")) {
                        int rs = Integer.parseInt(tempList.pop());
                        int ls = Integer.parseInt(tempList.pop());
                        int result = ls * rs;
                        tempList.push("" + result);
                    } else if (temp.equals("-")) {
                        int rs = Integer.parseInt(tempList.pop());
                        int ls = Integer.parseInt(tempList.pop());
                        int result = ls - rs;
                        tempList.push("" + result);
                    } else if (temp.equals("/")) {
                        int rs = Integer.parseInt(tempList.pop());
                        int ls = Integer.parseInt(tempList.pop());
                        int result = ls / rs;
                        tempList.push("" + result);
                    } else if (temp.equals("+")) {
                        int rs = Integer.parseInt(tempList.pop());
                        int ls = Integer.parseInt(tempList.pop());
                        int result = ls + rs;
                        tempList.push("" + result);
                    }
                } else {
                    return "Error"; // return "error" if inappropriately formatted
                }
            }
        } else {
            System.out.println("Check your formatting\n");
        }
        return tempList.pop(); // pop the item off the stack
    }
}

0 个答案:

没有答案