A string expression calculation method based on N-ary tree

Through the calculation method based on N-ary trees, the problems of poor fault tolerance and weak processing power in string expression calculation in the existing technology are solved, support for complex expressions and multivariate operations is realized, and the applicable scope of calculation is expanded.

CN112307719BInactive Publication Date: 2024-09-24JIANGHAN UNIVERSITY
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202011092954.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-10-13
Publication Date
2024-09-24
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

The existing technology has problems in string expression calculations such as poor fault tolerance, weak ability to handle error prompts, and can only perform simple calculations. In particular, it cannot handle complex expressions, variables, and multivariate operator operations.

Method used

Using a calculation method based on N-ary trees, the input string expression is standardized and split into operators, operands and identifiers, and the operator stack and data stack are used to convert it into an N-ary tree to achieve complex expressions. Calculation of formula.

Benefits of technology

It improves the computing power of string expressions, can handle complex arithmetic, relational, logical and other expressions, supports multi-operator operations, and handles variables and functions in expressions, expanding the scope of application.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure 00000000_0000
    Figure 00000000_0000
Patent Text Reader

Abstract

The present invention provides a method for calculating a string expression based on an N-ary tree, the method comprising: S1, inputting a string expression; S2, performing standardization processing on the input string expression; S3, scanning the string after standardization processing, and splitting the scanned string into operators, operands, identifiers and non-identifiers; S4, converting the split characters into an N-ary tree according to operator operation rules, using an operator stack and a data stack; S5, realizing string operation using the N-ary tree. The present invention calculates strings based on the N-ary tree, so that the calculation ability is stronger and the application range is wider.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a character string expression calculation method based on an N-ary tree. Background Art

[0002] A string expression is a character string of any style (including full-width characters). An N-ary tree is an ordered tree structure in which each node has several nodes. When a node is an operator node, its children are operands or operator nodes. When an operator node has 1 child, it means that the node is a unary operator node. When an operator has 2 children, it means that the node is a binary operator node. When an operator has 3 children, it means that the node is a ternary operator node, and so on. When a node is a function node, its children represent function parameters, and when a function node has no children, it means that the function has no parameters.

[0003] In the current prior art, when evaluating a string expression, the string style of the operation is numbers and mathematical symbols. There are currently some string expression calculation methods, but they have poor fault tolerance, poor error handling capabilities, and can only perform simple calculations. Some calculation schemes only refer to being able to handle a certain type of operation, and can only handle simple binary operations, and some cannot even handle unary operator operations, let alone multi-element operator operations; if variables appear in the expression, it cannot be processed; secondly, if complex relational expressions or logical expressions appear in the current expression, they cannot be processed; finally, the prior art cannot calculate string expressions containing functions.

[0004] Therefore, in order to solve the problems existing in the string expression calculation process, such as the limitation of string expressions, poor fault tolerance of the calculation method, poor error prompt processing ability, and only simple calculation can be performed, the present invention proposes a string expression calculation method based on an N-ary tree, which has stronger calculation ability and wider application scope. Summary of the invention

[0005] The present invention provides a string expression calculation method based on an N-ary tree, which is used to solve the problems that string expressions are limited, the calculation method has poor fault tolerance, poor error processing prompt capability, and can only perform simple calculations.

[0006] A method for calculating a string expression based on an N-ary tree, the method comprising:

[0007] S1, input string expression;

[0008] S2, standardize the input string expression;

[0009] S3, scan the standardized string and split the scanned string into operators, operands, identifiers and non-identifiers;

[0010] S4, converting the separated characters into an N-ary tree according to the operator operation rules, using the operator stack and the data stack;

[0011] S5. Use N-ary tree to implement string operations.

[0012] Preferably, the output string expression includes: ASCII code characters and full-width characters of numbers, letters, and operation symbols.

[0013] Preferably, the standardization process includes:

[0014] S21, changing the full-width characters outside the quotation marks in the input string to the corresponding ASCII code characters, and the contents inside the quotation marks remain unchanged;

