以下是我的错误:
postfix.cpp: In function ‘int main()’:
postfix.cpp:32: warning: pointer to a function used in arithmetic
postfix.cpp:87: warning: pointer to a function used in arithmetic
postfix.cpp:93: warning: pointer to a function used in arithmetic
postfix.cpp:99: warning: pointer to a function used in arithmetic
postfix.cpp:140: error: expected ‘(’ before ‘{’ token
postfix.cpp:140: error: expected type-specifier before ‘{’ token
postfix.cpp:140: error: expected ‘)’ before ‘{’ token
postfix.cpp:146: error: redeclaration of ‘std::string input’
postfix.cpp:20: error: ‘std::string input’ previously declared here
我想知道前4个错误是什么。我需要解决所有错误,但我主要关注的是前4个。
以下是该计划:
#include <iostream>
#include <stack>
#include <string>
#include <cstdlib>
using namespace std;
bool isOp(string s);
bool isDigit(int ch);
int main()
{
system("clear");
bool keepGoing = true;
cout << "@@@@@@@@@@@@@@@@ THE POSTFIX EVALUATOR @@@@@@@@@@@@@@@@" << endl;
while(keepGoing)
{
string input;
cout << "Your RPN Expression : ";
getline(cin, input);
try
{
stack<int> s;
bool anySemicolon = false;
for (int i = 0; i < input.size(); i++)
if (input[i] == ' ')
return 0;
else if(isOp[input[i]])
{
cout << "Token = " << input[i] << "\t";
if(s.size() == 0)
throw "Stack underflow exception.";
int b = s.top();
s.pop();
cout << "Pop " << b << "\t";
if(s.size() == 0)
throw "Stack underflow exception.";
int a = s.top();
s.pop();
cout << "Pop " << a << "\t";
int res;
switch(input[i])
{
case '+':
res = a+b;
break;
case '-':
res = a-b;
break;
case '*':
res = a*b;
break;
case '/':
if(b == 0)
{
cout << endl;
throw "Division by zero exception.";
}
res = a/b;
break;
case '%':
if(b == 0)
{
cout << endl;
throw "Division by zero exception.";
}
res = a%b;
break;
}//End switch
s.push(res);
cout << "Push " << res << endl;
}//End else if statement
else if(isDigit[input[i]] || input[i] == '_')
{
bool negative = input[i] == '_';
if(negative)
{
if(i == input.size()-1 || !isDigit[input[i+1]])
throw "You have entered an invalid expression.";
i++;
}
int value = 0;
while(i < input.size() && isDigit[input[i]])
{
value *= 10;
value += input[i]-'0';
i++;
}
i--;
s.push(value);
cout << "Token = " << s.top() << "\tPush " << s.top() << endl;
}
else if(input[i] == ';')
{
cout << "Token = ;\t";
anySemicolon = true;
i++;
while(i < input.size() && (input[i] == ' ' || input[i] == '\t'))
i++;
if(i < input.size())
{
cout << endl;
throw "No more tokens after ; is expected.";
}
}
else
{
throw "You have entered an invalid expression.";
}
if(!anySemicolon)
throw "Semicolon expected.";
if(s.size() == 0)
throw "Stack underflow exception.";
else if(s.size() != 1)
throw "Stack overflow exception.";
else
cout << "Pop " << s.top() << endl;
}
catch
{
cout << " Now Exiting." << endl;
return 1;
}
cout << "Go Again? (Y/N) ";
string input;
getline(cin, input);
if (input[0] != 'n' && input[0] != 'N')
keepGoing = true;
}//End while
return 0;
}// End Main
bool isOp(char s)
{
return (s == ';' || s == '_' ||
s == '+' || s == '-' || s == '*' || s == '/' ||
s == '%' );
}
bool isDigit(int ch)
{
return ch >= '0' && ch <= '9';
}
这只是尝试使用后缀函数并对其进行评估。
答案 0 :(得分:1)
else if(isOp[input[i]])
isOp
是一个函数,而不是数组。您需要isOp(input.substr(i,1))
,或者需要使isOp
成为布尔数组。