Use string parsing to complete the design idea of the calculator and the design idea of the calculator

Source: Internet
Author: User

Use string parsing to complete the design idea of the calculator and the design idea of the calculator

Statement:

A correct calculated string is in good format, such as: {ln (10) + 5 * [cos (2 π + 1/4 π) + sin (1 + 2 + 3 π)]}.

Computing is to associate or integrate a series of irrelevant data into a number, which is the meaning of computing.

Body:

As shown in the example, a correct calculated string contains the following formats: Numbers (0 ~ 9), letter (~ Z), Greek character (π Pai), Parentheses ({}, [], (), four Arithmetic Operators (+ ,-,*,/).

Then, according to the normal way of thinking of humans, let's use strings to differentiate priorities, and calculate the highest priority string as a numerical value, when the loop knows that there is no operator with the highest priority (only one number left), the computation is complete and the result is returned.

The incomplete Function Code is as follows:

// Declaration: Currently all operators are single-digit type // return private decimal getValue (string str) {decimal result = 0 based on a reasonable string; // store the int operatorIndex =-1; // The index string operatorType = ""; // store operatorIndex = getOperatorIndex (str); operatorType = getOperatorType (operatorIndex, str); int operator0Index =-1; // store the index of the previous operator 0 in the string // string operator0Type = ""; // store the type of operator0Index = getOperator0I of the previous operator 0 Ndex (str, operatorIndex); // operator0Type = getOperatorType (operator0Index, str); int operator1Index =-1; // store the index of the next operator 1 in the string // string operator1Type = ""; // store the type of operator1Index = getOperator1Index (str, operatorIndex); // operator1Type = getOperatorType (operator1Index, str); double num1 = 0.1000000000000000000001; // store the value of the first operand double num2 = 0.2000000000000000000002; // store the value of the second operand num1 = GetNum1 (operatorIndex, operator0Index, str); num2 = getNum2 (operatorIndex, operator1Index, str); result = Convert. toDecimal (getResult (num1, num2, operatorType); return result;} // The current string starts with a number and finds the priority operator, return the index private int getOperatorIndex (string str) {int operatorIndex =-1; // store the index where the first operator is located for (int I = 0; I <str. length; I ++) {switch (str [I]) {case '*': {operatorIndex = I;} break; case' /': {OperatorIndex = I;} break; case' + ': {operatorIndex = I;} break; case'-': {operatorIndex = I;} break ;}} return operatorIndex;} // returns the index of the previous Operator Based on the index of the Priority operator and the string of the operator. If no value is returned, the index is 0 private int getOperator0Index (string str, int operatorIndex) {int operator0Index =-1; // store the index of the previous operator for (int I = operatorIndex-1; I> = 0; I --) {switch (str [I]) {case '*': {operator0Index = I;} break; cas E '/': {operator0Index = I;} break; case '+': {operator0Index = I;} break; case '-': {operator0Index = I ;} break ;}}if (operator0Index =-1) {operator0Index = 0;} return operator0Index;} // returns the index of the next Operator Based on the index of the Priority operator, the string of the operator, if yes, an index is returned. If no index is returned, private int getOperator1Index (string str, int operatorIndex) {int operator1Index =-1; // store the index of the next operator for (int I = operatorIndex + 1; I <str. le Ngth-1; I ++) {switch (str [I]) {case '*': {operator1Index = I;} break; case '/': {operator1Index = I;} break; case '+': {operator1Index = I;} break; case '-': {operator1Index = I;} break ;}} if (operator1Index =-1) {operator1Index = str. length-1;} return operator1Index;} // return the operator type private string getOperatorType (int operatorIndex, string str) {string operatorType = ""; If (operatorIndex! = 0 | operatorIndex! = Str. length) {operatorType = str [operatorIndex]. toString () ;}return operatorType;} // Based on the index of the current operator, the index of the previous operator, the string of the operator, returns the private double getNum1 (int OperatorIndex, int Operator0Index, string str) {double num1 = 0.10000000000000000000001; num1 = Convert. toDouble (str. substring (Operator0Index, OperatorIndex-Operator0Index); return num1;} // Based on the index of the current operator, the index of the next operator, the string of the operator, returns the second operand of the current operator, private double getNum2 (int OperatorIndex, int Operator1Index, string str) {double num2 = 0.200000000000000000000002; num2 = Convert. toDouble (str. substring (OperatorIndex + 1, Operator1Index-OperatorIndex); return num2;} // return the private double getResult (double num1, double num2, string operatorType) based on two operands and one operator) {double result = 0.000000000000000000000000001; switch (operatorType) {case "+": {result = num1 + num2;} break; case "-": {result = num1-num2 ;} break; case "*": {result = num1 * num2;} break; case "/": {result = num1/num2;} break;} return result ;} private void btn_Empty_Click (object sender, EventArgs e) {textBox1.Text = "";} private void btn_Back_Click (object sender, EventArgs e) {textBox1.Text = textBox1.Text. substring (0, textBox1.Text. length-1 );}

If you are interested in your peers, do not forget to submit suggestions or add features. Contact me.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.