C language, simple calculator [Top], C language Calculator
Due to the need to study PHP extensions recently, it is inevitable that the C language is involved. C language has never been used in practical work since I got out of school, so I have to do some review work first. I personally think that the best way to get familiar with one thing is to get started with practice. As a result, I remembered a question assigned by the teacher at the university. I used the C language to analyze and evaluate simple mathematical expressions. Unfortunately, I couldn't finish the question at the beginning. I just want to try it again. It is a supplement to the original homework.
I still remember the original idea was to loop the C string. Store different computing items in a linked list. Then, cyclically evaluate the value. Recursive call is performed if parentheses are encountered. Recall and sort out the original ideas as follows.
1. Enter 3 + 5*(2-6)/2
2. resolved
3. Calculate the result by calculating different priorities in two cycles.
Calculate */for the first time, calculate the current node, and the next node of the node, and assign the value to the next node, and delete its own node.
Evaluate +-for the second time until end
At first, I planned to follow this idea. Later I found that the parsing steps would be complicated and involved multiple string searches and comparisons. In addition, C itself does not support regular expressions, so it gave up its original idea. After finding some relevant information, I found a simpler and more scientific method, which is to convert the input to a suffix expression and then evaluate the value. What is the suffix expression, continue.
I. suffix expression
Baidu encyclopedia Introduction: contains no parentheses. Operators are placed behind two calculation objects. All calculation operations are performed strictly from left to right according to the order in which the operators appear. (The priority rules of operators are not considered, for example: (2 + 1) * 3, that is, 2 1 + 3 *
Only after learning about suffix expressions can we know that the mathematical expressions we used to use are called infix expressions. It took some time to study and find that the calculation of suffix expressions is quite simple and more suitable for computer operations.
Calculation method, from left to right. The first two digits of the operator number are used for calculation. The calculation result replaces the operator and the first two digits, and continues to calculate the result to the rightmost.
This may be difficult to understand. Let's look at several examples.
The simplest 21 +
The calculation process is 2 + 1 = 3
The value is 3.
Ordinary 325-2 * +
In the calculation process, 2-5 =-3 is calculated from left to right, and 3-32 * + is replaced by-3. The complete calculation steps are as follows:
After calculation 2-5 = 3, the expression is 3-3 2 * +.
After calculation-3*2 =-6, the expression is 3-6 +.
After calculation 3 +-6 =-3, the expression is-3.
The value is-3.
A little more complicated 21 + 3*5387 -/*-
In the calculation process, 2 + 1 = 3, 4 is calculated from left to right to replace the previous 21 + and then 33*5387-/*. The complete calculation steps are as follows:
After calculation 2 + 1 = 3, the expression is 3 3*5 3 8 7 -/*-
After calculation 3*3 = 9, the expression is 9 5 3 8 7 -/*-
After 8-7 = 1 is calculated, the expression is 9 5 3 1 /*-
After calculation 3/1 = 3, the expression is 9 5 3 *-
After calculation 5*3 = 15, the expression is 9 15-
Calculate 9-15 =-6 and the result is-6.
The value is-6.
After reading the above results, we will find that each number involved in the calculation is the two digits closest to the operator number. Then, the calculated result replaces the numbers and operators involved in the operation until there is only one value left in the expression, and the calculation is complete.
According to this rule, the program is easy to process.
1. Loop the entire input from left to right.
2. determine whether it is a number. If it is a number, save it. If a symbol is encountered, the first two values are saved and the calculation result is saved back.
3. After the loop is completed, the remaining expression is the calculation result.
According to the above rules, it is not difficult to find that the two numbers involved in computing are stored at the end, so that we can use the stack to easily complete such a program. The following is a brief introduction to the stack.
Ii. Stack
Baidu Baike's explanation is complicated and won't be excerpted. In fact, the stack can be simply understood as a space for storing data, and the data is accessed according to the principle of first-in-first-out. Data operations include push and pop, which are called push and pop.
Because it is relatively simple, a simple stack is implemented directly using code, which includes the following four methods.
The input result is
Item is: 1.120000
Item is: 2.800000
2.800000
1.120000
After the expected output is realized, a simple stack is done, and then the entire simple stack can be used to complete the value-seeking function.
Iii. Specific implementation of suffix expression
Since the stack has been implemented, you only need to perform operations based on the logic of the suffix expression to evaluate the value in combination with the stack to implement the entire computing process. The method is relatively simple. A simple suffix expression evaluate function can be completed by processing numbers and operators differently with the while loop and switch. The implementation code of the first version is as follows:
In the first version, a C language knowledge point is that the C language string Pointer Points to the address of the first character of the string.
So when I is 0, & str [I] = & str, atof receives a string pointer. If STACKpush (atof (& str [I]) is used, when I is 0, the entire string is passed in for conversion. A char variable is used to copy str [I] and input & num to solve this problem.
In fact, this program also involves a pointer operation knowledge point, but the procedures involved here are relatively easy to understand, and there are more difficult areas to involve this knowledge point in the future, so I will share it with you later.
The above method is tested. input the three expressions we analyzed and the result is as follows:
Printf ("% f \ n", calculate ("21 +"); // 3
Printf ("% f \ n", calculate ("325-2 * +"); //-3
Printf ("% f \ n", calculate ("21 + 3*5387-/*-"); //-6
The test is passed and the previous calculation result remains unchanged. The above is a simple extension expression calculation program. After several tests, we found a small problem, that is, multi-Digit Recognition is currently unavailable. Because the program once pushed a single digit into the station. I thought about adding a space character to each calculated item of the expression as a digital distinction. Changed to 21 3 +, so I made a little change to the code.
Added space processing and multi-Digit Processing in the switch.
// If a space is encountered, the flag is reset.
In this way, you can recognize multiple digits. Tested
Printf ("% f \ n", calculate ("21 1 +"); // 22
Printf ("% f \ n", calculate ("2 11 +"); // 13
The correct result is obtained. The suffix expression is calculated in the first step of the simple calculator.
If you are interested, you can test the Code. If you encounter any problems, you can report it to me through the public account.
Of course, this program still has some to be improved. For example, the expression validity check, the processing of decimal places, and the processing of negative numbers are reserved for the time being. We will share with you how to implement and what knowledge points will be used.
The next article describes how to convert an infix expression into a suffix expression in the second step of simple mathematical expression calculation.
Welcome to the public account ~