用栈来完成表达式求值

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

#define TRUE 1
#define FALSE 0
#define MAX_SIZE 50
#define LEN sizeof(LinkStackNode)
#define StackElementType char

typedef struct node
{
	StackElementType data;
	struct node* next;
}LinkStackNode;

typedef LinkStackNode* LinkStack;

int InitStack(LinkStackNode **L);
int IsEmpty(LinkStackNode *L);
int IsFull(LinkStackNode *p);
int Push(LinkStackNode *L, StackElementType x);
int Pop(LinkStackNode *L, StackElementType *x);
int GetTop(LinkStackNode *L, StackElementType *x);
int ExpEvaluation(LinkStack operand, LinkStack operator1);
char Compare(StackElementType x, StackElementType ch);
int Grading(StackElementType x);

int main()
{
	LinkStack L_Digit, L_Operator;
	InitStack(&L_Digit);
	InitStack(&L_Operator);
	printf("\n%d\n",ExpEvaluation(L_Digit, L_Operator));
	return 0;
}

int Grading(StackElementType x)
{
	switch(x)
	{
		case '#':
			return -1;
		case '(':
			return 1;
		case '+':
		case '-':
			return 2;
		case '*':
		case '/':
			return 3;
		case '^':
			return 4;
		case ')':
			return 5;
	}
	return (FALSE);
}

char Compare(StackElementType Left, StackElementType Right)
{
	if(Left == '(' && Right == ')')
		return '=';
	if(Left == '(' || Right == '(')
		return '<';
	if(Left == ')' || Right == ')')
		return '>';
	if(Grading(Left) > Grading(Right))
		return '>';
	else if(Grading(Left) < Grading(Right))
		return '<';
	else
	{
		if(Left == '#')
			return '=';
		else
			return '>';
	}
}

int Execute(int a, char op, int b)
{
	switch(op)
	{
		case '+':
			return a + b;
		case '-':
			return a - b;
		case '*':
			return a * b;
		case '/' :
			return a / b;
		case '^':
			return pow(a,b);
	}
}

int ExpEvaluation(LinkStack L_C, LinkStack L_D)
{
	char ch;
	StackElementType top_ch, op, digit, a, b;
	int value = 0, temp;
	Push(L_C, '#');
	printf("\n \nPlase input an expression(Ending with #) :");
	ch = getchar();
	GetTop(L_C, &top_ch);
	while(ch != '#' || top_ch != '#')
	{
		if(ch >= '0' && ch <= '9')
		{
			temp = ch - '0';
			ch = getchar();
			while(ch >= '0' && ch <= '9')
			{
				temp = temp * 10 + ch - '0';
				ch = getchar();
			}
			Push(L_D, temp);
			putchar(temp+'0');
		}
		else
		{
			GetTop(L_C, &top_ch);
			switch(Compare(top_ch, ch))
			{
				case '<':
					Push(L_C, ch);
					ch = getchar();
					break;
				case '=' :
					Pop(L_C, &top_ch);
					ch = getchar();
					break;
				case '>':
					Pop(L_C, &op);
					if(op != '(' && op != ')')
						putchar(op);
					Pop(L_D, &b);
					Pop(L_D, &a);
					value = Execute((int)a, op, (int)b);
					Push(L_D, value);
					break;
			}
		}

		GetTop(L_C, &top_ch);
	}
	GetTop(L_D, &digit);
	value = digit;
	return (value);
}

//进栈操作
int Push(LinkStack top, StackElementType x)
{
	LinkStackNode* temp;
	temp = (LinkStackNode*)malloc(sizeof(LinkStackNode));
	if(temp == NULL)
		return (FALSE);
	temp->data = x;
	temp->next = top->next;
	top->next = temp;
	return (TRUE);
}

//出栈操作
int Pop(LinkStack top, StackElementType *x)
{
	LinkStackNode* temp;
	temp = top->next;
	if(temp == NULL)
		return (FALSE);
	top->next = temp->next;
	*x = temp->data;
	free(temp);
	temp = NULL;
	return (TRUE);
}

int InitStack(LinkStackNode **L)
{
	(*L) = (LinkStackNode*)malloc(LEN);
	(*L)->next = NULL;
	if(*L == NULL)
		return (FALSE);
	else
		return (TRUE);
}

int IsEmpty(LinkStackNode *L)
{
	return (L->next == NULL? TRUE:FALSE);
}

int IsFull(LinkStackNode *p)
{
	int num = 0;
	while(p->next != NULL)
	{
		p = p->next;
		num++;
	}
	return (num == MAX_SIZE? TRUE:FALSE);
}

int GetTop(LinkStackNode *L, StackElementType *x)
{
	if(IsEmpty(L) == TRUE)
		return (FALSE);
	else
	{
		*x = L->next->data;
		return (TRUE);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值