您的位置:首页 > 理论基础 > 数据结构算法

数据结构之栈:算数表达式的转换 (sdut oj2484)

2016-08-02 19:21 330 查看


算术表达式的转换



Time Limit: 1000MS Memory limit: 65536K


题目描述

小明在学习了数据结构之后,突然想起了以前没有解决的算术表达式转化成后缀式的问题,今天他想解决一下。
   因为有了数据结构的基础小明很快就解出了这个问题,但是他突然想到怎么求出算术表达式的前缀式和中缀式呢?小明很困惑。聪明的你帮他解决吧。


输入

 输入一算术表达式,以\'#\'字符作为结束标志。(数据保证无空格,只有一组输入)


输出

 输出该表达式转换所得到的前缀式 中缀式 后缀式。分三行输出,顺序是前缀式 中缀式 后缀式。


示例输入

a*b+(c-d/e)*f#



示例输出

+*ab*-c/def
a*b+c-d/e*f
ab*cde/-f*+


提示

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

int cmp(char c)
{
if(c == '+'||c == '-') return 1 ;
if(c == '*'||c == '/') return 2 ;
if(c == '(')  return 3 ;
if(c == ')')  return 4 ;
return 0 ;
}

void Qianzhui(char a[])
{
int top2 = -1, top3=-1;
int l = strlen(a);
char stack2[100], stack3[100];
for(int i = l-2; i >= 0; i--)
{
if(a[i] >= 'a'&&a[i]<= 'z')
{
stack3[++top3] = a[i];
}
else
{
if(top2 == -1||stack2[top2] == ')'|| a[i] == ')')
{
stack2[++top2] = a[i];
}
else if(a[i] == '(')
{
while(stack2[top2] != ')')
{
stack3[++top3] = stack2[top2--];
}
top2--;
}
else
{
if(cmp(a[i]) < cmp(stack2[top2]))
{
stack3[++top3]= stack2[top2--];
i++;
}
else
{
stack2[++top2] = a[i];
}

}
}
}
while(top2 != -1)
{
stack3[++top3] = stack2[top2--];
}
for(int i = top3; i >= 0; i--)
printf("%c", stack3[i]);
printf("\n");
}

void Zhongzhui(char a[])
{
for(int i = 0; a[i] != '#'; i++)
{
if(a[i] !='(' &&a[i] != ')')
printf("%c", a[i]);
}
printf("\n");
}

void Houzhui(char a[])
{
int top1 = 0;
char stack1[100] ;
for(int i = 0; a[i] != '#'; i++)
{
if(a[i] >= 'a'&&a[i] <= 'z')
{
printf("%c", a[i]) ;
}
else
{
if(top1 == 0)
{
top1++ ;
stack1[top1] = a[i] ;
}
else if(cmp(a[i]) > cmp(stack1[top1]))
{
if(cmp(a[i]) == 4)
{
while(stack1[top1] != '(')
{
printf("%c", stack1[top1--]);
}
top1-- ;
}
else
{
top1++ ;
stack1[top1] = a[i];
}
}
else
{
if(stack1[top1] != '(')
{
printf("%c", stack1[top1]) ;
stack1[top1] = a[i] ;
}
else
{
top1++ ;
stack1[top1] = a[i] ;
}
}
}
}
while(top1 != 0)
{
printf("%c", stack1[top1]) ;
top1-- ;
}
printf("\n") ;
}

int main()
{
char a[1005];
scanf("%s",a);
Qianzhui(a);
Zhongzhui(a);
Houzhui(a);
return 0;
}


2:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int judge(char a,char b)
{
if((a=='*'||a=='/')&&(b=='+'||b=='-'))
return 1;
else if((a=='+'||a=='-')&&(b=='*'||b=='/'))
return -1;
else if((a=='+'||a=='-')&&(b=='+'||b=='-'))
return 0;
else if((a=='*'||a=='/')&&(b=='*'||b=='/'))
return 0;
}
void houzhui(char a[])
{
char c2[1005],c1[1005];
int t1,t2,i;
t1=t2=-1;
for(i=0; a[i]!='#'; i++)
{
if(a[i]!='*'&&a[i]!='-'&&a[i]!='+'&&a[i]!='/'&&a[i]!='('&&a[i]!=')')
{
c2[++t2]=a[i];
}
else
{
if(t1==-1||c1[t1]=='('||a[i]=='(')
{
c1[++t1]=a[i];
//  printf("%c",c1[t1]);
}
else if(a[i]==')')
{
//t1--;
while(c1[t1]!='(')
{
c2[++t2]=c1[t1--];
}
t1--;
}
else
{
if(judge(a[i],c1[t1])==1)
c1[++t1]=a[i];
else
{
c2[++t2]=c1[t1--];
i--;
}
}
}
}
while(t1!=-1)
{
c2[++t2]=c1[t1--];
}
for(int i=0; i<=t2; i++)
printf("%c",c2[i]);
printf("\n");

}
void zhongzhui(char a[])
{
char c2[1005],c1[1005];
int t1,t2,i;
t1=t2=-1;
for(i=0; a[i]!='#'; i++)
{
if(a[i]!='('&&a[i]!=')')
{
c2[++t2]=a[i];
}
}
for(int i=0; i<=t2; i++)
printf("%c",c2[i]);
printf("\n");

}
void qianzhui(char a[])
{
char c2[1005],c1[1005];//c2��c1���符
int t1,t2,i;
t1=t2=-1;
int l=strlen(a);
for(i=l-2; i>=0; i--)
{
if(a[i]!='*'&&a[i]!='-'&&a[i]!='+'&&a[i]!='/'&&a[i]!='('&&a[i]!=')')
{
c2[++t2]=a[i];
}
else
{
if(t1==-1||c1[t1]==')'||a[i]==')')
{
c1[++t1]=a[i];
//  printf("%c",c1[t1]);
}
else if(a[i]=='(')
{
//t1--;
while(c1[t1]!=')')
{
c2[++t2]=c1[t1--];
}
t1--;
}
else
{
if(judge(a[i],c1[t1])==-1)
{
c2[++t2]=c1[t1--];
i++;
}
else
c1[++t1]=a[i];
}
}
}
while(t1!=-1)
{
c2[++t2]=c1[t1--];
}
for(int i=t2; i>=0; i--)
printf("%c",c2[i]);
printf("\n");

}
int main()
{
char a[1005];
scanf("%s",a);
qianzhui(a);
zhongzhui(a);
houzhui(a);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构