繁体   English   中英

后缀计算器Java

[英]Postfix Calculator Java

好的,我必须从文件中读取后缀表达式。 后缀表达式必须具有空格以分隔每个运算符或操作数。 到目前为止,只有在输入文件中的运算符或操作数之间没有空格时,我才能使用它。 (即如果文件有12+,我得到的结果是3.)为了做到这一点,我认为我需要对输入进行标记化,但我不确定如何。 这就是我到目前为止所拥有的。 感谢您的回复。

import java.util.*;
import java.io.*;
public class PostfixCalc{
public static void main (String [] args) throws Exception {
File file = new File("in.txt");
Scanner sc = new Scanner(file);
String input = sc.next();
Stack<Integer> calc = new Stack<Integer>();
while(sc.hasNext()){
for(int i = 0; i < input.length(); i++){
    char c = input.charAt(i);
    int x = 0;
    int y = 0;
    int r = 0;
    if(Character.isDigit(c)){
       int t = Character.getNumericValue(c);
        calc.push(t);
    }
    else if(c == '+'){
        x = calc.pop();
        y = calc.pop();
        r = x+y;
        calc.push(r);
    }
     else if(c == '-'){
        x = calc.pop();
        y = calc.pop();
        r = x-y;
        calc.push(r);
    }
     else if(c == '*'){
        x = calc.pop();
        y = calc.pop();
        r = x*y;
        calc.push(r);
    }
     else if(c == '/'){
        x = calc.pop();
        y = calc.pop();
        r = x/y;
        calc.push(r);
    }
}
 }
 int a = calc.pop();
System.out.println(a);
 }
 } 

您需要更改几件事,您可以一步一步地完成。

  1. 声明您的Stack包含Integer而不是Character
  2. 在读取输入的代码中,依赖于String而不是Character s。
  3. 使用Integer.parseInt()解析操作数。 这会将String s转换为Integer (实际上,它将它们转换为int ,但在你的情况下,这种差异并不重要。)
  4. 使用Scanner.useDelimiter()将扫描仪分隔符设置为\\s+ ,这将匹配任何空白字符的序列。

当然有无数其他方法来处理您的输入,但我试图让您了解如何更改现有代码以执行其需要执行的操作。

为了标记化,您可以使用String.split()将单个空格作为分隔符 ;

String[] inputs = input.split(" ");

这是一个完整的解决方案,我刚刚编写了一个使用基于单链表Stack实现来制作后缀计算器;

A - PostFixCalculator

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class PostFixCalculator {

    private static final String ADD = "+"; 
    private static final String SUB = "-";
    private static final String MUL = "*";
    private static final String DIV = "/";

    public void calculateFile(String fileName) throws IOException {
        BufferedReader br = null;
        StringBuilder sb = null;
        try {
            FileReader fileReader = new FileReader(fileName);
            br = new BufferedReader(fileReader);

            sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                line = br.readLine();
            }

            String input = sb.toString();
            System.out.println(input + " = " + calculate(input));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            br.close();
        }
    }

    private int calculate(String input) {
        SinglyLinkedListStack<Integer> stack = new SinglyLinkedListStack<>();

        String[] inputs = input.split(" ");

        return handleCalculation(stack, inputs);
    }

    private static int handleCalculation(SinglyLinkedListStack<Integer> stack, String[] el) {
        int operand1, operand2;

        for(int i = 0; i < el.length; i++) {
            if( el[i].equals(ADD) || el[i].equals(SUB) || el[i].equals(MUL) || el[i].equals(DIV) ) {
                operand2 = stack.pop();
                operand1 = stack.pop();
                switch(el[i]) {
                    case ADD: {
                        int local = operand1 + operand2;
                        stack.push(local);
                        break;
                    }

                    case SUB: {
                        int local = operand1 - operand2;
                        stack.push(local);
                        break;
                    }

                    case MUL: {
                        int local = operand1 * operand2;
                        stack.push(local);
                        break;
                    }

                    case DIV: {
                        int local = operand1 / operand2;
                        stack.push(local);
                        break;
                    }
                }
            } else {
                stack.push(Integer.parseInt(el[i]));
            }
        }

        return stack.pop();
    }

}

B - SinglyLinkedListStack

public class SinglyLinkedListStack<T> {

    private int size;
    private Node<T> head;

    public SinglyLinkedListStack() {
        head = null;
        size = 0;
    }

    public void push(T element) {
        if(head == null) {
            head = new Node(element);
        } else {
            Node<T> newNode = new Node(element);
            newNode.next = head;
            head = newNode;
        }

        size++;
    }

    public T pop() {
        if(head == null)
            return null;
        else {
            T topData = head.data;

            head = head.next;
            size--;

            return topData;
        }
    }

    public T top() {
        if(head != null)
            return head.data;
        else
            return null;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    private class Node<T> {
        private T data;
        private Node<T> next;

        public Node(T data) {
            this.data = data;
        }

    }

}

C - 演示

import java.io.IOException;

public class PostFixCalculatorDemo {
    public static void main(String[] args) throws IOException {
        PostFixCalculator calc = new PostFixCalculator();
        calc.calculateFile("postfix.txt");
    }
}

D - 示例输入文件:“postfix.txt”

6 5 2 3 + 8 * + 3 + * 

E - 演示输出

6 5 2 3 + 8 * + 3 + *  = 288

你不需要扫描仪

只需使用BufferedReader读取文件,然后使用它的方法readLine来获取该行

然后用

String tokens[] = line.split("\\s+?") 

并且您将获得“令牌”数组,可以在您的代码中处理。

要识别号码,您可以使用以下正则表达式:

Pattern isNumber = Pattern.compile("^\\d+?$")
if (isNumber.matcher(token).matches()) {
    push(Integer.parseInt(token));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM