Problem Description
对于一个基于二元运算符的算术表达式,转换为对应的后缀式,并输出之。
Input
输入一个算术表达式,以‘#’字符作为结束标志。
Output
输出该表达式转换所得到的后缀式。
Example Input
a*b+(c-d/e)*f#
Example Output
ab*cde/-f*+
Hint
Author
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INIT_STACK_SIZE 100000
#define stackincrement 100
typedef int element ;
typedef struct
{
element *base,*top;
int stacksize;
} Sqstack;
int Initstack(Sqstack *s)
{
s->base=(element *)malloc(INIT_STACK_SIZE*sizeof(element ));
if(!s->base)
return -1;
s->top=s->base;
s->stacksize=INIT_STACK_SIZE;
return 0;
}
int Pushstack(Sqstack *s,char ch)
{
if(s->top-s->base>=s->stacksize)
{
s->base=(element *)realloc(s->base,(INIT_STACK_SIZE+stackincrement)*sizeof(element));
if(!s->base) return -1;
s->top=s->base+s->stacksize;
s->stacksize+=stackincrement;
}
*(s->top++)=ch;
return 0;
}
int Popstack(Sqstack *s)
{
if(s->top==s->base) return -1;
--s->top;
return 0;
}
int panduan(char ch) //比较字符的优先级
{
if(ch=='*'||ch=='/')
return 3;
else if(ch=='+'||ch=='-')
return 2;
else if( ch=='#')
return 1;
else
return 0;
}
void biaoda(Sqstack *s)
{
int k=0;
int i;
char ch[100],a[100];
scanf("%s",ch);
Pushstack(s,'#');
for(i=0; i<strlen(ch); i++)
{
if(ch[i]=='*'||ch[i]=='/'||ch[i]=='+'||ch[i]=='-') //当字符为加减乘除时,如果大于栈顶元素优先级,则直接入栈,否则栈顶元素出栈
{
if(panduan(ch[i])>panduan(*(s->top-1)))
{
Pushstack(s,ch[i]);
}
else
{
while(panduan(ch[i])<=panduan(*(s->top-1)))
{
a[k++]=*(s->top-1);
Popstack(s);
}
Pushstack(s,ch[i]);
}
}
else if(ch[i]=='(') //当字符为左括号时,直接入栈
Pushstack(s,'(');
else if(ch[i]==')') //当字符为右括号时,栈顶元素出栈,直到遇到左括号,左括号出栈
{
while(*(s->top-1)!='(')
{
a[k++]=*(s->top-1);
Popstack(s);
}
Popstack(s);
}
else if(ch[i]=='#') //'#'为结束标志,栈里剩余元素全部出栈
{
while(*(s->top-1)!='#')
{
a[k++]=*(s->top-1);
Popstack(s);
}
}
else
{
a[k++]=ch[i];
}
}
for(i=0; i<k; i++) //输出表达式
printf("%c",a[i]);
printf("\n");
}
int main()
{
Sqstack s;
Initstack(&s);
biaoda(&s);
return 0;
}
#include <stdlib.h>
#include <string.h>
#define INIT_STACK_SIZE 100000
#define stackincrement 100
typedef int element ;
typedef struct
{
element *base,*top;
int stacksize;
} Sqstack;
int Initstack(Sqstack *s)
{
s->base=(element *)malloc(INIT_STACK_SIZE*sizeof(element ));
if(!s->base)
return -1;
s->top=s->base;
s->stacksize=INIT_STACK_SIZE;
return 0;
}
int Pushstack(Sqstack *s,char ch)
{
if(s->top-s->base>=s->stacksize)
{
s->base=(element *)realloc(s->base,(INIT_STACK_SIZE+stackincrement)*sizeof(element));
if(!s->base) return -1;
s->top=s->base+s->stacksize;
s->stacksize+=stackincrement;
}
*(s->top++)=ch;
return 0;
}
int Popstack(Sqstack *s)
{
if(s->top==s->base) return -1;
--s->top;
return 0;
}
int panduan(char ch) //比较字符的优先级
{
if(ch=='*'||ch=='/')
return 3;
else if(ch=='+'||ch=='-')
return 2;
else if( ch=='#')
return 1;
else
return 0;
}
void biaoda(Sqstack *s)
{
int k=0;
int i;
char ch[100],a[100];
scanf("%s",ch);
Pushstack(s,'#');
for(i=0; i<strlen(ch); i++)
{
if(ch[i]=='*'||ch[i]=='/'||ch[i]=='+'||ch[i]=='-') //当字符为加减乘除时,如果大于栈顶元素优先级,则直接入栈,否则栈顶元素出栈
{
if(panduan(ch[i])>panduan(*(s->top-1)))
{
Pushstack(s,ch[i]);
}
else
{
while(panduan(ch[i])<=panduan(*(s->top-1)))
{
a[k++]=*(s->top-1);
Popstack(s);
}
Pushstack(s,ch[i]);
}
}
else if(ch[i]=='(') //当字符为左括号时,直接入栈
Pushstack(s,'(');
else if(ch[i]==')') //当字符为右括号时,栈顶元素出栈,直到遇到左括号,左括号出栈
{
while(*(s->top-1)!='(')
{
a[k++]=*(s->top-1);
Popstack(s);
}
Popstack(s);
}
else if(ch[i]=='#') //'#'为结束标志,栈里剩余元素全部出栈
{
while(*(s->top-1)!='#')
{
a[k++]=*(s->top-1);
Popstack(s);
}
}
else
{
a[k++]=ch[i];
}
}
for(i=0; i<k; i++) //输出表达式
printf("%c",a[i]);
printf("\n");
}
int main()
{
Sqstack s;
Initstack(&s);
biaoda(&s);
return 0;
}