[0015] S22. Delete extra space characters in the string, and keep the spaces within the quotation marks unchanged.

[0016] Preferably, the operators include logical operators and simple operators, the logical operators include: and, or, not and true, the simple operators include: ,, (,), +, -, *, / , \, %, ^, &, |, xor, >, >=, <, <=, <>, = and ";

[0017] The operands include: string constants, numerical constants and logical value constants;

[0018] The identifier is composed of underscores, numbers and letters and cannot start with a number. In the identifier, lowercase letters are used to represent variables, and uppercase letters are used to represent constants.

[0019] Preferably, the step S4, converting the string into an N-ary tree, comprises:

[0020] S41, determine whether the data has been read. If the data has not been read, execute A4. If the data has been read, proceed to S42;

[0021] S42, read a data or operand from the string expression to data;

[0022] S43, determine whether data is a numerical constant. If data is a numerical constant, create a data node and push the data node into the data stack. If data is not a numerical constant, directly proceed to S44.

[0023] S44, determine whether data is an operator. If data is an operator, determine whether data is + or - and ). When data is + or -, execute part A1. When data is ), execute part A2. When data is neither + or - nor ), execute part A3. If data is not an operator, directly execute S45.

[0024] S45, determine whether data is an identifier. If data is an identifier, further determine whether data is a function or a system operator. If data is a function or a system operator, push data into the operator stack, otherwise push data into the data stack. If data is not an identifier, data is an illegal identifier, exit early, and return an error message.

[0025] Loop S41-S45 in sequence. If there is data in the operator stack, execute part A4; if the operator stack is empty and there is only one operand node in the data stack, return to that node;

[0026] The A1 part indicates that when the string read is +, - or not, a subtree creation operation is performed; the A2 part indicates that when the string read is a right bracket, a subtree creation operation is processed; the A3 part indicates that when other operators except +, - or not are scanned, a subtree creation operation is performed; the A4 part indicates that when all strings are scanned, the operand stack and operator stack need to be processed to perform a subtree creation operation.

[0027] Preferably, the S4 further comprises: creating a related class;

[0028] The creating related classes includes: creating an expression class, creating an operator class and creating a node class.

[0029] Preferably, when the expression class is created, the string contains: a String type attribute exp, which holds the processed input string; an int type flag, which indicates the data currently obtained from the string exp, the initial value of flag is 0, flag=0 indicates that the string has not been scanned or a space is obtained, flag=1 indicates that a numerical value is obtained, flag=2 indicates that an operator character is obtained, flag=3 indicates that an identifier is obtained, and the identifier includes a legal identifier and an illegal identifier, flag=4 indicates that a string is obtained; and a point attribute, which indicates the subscript position of the character currently read from the string exp.

[0030] Preferably, the method for creating an expression class includes:

[0031] S4011. Determine whether the string is an operator. If the string is an operator, return true; otherwise, return false.

[0032] S4012, determine whether the string is an operand, if the string is an operand, return true, otherwise return false;

[0033] S4013, judging whether the string is a legal identifier, if the string is a legal identifier, returning true, otherwise returning false;

[0034] S4014, determine whether the string is a function name, if the string is a function name, return true, otherwise return false;

[0035] S4015. Determine whether the string is a variable. If the string is a variable, return true; otherwise, return false.

[0036] Preferably, the method for creating an operator class includes:

[0037] S4021. Obtain the priority of the corresponding operator according to the operator and the operation type;

[0038] S4022, determining whether the given operator string is a specified operator;

[0039] Preferably, the types of the node creation class include operators, operands and function types. The node creation class is mainly used to implement various operations and return the final operation result. The method includes:

[0040] S4031, judging whether the node number is an operand node. When the node number is an operand node, judging whether the node is an operand constant. If the node is an operand constant, returning the node value. If the node is not an operand constant, judging whether the variable is in the variable queue variableList. When the variable exists in the variable queue variableList, returning the variable value. Otherwise, returning the variable does not exist error information. When the node number is not an operand node, directly proceeding to S4032.

