0

我如何从 txt 文件中读取方程并获取这些方程的系数。3.2x-5.6y=10在 txt 文件中,我需要3.2-5.6并且10用于制作图形 gui 程序。

我试过了bufferedreader,但我不能得到系数。

BufferedReader reader = null;
reader = new BufferedReader(new FileReader("input.txt"));
String line = reader.readLine();
4

3 回答 3

0

使用 Scanner 对象。您可以使用它使用 RegExp 或您想要的任何其他工具来解析一行。

于 2014-04-26T17:53:28.337 回答
0

这是解决方案。

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Test {

    /*
     * Constructor just for test
     */
    Test()
    {
        Equation equation = getMembers("-3.2x - 5.6y + 34 = -10");

        System.out.print("Left members: ");
        for(EquationMember m : equation.leftMembers)
            System.out.print(m + " ");

        System.out.print("\nRight members: ");
        for(EquationMember m : equation.rightMembers)
            System.out.print(m + " ");
    }

    /*
     * Main method
     */
    public static void main(String args[])
    {
        new Test();
    }

    /*
     * Transforms a strings representation of an equation into an Equation class
     */
    Equation getMembers(String equationString)
    {
        Equation equation = new Equation();
        String eq = equationString.replace(" ", "");
        char[] charArray = eq.toCharArray();

        boolean isLeft = true;
        String lastMember = "";

        for(int i = 0; i < charArray.length; i++)
        {   
            if(charArray[i] == '-' || charArray[i] == '+' || charArray[i] == '=')
            {
                if(lastMember.length() > 0)
                {
                    EquationMember m = new EquationMember();
                    m.constant = Float.parseFloat(match("([-+]?[\\d\\.]+)[A-Za-z]?", lastMember, 1));
                    m.variable = match("[\\d\\.]+([A-Za-z]?)", lastMember, 1);

                    if(isLeft)
                        equation.leftMembers.add(m);
                    else
                        equation.rightMembers.add(m);
                }
                lastMember = charArray[i] == '=' ? "" : String.valueOf(charArray[i]);
            }
            else
            {
                lastMember += charArray[i];
            }

            if(charArray[i] == '=')
            {
                isLeft = false;
            }
        }

        EquationMember m = new EquationMember();
        m.constant = Float.parseFloat(match("([-+]?[\\d\\.]+)[A-Za-z]?", lastMember, 1));
        m.variable = match("[\\d\\.]+([A-Za-z]?)", lastMember, 1);
        equation.rightMembers.add(m);

        return equation;
    }

    /*
     * Performs a regex match
     */
    String match(String regexp, String text, int group)
    {
        Pattern p1 = Pattern.compile(regexp);
        Matcher m1 = p1.matcher(text);

        if(m1.find())
            return m1.group(group);
        else
            return null;
    }

    /*
     * Class that represents a single member inside an equation
     */
    class EquationMember
    {
        float constant;
        String variable;

        public String toString()
        {
            return constant + "" + variable;
        }
    }

    /*
     * Class that represents an entire equation
     */
    class Equation
    {
        ArrayList<EquationMember> leftMembers;
        ArrayList<EquationMember> rightMembers;

        Equation()
        {
            leftMembers = new ArrayList<EquationMember>();
            rightMembers = new ArrayList<EquationMember>();
        }
    }

}
于 2014-04-26T19:18:02.727 回答
0

你提到split了,那么为什么不在任何不是数字的地方分割字符串呢?

final String s = "3.2x-5.6y=10";
String[] result = s.split("([^\\d\\.\\+\\-])+");
System.out.println(Arrays.toString(result));
// [3.2, -5.6, 10]

该正则表达式对于数字的一般情况不是最佳的(考虑 123,456 或无效输入 +24-.2),但可以根据要求进行修复。

这没有做的是将变量与系数匹配,但您说您只需要这些值。

于 2014-04-26T20:10:19.017 回答