mathematical-expression 实现 数学表达式解析 Java 篇(最新版本)

在这里插入图片描述

mathematical-expression (MAE)

  • 切换至 中文文档

Community QQ group

访问链接进行交流信息的获取:https://diskmirror.lingyuzhao.top/DiskMirrorBackEnd/FsCrud/downLoad/18/Binary?fileName=Article/Image/-56202138/1734319937274.jpg

🫠 Important Notice

✅【commonly】 *
PS, please try to use version 1.3.1 and above as much as possible. This will help you use a more stable version and
fix all known bugs in 1.2.x
*

⚠️【important】 The content of

versions 1.3.7

and 1.4.0 is completely consistent, with the only
difference being the
change in the package module. ** Please note that we will refactor the package name
to io.github.beardedManZhao.mathematicalExpression in versions 1.4.0 and all subsequent versions to avoid
conflicting
package names in Java’s various dependencies~**

To avoid any compatibility issues caused by package updates, we have provided version 1.3.7. You can continue to use
the old package name, but we strongly recommend using the new version, as the new package name has been updated to ’
io.
github. beardedManZhao. mathematicalExpression’. If you have any questions or suggestions about changing the package
name or updating, please contact us in a timely manner!!

introduce

This framework is an effective tool for mathematical formula analysis. It can analyze mathematical formulas including
nested functions, including functions, and step accumulation of series. The return value is a numerical result object.
At the same time, it can also be used for comparison operations. When comparing again, the return value is a Boolean
result object.

  • Maven depends on coordinates

    You can directly use Maven to import this framework into the project and use it effectively


<dependencies><dependency><groupId>io.github.BeardedManZhao</groupId><artifactId>mathematical-expression</artifactId><version>1.4.7</version></dependency>
</dependencies>

You can also directly load “mathematical expression” into your framework through gradle, and use the following
dependencies.

dependencies {implementation 'io.github.BeardedManZhao:mathematical-expression:1.4.7'
}

Historical version

You can check in the https://github.com/BeardedManZhao/mathematical-expression/tree/main/update Detailed change reports
for all published versions were found in.

Why choose mathematical expression

Mathematical expression has advantages such as simplicity, speed, ease of use, and support for a wide range of
languages. It has almost the same API usage as the C Java Python version.

Easy to use API

Calling the library is very simple, and you can use the following code for calculation. Of course, if you don’t need to
check, you can also compress the calculation code below
into System.out.println(Mathematical_Expression.getInstance(Mathematical_Expression.bracketsCalculation2).calculation("(1+2)*3"));
Can effectively reduce code load!