[0041] S4032, determine whether the node is an operator. When the node is an operator, wait until the number of child nodes of the node is childrenNum, and determine whether the number of operands required by the current operator is equal to childrenNum. If the number of operands required by the current operator is equal to childrenNum, then the current children respectively call the method compute(ArrayList variableList,ArrayList functionList) of the node class to obtain the corresponding result as the operand, and then perform the childrenNum meta-operation, and return the final result value. If the number of operands required by the current operator is not equal to childrenNum, return the corresponding operator operation error; when the node is not an operator, directly proceed to S4033;

[0042] S4033. Determine whether the node is a function. When the node is a function, determine whether the current function is in the queue functionList. If the current function is not in the queue functionList, output a function non-existence error. If the current function exists in the queue functionList, obtain the number of child nodes of the node childrenNum, and then determine whether the number of parameters required by the current function is equal to childrenNum. When the number of parameters required by the current function is equal to childrenNum, the current children respectively call the node class method compute(ArrayList variableList, ArrayListfunctionList) to obtain the corresponding results as the function parameters. If these parameters are consistent with the parameter types specified by the function, call the function and return the result. When the number of parameters required by the current function is not equal to childrenNum, return a function parameter type inconsistency error. When the node is not a function, directly return an unknown node error.

[0043] The beneficial effects of the present invention are:

[0044] (1) The string expression input in the present invention can be in any format, which makes it more convenient for users;

[0045] (2) The present invention can process complex expression calculations, and the complex string expression can be any arithmetic, relational, logical, etc. expression;

[0046] (3) Variables may appear in the string expression calculated by the string expression calculation method of the present invention;

[0047] (4) The present invention can process string expressions containing functions, and the parameters of the functions can be uncertain;

[0048] (5) The string expression calculation method described in the present invention supports multi-operator operations.

[0049] The technical solution of the present invention is further described in detail below through the accompanying drawings and embodiments. BRIEF DESCRIPTION OF THE DRAWINGS

[0050] The accompanying drawings are used to provide a further understanding of the present invention and constitute a part of the specification. Together with the embodiments of the present invention, they are used to explain the present invention and do not constitute a limitation of the present invention. In the accompanying drawings:

[0051] Figure 1 A schematic diagram of a method for calculating a string expression based on an N-ary tree according to the present invention.

[0052] Figure 2 The present invention is a flowchart of splitting a string expression into operators, operands, identifiers, and identifiers.

[0053] Figure 3 The figure is a schematic diagram of the process of converting an expression into an N-ary tree according to the present invention.

[0054] Figure 4 A schematic diagram of the node calculation process described in the present invention.

[0055] Figure 5 It is an N-ary tree diagram obtained by converting a specific string expression instance in steps S1-S4 of the string expression calculation method based on an N-ary tree described in the present invention. DETAILED DESCRIPTION

[0056] The preferred embodiments of the present invention are described below in conjunction with the accompanying drawings. It should be understood that the preferred embodiments described herein are only used to illustrate and explain the present invention, and are not used to limit the present invention.

[0057] The embodiment of the present invention provides a method for calculating a string expression based on an N-ary tree, such as Figure 1 As shown, the string expression calculation method based on the N-ary tree includes:

[0058] S1, input string expression;

[0059] S2, standardize the input string expression;

[0060] S3, scan the standardized string, and split the scanned string into operators, operands, identifiers, and non-identifiers;

[0061] S4, converting the separated characters into an N-ary tree according to the operator operation rules, using the operator stack and the data stack;

[0062] S5. Use N-ary tree to implement string operations.

[0063] Among them, Figure 5 The detailed processing process of steps S1-S4 of the string expression calculation method based on the N-ary tree proposed in this embodiment is as follows:

[0064] S1. Input string expression; lengthOfString("ab cd")-f(a,b,3,5)>4 and -3+5×cos(π / 6)÷2>5;

