PTA 7-20 表达式转换 (25分)

算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。日常使用的算术表达式是采用中缀表示法,即二元运算符位于两个运算数中间。请设计程序将中缀表达式转换为后缀表达式。

输入格式:

输入在一行中给出不含空格的中缀表达式,可包含+-*\以及左右括号(),表达式不超过20个字符。

输出格式:

在一行中输出转换后的后缀表达式,要求不同对象(运算数、运算符号)之间以空格分隔,但结尾不得有多余空格。

输入样例:

2+3*(7-4)+8/4

输出样例:

2 3 7 4 - * + 8 4 / +
#include<stdio.h>
#include<stdlib.h>

typedef char ElementType;
typedef struct SNode *Stack;
struct SNode
{
	ElementType *Data;
	int Top;
	int MaxSize;
};
Stack CreatStack( int MaxSize)
{
	Stack S=(Stack)malloc(sizeof(struct SNode));
	S->Data=(ElementType *)malloc(sizeof(ElementType)*MaxSize);
	S->Top=-1;
	S->MaxSize=MaxSize;
	return S; 
	
}
void Push(Stack S,ElementType X)
{
	if(S->MaxSize-1==S->Top)
	{
		printf("stack full\n");
	}
	else
	S->Data[++(S->Top)]=X;
}
ElementType Pop(Stack S)
{
	if(S->Top==-1)
	printf("Stack empty");
	else
	return S->Data[(S->Top)--];
}
int Compare(char c)         //设置优先级 
{
	int flag;
	if(c=='*'||c=='/')
		flag=2;
	if(c=='+'||c=='-')
		flag=1;
	if(c=='(')
		flag=0;
		
	return flag;
}
bool IsEmpty(Stack S)
{
	return S->Top==-1;
}
bool IsFull(Stack S)
{
	return S->Top==S->MaxSize-1;
} 
ElementType top(Stack S)
{
	if(IsEmpty(S))
	{
		printf("stack empty");
		return false;
	}
	else
	{
		return S->Data[S->Top];
	}
}
bool isDight(char c)
{
	if(c>='0' && c<='9')
	{
		return true;
	}
	else
	{
		return false;
	}
}
int main()
{
	char c,ch,c1;
	int sign1=0,sign2=0,i=0; 
	Stack S=CreatStack(100);
	while((c=getchar())!='\n')
	{
		if( (c>='0'&&c<='9') ||c=='.')
		{
			if(sign2)
			{
				printf(" ");
				sign2=0;
			}
		    printf("%c",c);
			sign1=1;
		}
		else
		{
		    if( (c=='+'||c=='-') && ( sign1==0 || ( sign1&&(!isDight(c1)) && c1!=')') ) )//当操作数前有正负号时 
			{
				if(c=='-')
				printf("%c",c);
			}
			else if(IsEmpty(S))
			{
				Push(S,c);
				if(c!='(')      //假设第一个字符是( 时 
				sign2=1;
			}
			else if(c=='(')
			{
			
				Push(S,c);	
			}
			else if(c==')')
			{
				while((ch=Pop(S))!='(')
				{
					printf(" %c",ch); 
				} 
			}
			else
			{
				while( Compare(c)<=Compare(top(S)) )	//遇到比当前更低的运算符才停止弹出
				{
					printf(" %c",Pop(S));
					if(IsEmpty(S))
					{
						break;
					}
				} 
				Push(S,c);
				sign2=1;
			}
		}
	    c1=c;
	}
	while(!IsEmpty(S))
	printf(" %c",Pop(S)); 
	return 0;
}

 

序号    输入                     输出                                   说明
0    2+3*(7-4)+8/4            2 3 7 4 - * + 8 4 / +            正常测试6种运算符
1    ((2+3)*4-(8+2))/5       2 3 + 4 * 8 2 + - 5 /            嵌套括号
2    1314+25.5*12           1314 25.5 12 * +               运算数超过1位整数且有非整数出现
3    -2*(+3)                       -2 3 *                                运算数前有正负号
4    123                            123                                   只有一个数字
测试点数据参考博文链接:https://blog.csdn.net/qq_41231926/article/details/84778007 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值