表达式
2025-02-16 10:58:46
发布于:浙江
中缀转后缀
#include<bits/stdc++.h>
using namespace std;
stack<char>st;
string s,t;
bool level(char a,char b){
if(a=='*' || a=='/')return 1;
else{
if(b=='+' || b=='-')return 1;
}
return 0;
}
int main(){
cin>>s;
for(int i=0;i<s.size();i++){
if(s[i]>='a' && s[i]<='z'){
t+=s[i];
}
else if(s[i]=='('){
st.push('(');
}
else if(s[i]==')'){
while(st.top()!='('){
t+=st.top();
st.pop();
}
st.pop();
}
else{
while(st.size() && st.top()!='('){
if(level(st.top(),s[i])){
t+=st.top();
st.pop();
}
else{
break;
}
}
st.push(s[i]);
}
}
while(st.size()){
t+=st.top();
st.pop();
}
cout<<t;
return 0;
}
中缀转前缀
#include<bits/stdc++.h>
using namespace std;
stack<char>st;
string s,t;
bool level(char a,char b){
if(a=='*' || a=='/')return 1;
else{
if(b=='+' || b=='-')return 1;
}
return 0;
}
int main(){
cin>>s;
for(int i=s.size()-1;i>=0;i--){
if(s[i]>='a' && s[i]<='z'){
t+=s[i];
}
else if(s[i]==')'){
st.push(')');
}
else if(s[i]=='('){
while(st.top()!=')'){
t+=st.top();
st.pop();
}
st.pop();
}
else{
while(st.size() && st.top()!=')'){
if(level(st.top(),s[i])){
t+=st.top();
st.pop();
}
else{
break;
}
}
st.push(s[i]);
}
}
while(st.size()){
t+=st.top();
st.pop();
}
reverse(t.begin(),t.end());
cout<<t;
return 0;
}
后缀表达式求值
#include<bits/stdc++.h>
using namespace std;
stack<int>st;
int main(){
string s;
cin>>s;
for(int i=0;i<s.size();i++){
if(isdigit(s[i])){
st.push(s[i]-'0');
}
else if(s[i]=='+'){
int a=st.top();
st.pop();
int b=st.top();
st.pop();
st.push(b+a);
}
else if(s[i]=='-'){
int a=st.top();
st.pop();
int b=st.top();
st.pop();
st.push(b-a);
}
else if(s[i]=='*'){
int a=st.top();
st.pop();
int b=st.top();
st.pop();
st.push(b*a);
}
else if(s[i]=='/'){
int a=st.top();
st.pop();
int b=st.top();
st.pop();
st.push(b/a);
}
}
cout<<st.top();
return 0;
}
前缀表达式求值
#include<bits/stdc++.h>
using namespace std;
stack<int>st;
int main(){
string s;
cin>>s;
reverse(s.begin(),s.end());
for(int i=0;i<s.size();i++){
if(isdigit(s[i])){
st.push(s[i]-'0');
}
else if(s[i]=='+'){
int a=st.top();
st.pop();
int b=st.top();
st.pop();
st.push(a+b);
}
else if(s[i]=='-'){
int a=st.top();
st.pop();
int b=st.top();
st.pop();
st.push(a-b);
}
else if(s[i]=='*'){
int a=st.top();
st.pop();
int b=st.top();
st.pop();
st.push(a*b);
}
else if(s[i]=='/'){
int a=st.top();
st.pop();
int b=st.top();
st.pop();
st.push(a/b);
}
}
cout<<st.top();
return 0;
}
中缀表达式求值(缝合怪)
#include<bits/stdc++.h>
using namespace std;
stack<char>st;
stack<int>stt;
string s,t;
bool level(char a,char b){
if(a=='*' || a=='/')return 1;
else{
if(b=='+' || b=='-')return 1;
}
return 0;
}
int main(){
cin>>s;
for(int i=0;i<s.size();i++){
if(isdigit(s[i])){
t+=s[i];
}
else if(s[i]=='('){
st.push('(');
}
else if(s[i]==')'){
while(st.top()!='('){
t+=st.top();
st.pop();
}
st.pop();
}
else{
while(st.size() && st.top()!='('){
if(level(st.top(),s[i])){
t+=st.top();
st.pop();
}
else{
break;
}
}
st.push(s[i]);
}
}
while(st.size()){
t+=st.top();
st.pop();
}
for(int i=0;i<t.size();i++){
if(isdigit(t[i])){
stt.push(t[i]-'0');
}
else if(t[i]=='+'){
int a=stt.top();
stt.pop();
int b=stt.top();
stt.pop();
stt.push(b+a);
}
else if(t[i]=='-'){
int a=stt.top();
stt.pop();
int b=stt.top();
stt.pop();
stt.push(b-a);
}
else if(t[i]=='*'){
int a=stt.top();
stt.pop();
int b=stt.top();
stt.pop();
stt.push(b*a);
}
else if(t[i]=='/'){
int a=stt.top();
stt.pop();
int b=stt.top();
stt.pop();
stt.push(b/a);
}
}
cout<<stt.top();
return 0;
}
全部评论 1
2025-02-16 来自 浙江
0
有帮助,赞一个