中缀到 C++ 中的后缀转换
infix to postfix conversion in c++
所以在这里我有一个中缀到后缀转换的代码。这个想法是发送一个包含中缀公式的字符串,并返回该公式的后缀表示法。操作器+ - * / () [] {}
函数tezinaOperacije()
如果操作+
或-
则返回 1,如果操作*
或/
则返回 2。否则返回 -1。当您在打开的括号上调用函数时,该函数zatvorenaZagrada()
返回右右括号。问题是该函数返回一个空字符串。有人可以帮忙吗?
string infixToPostfix(string& str){
stack<char> stek;
string s="";
for(int i=0; i<str.size(); i++){
s+=s[i];
}
char pre='(';
string copy="";
for(int i=0; i<s.size(); i++){
if(s[i]>='0' && s[i]<='9'){
if(pre==')' || prei==']' || pre=='}' || pre=='0') throw "wrong";
char number=s[i];
copy+=number;
pre=='0';
}
else if(s[i]=='(' || s[i]=='[' || s[i]=='{'){
if(pre=='0' || pre==')') throw "Wrong";
char sign=s[i];
stek.push(sign);
pre=sign;
}
else if(s[i]==')' || s[i]==']' || s[i]=='}'){
char sign=s[i];
while(!stek.empty() && tezinaOperacije(stek.top())>0){
copy+=stek.top();
stek.pop();
}
if(stek.empty() || sign !=ZatvorenaZagrada(stek.top())) throw "wrong declaration";
stek.pop();
pre=sign;
}
else if(tezinaOperacije(s[i])>0){
char sign=s[i];
if(tezinaOperacije(pre)>0 || pre=='(' || pre=='[' || pre=='{') throw "wrong";
while(!stek.empty() && tezinaOperacije(stek.top())>=tezinaOperacije(sign)){
copy+=stek.top();
stek.pop();
}
stek.push(sign);
pre=sign;
}
}
while(!stek.empty()){
copy+=stek.top();
stek.pop();
}
return copy;
}
string s="";
for(int i=0; i<str.size(); i++){
s+=s[i];
//---------^ s?
}
您获得的s
为零 ( ' '
)
我想你的意图是
string s="";
for(int i=0; i<str.size(); i++){
s+=str[i];
//---------^^^
}
s
充满零的情况下,以下for
for(int i=0; i<s.size(); i++){
if(s[i]>='0' && s[i]<='9'){
// ...
}
else if(s[i]=='(' || s[i]=='[' || s[i]=='{'){
// ...
}
else if(s[i]==')' || s[i]==']' || s[i]=='}'){
// ...
}
else if(tezinaOperacije(s[i])>0){
// ...
}
}
不做任何有用的东西,copy
保持空。
相关文章:
- 使用带有链表的堆栈数据结构将中缀转换为后缀
- 中缀后缀转换C ++,似乎无法得到正确的答案
- 如何使用 ASCII 转换使用字符堆栈计算后缀表达式
- 使用 C++ 将中缀转换为后缀(包括赋值运算符)
- 中缀到 C++ 中的后缀转换
- 使用堆栈的后缀转换器中缀
- 将中缀转换为后缀并获取分段错误核心转储错误
- 中缀到后缀表达式转换代码不起作用
- 使用Stacks从中缀表达式转换为后缀(C++)
- 我想看到一个在整个后缀表达式的上下文中查找转换类型 id 的示例
- 将中缀转换为后缀:无法识别堆栈错误
- 将中缀转换为后缀表示法
- 这个后缀转换 C++ 代码的中缀有什么问题
- 中缀到后缀转换器 + 评估程序正确打印到控制台,但不正确地打印到文本文件
- 使用堆栈将前缀转换为后缀
- 中缀到后缀转换括号错误
- 使用链表将中缀转换为后缀时出错
- 将后缀转换为求值的程序
- 中缀到后缀转换的代码
- 使用堆栈从后缀转换为中缀