使括号成为TAC

时间:2017-11-19 05:47:40

标签: java c++ math logic

所以我这里的代码在算术表达式中实现了三个地址代码。

class ThreeAddressCode {

private static final char[][] precedence = {
    {'/', '1'},
    {'*', '1'},
    {'+', '2'},
    {'-', '2'}
};
private static int precedenceOf(String t)
{
    char token = t.charAt(0);
    for (int i=0; i < precedence.length; i++)
    {
        if (token == precedence[i][0])
        {
            return Integer.parseInt(precedence[i][1]+"");
        }
    }
    return -1;
}

public static void main(String[] args) throws Exception
{
    int i, j, opc=0;
    char token;
    boolean processed[];
    String[][] operators = new String[10][2];
    String expr="", temp;
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("\nEnter an expression: ");
    expr = in.readLine();
    processed = new boolean[expr.length()];
    for (i=0; i < processed.length; i++)
    {
        processed[i] = false;
    }
    for (i=0; i < expr.length(); i++)
    {
        token = expr.charAt(i);
        for (j=0; j < precedence.length; j++)
        {
            if (token==precedence[j][0])
            {
                operators[opc][0] = token+"";
                operators[opc][1] = i+"";
                opc++;
                break;
            }
        }
    }
    System.out.println("\nOperators:\nOperator\tLocation");
    for (i=0; i < opc; i++)
    {
        System.out.println(operators[i][0] + "\t\t" + operators[i][1]);
    }
    //sort
    for (i=opc-1; i >= 0; i--)
    {
        for (j=0; j < i; j++)
        {
            if (precedenceOf(operators[j][0]) > precedenceOf(operators[j+1][0]))
            {
                temp = operators[j][0];
                operators[j][0] = operators[j+1][0];
                operators[j+1][0] = temp;
                temp = operators[j][1];
                operators[j][1] = operators[j+1][1];
                operators[j+1][1] = temp;
            }               
        }
    }
    System.out.println("\nOperators sorted in their precedence:\nOperator\tLocation");
    for (i=0; i < opc; i++)
    {
        System.out.println(operators[i][0] + "\t\t" + operators[i][1]);
    }
    System.out.println();
    for (i=0; i < opc; i++)
    {
        j = Integer.parseInt(operators[i][1]+"");
        String op1="", op2="";
        if (processed[j-1]==true)
        {
            if (precedenceOf(operators[i-1][0]) == precedenceOf(operators[i][0]))
            {
                op1 = "t"+i;
            }
            else
            {
                for (int x=0; x < opc; x++)
                {
                    if ((j-2) == Integer.parseInt(operators[x][1]))
                    {
                        op1 = "t"+(x+1)+"";
                    }
                }
            }
        }
        else
        {
            op1 = expr.charAt(j-1)+"";
        }
        if (processed[j+1]==true)
        {
            for (int x=0; x < opc; x++)
            {
                if ((j+2) == Integer.parseInt(operators[x][1]))
                {
                    op2 = "t"+(x+1)+"";
                }
            }
        }
        else
        {
            op2 = expr.charAt(j+1)+"";
        }
        System.out.println("t"+(i+1)+" = "+op1+operators[i][0]+op2);
        processed[j] = processed[j-1] = processed[j+1] = true;
    }
}
}

示例输出

输入:a * b / c

t1 = a * b

t2 = t1 / c

程序所做的是评估算术表达式并由操作员逐步显示它们。

您能帮助我在优先级中加入括号吗?并实现这样的输出

示例输出

输入:a *(b + c)

t1 = b + c

t2 = a * t2

现在,括号被视为操作数。

1 个答案:

答案 0 :(得分:0)

我没有使用你的任何代码。遗憾。

这是一个有趣的想法。我从未考虑过你会怎么做这样的事情。它并没有遵循“T”的所有最佳实践,但这个问题激发了我思考如何以一种基本的方式做到这一点。

你可以通过使用更多的Java框架来减少这些代码的使用,但是严格地尝试逻辑地解决这个问题是很愉快的。

此代码缺少大多数验证(即用户输入错误的表达式)

然而,它会检查是否有相同数量的打开和关闭括号。

最后,我不得不把事情搞砸,所以我没有扩展到带有嵌套括号的表达式。

实施例a *(b *(c / d)-e)&gt;&gt;此代码不处理此方案,并且必须进行增强以适应此情况。

否则,它应该给出一个很好的想法,你可以用一种方法来构建一个程序来完成括号。

