我需要一个以字符串形式接受方程的函数,例如:"a *b+5*a+5+a+a+ 6“,并将其简化为"a *b+6*a +11”。但是我不能让我自己的课正常工作,我也找不到一个图书馆。我希望你能帮助我
发布于 2019-03-17 19:31:18
使用Symja图书馆,您可以像下面这样解决您的问题:
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());
}
}
}发布于 2019-03-14 18:11:12
我不相信你的要求有标准的图书馆。但是,这里有一些提示:
1)有效性检查:删除所有空格(循环遍历每个字符,如果不是空格,将其添加到字符串中),确保所有值都是*或+或字母或数字。
2)计数变量:创建一个hashmap,它的键是变量,值是通过循环遍历字符串的字母数之和。
3)基于乘积的计数变量(如6* a)。循环遍历,查看变量是否如下:数字变量或变量编号。添加数字-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();
}
}https://stackoverflow.com/questions/55169181
复制相似问题