Calculator function implementation (stack)

Posted by deolsabh on Wed, 29 Dec 2021 18:52:23 +0100

Calculator function implementation (stack)

input

15*5+(5+1)/6-7

output

69

Problem solving ideas

Because priority should be considered in actual calculation, we need to establish a function. Here, I name it get (char x). X is the formula we enter, I set the priority of "(") as' 0 'at the lowest, followed by' + 'and' - 'as 1,' * ',' / 'as 2, and ^ as 3 at the highest. We also need to establish a function for calculating temporary partial results, which I name ls here (int a, int b, int c) C is the operator we take out from it. A and B are two adjacent values we take out from the stack. Because of the first in first out principle, we should put the first taken out later for calculation, otherwise the result will be wrong.
After entering the formula, we first establish two stacks to store values and symbols respectively, and then use size to calculate the length of our formula for circulation. In order to be able to read the value of multiple bits, we use the continuous multiplication of k by 10 and the continuous cycle of bits to calculate. When the calculation is completed, press it into the number stack.
We need to judge when we encounter characters, When "(" When a character that is not a bracket is encountered, we need to compare its priority with the character at the top of the stack. If the top of the stack is empty or the top of the stack priority is less than the priority of the symbol we read, we will directly press it into the stack. If the top of the stack priority is greater than the priority of the character we currently traverse, we will take out the symbol at the top of the stack first, Take two values from the digital stack for calculation. Remember to pop the top of the stack after taking them out. After calculation, press the calculated values back into the stack. Keep cycling until the character priority at the top of the stack is less than that of our current character, and then push the current character into the stack. Then go through the next step.
When the ")" is encountered, you need to stop traversing the formula. You need to constantly take out the characters that have been arranged according to the priority from the stack for operation until the "encountered" is traversed ("" stop. Then continue to traverse. After traversal, you need to calculate the remaining characters and numbers in the stack until the character stack is empty. Finally, take out the top of the number stack and end.

Example

15∗5+(5+1)/6-7
First, traverse to 1 is a number k=0 * 10 + 1 = 1, and the second is 5 or a number k=1 * 10 + 5 = 15
The third traversal to * is not a number, and 15 is pressed into the number stack. Because the character stack is empty, so * is directly pressed into the stack. The fourth traversal to 5 is a number, k=0 * 10 + 5 = 5. The fifth traversal to + is not a number, and 5 is pressed into the number stack. The priority of * is higher than +, so take out *, 5, 15, Calculate m=15 * 5 = 75 (remember that 15 should be in front, first in and last out, last in and first out. Although multiplication is no big problem, it will be very different if the subtraction rule is encountered). Press 5 and 15 pop-up 75 into the digital stack, the symbol stack pops up *, the symbol stack is empty, press + and continue to traverse. The sixth traversal is to (, directly into the stack, the seventh 5 is a number, k=0 * 10 + 5 = 5, and the eighth is + is not a number. Press 5 into the digital stack because (has the lowest priority, so + has a higher priority than (so press + directly into the top of the stack, the 9th is 1, which is a number, k=0 * 10 + 1 = 1, and the 10th is yes). If it is not a number, press 1 into the digital stack because it is encountered), then you need to stop the subsequent traversal until it pops up (so far, take out 1 and 5 in the digital stack, the top of the stack + in the symbol stack, calculate m=5+1=6, pop up 1 and 5, and then pop up +. The top of the subsequent stack is (, pop up) (, at this time, there is still + left in the stack. After pressing 6 at the top of the digital stack, there are 75,6 from the bottom of the stack. The 11th is /, / has a priority greater than +, so / directly enters the stack. The 12th is 6, which is a number, k=0 * 10 + 6 = 6, and the 13th is - not a number. Press 6 into the digital stack - has a priority less than /, so take /, 6, 6, and calculate m=6/6=1. Pop 6, 6, 1, press into the digital stack, and / push out the stack At present, the priority of +, + in the symbol stack = -, then take out +, 1, 75 for operation, m=75+1=76, the symbol stack pops up +, press in -, the number stack pops up 1, 75, press in 76, and finally traverse to 7, press in the number stack. After traversal, take out -, 7, 75,
m=76-7=69; Pop up - pop up 7, 76 and press 69. At this time, the symbol stack is empty and the top of the number stack is 69, which is also the only number in the number stack. The final result is 69;

Code display and comments

#include<bits/stdc++.h>
using namespace std;
stack<char>fh;//Build character stack 
stack<int>sz;//Build digital stack 
string t;
int sum=0,p1,p2,m;
int get(char x)//Judgment priority 
{
	if(x=='(') return 0;
	if(x=='+'||x=='-') return 1;
	if(x=='*'||x=='/') return 2;
	if(x=='^') return 3;
 } 
int ls(int a,int b,char c)//Use the values and symbols taken from the stack to calculate 
{
	if(c=='+') return b+a;//Since the stack is first in and last out, the value obtained first should be placed later, otherwise the result will be wrong 
	if(c=='-') return b-a;
	if(c=='*') return b*a;
	if(c=='/') return b/a;
	if(c=='^') return pow(b,a);
}
int main()//Main function 
{
	cin>>t;
	int i;
	for(i=0;i<t.size();)//Traversal string t.size(); Is the length 
	{	int k=0;
		if(t[i]>='0'&&t[i]<='9')//In order to get the value of multi bit number 
		{
			while(t[i]>='0'&&t[i]<='9')//Until the next non number is traversed 
			{
			k=k*10+t[i]-'0';
			i++;
			}
			sz.push(k);//Press the extracted value into the stack 
		}
		else{
			if(t[i]=='(') fh.push(t[i]);//When '(' is encountered, it is directly pushed into the stack 
			else if(t[i]!=')')
			{
				if(fh.empty()||get(fh.top())<get(t[i])) fh.push(t[i]);//When the top of the stack is empty or the priority of the top of the stack is less than the priority of the symbols we read, it is directly pushed into the stack
				else
				{
					while(!fh.empty()&&get(fh.top())>=get(t[i]))//When the priority of the top of the stack is greater than the symbol we are currently traversing, the symbols in the stack will be operated until the priority of the symbol at the top of the stack is less than the current symbol 
					{
					p1=sz.top();sz.pop();
					p2=sz.top();sz.pop();
					m=ls(p1,p2,fh.top());
					fh.pop(); 
					sz.push(m);
					}
					fh.push(t[i]);
				}
			}
			else
			{
				while(fh.top()!='(')//When the right parenthesis is encountered, the traversal after the formula will be stopped, and the characters that have been arranged according to the priority need to be continuously extracted from the stack for operation until the traversal stops when it encounters "(" 
					{
					p1=sz.top();sz.pop();
					p2=sz.top();sz.pop();
					m=ls(p1,p2,fh.top());
					fh.pop(); 
					sz.push(m);
					}
					fh.pop();
			}
			i++;
		}
	}
	while(!fh.empty())//Calculate the remaining characters in the stack 
	{
		p1=sz.top();sz.pop();
		p2=sz.top();sz.pop();
		m=ls(p1,p2,fh.top());
		fh.pop(); 
		sz.push(m);
	}
	sum=sz.top();//The top element of the digital stack is the final value 
	cout<<sum<<endl;
	return 0;
}

ending

Hope to help you!!!
If you have any questions, please leave a message below. Thank you

Topics: C Algorithm stack