[0065] S2. Standardize the input string expression to obtain lengthOfString("ab cd")-f(a,b,3,5)>4 and -3+5*cos(PI / 6) / 2>5;

[0066] S3,scan lengthOfString(“ab cd”)-f(a,b,3,5)>4 and -3+5*cos(PI / 6) / 2>5, and split the scanned lengthOfString(“ab cd”)-f(a,b,3,5)>4 and -3+5*cos(PI / 6) / 2>5 into lengthOfString,(,“ab cd”,),-,f,(,a,b,3,5,),>,4,and,3,+,5,cos,(,PI, / ,6,), / ,2,>,5;

[0067] S4,Convert the split lengthOfString,(,“ab cd”,),-,f,(,a,b,3,5,),>,4,and,3,+,5,cos,(,PI, / ,6,), / ,2,>,5,into an N-ary tree root according to the operator operation rules,with the help of operator stack and data stack.

[0068] The principle of the above technical solution is as follows: in the process of calculating a string expression, first input the string expression, then standardize the input string expression, then scan the standardized string, and split the scanned string into operators, operands, identifiers and non-identifiers, and then convert the split characters into an N-ary tree according to the operator operation rules with the help of an operator stack and a data stack, and finally use the N-ary tree to implement string operations.

[0069] Beneficial effects of the above technical solution: The string expression calculation method described in the above technical solution book not only has strong computing power and a wide range of applications, but can also calculate arithmetic, logical, relational and other expressions, and the expressions can also contain system functions and user-defined functions.

[0070] The embodiment of the present invention provides a method for calculating a string expression based on an N-ary tree, wherein the input string expression includes: numbers, letters and operation symbols under full-width characters.

[0071] The principle of the above technical solution is: when inputting a string expression, the input string expression can be a number under a full-width character, a letter under a full-width character, or an operation symbol under a full-width character.

[0072] The beneficial effects of the above technical solution are: by being able to calculate the string expression composed of numbers, letters and operation symbols under the input full-width characters, the expression is more fault-tolerant, and the input string can be close to the arithmetic expression of natural language, which is more convenient to use.

[0073] The embodiment of the present invention provides a method for calculating a string expression based on an N-ary tree, wherein the standardization process includes:

[0074] S21, changing the full-width characters outside the quotation marks in the input string to the corresponding ASCII code characters, and the contents inside the quotation marks remain unchanged;

[0075] S22. Delete extra space characters in the string, and keep the spaces within the quotation marks unchanged.

[0076] The principle of the above technical solution is: when calculating a string expression, the input string is standardized, that is, the full-width characters in the input string are changed into corresponding ASCII code characters, and redundant space characters are deleted, but the full-width characters inside the quotation marks are rewritten, and the space characters therein are also retained.

[0077] The beneficial effect of the above technical solution is as follows: by standardizing the input character string, ASCII code characters under a unified standard can be obtained, and the corresponding ASCII code characters obtained may contain space characters, which need to be deleted. However, the content in the quotation marks is specially described or specially restricted, so the content of the special description or special restriction cannot be changed. Therefore, in the standardization process, only the full-width characters outside the quotation marks are converted into ASCII code characters and space characters are deleted to avoid changing the content of the special description or special restriction in the quotation marks.

[0078] The embodiment of the present invention provides a method for calculating a string expression based on an N-ary tree, such as Figure 2As shown, in S3, the operators include logical operators and simple operators, the logical operators include: and (and), or (or), not (negation) and true (logical truth), the simple operators include: , (comma), ((left bracket), )(right bracket), + (addition or positive sign or string connection), - (subtraction or negative sign or string subtraction operation), * (multiplication), / (division), \ (integer division), % (% remainder), ^ (nth power), & (bitwise AND), | (bitwise OR), # (exclusive OR operation), > (greater than), >= (greater than or equal to), < (less than), <= (less than or equal to), < > (not equal), = (equal) and "(string separator);