我希望它有所帮助

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MathPriority {

    public static void main(String[] args) {

        String expression = "a * (b * c) + (d / e)"; //You can work out how you want input to com in
        List<String> priorityList = getPriorityList(expression);//Find parenthesis and sets priority.
        expression = expression.replace(" ", "").replace("(", "").replace(")", "");//Take out any spaces and parenthesis

        for (int i = 0; i < priorityList.size(); i++) {//Replaces the piece in parenthesis with var and outputs var           
            expression = expression.replace(priorityList.get(i), "t" + (i + 1));
            System.out.println("t" + (i + 1) + " = " + priorityList.get(i));
        }
        System.out.println("t" + (priorityList.size() + 1) + " = " + expression);//pushes final variable set
    }
    //You can use this to build a List of indexes of a character within a String
    public static List<Integer> getAllOccurencesOfChar(List<String> expression, String indexO) {      
        List<Integer> parIndexList = new ArrayList<>();
        for (int i = 0; i < expression.size(); i++) {
            if (expression.get(i).contains(indexO)) {
                if (!parIndexList.contains(i) && i > 0) {
                    parIndexList.add(i);
                }
            }
        }
        return parIndexList;
    }
    //Outputs a list of substrings. They can later be used to parse through the inital string
    public static List<String> getPriorityList(String expression) {

        List<String> priorityList = new ArrayList<>();
        expression = expression.replace(" ", "");
        String[] eParts = expression.split("");
        List<String> expressionParts = new ArrayList<>();//Get expression into single chars
        for (String e : eParts) {//If you change this to an Array.List, it will not work. This type of list is fixed in size
            expressionParts.add(e);
        }
        List<Integer> parIndexList = getAllOccurencesOfChar(expressionParts, "(");//find all open paranthesis
        List<Integer> rParIndexList = getAllOccurencesOfChar(expressionParts, ")");//find all close paranthesis

        if (parIndexList.size() != rParIndexList.size()) {
            System.out.println("Your Equation does not have an equal number of open and close parenthesis");
            System.exit(0);
        }

        //Work out the parenthesis
        int loopIterator = parIndexList.size();//This will change as we iterate
        for (int pars = loopIterator - 1; pars >= 0; pars--) {

            int start = parIndexList.get(pars); //Define a start
            int end = 0;                        //and End
            //int end = rParIndexList.get(pars);

            for (int contemplate = 0; contemplate < loopIterator; contemplate++) {//contemplate where given parenthesis starts and where its closing tag is
                if (parIndexList.get(pars) < rParIndexList.get(contemplate)) {
                    end = rParIndexList.get(contemplate);//find first occurence and set true end
                    break;//then stop
                }
            }

            String expre = "";
            for (int concat = start + 1; concat < end; concat++) {
                expre += expressionParts.get(concat);//put the priorityList's subExpression together
            }

            priorityList.add(expre);//add that subExpression to the list
            expressionParts.subList(start, end + 1).clear();//remove these expressionParts

            /*Re-establish where the parenthesis are, since we removed parts of the expression in the list*/
            parIndexList = getAllOccurencesOfChar(expressionParts, "(");//find all open paranthesis
            rParIndexList = getAllOccurencesOfChar(expressionParts, ")");//find all close paranthesis
            loopIterator = parIndexList.size();//resize the forLoop
        }

        return priorityList;

    }

    public static List<Integer> getStartEndPosition(String fullExpression, String subExpression) {
        List<Integer> sAndE = new ArrayList<>();

        String[] eParts = subExpression.split("");
        List<String> wordParts = new ArrayList<>();
        wordParts.addAll(Arrays.asList(eParts));
        /*Find multiples of same operand*/
        List<Integer> add = getAllOccurencesOfChar(wordParts, "+");
        List<Integer> subtract = getAllOccurencesOfChar(wordParts, "-");
        List<Integer> divide = getAllOccurencesOfChar(wordParts, "/");
        List<Integer> multiply = getAllOccurencesOfChar(wordParts, "*");

        /*Find single Operands*/
        int plus = subExpression.indexOf("+");
        int minus = subExpression.indexOf("-");
        int div = subExpression.indexOf("/");
        int mult = subExpression.indexOf("*");

        int multiOperands = plus + minus + div + mult;//See if multiples exist
        int startingPosition = 0;

        if (add.size() > 1 || subtract.size() > 1 || divide.size() > 1 || multiply.size() > 1
                || multiOperands > 0) {
            //expression has multiple opreands of different types
            String findStart = wordParts.get(0) + wordParts.get(1);
            String findEnd = wordParts.get(wordParts.size() - 2) + wordParts.get(wordParts.size() - 1);
            startingPosition = fullExpression.indexOf(findStart);
            sAndE.add(startingPosition);
            int endPosition = fullExpression.indexOf(findEnd);
            sAndE.add(endPosition);

        } else {

            startingPosition = fullExpression.indexOf(subExpression);
            sAndE.add(startingPosition);
            sAndE.add(startingPosition + subExpression.length());

        }

        return sAndE;
    }

}
  

String expression =“a *(b * c)+(d / e)”

<强>输出:

t1 = d/e
t2 = b*c
t3 = a*t2+t1