a/c cho em hỏi code bài này e code theo code của geeksforgeeks, khi chạy biểu thức của a+b*(c^d-e)^(f+g*h)-i thì đúng mà chạy biểu thức này thì lại sai: A * (B+C–D/E)/F
a/c cho em hỏi là sai chỗ nào không ạ và sửa như nào ạ. em cảm ơn ạ;
#include<bits/stdc++.h>
using namespace std;
int prec(char c){
if( c == '(') return 0;
if( c == '+' || c == '-' ) return 1;
if( c == '*' || c == '/' || c == '%' ) return 2;
if( c == '^' ) return 3;
}
void infixToPostfix(string s){
int l=s.length(); string ns;
stack<char> st; st.push('N');
for(int i=0;i<l;i++){
if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')) {
ns+=s[i];
}
else if(s[i]=='('){
st.push(s[i]);
}
else if(s[i]==')'){
while(st.top()!='N'&&st.top()!='('){
char c=st.top();
st.pop();
ns+=c;
}
if(st.top()=='(') {
char c = st.top();
st.pop();
}
}
else{
while(st.top()!='N'&&prec(s[i])<=prec(st.top())){
char c=st.top();
st.pop();
ns+=c;
}
st.push(s[i]);
}
}
while(st.top()!='N'){
char c=st.top();
st.pop();
ns+=c;
}
cout<<ns;
}
main(){
string exp = "A*(B+C–D/E)/F";
infixToPostfix(exp);
}