728x90
반응형
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define TRUE	1
#define FALSE	0

/***** Set the Stack *****/
typedef struct _node
{
	int data;
	struct _node* next;
}Node;

typedef struct _stack
{
	Node* head;
	int count;
}Stack;

void StackInit(Stack* pstack)
{
	pstack->head = NULL;
	pstack->count = 0;
}

void Push(Stack* pstack, int data)
{
	Node* newNode = (Node*)malloc(sizeof(Node));
	newNode->data = data;
	newNode->next = pstack->head;

	pstack->head = newNode;
	(pstack->count)++;
}

int Empty(Stack* pstack)
{
	if (pstack->head == NULL)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

int Pop(Stack* pstack)
{
	int temp_data;
	Node* temp;

	if (Empty(pstack))
	{
		printf("Stack Memory Error!");
		exit(-1);
	}

	temp_data = pstack->head->data;
	temp = pstack->head;

	pstack->head = pstack->head->next;
	(pstack->count)--;
	free(temp);

	return temp_data;
}

int Peek(Stack* pstack)
{
	if (Empty(pstack))
	{
		printf("Stack Memory Error!");
		exit(-1);
	}

	return pstack->head->data;
}

int Count(Stack* pstack)
{
	return pstack->count;
}

/***** Set the infix to postfix *****/

int GetOp(char op)
{
	switch (op)
	{
	case '*':
	case '/':
		return 5;

	case '+':
	case '-':
		return 3;

	case '(':
		return 1;
	}

	return -1;
}

int CompareOp(char op1, char op2)
{
	int comOp1 = GetOp(op1);
	int comOp2 = GetOp(op2);

	if (comOp1 > comOp2)
	{
		return 1;
	}

	else if (comOp1 < comOp2)
	{
		return -1;
	}

	else
	{
		return 0;
	}
}

void Covert(char exp[])
{
	Stack stack;
	int expLen = strlen(exp);
	char* convExp = (char*)malloc(expLen + 1);

	int i;
	int idx = 0;
	char tok;
	char popOp;

	memset(convExp, 0, sizeof(char) * expLen + 1);
	StackInit(&stack);

	for (i = 0; i < expLen; i++)
	{
		tok = exp[i];
		if (isdigit(tok))
		{
			convExp[idx++] = tok;
		}
		else
		{
			switch (tok)
			{
			case '(':
				Push(&stack, tok);
				break;

			case ')':
				while (1)
				{
					popOp = Pop(&stack);
					if (popOp == '(')
						break;
					convExp[idx++] = popOp;
				}
				break;

			case '+': case '-':
			case '*': case '/':
				while (!Empty(&stack) && CompareOp(Peek(&stack), tok) >= 0)
				{
					convExp[idx++] = Pop(&stack);
				}
				Push(&stack, tok);
				break;
			}
		}
	}

	while (!Empty(&stack))
	{
		convExp[idx++] = Pop(&stack);
	}
	strcpy(exp, convExp);
	free(convExp);
}

int main(void)
{
	/***** Check the Stack *****/
	//Stack stack;
	//StackInit(&stack);

	//Push(&stack, 1);
	//Push(&stack, 2);
	//Push(&stack, 3);
	//Push(&stack, 4);
	//Push(&stack, 5);

	//printf("%d\n", Count(&stack));

	//while (!Empty(&stack))
	//{
	//	printf("%d", Pop(&stack));
	//}


	/***** Check the infix to postfix *****/
	//char exp1[] = "1+2*3";
	//char exp2[] = "(1+2)*3";
	//char exp3[] = "((1-2)+3)*(5-2)";

	//Covert(exp1);
	//Covert(exp2);
	//Covert(exp3);

	//printf("%s \n", exp1);
	//printf("%s \n", exp2);
	//printf("%s \n", exp3);

	return 0;
}
728x90
반응형

'Data Structure & Algorithm' 카테고리의 다른 글

Circular Queue(based in Array)  (0) 2022.10.29
Spinning Donuts  (0) 2022.10.28
Stack (Calculator)  (0) 2022.10.27
[C] ListBaseStack  (0) 2022.10.26
[C] ArrayBaseStack  (0) 2022.10.26

+ Recent posts