如何解决我的问题是我们为什么要使用#include<bits/stdc++.h>?
这个头文件是在哪里定义的 解释这个头文件是如何在这个程序中使用的以及它是如何工作的。这段代码是有效的,但我不知道这个头文件在这个程序中是如何使用的。通常我们为堆栈定义类及其内部函数,但在这里我们有没有完成,我们只是使用这个头文件和如何将中缀转换为后修复表达式的功能。
#include<bits/stdc++.h>
using namespace std;
int prec(char c)
{
if(c == '^')
return 3;
else if(c == '*' || c == '/')
return 2;
else if(c == '+' || c == '-')
return 1;
else
return -1;
}
void infixToPostfix(string s)
{
std::stack<char> st;
st.push('N');
int l = s.length();
string ns;
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('(');
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 << endl;
}
int main()
{
string exp = "a+b*(c^d-e)^(f+g*h)-i";
infixToPostfix(exp);
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。