C语言程序:用栈实现四则运算

用栈实现四则运算

第一部分 :LinkStack.h

#ifndef LINKSTACK_H
#define	LINKSTACK_H

#include 
#include 
#define SUCCESS 10000
#define FAILURE 10001
#define	TRUE	10002
#define FALSE	10003

typedef struct StackNode
{
	int data;
	struct StackNode *next;
}Node;

typedef struct StackInfo
{
	struct StackNode *top;
	int length;
}Stack;

int InitStack(Stack **s);
int Push(Stack*s, int e);
int EmptyStack(Stack *s);
int GetTop(Stack *s);
int Pop(Stack *s);
int ClearStack(Stack *s);
int DestroyStack(Stack **s);
#endif


第二部分 :LinkStack.c

#include "LinkStack.h"

int InitStack(Stack **s)
{
	if (NULL == s)
	{
		return FAILURE;
	}
	*s = (Stack *)malloc(sizeof(Stack));
	if (NULL == *s)
	{
		return FAILURE;
	}
	(*s)->top = NULL;
	(*s)->length = 0;
	return SUCCESS;
}


int Push(Stack *s, int e)
{
	if (NULL == s)
	{
		return FAILURE;
	}

	Node *tmp = (Node *)malloc(sizeof(Node));
	if (NULL == tmp)
	{
		return FAILURE;
	}

	tmp->data = e;
	tmp->next = s->top;
	s->top = tmp;
	s->length++;

	return SUCCESS;
}

int EmptyStack(Stack *s)
{
	if (NULL == s)
	{
		return FAILURE;
	}
	return s->top == NULL ? TRUE : FALSE;
}

int GetTop(Stack *s)
{
	if (NULL == s)
	{
		return FAILURE;
	}
	if (NULL == s->top)
	{
		return FAILURE;
	}
	return s->top->data;
}

int Pop(Stack *s)
{
	if (NULL == s)
	{
		return FAILURE;
	}
	if (s->top == NULL)
	{
		return FAILURE;
	}

	int tmp;
	Node *stmp = s->top;
	tmp = s->top->data;
	s->top = stmp->next;
	free(stmp);
	stmp = NULL;
	s->length--;

	return tmp;

}

int ClearStack(Stack *s)
{
	if (NULL == s)
	{
		return FAILURE;
	}
	Node *tmp;
	while (s->top != NULL)
	{
		tmp = s->top->next;
		free(s->top);
		s->top = tmp;
	}
	s->length = 0;
	return SUCCESS;
}

int DestroyStack(Stack **s)
{
	if (NULL == s)
	{
		return FAILURE;
	}

	free(*s);
	*s = NULL;
	return SUCCESS;
}

第三部分 :Calculator.c

#include "LinkStack.h"

int Priority(char ch)	//判断操作符优先级
{
	switch (ch)
	{
		case '(':
			return 3;
		case '*':
		case '/':
			return 2;
		case '+':
		case '-':
			return 1;
		default :
			return 0;
	}
}

int main()
{
	Stack *s_num, *s_opt;
	char opt[128]  ={0};
	int i = 0, tmp = 0;
	int num1, num2;
	if ((InitStack(&s_num) == FAILURE) || (InitStack(&s_opt) == FAILURE))
	{
		printf("Init Failure !\n");
	}
	printf("输入四则表达式:");
	scanf("%s", opt);

	while (opt[i] != '\0' || EmptyStack(s_opt) != TRUE)	//表达式没结束 或 操作符不为空
	{
		if (opt[i] >= '0' && opt[i] <= '9')	//判断表达式是否为数字,数字直接进栈
		{
			tmp = tmp * 10 + opt[i] - '0';	//后一个为数字时 前一个乘以10 加上后一个
			i++;
			if (opt[i] >'9' || opt[i] < '0')	//判断后一个是否为操作符
			{
				Push(s_num, tmp);	//操作数进栈
				tmp = 0;	//清零,用以记录下一个操作数
			}
		}
		else	//表达式为操作符
		{
			//	1、操作符出栈不计算
			if (GetTop(s_opt) == '(' && opt[i] ==')')	//直接出栈,不计算,栈顶为'(' ,表达式为')'
			{
				Pop(s_opt);	//括号直接出栈
				i++;
				continue;	//继续下一次循环
			}
			//	2、操作符进栈
			if ((EmptyStack(s_opt) == TRUE) 	//操作符栈为空
				|| (Priority(opt[i]) > Priority(GetTop(s_opt))) 	//表达式操作符优先级 > 栈顶操作符优先级
				|| (GetTop(s_opt) == '(' && opt[i] != ')')) 	//栈顶为'('  && 表达式当前值不为 ')'
			{
				Push(s_opt,opt[i]);	//操作符进栈
				i++;
				continue;	//继续下一次循环
			}
			//	2、操作符出栈计算	
			if (Priority(opt[i]) <= Priority(GetTop(s_opt)) 	//表达式操作符优先级 <= 栈顶操作符优先级
				|| (opt[i] == '0' && EmptyStack(s_opt) != TRUE) 	//表达式为空 && 操作符栈不为空
				|| opt[i] == ')')	//表达式为')'
			{
				num1 = Pop(s_num);	//数字栈顶出栈
				num2 = Pop(s_num);	//数字栈第二个数字出栈
				switch (Pop(s_opt))	//后出的数字在前
				{
					case '+':
						Push(s_num, num2 + num1);
						break;
					case '-':
						Push(s_num, num2 - num1);
						break;
					case '*':
						Push(s_num, num2 * num1);
						break;
					case '/':
						Push(s_num, num2 / num1);
						break;
				}	A  
				// i++;			//在操作符进栈时已经自加
				// continue;
			}
		}
	}

	printf("Result = %d\n", GetTop(s_num));
	return 0;
}

至此已经实现四则混合运算

你可能感兴趣的:(C语言)