【发布时间】:2009-12-09 22:37:43
【问题描述】:
我正在尝试在 C++ 中实现前缀到中缀,这就是我到目前为止所得到的。例如,输入应该是这样的:
/7+23
然后输出:
7/(2+3) or (7/(2+3))
但是我得到了:
(/)
这是我目前写的代码:
void pre_to_in(stack<char> eq) {
if(nowe.empty() != true) {
char test;
test = eq.top();
eq.pop();
if(test == '+' || test == '-' || test == '/' || test == '*') {
cout << "(";
pre_to_in(eq);
cout << test;
pre_to_in(eq);
cout << ")";
} else {
cout << test;
}
}
}
// somewhere in main()
char arr[30];
stack<char> stosik;
int i = 0;
cout << "write formula in prefix notation\n";
cin >> arr;
while(i < strlen(arr)) {
stosik.push(arr[i]);
i++;
}
pre_to_in(stc);
【问题讨论】:
-
这是作业吗?如果是这样,请将其标记为这样。
-
感谢您先尝试该问题,然后向我们展示您拥有的所有信息。 谢谢。
-
你的意思是按值复制,还是忘记了&,即:pre_to_in(stack
& eq)? -
@Mic。问题已解决。谢谢。
标签: c++ algorithm prefix infix-notation