中缀表达式转成后缀表达式 后缀表达式求值

#include <iostream>
#include <cctype>
#include <string>
using namespace std;

const int MAXSIZE = 100;
typedef char Datatype;
typedef struct{ int top; Datatype data[MAXSIZE]; }Seqstack, *pSeqstack;

pSeqstack init(void)
{
	pSeqstack s;
	s = (pSeqstack)malloc(sizeof(Seqstack));
	if (s)
		s->top = -1;
	return(s);
}

int empty(pSeqstack s)
{
	if (s->top == -1)
		return(1);
	else
		return(0);
}

int push(pSeqstack s, Datatype x)
{
	if (s->top == MAXSIZE - 1)
		return(0);
	else
	{
		++s->top;
		s->data[s->top] = x;
		return(1);
	}
}

int pop(pSeqstack s, Datatype *x)
{
	if (empty(s))
		return(0);
	else
	{
		*x = s->data[s->top];
		--s->top;
		return(1);
	}
}

int get_top(pSeqstack s, Datatype *x)
{
	if (empty(s))
		return(0);
	else
	{
		*x = s->data[s->top];
		return(1);
	}
}
/********************************************************************************
使用一个栈用来存放操作数,从左向右依此扫描表达式,若是操作数则直接入栈,
若是操作符则取出栈中两个操作数做运算在放入,表达式扫描结束后,栈顶元素就是结果。
********************************************************************************/
//double postfix_exp(const char *a)
//{
//	double t0, t1, t2;
//	pSeqstack s=init();
//	while (a && *a)
//	{
//		if (isalnum(*a))
//			push(s, *a-'0');
//		else
//		{
//			pop(s, &t2);
//			pop(s, &t1);//注意取出操作数的顺序
//			switch (*a)
//			{
//			case '+' : t0 = t1 + t2; break;
//			case '-' : t0 = t1 - t2; break;
//			case '*' : t0 = t1 * t2; break;
//			case '/' : t0 = t1 / t2; break;
//			case '%' : t0 = (int)t1 % (int)t2; break;
//			}
//			push(s, t0);
//		}
//		a++;
//	}
//	get_top(s, &t0);
//	return(t0);
//}

/********************************************************************************
将中缀表达式转换为后缀表达式:

从左向右扫描表达式,若是操作数,则输出;
若不是操作数:
	若当前栈顶为#,且该操作符为#,则终止;
	否则将该操作符压栈;
	若当前栈顶不是#,
		若栈顶元素是‘(’,并且当前元素是‘)’,则去括号;
							否则将操作符压栈;
		若栈顶元素不是‘(’,则比较两个元素优先级,
		若栈顶元素优先级高,则输出当前操作符;
		否则,压栈操作符。
********************************************************************************/
int get_prio(char op)
{
	switch (op)
	{
	case '#':return(1);
	case ')':return(2);
	case '+':
	case '-':return(3);
	case '*':
	case '/':return(4);
	case '(':return(5);
	default:return(0);
	}
}

string mid_to_post(const char *a)
{
	pSeqstack s = init();
	string re;
	char ch, temp;
	while (*a)
	{
		if (*a >= '0' && *a <= '9')
			re += *a;
		else
		{
			get_top(s, &ch);

			if (ch == '(')
			{
				if (*a == ')')
					pop(s, &temp);
				else
					push(s, *a);
			}
			else
			{
				while (get_prio(ch) >= get_prio(*a) && ch != '(' && !empty(s))
				{
					re += ch;
					pop(s, &temp);
					get_top(s,&ch);
				}
				if (ch == '(' && *a == ')')
					pop(s, &temp);
				else 
				 push(s, *a);
			}
		}
		a++;
	}
	while (!empty(s))
	{
		pop(s, &ch);
		re += ch;
	}
	return re;
}



int main()
{
	char* r = "1285-*+42/-";
	char* t = "1+2*(8-5*(2-5))-4/2";
	cout << r << endl;

	string s = mid_to_post(t);
	cout << s << endl;
	//double rs = postfix_exp(s);
	//cout << rs << endl;
	
	while (1)
	{

	}
	return(1);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值