import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.Calculation;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;public class MAIN {public static void main(String[] args) throws WrongFormat {final Calculation instance = Mathematical_Expression.getInstance(// Select the different computing components you want to use hereMathematical_Expression.bracketsCalculation2);// If you ensure the correctness of the expression, you can skip checkinginstance.check("(1+2)*3");System.out.println(instance.calculation("(1+2)*3"));}
}

Superb functionality, capable of handling numerous functions

Are you unfamiliar with programming? It’s simple, you can completely customize functions using mathematical expressions,
and we also have many built-in functions that are unified
in function.io.github.beardedManZhao.mathematicalExpression.core.calculationFunctionPackage
In class!!

// Import necessary classes and packages for mathematical calculations and functionsimport io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.Calculation;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.FunctionPackage;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.Functions;
import io.github.beardedManZhao.mathematicalExpression.core.container.CalculationResults;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;// Define a class 'MAIN' with a function that calculates the factorial of x plus 1
@Functions("f(x) = x! + 1")
public class MAIN {public static void main(String[] args) throws WrongFormat {// Import built-in functions like 'sum' for use// Register the math function libraryMathematical_Expression.register_function(FunctionPackage.MATH);// Alternatively, register custom functions, e.g., a function adding two numbersMathematical_Expression.register_function("fTwo(x, y) = x + y");// Register all annotated functions in the 'MAIN' class for useMathematical_Expression.register_function(MAIN.class);// Initialize the calculation componentfinal Calculation instance = Mathematical_Expression.getInstance(Mathematical_Expression.functionFormulaCalculation2);// Perform a simple check on the expressioninstance.check("1 + sum(1,2,3,4) + f(3) * fTwo(1, 2)");// Calculate the expression, which can include functionsfinal CalculationResults calculation = instance.calculation("1 + sum(1,2,3,4) + f(3) * fTwo(1, 2)");// Print the resultSystem.out.println(calculation.getResult());}
}

Not enough support for calculating symbols? Don’t worry, this library supports a wide variety of operators, and you can
see all the operators here!

Symbol NameSymbolic syntax (n represents operands)Supported versionsSymbolic significance
Addition operatorn + n1.0.0Add two operands
Subtraction operatorn - n1.0.0Subtracting two operands
Multiplication operatorn * n1.0.0Multiplying two operands
Division operatorn / n1.0.0Dividing two operands
Remainder operatorn % n1.0.0Perform remainder operation on two operands
Factorial operatorn!1.3.2Performing factorial operations on operands
Power operatorn ^ n1.3.5Exponentiation operands

You can also achieve calculation operations with precision and caching operations by adjusting settings!

import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.Calculation;
import io.github.beardedManZhao.mathematicalExpression.core.container.CalculationResults;import java.math.BigDecimal;/*** This is the main entry point for the application, demonstrating mathematical expression parsing and evaluation.*/
public class MAIN {public static void main(String[] args) {// Obtain an instance of the calculation component, which supports parentheses handling.final Calculation calculationInstance = Mathematical_Expression.getInstance(Mathematical_Expression.bracketsCalculation2);// Define a sample mathematical expression to evaluate.final String inputExpression = "0.3 * 3";// Enable caching to improve performance.Mathematical_Expression.Options.setUseCache(true);// Enable BigDecimal for more accurate results.Mathematical_Expression.Options.setUseBigDecimal(true);// Evaluate the expression and print the result.System.out.println(calculationInstance.calculation(inputExpression));// Disable BigDecimal for faster performance.Mathematical_Expression.Options.setUseBigDecimal(false);// Evaluate the expression and print the result.final CalculationResults calculation = calculationInstance.calculation(inputExpression);System.out.println(calculation);// Can extract different numerical objectsSystem.out.println("Can extract different numerical objects!");final double result = (double) calculation.getResult();final BigDecimal bigDecimalResult = calculation.getBigDecimalResult();System.out.println(result);System.out.println(bigDecimalResult);}
}

A wide variety of computing components

In mathematical expression, we provide various computing components, and you can choose different computing components
according to your needs to achieve different functions while maintaining the same API calling method.

import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.Calculation;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.Functions;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;// Prepare a mathematical function x factorial+1
@Functions("f(x) = x! + 1")
public class MAIN {public static void main(String[] args) throws WrongFormat {// Register and use the main annotated functionMathematical_Expression.register_function(MAIN.class);final Calculation instance = Mathematical_Expression.getInstance(// Select the function calculation component hereMathematical_Expression.functionFormulaCalculation2);// If you ensure the correctness of the expression, you can skip checkinginstance.check("f(1 + 2) - 3");System.out.println(instance.calculation("f(1 + 2) - 3"));/*----------------------------------*/// You can also use the quick calculation component to calculate the sum between intervals [1+2, 30]final Calculation instance1 = Mathematical_Expression.getInstance(// Select the quick sum calculation component here, and the API will be the same as aboveMathematical_Expression.fastSumOfIntervalsBrackets);instance1.check("1 + 2, 30");System.out.println(instance1.calculation("1 + 2, 30"));}
}

Ultra-high flexibility

The functions required in any step of it, as well as any calculated object, can be individually obtained for the desired
operation. For example, a mathematical function is a complex object, and after its compilation is successful, you can
directly obtain its function object, which is not limited to the following
Used in mathematical expression!

import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.Functions;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.ManyToOneNumberFunction;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;// Prepare a mathematical function x factorial+1
@Functions("f(x) = x! + 1")
public class MAIN {public static void main(String[] args) throws WrongFormat {// Register and use the main annotated functionMathematical_Expression.register_function(MAIN.class);// Extract f (x)=x+ We know that the name of the function object of 1 is ffinal ManyToOneNumberFunction f = Mathematical_Expression.getFunction("f");// Calculate using f alonefinal double run = f.run(3);System.out.println(run);}
}

Detailed execution records

In some calculation components, you can use the ‘explain’ function to calculate expressions. This function can fully
plot the calculation process of the calculation component as a log result object, which can be plotted as a graph. The
following are the supported components and usage examples.

Calculation component nameDoes it support explainWhen did support startRelated knowledge
io.github.beardedManZhao.mathematicalExpression.core.calculation.PrefixExpressionOperationyesv1.3.5click this
io.github.beardedManZhao.mathematicalExpression.core.calculation.BracketsCalculation2yesv1.3.5click this
io.github.beardedManZhao.mathematicalExpression.core.calculation.CumulativeCalculationyesv1.3.6click this
io.github.beardedManZhao.mathematicalExpression.core.calculation.FunctionFormulaCalculation2yesv1.3.6click this
Introducing a flowchart code generation library

You only need to import the dependency coordinates below to automatically import the relevant components. This library
will help you draw a flowchart of the calculation process of computational components.

<!-- If your mathematicalExpression version is greater than 1.4.2, there is no need to introduce this dependency! -->
<dependency><groupId>io.github.BeardedManZhao</groupId><artifactId>varFormatter</artifactId><version>1.0.4</version>
</dependency>
开始进行生成

After importing the library, we can generate a flowchart as shown below.

import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.Calculation;
import io.github.beardedManZhao.mathematicalExpression.core.container.LogResults;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;
import top.lingyuzhao.varFormatter.core.VarFormatter;/*** This is the main entry point for the application, demonstrating mathematical expression parsing and evaluation.*/
public class MAIN {public static void main(String[] args) throws WrongFormat {// Obtain an instance of the calculation component, which supports parentheses handling.final Calculation calculationInstance = Mathematical_Expression.getInstance(Mathematical_Expression.bracketsCalculation2);// Define a sample mathematical expression to evaluate.final String inputExpression = "1 + 2 ^ (2 + (10 - 7)) * 3 + 2";// Check the input expression for correct formatting.calculationInstance.check(inputExpression);// Explain the execution process, which returns a log object containing the result.final LogResults explanation = calculationInstance.explain(inputExpression, true);System.out.println("计算结果: " + explanation.getResult());// The LogResults object is primarily used for visualizing the execution flow.// Disable name joining when outputting the visualization, as multiple variables need to be associated.explanation.setNameJoin(false);// Format the visualization using the VarFormatter in MERMAID syntax.System.out.println("graph LR");System.out.println(explanation.explain(VarFormatter.MERMAID));}
}

The result after running the program is as follows.

计算结果:99.0
graph LR
f_-1523352178("1 + 2 ^ (2 + (10 - 7)) * 3 + 2")
f_-1523352178==Map>Map==>f_1563255009
f_1563255009("2 + (10 - 7)")
f_1563255009==Map>Map==>f_1448155011
f_1448155011("10 - 7")
f_1448155011==Map>Map==>f_1507337
f_1507337("10-7+0")
f_1507337==Map>Map==>f_1507337_优先f_1507337_优先==Map>Map==>f_1571371271_计算
f_1571371271("10.0 - 7.0")
f_1571371271_计算==Map>String/Number==>f_1571371271
f_1571371271--Map>value-->f_1571371271v{"3.0"}
f_1571371271_计算==Map>Map==>f_1507337_最终f_1507337_最终==Map>Map==>f_1481348562_计算
f_1481348562("3.0 + 0.0")
f_1481348562_计算==Map>String/Number==>f_1481348562
f_1481348562--Map>value-->f_1481348562v{"3.0"}
f_1563255009==Map>Map==>f_47507548
f_47507548("2+3.0+0")
f_47507548==Map>Map==>f_47507548_优先f_47507548_优先==Map>Map==>f_-1006161388_计算
f_-1006161388("2.0 + 3.0")
f_-1006161388_计算==Map>String/Number==>f_-1006161388
f_-1006161388--Map>value-->f_-1006161388v{"5.0"}
f_-1006161388_计算==Map>Map==>f_47507548_最终f_47507548_最终==Map>Map==>f_-2133560364_计算
f_-2133560364("5.0 + 0.0")
f_-2133560364_计算==Map>String/Number==>f_-2133560364
f_-2133560364--Map>value-->f_-2133560364v{"5.0"}
f_-1523352178==Map>Map==>f_-418786079
f_-418786079("1+2^5.0*3+2+0")
f_-418786079==Map>Map==>f_-418786079_优先f_-418786079_优先==Map>Map==>f_-959059895_计算
f_-959059895("2.0 ^ 5.0")
f_-959059895_计算==Map>String/Number==>f_-959059895
f_-959059895--Map>value-->f_-959059895v{"32.0"}
f_-959059895_计算==Map>Map==>f_1855628224_计算
f_1855628224("32.0 * 3.0")
f_1855628224_计算==Map>String/Number==>f_1855628224
f_1855628224--Map>value-->f_1855628224v{"96.0"}
f_1855628224_计算==Map>Map==>f_2037586494_计算
f_2037586494("96.0 + 2.0")
f_2037586494_计算==Map>String/Number==>f_2037586494
f_2037586494--Map>value-->f_2037586494v{"98.0"}
f_2037586494_计算==Map>Map==>f_-418786079_最终f_-418786079_最终==Map>Map==>f_-929530109_计算
f_-929530109("1.0 + 98.0")
f_-929530109_计算==Map>String/Number==>f_-929530109
f_-929530109--Map>value-->f_-929530109v{"99.0"}
f_-929530109_计算==Map>String/Number==>result
result--Map>value-->resultv{"99.0"}

After the program runs, there is a graph code for ‘mermaid’ in the result, which we will display below for everyone to
watch!

After version 1.4.6, the visualization effect of ‘explain’ has been optimized. Here is the visualization effect of the
new version, which you can see in
the update record

Map>Map
Map>Map
Map>Map
Map>Map
Map>Map
Map>String/Number
Map>value
Map>Map
Map>Map
Map>String/Number
Map>value
Map>Map
Map>Map
Map>Map
Map>String/Number
Map>value
Map>Map
Map>Map
Map>String/Number
Map>value
Map>Map
Map>Map
Map>Map
Map>String/Number
Map>value
Map>Map
Map>String/Number
Map>value
Map>Map
Map>String/Number
Map>value
Map>Map
Map>Map
Map>String/Number
Map>value
Map>String/Number
Map>value
1 + 2 ^ (2 + (10 - 7)) * 3 + 2
2 + (10 - 7)
10 - 7
10-7+0
f_1507337_优先
f_1571371271_计算
10.0 - 7.0
3.0
f_1507337_最终
f_1481348562_计算
3.0 + 0.0
3.0
2+3.0+0
f_47507548_优先
f_-1006161388_计算
2.0 + 3.0
5.0
f_47507548_最终
f_-2133560364_计算
5.0 + 0.0
5.0
1+2^5.0*3+2+0
f_-418786079_优先
f_-959059895_计算
2.0 ^ 5.0
32.0
f_1855628224_计算
32.0 * 3.0
96.0
f_2037586494_计算
96.0 + 2.0
98.0
f_-418786079_最终
f_-929530109_计算
1.0 + 98.0
99.0
result
99.0

Framework

Obtain and calculate the calculation components directly through the mathematical-expression library

import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.Calculation;
import io.github.beardedManZhao.mathematicalExpression.core.container.CalculationResults;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;public class MAIN {public static void main(String[] args) throws WrongFormat {// Build two expressions to be evaluatedString s1 = "1 + 20 - 2 + 4", s2 = "1 + 20 - (2 + 4)";// Obtain the calculation component for calculating the expression without parentheses through the libraryCalculation prefixExpressionOperation = Mathematical_Expression.getInstance(Mathematical_Expression.prefixExpressionOperation, "prefixExpressionOperation");// Obtain the calculation component for calculating bracketed expressions through the libraryCalculation bracketsCalculation2 = Mathematical_Expression.getInstance(Mathematical_Expression.bracketsCalculation2, "bracketsCalculation2");// Pass the first formula to the calculation component of an expression without parenthesesprefixExpressionOperation.check(s1);CalculationResults calculation1 = prefixExpressionOperation.calculation(s1);// Print the calculation result of the first expressionSystem.out.println("计算层数:" + calculation1.getResultLayers() + "\n计算结果:" + calculation1.getResult() +"\n计算来源:" + calculation1.getCalculationSourceName());// Pass the second formula to the calculation component of the parenthesis expressionbracketsCalculation2.check(s2);CalculationResults calculation2 = bracketsCalculation2.calculation(s2);// Print the calculation result of the second expressionSystem.out.println("计算层数:" + calculation2.getResultLayers() + "\n计算结果:" + calculation2.getResult() +"\n计算来源:" + calculation2.getCalculationSourceName());}
}
  • Running results

    By importing packages, the module objects of each computing component can be obtained, which can effectively reduce
    the amount of code imported into the package.

计算层数:1
计算结果:23.0
计算来源:prefixExpressionOperation
计算层数:2
计算结果:15.0
计算来源:bracketsCalculation2

Calculation Manager

  • Full class name:io.github.beardedManZhao.mathematicalExpression.core.manager.CalculationManagement

  • introduce:

    The manager is a component designed to use both singletons and dynamic objects. The existence of the manager enables
    each component to be obtained by name. Components with the same name have the same storage address in memory, avoiding
    the use of redundant components. At the same time, for components that need to use dynamic members, a new component
    can also be obtained by a new name.

  • API Usage Example

import io.github.beardedManZhao.mathematicalExpression.core.calculation.number.FunctionFormulaCalculation;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.number.PrefixExpressionOperation;
import io.github.beardedManZhao.mathematicalExpression.core.manager.CalculationManagement;public class MAIN {public static void main(String[] args) {// Instantiate three components// TODO getInstance will be automatically obtained from the manager. //  If it is not obtained, it will be created and registered, and then the data will be returned. //  This method can be used to create or access the managerPrefixExpressionOperation a = PrefixExpressionOperation.getInstance("a");FunctionFormulaCalculation b = FunctionFormulaCalculation.getInstance("b");PrefixExpressionOperation a1 = PrefixExpressionOperation.getInstance("a1");// Register a bracketed expression calculation component with the name:"a"CalculationManagement.register(a);// Register a function expression calculation component named:"b"CalculationManagement.register(b);// Register a bracketed expression calculation component with the name:"a1"CalculationManagement.register(a1);// Print whether the memory data of the component we instantiated is consistent with that obtained from the managerSystem.err.println(a + "  " + CalculationManagement.getCalculationByName("a"));System.err.println(b + "  " + CalculationManagement.getCalculationByName("b"));System.err.println(a1 + "  " + CalculationManagement.getCalculationByName("a1"));}
}
  • Running results

    The last three lines are the comparison of memory data. The instantiated components are the same as the components in
    the manager, but the components with different names are different.

[INFO][Calculation Management][24-05-15:11]] : +============================== Welcome to [mathematical expression] ==============================+
[INFO][Calculation Management][24-05-15:11]] : + 	Start time Wed May 15 11:29:32 CST 2024
[INFO][Calculation Management][24-05-15:11]] : + 	version: 1.37
[INFO][Calculation Management][24-05-15:11]] : + 	Calculation component manager initialized successfully
[INFO][Calculation Management][24-05-15:11]] : + 	For more information, see: https://github.com/BeardedManZhao/mathematical-expression.git
[INFO][Calculation Management][24-05-15:11]] : +--------------------------------------------------------------------------------------------------+
[INFO][Calculation Management][24-05-15:11]] : A computing component is registered prefixExpressionOperation
[INFO][Calculation Management][24-05-15:11]] : A computing component is registered PrefixExpressionOperation
[INFO][Calculation Management][24-05-15:11]] : A computing component is registered bracketsCalculation2
计算层数:1
计算结果:23.0
计算来源:prefixExpressionOperation
计算层数:1
计算结果:15.0
计算来源:PrefixExpressionOperation