[0079] The operands include: string constants, numerical constants and logical value constants;

[0080] The identifier is composed of underscores, numbers and letters and cannot start with a number. In the identifier, lowercase letters are used to represent variables, and uppercase letters are used to represent constants.

[0081] The principle of the above technical solution is: scan the string after standardization processing, and split the scanned string into operators, operands, identifiers and non-identifiers. The operators include logical operators and simple operators. The operands include: string constants, numerical constants and logical value constants. The identifier is composed of underscores, numbers and letters, and cannot start with a number. If it is neither an operator nor an operator and does not meet the identifier, it is deemed to be a non-identifier.

[0082] The beneficial effects of the above technical solution: In the above technical solution, not only can the characters in the string be split, but also the operators and the contents contained in the operators and the definitions of the identifiers are clearly pointed out, which can be used to randomly check whether there are obvious errors in the operators, operands, identifiers and illegal identifiers split through the process.

[0083] The embodiment of the present invention provides a method for calculating a string expression based on an N-ary tree, such as Figure 3 As shown, the S4 converts the string into an N-ary tree, including:

[0084] S41, determine whether the data has been read. If the data has not been read, execute A4. If the data has been read, proceed to S42;

[0085] S42, read a data or operand from the string expression to data;

[0086] S43, determine whether data is a numerical constant. If data is a numerical constant, create a data node and push the data node into the data stack. If data is not a numerical constant, directly proceed to S44.

[0087] S44, determine whether data is an operator. If data is an operator, determine whether data is + or - and ). When data is + or -, execute part A1. When data is ), execute part A2. When data is neither + or - nor ), execute part A3. If data is not an operator, directly execute S45.

[0088] S45, determine whether data is an identifier. If data is an identifier, further determine whether data is a function or a system operator. If data is a function or a system operator, push data into the operator stack, otherwise push data into the data stack. If data is not an identifier, data is an illegal identifier, exit early, and return an error message.

[0089] Loop S41-S45 in sequence. If there is data in the operator stack, execute part A4; if the operator stack is empty and there is only one operand node in the data stack, return to that node;

[0090] The A1 part indicates that when the string read is +, - or not, a subtree creation operation is performed; the A2 part indicates that when the string read is a right bracket, a subtree creation operation is processed; the A3 part indicates that when other operators except +, - or not are scanned, a subtree creation operation is performed; the A4 part indicates that when all strings are scanned, the operand stack and operator stack need to be processed to perform a subtree creation operation.

[0091] The principle of the above technical solution is as follows: when converting a string into an N-ary tree, data is read from the string expression to data; it is determined whether data is a numerical constant, if it is a numerical constant, a data node is created and pushed into a data stack; it is determined whether data is an operator string, if the operator string is + or -, a subtree creation operation is performed, if the operator string is a right bracket, a subtree creation operation is performed, otherwise, when other operators are scanned, a subtree creation operation is performed; it is determined whether data is an identifier, if data is a legal identifier, it is further determined whether data is a function and an operator, and data is pushed into the operator stack, otherwise data is pushed into the data stack, if data is an illegal identifier, it ends early and an error message is returned; it is determined whether there is data in the operator stack, if there is data in the operator stack, the operator is taken out, and a subtree is created according to the operator priority and the operator operation rules, if the operator stack is empty and there is only one operand node in the data stack, the order of returning the node is performed in sequence.

[0092] The beneficial effect of the above technical solution is as follows: the string expression is converted into an N-ary tree through the above steps, and different steps are performed according to different judgment results by means of judgment in the process, so as to realize the conversion of the string expression into the N-ary tree conveniently and quickly.

[0093] The embodiment of the present invention provides a method for calculating a string expression based on an N-ary tree, wherein S4 further includes: creating a related class;

[0094] The creating related classes includes: creating an expression class, creating an operator class and creating a node class.

[0095] The principle of the above technical solution is: before the step of converting the string into an N-ary tree, an expression class, an operator class and a node class are created according to the characters in the string, and then the string is converted into an N-ary tree with the help of an operator stack and a data stack.

