用java解决带有未知变量的方程

时间:2019-03-14 17:59:07

标签: java math

我需要一个函数,该函数采用String形式的方程式, 例如:“ a * b + 5 * a + 5 + a + 6”,并将其简化为: “ a * b + 6 * a +11”。 但是我不能自己上课,也找不到图书馆。 我希望你能帮助我

2 个答案:

答案 0 :(得分:0)

我不相信您有什么标准库。但是,这里有一些指针:

1)有效性检查:        删除所有空格(循环遍历每个字符,如果没有空格则将其添加到字符串中)        确保所有值为*或+或[字母]或[数字]

2)计算变量:        通过遍历字符串,创建一个哈希表,其键为变量,值是字母数的总和。

3)基于乘法的变量计数(例如6 * a)。循环浏览,查看变量是否如下:[number] [variable]或[variable] [number]。添加数字-1(在第2步中已经计算过一次。

4)计数常数。与3相同,只是[number] + [number]。

5)打印出来,以使您觉得合适。

我稍微改变了图案。此外,为使程序正常运行,变量必须为小写字母和单个字符。尽管除法和减法与大多数代码非常相似,但尚未实现。最后,系统只对整数进行运算才能正常工作。

package com.Bif.MathCondenser;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class MathCondenser {
    public static void main(String[] args) {
        //input equations
        String equation = "a * b + 10 * 3 + 50 * a + c * 3 * b * a * 4 * 3";
        String equation_temp = "";

        //constant character lists
        ArrayList<String> alphabet = new ArrayList<String>();
        ArrayList<String> numbers = new ArrayList<String>();
        ArrayList<String> functions = new ArrayList<String>();

        //equation separated into a list of products
        ArrayList<String> products = new ArrayList<String>();

        //variable definitions of each product
        ArrayList<String> definitions = new ArrayList<String>();
        ArrayList<String> definitionsDuplicate = new ArrayList<String>();

        //collected constants for each product
        ArrayList<String> multipliers = new ArrayList<String>();

        //reduced terms for the equation
        ArrayList<String> terms = new ArrayList<String>();

        //fill alphabet arrayList
        for(char character = 'a'; character <= 'z'; ++character){
            alphabet.add(character-'a', character + "");
        }

        //fill numbers arrayList
        for(int k = 0; k < 10; k++) {
            numbers.add("" + k);
        }

        //fill functions arrayList
        functions.add("*");
        functions.add("+");

        //remove spaces from equation
        for(int k = 0; k < equation.length(); k++) {
            if(equation.charAt(k) == ' ')
                continue;
            equation_temp += equation.charAt(k);
        }
        equation = equation_temp;
        System.out.println("Equation: " + equation);

        //validate allowed characters; exit if not a-z, 0-9, or *,+
        for(int k = 0; k < equation.length(); k++) {
            if(!alphabet.contains(equation.charAt(k) + "") && !numbers.contains(equation.charAt(k) + "") && !functions.contains(equation.charAt(k) + "")) {
                System.out.println("Valid Characters: false, exiting");
                System.exit(0);
            }
        }
        System.out.println("Valid Characters: true, continuing");

        //parse the equation into sets of products
        Scanner scan = new Scanner(equation);
        scan.useDelimiter("\\+");
        while(scan.hasNext()) {
            products.add(scan.next());
        }
        System.out.println("Product set:" + products);

        //fill definition such that (2 * b * a * 3 * c) -> (abc)
        String productAtLocationK;
        for(int k = 0; k < products.size(); k++) {
            productAtLocationK = products.get(k);
            String definition = "";
            for(int j = 0; j < productAtLocationK.length(); j++) {
                //if it is a letter add it to definition
                if(alphabet.contains(productAtLocationK.charAt(j) + "")) {
                    definition += productAtLocationK.charAt(j);
                }
            }

            //alphabetizes definition
            char[] tempDef = definition.toCharArray();
            Arrays.sort(tempDef);
            definitions.add(new String(tempDef));
            definitionsDuplicate.add(new String(tempDef));
        }

        System.out.println("Definition set: " + definitions);       

        //fill multiplier set such that (2 * b * a * 3 * c) -> (6)
        for(int k = 0; k < products.size(); k++) {          
            //get the product; default multiplier = 1; character at Location in product;
            productAtLocationK = products.get(k);
            int multiplier = 1;
            String letterInProduct;

            //set up scanner for every product in products array with * separator
            scan = new Scanner(productAtLocationK);
            scan.useDelimiter("\\*");

            //loop through product, if (not letter -> number) then update multiplier
            while(scan.hasNext()) {
                letterInProduct = scan.next();
                if(!alphabet.contains(letterInProduct)) {
                    multiplier *= Integer.parseInt(letterInProduct);
                }   
            }
            multipliers.add(multiplier + "");
        }
        System.out.println("Multiplier set: " + multipliers);

        //combine duplicate definitions
        int indexOfMultiplier = 0;
        while(!definitionsDuplicate.isEmpty()) {
            //sum of the constant and its duplicates to combine terms
            int constantSum = Integer.parseInt(multipliers.get(indexOfMultiplier++));
            String definition = definitionsDuplicate.remove(0);

            //check for duplicates, add them to sum
            while(definitionsDuplicate.contains(definition)) {
                constantSum += Integer.parseInt(multipliers.get(definitionsDuplicate.indexOf(definition)));
                definitionsDuplicate.remove(definitionsDuplicate.indexOf(definition));
            }

            //ignore constant if 1
            if(constantSum != 1)
                terms.add(constantSum + definition);
            else
                terms.add(definition);
        }
        System.out.println("Terms Set: " + terms);

        //Format equation
        String reducedEquation = "";
        for(String term : terms) {
            reducedEquation += term + " + ";
        }
        if(reducedEquation.length() > 1) {
            reducedEquation = reducedEquation.substring(0, reducedEquation.length() - 2);
        }
        System.out.println("Reduced Equation: " + reducedEquation);

        //cleanup
        scan.close();
    }

}

答案 1 :(得分:0)

使用Symja library,您可以按照以下代码段解决问题:

package org.matheclipse.core.examples;

import org.matheclipse.core.eval.ExprEvaluator;
import org.matheclipse.core.expression.F;
import org.matheclipse.core.interfaces.IExpr;
import org.matheclipse.parser.client.SyntaxError;
import org.matheclipse.parser.client.math.MathException;

public class SimplifySO55169181 {

    public static void main(String[] args) {
        try {
            ExprEvaluator util = new ExprEvaluator();
            IExpr result = util.eval("a * b + 5 * a + 5 + a + 6");
            // print: 11+6*a+a*b
            System.out.println(result.toString());
        } catch (SyntaxError e) {
            // catch Symja parser errors here
            System.out.println(e.getMessage());
        } catch (MathException me) {
            // catch Symja math errors here
            System.out.println(me.getMessage());
        }
    }
}