Interesting Data Structure Algorithm10-Introduction to Suffix Expressions and Results of Computing Suffix Expressions Using Stack

Keywords: github

Interesting Data Structure Algorithm10-Introduction to Suffix Expressions and Results of Computing Suffix Expressions Using Stack

The previous day, the stack was used to complete the conversion from binary to octal. However, there are many applications of stack. This time I will explain how to calculate the results of suffix expressions.

Thoughts on Problem Solving

The suffix expression, also known as the inverse Polish expression, is a way of evaluating in the computer. Its evaluation process can be used to store the suffix on the stack.
In general expressions, binary operators are always placed between two related operation objects. This representation is also called infix representation, such as 1 + 2. In 1929, J.Lukasiewicz, a Polish logician, proposed another method of expressing expressions. According to this method, each operator is placed after its operation object, so it is called suffix representation, such as 12+.
Its advantage is that it only uses two simple operations, the stack and the stack can be used to solve the operation of any ordinary expression. Its operation is as follows:
If the current character is a variable or a number, then the stack, if it is an operator, pops up two elements on the top of the stack for corresponding operations, and the results are re-entered into the stack. Finally, when the expression is scanned, the stack is the result.

Assuming that the suffix expression to be evaluated is 12+536+7-, the evaluation process is as follows:
1. Traversing expressions, encountering 1 and 2, are pushed into the stack. At this point, the stack is shown as follows:


2. Then read the + sign, pop up 2 and 1, perform the addition operation, get 3, and put it on the stack again. At this point, the stack is shown as follows:


3. Encounter 5, 3 and 6 and press them into the stack. At this point, the stack is shown as follows:


4. Then read the + sign, pop up 6 and 3, perform the addition operation, get 9, and put it on the stack again. At this point, the stack is shown as follows:


5. Then read the * number, pop-up 9 and 5, perform the addition operation, get 45, re-stack. At this point, the stack is shown as follows:


6. Then read the + sign, pop-up 45 and 3, perform the addition operation, get 48, re-stack. At this point, the stack is shown as follows:


7. Encounter 7 and press it into the stack. At this point, the stack is shown as follows:


8. Then read the-number, pop-up 7 and 48, perform the addition operation, get 41, re-stack.
9. Since the expression has ended, the last element in the stack is read out, which is result 41.

Implementation code

#include <stdio.h>
#include <stdlib.h>
#define Maxsize 10
typedef double Ele;

struct stack
{
	Ele* top;
	Ele* bottom;
	int stacksize;
};

typedef struct stack* Stack;

void init(Stack s){			//Initialization stack
	Ele *p;
	p = (Ele*)malloc(Maxsize * sizeof(Ele));
	if (p == NULL){
		printf("error");
		exit(1);
	}
	s->top = s->bottom = p;
	s->stacksize = Maxsize;
}

void PUSH(Stack s, Ele data){		//Push
	int length;
	length = s->top - s->bottom;
	if (length >= s->stacksize){
		s->bottom = (Ele*)realloc(s->bottom,(s->stacksize + Maxsize) * sizeof(Ele));
		if (s->bottom == NULL){
			printf("memory allocation failed\n");
			exit(1);
		}
	}
	*(s->top) = data;
	s->top++;
}
Ele POP(Stack s){					//Stack out
	Ele num;
	if (s->top == s->bottom){
		printf("There are no elements in the stack!\n");
		exit(1);
	}
	s->top--;
	num = *(s->top);
	return num;
}

int main(){
	struct stack s;			//Definition stack
	char c = 0,str[10];		//c is used to retrieve characters from the keyboard, and str is used to store the numbers entered in each round, which are then converted to double type.
	Ele a1, a2, num;		//a1, a2 for stack operation
	int i = 0;				//temp


	init(&s);				//Initialization stack
	printf("Please enter an equation:");		

	scanf("%c", &c);		//Enter a character
	while (c != '#')
	{
		while ((c <= '9' && c >= '1') || c == '.'){	//This section is used to get a double type number
			str[i++] = c;
			str[i] = '\0';
			if (i >= 10){
				printf("Excessive input data");
				exit(1);
			}
			scanf("%c", &c);
			if (!(c <= '9' && c >= '1') && !(c == '.')){
				a1 = atof(str);						//This function is used to convert strings into bits double
				PUSH(&s, a1);
				i = 0;
				break;
			}
		}
		switch (c)								//Judging whether it belongs to addition, subtraction, multiplication and division
		{
		case '+': 
			a1 = POP(&s);
			a2 = POP(&s); 
			PUSH(&s, a2 + a1);
			break;
		case '-': 
			a1 = POP(&s);
			a2 = POP(&s); 
			PUSH(&s, a2 - a1);
			break;
		case '*': 
			a1 = POP(&s);
			a2 = POP(&s); 
			PUSH(&s, a2 * a1);
			break;
		case '/': 
			a1 = POP(&s);
			a2 = POP(&s); 
			PUSH(&s, a2 / a1);
			break;
		case ' ':
			break;
		case '#':
			break;
		default:
			printf("Error typing symbol!\n");
			break;
		}
		scanf("%c", &c);
	}
	num = POP(&s);
	printf("%.3f\n", num);

	return 0;
}

GITHUB Download Connection

https://github.com/bubbliiiing/Data-Structure-and-Algorithm

I hope to be liked by my friends.
If you have any questions, you can ask them.

Posted by danrl on Wed, 24 Jul 2019 04:30:06 -0700