[0096] The beneficial effect of the above technical solution is: by creating relevant classes and then performing string conversion, and then performing string operations, the string operation method can realize the calculation of complex expressions, even if variables appear in the string or the parameters in the string expression of the function are uncertain or the operator is multiple.

[0097] The embodiment of the present invention provides a method for calculating a string expression based on an N-ary tree. When creating an expression class, the string contains: a String type attribute exp, which holds a processed input string; an int type flag, which indicates the data currently obtained from the string exp, the initial value of the flag is 0, flag=0 indicates that the string has not been scanned or a space is obtained, flag=1 indicates that a numerical value is obtained, flag=2 indicates that an operator character is obtained, flag=3 indicates that an identifier is obtained, and the identifier includes a legal identifier and an illegal identifier, and flag=4 indicates that a string is obtained; and a point attribute, which indicates the subscript position of the character currently read from the string exp.

[0098] The principle of the above technical solution is: when creating an expression class, it is performed according to the type contained in the string. The String type attribute exp holds the processed input string; the int type flag indicates the current data obtained from the string exp.

[0099] The beneficial effects of the above technical solution: When the above technical solution creates an expression class, the flag values ​​0, 1, 2, 3, and 4 are used to respectively indicate that the string has not been scanned or a space has been obtained, a numeric value has been obtained, an operator character has been obtained, an identifier (such as a letter, underscore, etc.) has been obtained, including a legal identifier that matches an illegal identifier and a string has been obtained, which simplifies the complexity of the string expression calculation process and reduces the probability of errors during the calculation process.

[0100] The embodiment of the present invention provides a method for calculating a string expression based on an N-ary tree, and the method for creating an expression class includes:

[0101] S4011. Determine whether the string is an operator. If the string is an operator, return true; otherwise, return false.

[0102] S4012, determine whether the string is an operand, if the string is an operand, return true, otherwise return false;

[0103] S4013, judging whether the string is a legal identifier, if the string is a legal identifier, returning true, otherwise returning false;

[0104] S4014, determine whether the string is a function name, if the string is a function name, return true, otherwise return false;

[0105] S4015. Determine whether the string is a variable. If the string is a variable, return true; otherwise, return false.

[0106] The principle of the above technical solution is: to create an expression class by determining whether a string is an operator, whether a string is an operand, whether a string is a legal identifier, whether a string is a function name, and whether a string is a variable.

[0107] The beneficial effects of the above technical solution: Through the above technical solution, it is possible to judge the string, quickly obtain whether the string contains factors such as operators, operands, legal identifiers, function names and variables, and accurately create an expression class based on the judgment results of these factors.

[0108] The embodiment of the present invention provides a method for calculating a string expression based on an N-ary tree, wherein the method for creating an operator class includes:

[0109] S4021. Obtain the priority of the corresponding operator according to the operator and the operation type;

[0110] S4022. Determine whether the given operator string is the specified operator.

[0111] The principle of the above technical solution is: when creating an operator class, first obtain the priority of the corresponding operator according to the operator and the operation type, and then determine whether the given operator string is the specified operator.

[0112] The beneficial effects of the above technical solution are as follows: by creating an operator class through the above technical solution, the calculation can be performed according to the priority of the operator when calculating the string expression, thereby avoiding calculation errors, and by judging whether a given operator string is a specified operator, the specified operation can be performed when calculating the given operator part during the calculation process.

[0113] The embodiment of the present invention provides a method for calculating a string expression based on an N-ary tree, such as Figure 4 As shown, the types of the node creation class include operators, operands and function types. The node creation class is mainly used to implement various operations and return the final operation results. The method includes:

[0114] S4031, determine whether the node number is an operand node. If the node number is an operand node, determine whether the node is an operand constant. If the node is an operand constant, return the node value. If the node is not an operand constant, determine whether the variable is in the variable queue variableList. If the variable exists in the variable queue variableList, return the variable value. Otherwise, return an error message indicating that the variable does not exist. If the node number is not an operand node, directly proceed to S4032.