Calculation component introduce

NotBracketedExpression

  • Full class name:io.github.beardedManZhao.mathematicalExpression.core.calculation.number.PrefixExpressionOperation

  • introduce

    This component is designed for a mathematical expression without parentheses, but with operations such as addition,
    subtraction, multiplication, division and remainder. This component can realize the function with priority
    calculation, in which the prefix expression is used to parse and calculate, and the operand and operator are stored on
    the stack together with the calculation priority comparison If the current priority is low, first operate the previous
    operand and operator with the current operand to form a new value, and then put it on the stack.

  • API Usage Example

    The operators supported by this component are: a+b a-b a*b a/b a%b a^b

import io.github.beardedManZhao.mathematicalExpression.core.calculation.number.PrefixExpressionOperation;
import io.github.beardedManZhao.mathematicalExpression.core.container.CalculationNumberResults;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;public class MAIN {public static void main(String[] args) throws WrongFormat {// Gets the calculation component of a function that evaluates an expression without parenthesesPrefixExpressionOperation prefixExpressionOperation = PrefixExpressionOperation.getInstance("p");// Create an expressionString s = "1 + 2 + 4 * 10 - 3";// Check the expression for errorsprefixExpressionOperation.check(s);// Start calculating resultsCalculationNumberResults calculation = prefixExpressionOperation.calculation(s);// Print result valueSystem.out.println("计算层数:" + calculation.getResultLayers() + "\n计算结果:" + calculation.getResult() +"\n计算来源:" + calculation.getCalculationSourceName());}
}
  • Running results

    In the API call, the Running results of the function are printed. It can be seen that the returned value calculated by
    the component is a result set object, in which a lot of information about the calculation results is stored.

计算层数:2
计算结果:40.0
计算来源:p

NestedParenthesisExpression

  • Full class name:io.github.beardedManZhao.mathematicalExpression.core.calculation.number.BracketsCalculation2

  • introduce:

    Nested parenthesis expression parsing component, which can parse and calculate the results of mathematical expressions
    with multiple parentheses, and parse and calculate the priority of nested parentheses. This component relies on “core.
    calculation. number. PrefixExpressionOperation”, and uses recursion to parse parentheses in this component, Then
    provide the innermost expression to “core. calculation. number. PrefixExpressionOperation” for calculation.

  • API Usage Example

    The operators supported by this component are:a+b a-b a*b a/b a%b a^b ( ) a!

import io.github.beardedManZhao.mathematicalExpression.core.calculation.number.BracketsCalculation2;
import io.github.beardedManZhao.mathematicalExpression.core.container.CalculationNumberResults;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;public class MAIN {public static void main(String[] args) throws WrongFormat {// Get a calculation component that evaluates nested parenthesis expressionsBracketsCalculation2 bracketsCalculation = BracketsCalculation2.getInstance("BracketsCalculation");// Create an expressionString s = "1 + 2 + 4 * (10 - 3)";// Check the expression for errorsbracketsCalculation.check(s);// Start calculating resultsCalculationNumberResults calculation = bracketsCalculation.calculation(s);// Print result valueSystem.out.println("计算层数:" + calculation.getResultLayers() + "\n计算结果:" + calculation.getResult() +"\n计算来源:" + calculation.getCalculationSourceName());}
}
  • Running results

    In the API call, the calculation result of the expression is printed. It can be seen that the return value of the
    component calculation is a numerical result object, in which a lot of information about the calculation result is
    stored.

计算层数:2
计算结果:31.0
计算来源:BracketsCalculation

Mathematical comparison expression

  • Full class name:io.github.beardedManZhao.mathematicalExpression.core.calculation.bool.BooleanCalculation2

  • introduce

    A component that uses the comparison operator to determine whether two parenthesis expressions are mutually valid. The
    return value is a Boolean result object. This component can compare the size of two numeric values, or the
    relationship between two expressions, depending on the component “core. calculation. bool. BooleanCalculation2”

  • API Usage Example

    The operators supported by this component are shown in the API


import io.github.beardedManZhao.mathematicalExpression.core.calculation.bool.BooleanCalculation2;
import io.github.beardedManZhao.mathematicalExpression.core.container.CalculationBooleanResults;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;public class MAIN {public static void main(String[] args) throws WrongFormat {// 获取一个计算数学比较表达式的组件BooleanCalculation2 booleanCalculation2 = BooleanCalculation2.getInstance("Bool");// 创建3个表达式String s1 = "1 + 2 + 4 * (10 - 3)";String s2 = "2 + 30 + (2 * 3) - 1";String s3 = "1 + 3 * 10";extracted(booleanCalculation2, s1 + " > " + s2);// falseextracted(booleanCalculation2, s1 + " < " + s2);// trueextracted(booleanCalculation2, s1 + " = " + s3);// trueextracted(booleanCalculation2, s1 + " == " + s3);// trueextracted(booleanCalculation2, s1 + " != " + s3);// falseextracted(booleanCalculation2, s1 + " <> " + s3);// falseextracted(booleanCalculation2, s1 + " <= " + s3);// trueextracted(booleanCalculation2, s1 + " >= " + s3);// trueextracted(booleanCalculation2, s1 + " != " + s2);// trueextracted(booleanCalculation2, s1 + " <> " + s2);// true}private static void extracted(BooleanCalculation2 booleanCalculation2, String s) throws WrongFormat {// Check the expression for errorsbooleanCalculation2.check(s);// Start calculating resultsCalculationBooleanResults calculation = booleanCalculation2.calculation(s);// Print result valueSystem.out.println("计算层数:" + calculation.getResultLayers() + "\t计算结果:" + calculation.getResult() +"\t计算来源:" + calculation.getCalculationSourceName());}
}
  • Running results
计算层数:2	计算结果:false	计算来源:Bool
计算层数:2	计算结果:true	计算来源:Bool
计算层数:2	计算结果:true	计算来源:Bool
计算层数:2	计算结果:true	计算来源:Bool
计算层数:2	计算结果:false	计算来源:Bool
计算层数:2	计算结果:false	计算来源:Bool
计算层数:2	计算结果:true	计算来源:Bool
计算层数:2	计算结果:true	计算来源:Bool
计算层数:2	计算结果:true	计算来源:Bool
计算层数:2	计算结果:true	计算来源:Bool

IntervalAccumulationExpression

  • Full class name:io.github.beardedManZhao.mathematicalExpression.core.calculation.number.CumulativeCalculation

  • introduce

    In mathematical expressions, there is often such a formula. The content of the formula is shown in the following
    figure. You can see the number sequence operations that need to be accumulated.

    Then, you can use the Full class name above to achieve the purpose you need.

    img_1

  • API Usage Example

    The syntax level is almost the same as that of other components. The calculation example of the mathematical
    expression written in the component is shown below. What is shown here is the calculation of an accumulative
    mathematical formula.


import io.github.beardedManZhao.mathematicalExpression.core.calculation.number.CumulativeCalculation;
import io.github.beardedManZhao.mathematicalExpression.core.container.CalculationNumberResults;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;public class MAIN {public static void main(String[] args) throws WrongFormat {// Get a component that calculates the cumulative mathematical expressionCumulativeCalculation cumulativeCalculation = CumulativeCalculation.getInstance("zhao");// Construct a mathematical expression. Here, "n [1,10,1]" is similar to the accumulation symbol in mathematics. N will increase continuously in this interval. Every increase will be brought into the formula for calculation// Wherein, the last 1 in [1,10,1] represents the increase step, which can realize the accumulation of different equal difference values in the intervalString s = "n[1,10,1] 2 * (n + 1)";// Check mathematical expressionscumulativeCalculation.check(s);// Calculation resultsCalculationNumberResults calculation = cumulativeCalculation.calculation(s);System.out.println("计算层数:" + calculation.getResultLayers() + "\t计算结果:" + calculation.getResult() +"\t计算来源:" + calculation.getCalculationSourceName());}
}
  • Running results
计算层数:21	计算结果:130.0	计算来源:zhao

FunctionOperationExpression

  • Full class name:io.github.beardedManZhao.mathematicalExpression.core.calculation.number.FunctionFormulaCalculation

  • introduce

    The framework also supports the operation of some functions. You can use the above classes to write mathematical
    expressions that require functions. It should be noted that all functions used in expressions need to be logically
    registered in “Calculation Management” so that functions can be accessed during calculation

  • API Usage Example


import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.ManyToOneNumberFunction;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.number.FunctionFormulaCalculation;
import io.github.beardedManZhao.mathematicalExpression.core.container.CalculationNumberResults;
import io.github.beardedManZhao.mathematicalExpression.core.manager.CalculationManagement;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;public class MAIN {public static void main(String[] args) throws WrongFormat {// Instantiate a function named DoubleValue to multiply a value by 2ManyToOneNumberFunction myFunction = new ManyToOneNumberFunction("DoubleValue") {/*** 函数的运行逻辑实现** @param numbers 这里是函数的数据输入对象,由框架向这里传递数据输入参数* @return 这里是数据经过函数转换之后的数据*/@Overridepublic double run(double... numbers) {// Among the parameters here, the first parameter is the parameter passed in by FunctionFormulaCalculationreturn numbers[0] * 2;}};// Register function to managerCalculationManagement.register(myFunction);// Get a component that calculates the cumulative mathematical expressionFunctionFormulaCalculation functionFormulaCalculation = FunctionFormulaCalculation.getInstance("zhao");// Build a mathematical expression that uses the function DoubleValueString s = "2 * DoubleValue(2 + 3) + 1";// Check mathematical expressionsfunctionFormulaCalculation.check(s);// Calculation resultsCalculationNumberResults calculation = functionFormulaCalculation.calculation(s);System.out.println("计算层数:" + calculation.getResultLayers() + "\t计算结果:" + calculation.getResult() +"\t计算来源:" + calculation.getCalculationSourceName());}
}
  • Running results
[INFO][zhao][22-11-14:11]] : Find and prepare the startup function: DoubleValue
计算层数:1	计算结果:21.0	计算来源:BracketsCalculation2

Multi parameter function operation expression

  • Full class name: io.github.beardedManZhao.mathematicalExpression.core.calculation.number.FunctionFormulaCalculation2

  • 介绍

    For some expression calculations that use functions in expressions, the above class can be used for operations. It is
    an upgraded version of the “core. calculation. number. FunctionFormulaCalculation” class, which has appeared since
    version 1.1, is also an extended implementation of its subclass.

    Compared with the parent class, this component makes up for the deficiency that the parent class can only parse the
    function expression with one parameter. In this component, you can use many real parameters for function operations,
    such as sum (1,2,3)

    This type of function is a multi parameter function. Next, let’s look at the API usage example, in which the
    calculation and results of the multi parameter function expression are shown.


import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.ManyToOneNumberFunction;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.number.FunctionFormulaCalculation2;
import io.github.beardedManZhao.mathematicalExpression.core.container.CalculationNumberResults;
import io.github.beardedManZhao.mathematicalExpression.core.manager.CalculationManagement;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;public class MAIN {public static void main(String[] args) throws WrongFormat {// Implement a sum functionManyToOneNumberFunction manyToOneNumberFunction = new ManyToOneNumberFunction("sum") {@Overridepublic double run(double... numbers) {double res = 0;for (double number : numbers) {res += number;}return res;}};// Register this function to the administratorCalculationManagement.register(manyToOneNumberFunction);// Get the new version of function calculation componentFunctionFormulaCalculation2 functionFormulaCalculation2 = FunctionFormulaCalculation2.getInstance("zhao");// Build the formula we need to calculate// TODO The function sum parameter in this expression is not only one, but also a multi parameter functionString s = "2 * (200 - sum(1 + 10.1, 2, 3)) + sum(10, 20)";// Enabling the shared pool can speed up the calculation. The more complex the calculation formula is, the more significant the effect of the shared pool isfunctionFormulaCalculation2.setStartSharedPool(true);// Check the formula for errorsfunctionFormulaCalculation2.check(s);// Get the calculation resultsCalculationNumberResults calculation = functionFormulaCalculation2.calculation(s);System.out.println("计算层数:" + calculation.getResultLayers() + "\t计算结果:" + calculation.getResult() +"\t计算来源:" + calculation.getCalculationSourceName());}
}
  • 运行结果
计算层数:2	计算结果:397.8	计算来源:BracketsCalculation2
Mathematical function expression registration

You can directly use the mathematical expression of a function to register a function. The format of the function
expression is’ function name (parameter 1, parameter 2, parameter 3)=function logical expression, such as parameter
1+parameter 2 '. Below is a registration example

This registered function can also be directly used in mathematical expressions!


import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.manager.ConstantRegion;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;public class MAIN {public static void main(String[] args) throws WrongFormat {System.out.println(ConstantRegion.VERSION);// Start registering TODO. We have registered a function called mySum here, which takes in two parameters and outputs the sum of the two parametersif (Mathematical_Expression.register_function("mySum(a, b) = a + b")) {System.out.println("函数注册成功!");}}
}

Fast interval sum calculation component (based on parenthesis expression)

  • Full class name:io.github.beardedManZhao.mathematicalExpression.core.calculation.number.FastSumOfIntervalsBrackets

  • introduce

    The new product of version 1.15, the interval fast sum component, is a fast component that sums all elements of an
    interval with an equal difference of 1. It logically simulates an interval into a mathematical sequence and quickly
    sums it through the sum formula.

    This component implements the shared pool computing function. It will check, calculate, and record the results of the
    last time, which can speed up computing. The specific API calls are shown below.


import io.github.beardedManZhao.mathematicalExpression.core.calculation.number.FastSumOfIntervalsBrackets;
import io.github.beardedManZhao.mathematicalExpression.core.container.CalculationNumberResults;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;public class MAIN {public static void main(String[] args) throws WrongFormat {// Get the quick sum calculation component of the intervalFastSumOfIntervalsBrackets fast = FastSumOfIntervalsBrackets.getInstance("fast");// Build an expression to be calculated.// The following expression represents that the result of adding 11=(1+10) to 13=(20 - (5+2)) should be 36String s = "1 + 10, 20 - (5 + 2)";// Check the expression. The shared pool has been enabled by default since version 1.2! No need to set manually// fast.setStartSharedPool(true);fast.check(s);// After version 1.2, the accumulation component supports setting step parameters, and since version 1.2.1, it officially supports the summation of step intervalsfast.step = 2;// Start calculationCalculationNumberResults calculation = fast.calculation(s);// Print calculation resultsSystem.out.println("计算层数:" + calculation.getResultLayers() + "\t计算结果:" + calculation.getResult() +"\t计算来源:" + calculation.getCalculationSourceName());}
}
  • Running results

    From the above code, we can see that the formula for fast interval summation is composed of two parentheses separated
    by commas.

计算层数:3	计算结果:24.0	计算来源:fast

Fast interval cumulative calculation component (based on parenthesis expression)

  • Full class
    name:io.github.beardedManZhao.mathematicalExpression.core.calculation.number.FastMultiplyOfIntervalsBrackets

  • Introduction

    A new product of version 1.1.5, the interval fast accumulation component, is a fast component that accumulates all
    elements of an interval with an equal difference of n. It logically simulates an interval into a mathematical sequence
    and performs fast accumulation through the sum formula.

    This component implements the shared pool computing function. It will check, calculate, and record the results of the
    last time, which can speed up computing. The specific API calls are shown below.


import io.github.beardedManZhao.mathematicalExpression.core.calculation.number.FastMultiplyOfIntervalsBrackets;
import io.github.beardedManZhao.mathematicalExpression.core.container.CalculationNumberResults;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;public class MAIN {public static void main(String[] args) throws WrongFormat {// Get the quick Multiply calculation component of the intervalFastMultiplyOfIntervalsBrackets fast = FastMultiplyOfIntervalsBrackets.getInstance("fast");// Build an expression to be calculated.// The following expression indicates that the result of multiplying 11=(1+10) by 13=(20 - (5+2)) should be 143// The result should be 11 * 13=143String s = "1 + 10, 20 - (5 + 2)";// Check the expression. The shared pool has been enabled by default since version 1.2! No need to set manually// fast.setStartSharedPool(true);fast.check(s);// After version 1.2, the accumulation component supports setting step parameters, and since version 1.2.1, it officially supports the summation of step intervalsfast.step = 2;// Start calculationCalculationNumberResults calculation = fast.calculation(s);// Print calculation resultsSystem.out.println("计算层数:" + calculation.getResultLayers() + "\t计算结果:" + calculation.getResult() +"\t计算来源:" + calculation.getCalculationSourceName());}
}
  • Running results

    From the above code, we can see that the formula for fast interval summation is composed of two parentheses separated
    by commas

计算层数:3	计算结果:143.0	计算来源:fast

Complex calculation component

  • Full class name:io.github.beardedManZhao.mathematicalExpression.core.calculation.number.ComplexCalculation
  • Starting from version 1.4.5, we have implemented a calculation component for complex calculation expressions. It can
    calculate a mathematical expression such as (3 * 2-1)+2 * 3+f (10,5) i and return its object. We can use the
    returned object to perform a series of operations. Here is an example!
import io.github.beardedManZhao.algorithmStar.operands.ComplexNumber;
import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.Functions;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.number.ComplexCalculation;
import io.github.beardedManZhao.mathematicalExpression.core.container.CalculationComplexResults;
import io.github.beardedManZhao.mathematicalExpression.core.container.ComplexExpression;
import io.github.beardedManZhao.mathematicalExpression.core.container.FunctionExpression;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;@Functions({"f(x, y) = x - y"
})
public class MAIN {public static void main(String[] args) throws WrongFormat {Mathematical_Expression.register_function(MAIN.class);// 将一个复数编译为计算表达式对象final ComplexCalculation instance = (ComplexCalculation) Mathematical_Expression.getInstance(Mathematical_Expression.complexCalculation);final String s = "3 * 2 - 1 + 2*3 + f(10, 5)i";instance.check(s);final ComplexExpression compile = instance.compile(s, true);// 我们还可以直接获取到复数的实部 和 虚部的表达式对象!final FunctionExpression real = compile.getFunctionExpression1();final FunctionExpression imaginary = compile.getFunctionExpression2();System.out.println(real.getExpressionStr());System.out.println(imaginary.getExpressionStr());// 直接计算出复数的结果final CalculationComplexResults calculation = compile.calculationCache(false);// 查看结果System.out.println(compile);System.out.println(calculation);// 获取到复数对象final ComplexNumber complexNumber = calculation.toComplexNumber();// 直接 使用科学计算库 参与共轭计算final ComplexNumber conjugate = complexNumber.conjugate();System.out.println(conjugate);// 还可以参与加法等运算 在这里是 自己 + 自己final ComplexNumber add = complexNumber.add(conjugate);System.out.println(add);}
}
  • Running results
3*2
-1+2*3+f(10,5)
io.github.beardedManZhao.mathematicalExpression.core.container.ComplexExpression@2698dc7
6.0 - 10.0i
6.0 + 10.0i
12.0 + 0.0i

Advanced Operations

Registration and calculation of mathematical functions


import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.Calculation;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;public class MAIN {public static void main(String[] args) throws WrongFormat {// 将 f 函数注册进来Mathematical_Expression.register_function("f(x) = x * x");// 准备要计算的表达式final String data = "1 + f(20) + 3";// 获取到计算组件final Calculation instance = Mathematical_Expression.getInstance(Mathematical_Expression.functionFormulaCalculation2);// 检查与计算instance.check(data);System.out.println(instance.calculation(data));}
}
  • Running results
CalculationNumberResults{result=404.0, source='BracketsCalculation2'}

Registering anonymous implemented functions for computation


import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.Calculation;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.ManyToOneNumberFunction;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;public class MAIN {public static void main(String[] args) throws WrongFormat {// 将 f 函数注册进来Mathematical_Expression.register_function(new ManyToOneNumberFunction("f") {@Overridepublic double run(double... numbers) {return numbers[0] * numbers[0];}});// 准备要计算的表达式final String data = "1 + f(20) + 3";// 获取到计算组件final Calculation instance = Mathematical_Expression.getInstance(Mathematical_Expression.functionFormulaCalculation2);// 检查与计算instance.check(data);System.out.println(instance.calculation(data));}
}
  • Running results
CalculationNumberResults{result=404.0, source='BracketsCalculation2'}

Annotation based implementation of function registration and calculation

import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.Calculation;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.Functions;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;@Functions({// 这里是需要被注册的两个函数 在这里标记一下"f(x) = x * x","ff(x) = f(x) + 1"
})
public class MAIN {public static void main(String[] args) throws WrongFormat {// 将 MAIN 类中标记的所有函数注册if (Mathematical_Expression.register_function(MAIN.class)) {// 构建需要计算的表达式final String string = "1 + ff(1 + 2) * 2";// 获取到函数计算组件Calculation calculation = Mathematical_Expression.getInstance(Mathematical_Expression.functionFormulaCalculation2);// 开始进行计算calculation.check(string);System.out.println(calculation.calculation(string));}}
}

Here are the calculation results

CalculationNumberResults{result=21.0, source='functionFormulaCalculation2'}

Directly Acquiring Function Objects and Saving Function Objects to Files

All function objects can be extracted, and functions registered based on “mathematical expression” can be saved to
files. We can directly perform serialization operations to save them. Below, we will show some examples.

Extracting Function Objects for Individual Computation

import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.Functions;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.ManyToOneNumberFunction;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;@Functions({// 这里是需要被注册的两个函数 在这里标记一下 分别是 f 和 ff 两个函数"f(x) = x * x","ff(x) = f(x) + 1"
})
public class MAIN {public static void main(String[] args) throws WrongFormat {// 将 MAIN 类中标记的所有函数注册if (Mathematical_Expression.register_function(MAIN.class)) {// 获取到 ff 函数final ManyToOneNumberFunction ff = Mathematical_Expression.getFunction("ff");// 计算出结果final double run = ff.run(1024);System.out.println(run);}}
}

下面就是计算结果

1048577.0
Extracting Function Objects and Saving Them to Files

Note that if your saved function uses other functions, e.g., ff(x) = f(x) + 1, you need to save f(x) = x * x to the
file as well. The following demonstrates how to save a single function:


import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.ExpressionFunction;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.Functions;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.ManyToOneNumberFunction;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;@Functions({// 这里是需要被注册的两个函数 在这里标记一下 分别是 f 和 ff 两个函数"f(x) = x * x","ff(x) = f(x) + 1"
})
public class MAIN {public static void main(String[] args) throws WrongFormat, IOException {// 将 MAIN 类中标记的所有函数注册if (Mathematical_Expression.register_function(MAIN.class)) {// 获取到 ff 函数final ManyToOneNumberFunction ff = Mathematical_Expression.getFunction("f");// 保存到文件 TODO ExpressionFunction 的函数是可以被保存到文件中的try (final ObjectOutputStream objectOutput = new ObjectOutputStream(Files.newOutputStream(Paths.get("C:\\Users\\zhao\\Desktop\\fsdownload\\f.me")))) {// 将 ff 保存到数据流中((ExpressionFunction) ff).saveTo(objectOutput);}}}
}

In fact, you can also save multiple function objects to a single file, which helps resolve function dependency issues!


import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.ExpressionFunction;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.Functions;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.ManyToOneNumberFunction;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;import java.io.IOException;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;@Functions({// 这里是需要被注册的两个函数 在这里标记一下 分别是 f 和 ff 两个函数"f(x) = x * x","ff(x) = f(x) + 1"
})
public class MAIN {public static void main(String[] args) throws WrongFormat, IOException {// 将 MAIN 类中标记的所有函数注册if (Mathematical_Expression.register_function(MAIN.class)) {// 获取到 ff 函数 以及 f 函数final ManyToOneNumberFunction ff = Mathematical_Expression.getFunction("ff");final ManyToOneNumberFunction f = Mathematical_Expression.getFunction("f");// 保存到文件 TODO ExpressionFunction 的函数是可以被保存到文件中的try (final ObjectOutputStream objectOutput = new ObjectOutputStream(Files.newOutputStream(Paths.get("C:\\Users\\zhao\\Desktop\\fsdownload\\f.me")))) {// 将 ff 和 f 保存到数据流中((ExpressionFunction) ff).saveTo(objectOutput);((ExpressionFunction) f).saveTo(objectOutput);}}}
}

Loading Function Objects Saved in Files into the Library

Functions can be serialized and, consequently, deserialized. After deserialization, you can either register them back
into the library or use them directly!!!

package utils;import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import function.io.github.beardedManZhao.mathematicalExpression.core.calculationExpressionFunction;
import function.io.github.beardedManZhao.mathematicalExpression.core.calculationManyToOneNumberFunction;import java.io.IOException;
import java.io.ObjectInputStream;
import java.nio.file.Files;
import java.nio.file.Paths;public class MAIN {public static void main(String[] args) throws IOException {ManyToOneNumberFunction function;try (final ObjectInputStream objectInputStream = new ObjectInputStream(Files.newInputStream(Paths.get("C:\\Users\\zhao\\Desktop\\fsdownload\\f.me")))) {// 在这里读取到函数对象function = ExpressionFunction.readFrom(objectInputStream);}// 把函数注册回 Mathematical_ExpressionMathematical_Expression.register_function(function);// 也可以直接使用它final double run = function.run(1024);System.out.println(run);}
}

Deserialization also supports loading multiple function objects. Here’s an example:

package utils;import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import function.io.github.beardedManZhao.mathematicalExpression.core.calculationExpressionFunction;
import function.io.github.beardedManZhao.mathematicalExpression.core.calculationManyToOneNumberFunction;import java.io.IOException;
import java.io.ObjectInputStream;
import java.nio.file.Files;
import java.nio.file.Paths;public class MAIN {public static void main(String[] args) throws IOException {ManyToOneNumberFunction ff, f;try (final ObjectInputStream objectInputStream = new ObjectInputStream(Files.newInputStream(Paths.get("C:\\Users\\zhao\\Desktop\\fsdownload\\f.me")))) {// 在这里读取到函数对象(要注意这里和保存时的顺序一致哦!!)// 如果要是不确定顺序,可以读取直接读取出来注册到库里 库会自动将函数的名称解析出来ff = ExpressionFunction.readFrom(objectInputStream);f = ExpressionFunction.readFrom(objectInputStream);}// 把函数注册回 Mathematical_ExpressionMathematical_Expression.register_function(ff);Mathematical_Expression.register_function(f);// 也可以直接使用它final double run = ff.run(1024);System.out.println(run);}
}

Batch Serialization/Registration of Functions

Starting from version 1.3.4, you can directly serialize/register functions using data streams within the
Mathematical_Expression class, which simplifies the code. Here’s an example:


import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.Functions;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.ManyToOneNumberFunction;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;import java.io.FileOutputStream;
import java.io.IOException;@Functions({// 这里是需要被注册的两个函数 在这里标记一下 分别是 f 和 ff 以及 fff 几个函数"f(x) = x * x","ff(x) = f(x) + 1","fff(x) = x! + ff(x) + f(x)"
})
public class MAIN {public static void main(String[] args) throws IOException, WrongFormat {// 将函数注册一下Mathematical_Expression.register_function(MAIN.class);// 获取到 几个函数 的对象ManyToOneNumberFunction f = Mathematical_Expression.getFunction("f");ManyToOneNumberFunction ff = Mathematical_Expression.getFunction("ff");ManyToOneNumberFunction fff = Mathematical_Expression.getFunction("fff");// 直接将这几个函数输出到文件中try (final FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\zhao\\Desktop\\fsdownload\\f.ME")) {Mathematical_Expression.saveFunction(fileOutputStream, f, ff, fff);}}
}

Next, we can manually read the file into memory and use it, demonstrating the deserialization operation in
Mathematical_Expression.


import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.function.ManyToOneNumberFunction;
import io.github.beardedManZhao.mathematicalExpression.exceptional.WrongFormat;import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;public class MAIN {public static void main(String[] args) throws WrongFormat, IOException {// 将函数注册一下try (final FileInputStream fileInputStream = new FileInputStream("C:\\Users\\zhao\\Desktop\\fsdownload\\f.ME")) {// 直接在这里使用数据流来进行反序列化操作,这个数据流对应的文件包含的函数都会开始尝试注册final Map.Entry<Integer, Integer> integerIntegerEntry = Mathematical_Expression.register_function(fileInputStream);// 注册完毕之后在这里就可以查看到结果System.out.println("注册成功的数量:" + integerIntegerEntry.getKey());System.out.println("注册失败的数量:" + integerIntegerEntry.getValue());}// 然后我们就可以开始使用了 在这里的数据流中 包含的三个函数分别是 f ff ffffinal ManyToOneNumberFunction f = Mathematical_Expression.getFunction("f");final ManyToOneNumberFunction ff = Mathematical_Expression.getFunction("ff");final ManyToOneNumberFunction fff = Mathematical_Expression.getFunction("fff");System.out.println(f.run(10));System.out.println(ff.run(10));System.out.println(fff.run(10));}
}

Below are the calculation results:

[INFO][Calculation Management][24-03-22:01]] : A computing component is registered PrefixExpressionOperation
[INFO][Calculation Management][24-03-22:01]] : A computing component is registered BracketsCalculation2
[INFO][Calculation Management][24-03-22:01]] : A function is registered f
[INFO][Calculation Management][24-03-22:01]] : A function is registered ff
[INFO][Calculation Management][24-03-22:01]] : A function is registered fff
注册成功的数量:3
注册失败的数量:0
[INFO][Calculation Management][24-03-22:01]] : Get a function component from the manager. => f
[INFO][Calculation Management][24-03-22:01]] : Get a function component from the manager. => ff
[INFO][Calculation Management][24-03-22:01]] : Get a function component from the manager. => fff
[INFO][functionFormulaCalculation_temp][24-03-22:01]] : Use shared pool data. The identity of the data is: functionFormulaCalculation_temp(10.0*10.0)
100.0
[INFO][functionFormulaCalculation_temp][24-03-22:01]] : No Use shared pool: functionFormulaCalculation_temp(f(10.0) + 1)
[INFO][Calculation Management][24-03-22:01]] : Get a function component from the manager. => f
[INFO][functionFormulaCalculation_temp][24-03-22:01]] : Use shared pool data. The identity of the data is: functionFormulaCalculation_temp(10.0*10.0)
101.0
[INFO][functionFormulaCalculation_temp][24-03-22:01]] : No Use shared pool: functionFormulaCalculation_temp(10.0! + ff(10.0) + f(10.0))
[INFO][Calculation Management][24-03-22:01]] : Get a function component from the manager. => f
[INFO][functionFormulaCalculation_temp][24-03-22:01]] : Use shared pool data. The identity of the data is: functionFormulaCalculation_temp(10.0*10.0)
[INFO][Calculation Management][24-03-22:01]] : Get a function component from the manager. => ff
[INFO][functionFormulaCalculation_temp][24-03-22:01]] : Use shared pool data. The identity of the data is: functionFormulaCalculation_temp(f(10.0) + 1)
3629001.0

Mathematical expression compilation

Math expression compilation functionality translates a mathematical expression into an Expression object. All
calculation components that
implement io.github.beardedManZhao.mathematicalExpression.core.calculation.CompileCalculation can compile a
mathematical expression!
This operation enables users to store mathematical expressions or defer the computation time, allowing for features like
expression storage or deferring calculations. For expressions that need to be computed repeatedly, using a compiled
expression can save significant computation time!

PS: The support for io.github.beardedManZhao.mathematicalExpression.core.calculation.CompileCalculation will
gradually increase as the version increases!

Basic usage examples

The case demonstrated here is based on version 1.4.2, so you need to ensure that the version you are using is greater
than or equal to 1.4.2 before you can use it! The usage in version 1.4.1 is similar, but some features are not
supported!

import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.number.PrefixExpressionOperation;
import io.github.beardedManZhao.mathematicalExpression.core.container.Expression;public class MAIN {public static void main(String[] args) {// 获取到计算表达式组件final PrefixExpressionOperation instance = (PrefixExpressionOperation) Mathematical_Expression.getInstance(Mathematical_Expression.prefixExpressionOperation);// 将表达式 3*0.3 编译为一个表达式对象,我们在 1.4.1 版本中新增了compile & compileBigDecimal 方法,他们可以将表达式编译为对象,方便我们进行使用。final Expression compile = instance.compile("3 * 0.3", true);// 获取到计算结果 在这里有一个参数,设置为 false 性能会好些!设置为 true 功能会多些System.out.println(compile.calculationCache(true));// 我们可以使用这个表达式重复的计算,对于表达式对象来说 多次调用 calculationCache 的效率会很高// 除了第一次 calculationCache,其余调用 calculationCache 的复杂度皆为 O(1)System.out.println(compile.calculationCache(false));// 值得注意的是,在编译对象中,我们还提供了一个 calculationBigDecimalsCache 函数// 当我们在编译的时候 如果是使用 compile 编译出来的就需要使用 calculationCache 计算// 如果是使用 compileBigDecimal 编译出来的就需要使用 calculationBigDecimalsCache 计算if (compile.isBigDecimal()) {// 我们可以使用 isBigDecimal 函数来判断它是什么类型的计算模式System.out.println(compile.calculationBigDecimalsCache(false));}}
}

You can also manually adjust the calculation mode. Here is an example!

import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.CompileCalculation;
import io.github.beardedManZhao.mathematicalExpression.core.container.Expression;public class MAIN {public static void run(Expression expression) {if (expression.isBigDecimal()) {// 代表支持高精度的计算 TODO 这里使用的是不带 cache 的计算 这是为了测试效果,事实上 在此案例之外,我们建议您使用 cache 的计算!System.out.println("高精度计算结果:" + expression.calculationBigDecimals(true).getResult());}if (expression.isUnBigDecimal()) {// 代表支持非高精度的计算System.out.println("非精度计算结果:" + expression.calculation(true).getResult());}}public static void main(String[] args) {// 获取到计算表达式组件final CompileCalculation instance = (CompileCalculation) Mathematical_Expression.getInstance(Mathematical_Expression.bracketsCalculation2);// 将表达式 3*0.3 编译为一个表达式对象,我们在 1.4.1 版本中新增了compile & compileBigDecimal 方法,他们可以将表达式编译为对象,方便我们进行使用。final Expression compile = instance.compile("1 + (3 * 0.3)", true);// 运行表达式 这里第一次计算的时候 它因为 compile 的编译,只支持使用 非精度计算模式System.out.println("--------");run(compile);if (compile.isAvailable()) {// TODO 此函数需要确保您在 calculationCache or calculationBigDecimalsCache 调用时没有设置为 false//  我们可以通过 compile.isAvailable() 方法来判断是否支持多精度计算模块的启用 启用之后 您可以随意调用 calculationCache or calculationBigDecimalsCache// 将表达式的多精度支持模式启用 请注意 此操作仅可以对表达式的最后一层计算起作用!// 例如 1 + (3*0.3) 最后一层就是 1 + 0.899999(compile编译的) 或者 1 + 0.9(compileBigDecimal 编译的)compile.convertToMultiPrecisionSupported();// 运行表达式 TODO 这样操作之后 我们会发现它可以在两种模式中进行计算了System.out.println("--------");run(compile);}}
}
Serializing an expression object

The expression object inherits the ‘Java. io. Serializable’ interface, so you can use ‘Java. io. Object OutputStream’
and ‘Java. io. Object Input Stream’`
Serialize an expression object!

import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.CompileCalculation;
import io.github.beardedManZhao.mathematicalExpression.core.container.NameExpression;import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;public class MAIN {public static void main(String[] args) throws IOException, ClassNotFoundException {// 获取到计算组件final CompileCalculation instance = (CompileCalculation) Mathematical_Expression.getInstance(Mathematical_Expression.bracketsCalculation2);// 准备一个路径对象final Path path = Paths.get("C:\\Users\\zhao\\Downloads\\表达式.me");// 将表达式对象保存到磁盘try (final ObjectOutputStream objectOutputStream = new ObjectOutputStream(Files.newOutputStream(path))) {// 编译一个表达式对象final NameExpression compile = instance.compile(" 1 + (20 * 3 - 3 + (10 -4)) + (30 -2)  /2 + 10", true);// 将表达式对象输出到磁盘objectOutputStream.writeObject(compile);}// 再将表达式从磁盘读取进来try (final ObjectInputStream objectInputStream = new ObjectInputStream(Files.newInputStream(path))) {// 从磁盘中将表达式重新加载到内存final NameExpression expression = (NameExpression) objectInputStream.readObject();// 查看表达式的信息System.out.println("表达式来源:" + expression.getCalculationName());System.out.println("表达式的格式:" + expression.getExpressionStr());System.out.println("表达式支持的模式:" + (expression.isBigDecimal() ? "【高精度 √】 " : "【高精度 ×】 ") + (expression.isUnBigDecimal() ? "【非精度 √】 " : "【非精度 ×】 "));System.out.println(">>> 开始为表达式对象添加多精度支持");expression.convertToMultiPrecisionSupported();System.out.println("表达式支持的模式:" + (expression.isBigDecimal() ? "【高精度 √】 " : "【高精度 ×】 ") + (expression.isUnBigDecimal() ? "【非精度 √】 " : "【非精度 ×】 "));System.out.println("计算结果:" + expression.calculation(false));}}
}

Mathematical_Expression.Options

We allow you to use Mathematical_Expression.Options is used to configure some patterns of the calculation components
of mathematical expressions, and the configured options will change the behavior of the calculation components.

Set cache calculation mode setUseCache

The cache computation mode is a mode that sacrifices memory but improves computation speed. It is worth noting that
cache operations will accompany all of your subexpressions, which means that even if your expression is modified, as
long as it contains some computed expressions, it can still be used with cache. You can enable it by following the steps
below.

import io.github.beardedManZhao.mathematicalExpression.core.Mathematical_Expression;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.CompileCalculation;
import io.github.beardedManZhao.mathematicalExpression.core.calculation.number.BracketsCalculation2;
import io.github.beardedManZhao.mathematicalExpression.core.container.Expression;public class MAIN {// 准备一个用于进行基准测试的表达式private static String s = "1 + 30 + (20 / 10)";static {s += '+' + s;s += '+' + s;s += '+' + s;s += '+' + s;s += '+' + s;s += '+' + s;s += '+' + s;s += '+' + s;s += '+' + s;s += '+' + s;s += '+' + s;s += '+' + s;s += '+' + s;s += "+ 100000";System.out.println("您要计算的表达式:" + s);}public static void main(String[] args) {// 获取到计算表达式组件final BracketsCalculation2 instance = (BracketsCalculation2) Mathematical_Expression.getInstance(Mathematical_Expression.bracketsCalculation2);// 预热final Expression compile0 = instance.compileBigDecimal(s, true);System.out.println(compile0.calculationBigDecimalsCache(false));System.out.println("-------");// 开启缓存时非常快,能够有多快,取决于您的表达式中重复的子表达式的数量,数量越多 效果越明显Mathematical_Expression.Options.setUseCache(true);run(instance);System.out.println("-------");// 不开启缓存的计算速度就慢了许多Mathematical_Expression.Options.setUseCache(false);run(instance);}/*** 基准测试函数** @param instance 需要使用的计算组件*/public static void run(CompileCalculation instance) {final long l = System.currentTimeMillis();// 第一次进行编译final Expression compile = instance.compileBigDecimal(s, true);System.out.println(compile.calculationBigDecimalsCache(false));final long l1 = System.currentTimeMillis();System.out.println("第一次计算所用时间:" + (l1 - l));// 再一次进行编译 为了结果严谨,这里再次计算 且第二次表达式有所改动 整体除2final Expression compile1 = instance.compileBigDecimal('(' + s + ") / 2", true);System.out.println(compile1.calculationBigDecimalsCache(false));System.out.println("第二次计算所用时间:" + (System.currentTimeMillis() - l1));}
}

Java API Structure Chart

在这里插入图片描述

More information

  • 切换至 中文文档
  • mathematical-expression-py
  • mathematical-expression-JS
  • mathematical-expression-C++

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/6758.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

http的请求体各项解析

一、前言 做Java开发的人员都知道&#xff0c;其实我们很多时候不单单在写Java程序。做的各种各样的系统&#xff0c;不管是PC的 还是移动端的&#xff0c;还是为别的系统提供接口。其实都离不开http协议或者https 这些东西。Java作为编程语言&#xff0c;再做业务开发时&#…

Java 大视界 -- Java 大数据中的自然语言生成技术与实践(63)

&#x1f496;亲爱的朋友们&#xff0c;热烈欢迎来到 青云交的博客&#xff01;能与诸位在此相逢&#xff0c;我倍感荣幸。在这飞速更迭的时代&#xff0c;我们都渴望一方心灵净土&#xff0c;而 我的博客 正是这样温暖的所在。这里为你呈上趣味与实用兼具的知识&#xff0c;也…

计算机网络三张表(ARP表、MAC表、路由表)总结

参考&#xff1a; 网络三张表&#xff1a;ARP表, MAC表, 路由表&#xff0c;实现你的网络自由&#xff01;&#xff01;_mac表、arp表、路由表-CSDN博客 网络中的三张表&#xff1a;ARP表、MAC表、路由表 首先要明确一件事&#xff0c;如果一个主机要发送数据&#xff0c;那么必…

Git Bash 配置 zsh

博客食用更佳 博客链接 安装 zsh 安装 Zsh 安装 Oh-my-zsh github仓库 sh -c "$(curl -fsSL https://install.ohmyz.sh/)"让 zsh 成为 git bash 默认终端 vi ~/.bashrc写入&#xff1a; if [ -t 1 ]; thenexec zsh fisource ~/.bashrc再重启即可。 更换主题 …

【问题】Chrome安装不受支持的扩展 解决方案

此扩展程序已停用&#xff0c;因为它已不再受支持 Chromium 建议您移除它。详细了解受支持的扩展程序 此扩展程序已停用&#xff0c;因为它已不再受支持 详情移除 解决 1. 解压扩展 2.打开manifest.json 3.修改版本 将 manifest_version 改为3及以上 {"manifest_ver…

在 Windows 系统上,将 Ubuntu 从 C 盘 迁移到 D 盘

在 Windows 系统上&#xff0c;如果你使用的是 WSL&#xff08;Windows Subsystem for Linux&#xff09;并安装了 Ubuntu&#xff0c;你可以将 Ubuntu 从 C 盘 迁移到 D 盘。迁移过程涉及导出当前的 Ubuntu 发行版&#xff0c;然后将其导入到 D 盘的目标目录。以下是详细的步骤…

qt QNetworkRequest详解

1、概述 QNetworkRequest是Qt网络模块中的一个核心类&#xff0c;专门用于处理网络请求。它封装了网络请求的所有关键信息&#xff0c;包括请求的URL、HTTP头部信息等&#xff0c;使得开发者能够方便地在Qt应用程序中执行网络操作&#xff0c;如文件下载、网页内容获取等。QNe…

Python!从0开始学爬虫:(一)HTTP协议 及 请求与响应

前言 爬虫需要基础知识&#xff0c;HTTP协议只是个开始&#xff0c;除此之外还有很多&#xff0c;我们慢慢来记录。 今天的HTTP协议&#xff0c;会有助于我们更好的了解网络。 一、什么是HTTP协议 &#xff08;1&#xff09;定义 HTTP&#xff08;超文本传输协议&#xff…

后盾人JS -- Map与WeakMap类型在JavaScript中的使用

Map类型特点与创建方法 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>Document</title> &l…

python实现http文件服务器访问下载

//1.py import http.server import socketserver import os import threading import sys# 获取当前脚本所在的目录 DIRECTORY os.path.dirname(os.path.abspath(__file__))# 设置服务器的端口 PORT 8000# 自定义Handler&#xff0c;将根目录设置为脚本所在目录 class MyHTT…

[STM32 - 野火] - - - 固件库学习笔记 - - -十一.电源管理系统

一、电源管理系统简介 电源管理系统是STM32硬件设计和系统运行的基础&#xff0c;它不仅为芯片本身提供稳定的电源&#xff0c;还通过多种电源管理功能优化功耗、延长电池寿命&#xff0c;并确保系统的可靠性和稳定性。 二、电源监控器 作用&#xff1a;保证STM32芯片工作在…

二叉树相关oj题 1. 检查两颗树是否相同。

二叉树相关oj题 检查两颗树是否相同。OJ链接 另一颗树的子树。OJ链接 if(rootnull)易漏掉 会导致空指针异常翻转二叉树。OJ链接

批量提取多个 Excel 文件内指定单元格的数据

这篇文章将介绍如何从多个相同格式的Excel文件中&#xff0c;批量提取指定单元格的数据&#xff0c;合并后保存到新的工作薄。 全程0代码&#xff0c;可视化操作。 提取前&#xff1a; 提取后&#xff1a; 准备数据 这里准备了3个测试数据 开始提取 打开的卢易表&#xff0…

【真机调试】前端开发:移动端特殊手机型号有问题,如何在电脑上进行调试?

目录 前言一、怎么设置成开发者模式&#xff1f;二、真机调试基本步骤&#xff1f; &#x1f680;写在最后 前言 edge浏览器 edge://inspect/#devices 谷歌浏览器&#xff08;开tizi&#xff09; chrome://inspect 一、怎么设置成开发者模式&#xff1f; Android 设备 打开设…

GA-CNN-LSTM-Attention、CNN-LSTM-Attention、GA-CNN-LSTM、CNN-LSTM四模型多变量时序预测一键对比

GA-CNN-LSTM-Attention、CNN-LSTM-Attention、GA-CNN-LSTM、CNN-LSTM四模型多变量时序预测一键对比 目录 GA-CNN-LSTM-Attention、CNN-LSTM-Attention、GA-CNN-LSTM、CNN-LSTM四模型多变量时序预测一键对比预测效果基本介绍程序设计参考资料 预测效果 基本介绍 基于GA-CNN-LST…

C++入门14——set与map的使用

在本专栏的往期文章中&#xff0c;我们已经学习了STL的部分容器&#xff0c;如vector、list、stack、queue等&#xff0c;这些容器统称为序列式容器&#xff0c;因为其底层是线性序列的数据结构&#xff0c;里面存储的是元素本身。而本篇文章我们要来认识一下关联式容器。 &am…

从崩溃难题看 C 标准库与 Rust:线程安全问题引发的深度思考

在软件开发的世界里&#xff0c;每一次技术的变革和尝试都伴随着未知的挑战。EdgeDB 团队在将部分网络 I/O 代码从 Python 迁移到 Rust 的过程中&#xff0c;就遭遇了一场棘手的问题&#xff0c;这个问题不仅暴露了 C 标准库的线程安全隐患&#xff0c;也让我们对 Rust 的 “安…

班迪录屏:一款好用的屏幕录制软件

Bandicam&#xff08;班迪录屏&#xff09;是一款4K超清屏幕录像软件&#xff0c;最新版Bandicam v8.1.0.2516 绿色正式版已经集成授权信息&#xff0c;用户无需联网验证授权&#xff0c;启动软件即可直接使用已授权版本&#xff0c;享受良好的录制体验。这款软件完全摆脱了试用…

2025年国产化推进.NET跨平台应用框架推荐

2025年国产化推进.NET跨平台应用框架推荐 1. .NET MAUI NET MAUI是一个开源、免费&#xff08;MIT License&#xff09;的跨平台框架&#xff08;支持Android、iOS、macOS 和 Windows多平台运行&#xff09;&#xff0c;是 Xamarin.Forms 的进化版&#xff0c;从移动场景扩展到…

PBFT算法

在我的博客中对于RAFT算法也有详细的介绍&#xff0c;raft算法包含三种角色&#xff0c;分别是&#xff1a;跟随者&#xff08; follower &#xff09;&#xff0c;候选人&#xff08;candidate &#xff09;和领导者&#xff08; leader &#xff09;。集群中的一个节点在某一…