-2

これが私のエラーです:

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';
}

これは、接尾辞関数を取得して評価しようとしているだけです。

4

1 に答える 1

1

else if(isOp[input[i]])

isOp配列ではなく関数です。が必要isOp(input.substr(i,1))isOp、ブール配列を作成する必要があります。

于 2013-03-01T03:41:45.737 に答える