[0115] S4032, determine whether the node is an operator. When the node is an operator, wait until the number of child nodes of the node is childrenNum, and determine whether the number of operands required by the current operator is equal to childrenNum. If the number of operands required by the current operator is equal to childrenNum, then the current children respectively call the method compute(ArrayList variableList,ArrayList functionList) of the node class to obtain the corresponding result as the operand, and then perform the childrenNum meta-operation, and return the final result value. If the number of operands required by the current operator is not equal to childrenNum, return the corresponding operator operation error; when the node is not an operator, directly proceed to S4033;

[0116] S4033. Determine whether the node is a function. When the node is a function, determine whether the current function is in the queue functionList. If the current function is not in the queue functionList, output a function non-existence error. If the current function exists in the queue functionList, obtain the number of child nodes of the node childrenNum, and then determine whether the number of parameters required by the current function is equal to childrenNum. When the number of parameters required by the current function is equal to childrenNum, the current children respectively call the node class method compute(ArrayList variableList, ArrayListfunctionList) to obtain the corresponding results as the function parameters. If these parameters are consistent with the parameter types specified by the function, call the function and return the result. When the number of parameters required by the current function is not equal to childrenNum, return a function parameter type inconsistency error. When the node is not a function, directly return an unknown node error.

[0117] The principle of the above technical solution: The above technical solution implements various operations by creating a node class and returns the final operation result. The method process of creating a node class includes: if the current node is an operand or a variable, the value of the operand or variable is returned; if the current node is an operator, the method compute(ArrayList variableList,ArrayList functionList) of its child node is recursively called, and then the operation result value is returned according to the operator operation rules; if the current node is a function node, the method compute(ArrayList variableList,ArrayList functionList) of its child node is recursively called first, and the calculation result values ​​returned by these children are used as the parameter values ​​of the function node, the function is called, and finally the function value is returned.

[0118] The beneficial effects of the above technical solution are as follows: performing string expression calculation by creating nodes not only has strong calculation capability but also has a wide range of applications, and the cause of the error can also be known in the node calculation method.

[0119] Obviously, those skilled in the art can make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.

Claims

