題目:給定一個中綴表達式,求其后綴表達式並輸出結果;
以下是轉換的思路:
⑴ 初始化兩個棧:運算符棧s1和儲存中間結果的棧s2;
⑵ 從左至右掃描中綴表達式;
⑶ 遇到操作數時,將其壓s2;
⑷ 遇到運算符時,比較其與s1棧頂運算符的優先級:
① 如果s1為空,或棧頂運算符為左括號“(”,則直接將此運算符入棧;
② 否則,若優先級比棧頂運算符的高,也將運算符壓入s1;
③ 否則,將s1棧頂的運算符彈出並壓入到s2中,再次轉到①與s1中新的棧頂運算符相比較;
⑸ 遇到括號時:
① 如果是左括號“(”,則直接壓入s1;
② 如果是右括號“)”,則依次彈出s1棧頂的運算符,並壓入s2,直到遇到左括號為止,此時將這一對括號丟棄;
⑹ 重復步驟⑵至⑸,直到表達式的最右邊;
⑺ 將s1中剩余的運算符依次彈出並壓入s2;
⑻ 依次彈出s2中的元素並輸出,結果的逆序即為中綴表達式對應的后綴表達式
然后就可以按照后綴表達式來計算了,(注意每一步都要輸出,其中有最后輸出空格和回車是無所謂的,強迫症把它們都去了防止不過,其實不去就好,省的弄錯,嘻嘻~)不會的可以參考后綴表達式
第一部分:中綴轉后綴:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
using namespace std;
char a[100020],s1[10000],s2[100000];
long long n,m,top1,top2;
int lev(char ch)
{
if(ch=='+' || ch=='-')
return 1;
if(ch=='*'||ch=='/')
return 2;
if(ch=='^')
return 3;
return 0;
}
int main()
{
scanf("%s",a);
n=strlen(a);
for(int i=0;i<n;i++)
{
if(a[i]>='0'&&a[i]<='9')
s2[++top2]=a[i];
else
{
if(a[i]=='(')
{
s1[++top1]=a[i];
continue;
}
if(lev(s1[top1])>=lev(a[i])&&a[i]!=')')
{
s2[++top2]=s1[top1--];
while(lev(s1[top1])>=lev(a[i]))
s2[++top2]=s1[top1--];
s1[++top1]=a[i];
continue;
}
if(lev(s1[top1])<lev(a[i])&&a[i]!=')')
{
s1[++top1]=a[i];
continue;
}
if(a[i]==')')
{
while(s1[top1]!='(')
{
s2[++top2]=s1[top1--];
}
top1--;
continue;
}
}
}
while(top1>0)
{
s2[++top2]=s1[top1--];
}
for(int i=1;i<=top2;i++)
cout<<s2[i]<<" ";
return 0;
}
第二部分:后綴表達式的求值:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char a[2000];
int s[2000],top;
int main()
{
scanf("%s",a);
int l=strlen(a);
for(int i=0;i<l-1;i++)
{
if(a[i]>='0'&&a[i]<='9')
{
s[++top]=a[i]-'0';
while(a[i+1]>='0'&&a[i+1]<='9')
{
s[top]=s[top]*10+a[i+1]-'0';
i++;
}
continue;
}
else
{
if(a[i]=='.')
continue;
int y=s[top],x=s[--top];
if(a[i]=='+')
{
s[top]=x+y;
continue;
}
if(a[i]=='-')
{
s[top]=x-y;
continue;
}
if(a[i]=='*')
{
s[top]=x*y;
continue;
}
if(a[i]=='/')
{
s[top]=x/y;
continue;
}
}
}
printf("%d",s[top]);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
char s1[100010],s2[100010],s3[100010],a[100010];
long long top2,top1,p;
long long js[100010],topjs;
int lev(char n)
{
if(n=='+'||n=='-') return 1;
if(n=='*'||n=='/') return 2;
if(n=='^') return 3;
return 0;
}
void print()
{
for(int i=1;i<=topjs;i++)
{
cout<<js[i]<<" ";
}
for(int i=p+1;i<=top2;i++)
{
cout<<s2[i];
if(i!=top2)
cout<<" ";
}
if(p!=top2)
cout<<endl;
}
int main()
{
long long n;
scanf("%s",a);
n=strlen(a);//???gets()
for(int i=0;i<n;i++)
{
if(a[i]>='0'&&a[i]<='9')
{
s2[++top2]=a[i];
}
else
{
if(a[i]=='(')
{
s1[++top1]=a[i];
continue;
}
if(s1[top1]=='('||top1==0)
{
s1[++top1]=a[i];
continue;
}
if(lev(s1[top1])>=lev(a[i])&&a[i]!=')')
{
s2[++top2]=s1[top1--];
while(lev(s1[top1])>=lev(a[i]))
{
s2[++top2]=s1[top1--];
}
s1[++top1]=a[i];
continue;
}
if(lev(s1[top1])<lev(a[i])&&a[i]!=')')
{
s1[++top1]=a[i];
continue;
}
if(a[i]==')')
{
while(s1[top1]!='(')
{
s2[++top2]=s1[top1--];
}
--top1;
continue;
}
}
}
/*for(int i=top1;i>=1;i--)
{
cout<<s1[i]<<endl;
}*/
while(top1>0)
{
s2[++top2]=s1[top1--];
} //???????????????????,????s2???
for(int i=1;i<=top2;i++)
{
cout<<s2[i]<<" ";
}
cout<<endl;//?????
for(int i=1;i<=top2;i++)
{ p=i;
if(s2[i]>='0'&&s2[i]<='9')
{
js[++topjs]=s2[i]-'0';
}
else
{
if(s2[i]=='+')
{
long long y=js[topjs];
long long x=js[--topjs];
long long ans=x+y;
js[topjs]=ans;
}
if(s2[i]=='-')
{
long long y=js[topjs];
long long x=js[--topjs];
long long ans=x-y;
js[topjs]=ans;
}
if(s2[i]=='*')
{
long long y=js[topjs];
long long x=js[--topjs];
long long ans=x*y;
js[topjs]=ans;
}
if(s2[i]=='/')
{
long long y=js[topjs];
long long x=js[--topjs];
long long ans=x/y;
js[topjs]=ans;
}
if(s2[i]=='^')
{
long long y=js[topjs];
long long x=js[--topjs];
long long ans=pow(x,y);
js[topjs]=ans;
}
print();
}
}
return 0;
}