【发布时间】:2015-03-14 21:19:49
【问题描述】:
我在这个程序中收到std::logic_error: basic_string::_s_construct null not valid。我该如何解决?我已经尝试过previously posted solution,但我自己无法更正。
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main()
{
stack<string> s;
string temp, exp[] = "abc-+de-fg-h+/*";
string ch1 = 0, ch2 = 0, ch = 0;
int sizeExp = sizeof(exp)/sizeof(*exp);
for(int i=0; i < sizeExp ; i++) {
ch = exp[i];
if (ch == "*" || ch == "/" || ch == "+" || ch == "-") {
if (s.empty() && sizeof(temp) != sizeof(exp)) {
cout << "Error in Expression." << endl;
}
else {
if (sizeof(s.top()) != sizeof(char)){
ch1 = s.top();
s.pop();
ch2 = s.top();
temp = '(' + ch2 + ')' + exp[i] + '(' + ch1 + ')';
s.push(temp);
}
else {
ch1 = s.top();
s.pop();
ch2 = s.top();
temp = ch2 + exp[i] + ch1;
s.push(temp);
}
}
}
else {
s.push(exp[i]);
}
}
cout << temp << endl << "Is your Infix Expression for ";
cout << exp;
return 0;
}
【问题讨论】:
-
您参考的是以前的哪个解决方案?
标签: c++