請告訴我,我做錯了什麼? 我需要確保表達式是平衡 我什麼都試過,但我甚至不得到錯誤平衡表達與堆棧
int main() {
ifstream infile;
infile.open("input.txt");
string exp;
cout << "Enter an expression ";
while (getline(infile, exp)) {
cout << exp << ": ";
if (matcher(exp))
cout << "Matched ok" << endl;
else
cout << "Match error" << endl;
cout << "Enter an expression: ";
}
cout << "--- Done ---" << endl;
return 0;
}
int matcher(string expression) {
stack<char> s;
for (int i = 0; i < expression.length(); i++) {
if (isOpener(expression[i]))
s.push(expression[i]);
else if (isCloser(expression[i])) {
if (s.empty()) return 1;
char opener = s.top();
s.pop();
if (!matches(opener, expression[i])) return 1;
}
}
if (!s.empty()) return 1;
return 0;
}
這裏有什麼問題? – tod
定義的'matches'函數在哪裏? '匹配(開叫者,表達[我])'? –
@ user3612601那麼如果你在問題中提到它會更好。 –