[英]solve equations with unknown variables in it in java
我需要一個以String形式接收方程的函數,例如:“a * b + 5 * a + 5 + a + 6”並簡化它,例如:“a * b + 6 * a + 11" 。 但我不能讓自己的班級工作,我找不到它的庫。 我希望有人幫助我
我不相信您有什么標准庫。 但是,這里有一些指針:
1)有效性檢查:刪除所有空格(循環遍歷每個字符,如果不是空格則將其添加到字符串中)確保所有值都是*或+或[letter]或[number]
2)計算變量:創建一個hashmap,其鍵是變量,值是循環字符串的字母數之和。
3)基於乘法計算變量(例如6 * a)。 循環查看變量是否如下:[number] [variable]或[variable] [number]。 添加數字-1(在步驟2中已經計算過一次)。
4)計算常量。 除為[number] + [number]外,與3相同。
5)打印出來以便您覺得合適。
我稍微改變了模式。 此外,為使程序正常運行,變量必須為小寫字母和單個字符。 盡管除法和減法與大多數代碼非常相似,但尚未實現。 最后,系統expesct整數只能正常運行。
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();
}
}
使用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());
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.