1. A method for calculating a string expression based on an N-ary tree, characterized in that: The method comprises: S1, input string expression; S2, standardize the input string expression; S3, scan the standardized string and split the scanned string into operators, operands, identifiers and non-identifiers; S4. Convert the separated characters into an N-ary tree according to the operator operation rules, with the help of the operator stack and the data stack, including: S41. Determine whether the data has been read. If the data has not been read, execute part A4. If the data has been read, proceed to S42; S42. Read a data or operand from the string expression to data; S43. Determine whether data is a numerical constant. If data is a numerical constant, create a data node and push the data node into the data stack. If data is not a numerical constant, proceed directly to S44; S44. Determine whether data is an operator. If data is an operator, determine whether data is + or - and ). When data is + or -, execute part A1. When data is ), execute part A2. When data is neither + or - nor ), execute part A3; if data is not an operator, execute S45 directly; S45. Determine whether data is a standard. Identifier, if data is an identifier, then further determine whether data is a function or a system operator. If data is a function or a system operator, then push data into the operator stack, otherwise push data into the data stack. If data is not an identifier, then data is an illegal identifier, exit early, and return an error message; loop S41-S45 in sequence, if there is data in the operator stack, execute part A4; if the operator stack is empty and there is only one operand node in the data stack, return the node; wherein, part A1 indicates: when the read string is +, - or not, a subtree creation operation is performed; part A2 indicates: when the read string is a right bracket, a subtree creation operation is processed; part A3 indicates: when other operators except +, - or not are scanned, a subtree creation operation is performed; part A4 indicates: when all strings are scanned, the operand stack and the operator stack need to be processed to perform a subtree creation operation; S5. Use N-ary tree to implement string operations; The S4 further includes: creating related classes; the creating related classes includes: creating expression classes, creating operator classes and creating node classes; wherein the types of the created node classes include operators, operands and function types, and the created node classes are mainly used to implement various operations and return the final operation results, and the method includes: S4031, determine whether the node number is an operand node. If the node number is an operand node, determine whether the node is an operand constant. If the node is an operand constant, return the node value. If the node is not an operand constant, determine whether the variable is in the variable queue variableList. If the variable exists in the variable queue variableList, return the variable value. Otherwise, return an error message indicating that the variable does not exist. If the node number is not an operand node, directly proceed to S4032. S4032, determine whether the node is an operator. When the node is an operator, obtain the number of child nodes of the node, childrenNum, and determine whether the number of operands required by the current operator is equal to childrenNum. If the number of operands required by the current operator is equal to childrenNum, the current children respectively call the compute method of the node class to obtain the corresponding result as the operand, and then perform the childrenNum meta-operation to return the final result value. If the number of operands required by the current operator is not equal to childrenNum, return the corresponding operator operation error; when the node is not an operator, directly proceed to S4033; S4033. Determine whether the node is a function. When the node is a function, determine whether the current function is in the queue functionList. If the current function is not in the queue functionList, output a function does not exist error. If the current function exists in the queue functionList, obtain the number of child nodes of the node childrenNum, and then determine whether the number of parameters required by the current function is equal to childrenNum. When the number of parameters required by the current function is equal to childrenNum, the current children respectively call the compute method of the node class to obtain the corresponding results as the parameters of the function. If these parameters are consistent with the parameter types specified by the function, call the function and return the result. When the number of parameters required by the current function is not equal to childrenNum, return a function parameter type inconsistency error; when the node is not a function, directly return an unknown node error.

2. The method according to claim 1, characterized in that The input string expression includes: ASCII code characters and full-width characters of numbers, letters, and operation symbols.

3. The method according to claim 1, characterized in that The standardization process comprises: S21, changing the full-width characters outside the quotation marks in the input string to the corresponding ASCII code characters, and the contents inside the quotation marks remain unchanged; S22. Delete extra space characters in the string, and keep the spaces within the quotation marks unchanged.

4. The method according to claim 1, characterized in that In S3, the operators include logical operators and simple operators. The logical operators include and, or, not and true. The simple operators include,, (,), +, -, *, / , \, %, ^, &, |, xor, >, >=, <, <=, <>, = and "; The operands include: string constants, numerical constants and logical value constants; The identifier is composed of underscores, numbers and letters and cannot start with a number. In the identifier, lowercase letters are used to represent variables, and uppercase letters are used to represent constants.

5. The method according to claim 1, characterized in that When the expression class is created, the string contains: a String type attribute exp, which holds the processed input string; an int type flag, which indicates the data currently obtained from the string exp, the initial value of flag is 0, flag=0 indicates that the string has not been scanned or a space is obtained, flag=1 indicates that a numerical value is obtained, flag=2 indicates that an operator character is obtained, flag=3 indicates that an identifier is obtained, and the identifier includes a legal identifier and an illegal identifier, flag=4 indicates that a string is obtained; and a point attribute, which indicates the subscript position of the character currently read from the string exp.

6. The method according to claim 1, characterized in that The method for creating an expression class includes: S4011. Determine whether the string is an operator. If the string is an operator, return true; otherwise, return false. S4012, determine whether the string is an operand, if the string is an operand, return true, otherwise return false; S4013, judging whether the string is a legal identifier, if the string is a legal identifier, returning true, otherwise returning false; S4014, determine whether the string is a function name, if the string is a function name, return true, otherwise return false; S4015. Determine whether the string is a variable. If the string is a variable, return true; otherwise, return false.

7. The method according to claim 1, characterized in that The method for creating an operator class includes: S4021. Obtain the priority of the corresponding operator according to the operator and the operation type; S4022. Determine whether the given operator string is the specified operator.