This document describes how to use Visual C # To automatically calculate four mixed operations. the NET1.1 framework does not yet contain all ready-made classes. However, after the introduction below, you can write one by yourself. What's the role? Nothing, just familiarize yourself with the stack usage, or if you have an elementary school, you can write a program to practice the four mixed operations for your child. Haha, you don't have to talk about it. Let's talk about the subject.
A typical arithmetic problem: (6 + 2*5)/4
From the question, we first calculate 2*5 = 10, then calculate 6 + 10 = 16, and finally calculate 16/4 = 4. Therefore, the result is 4.
How is the computer counted? Fortunately, our predecessors listed a bunch of algorithms for us. We can just select one.
First Algorithm
Stack is used to solve the problem, that is, to push the lowest possible to the bottom of the stack. According to the principle of advanced and backward release, the lowest priority is the final calculation.
Computing process:
We have created two stacks: one is the data stack and the other is the computing symbol stack. Let's take the example of (6 + 2*5)/4 to see how the bottom layer is computed.
Hypothesis:
1) Priority
The priority between symbols is as follows:
"(") "-1
"+", "-" 0
"*", "/" 1
The greater the value, the higher the priority.
2) Set "(", ")" as a special operator, I .e., a single object operation. The two adjacent operators can be canceled.
3) calculation conditions
(1) The current operator is not equal to "" (Special Terminator)
(2) Number of running computations in the operator stack> = 1
(3) When the output stack port has a higher operator priority than the operator to be added to the stack or the two can be canceled.
During calculation, the symbol output stack is used for calculation. The first two elements of the value stack exit Stack are used for calculation. The calculation result value is pushed to the value stack for recursion.
1) "(" pushed into the symbol stack 2) "6" pushed into the numerical Stack
3) "(" and "+" compare the priority. If "(" is lower than "+", the calculation conditions are not met, and "+" is pushed into the symbol stack.
1) press "2" into the value stack.
2) Compare "*" with "+". If the "+" priority is lower than "*", the calculation conditions are not met. Press "*" into the symbol stack.
1) Press "5" into the number planting stack. 2) Compare "*" and ")" to get "*" to ")" with a higher priority. Calculate and take "*", "5", and "2" out of the stack for calculation.
1) press the result of 2*5 = 10 into the numerical stack.
2) (recursion) compares "+" and ")" to "+" to ")". The priority is higher. After calculation, the "+" outbound stack, "10", and "6" stacks are used for calculation.
1) press the result of 6 + 10 = 16 into the numerical stack.
2) (recursive) Compare ")" and "(" priority, to get the two can be canceled, "(" symbol out of the stack, and ")" cancel, continue to take the next symbol.
1) Import "/" into the symbol stack.
2) Add "4" to the value stack.
3) if the "" formula Terminator is found, the "/", "4", and "16" are computed.
1) press the calculation result into the numerical stack.
Successful! The hard work of computing has finally been completed, and it seems much more complicated than the human brain computing :)
Second Algorithm
In the second method, we will briefly mention that the detailed process is not described here. The second method is to use a tree to organize a formula into a regular tree, and then traverse and calculate the result. The formula above is used as an example to illustrate the final tree style: (Note that the "()" symbol must be specially processed)
You can use the in-depth traversal of the tree to calculate the final result.
Program Implementation
This article provides the C # implementation method for processing four arithmetic operations using stacks, as follows:
CalUtility. cs
Using System;
Namespace Calculate
{
/// <Summary>
/// Summary of CalUtility.
/// Read-computation Tool
/// </Summary>
Public class CalUtility
{
System. Text. StringBuilder StrB;
Private int iCurr = 0;
Private int iCount = 0;
/// <Summary>
/// Constructor
/// </Summary>
Public CalUtility (string calStr)
{
StrB = new System. Text. StringBuilder (calStr. Trim ());
ICount = System. Text. Encoding. Default. GetByteCount (calStr. Trim ());
}
/// <Summary>
/// Take the segment to automatically analyze the value or operator
/// </Summary>
/// <Returns> </returns>
Public string getItem ()
{
// Ended
If (iCurr = iCount)
Return "";
Char ChTmp = StrB [iCurr];
Bool B = IsNum (ChTmp );
If (! B)
{
ICurr ++;
Return ChTmp. ToString ();
}
String strTmp = "";
While (IsNum (ChTmp) = B & iCurr <iCount)
{
ChTmp = StrB [iCurr];
If (IsNum (ChTmp) = B)
StrTmp + = ChTmp;
Else
Break;
ICurr ++;
}
Return strTmp;
}
/// <Summary>
/// Whether it is a number
/// </Summary>
/// <Param name = "c"> content </param>
/// <Returns> </returns>
Public bool IsNum (char c)
{
If (c> = '0' & c <= '9') | c = '.')
{
Return true;
}
Else
{
Return false;
}
}
/// <Summary>
/// Whether it is a number
/// </Summary>
/// <Param name = "c"> content </param>
/// <Returns> </returns>
Public bool IsNum (string c)
{
If (c. Equals (""))
Return false;
If (c [0]> = '0' & c [0] <= '9') | c [0] = '.')
{
Return true;
}
Else
{
Return false;
}
}
/// <Summary>
/// Compare the priorities of str1 and str2 operators. True indicates that str1 is higher than str2, false indicates that str1 is lower than str2
/// </Summary>
/// <Param name = "str1"> operator 1 </param>
/// <Param name = "str2"> operator 2 </param>
/// <Returns> </returns>
Public bool Compare (string str1, string str2)
{
Return getPriority (str1)> = getPriority (str2 );