Example #1
0
long double Calculation::Resolve(const string& str) const
    throw(invalid_argument)
{
    if (!CheckParenthesis(str))
        throw invalid_argument("The number of left parenthesis is not equal with"
                               " the number of right parenthesis");

    /*
     * TODO
     *  To test special cases.
     *      5 + * 2
     *      5 + 2 *
     *      * 5 + 2
     *
     *      5 (2 + 3)
     *      (2 + 3) 5
     *
     *      5 2 * 3
     *      5 sin(90)
     *
     *      sin(90) cos(90)
     *      sin 90 cos(45)
     *
     *  To test if there are the functions that are called.
     */



    Lexer l;
    list<Token*> tokens = l.Split(str);
    long double r;

    if (l.PrintErrors())
    {
        throw invalid_argument("Invalid input");
    }

    if (tokens.size() == 0)
    {
        throw invalid_argument("No tokens");
    }

    AddZero(tokens);

    try
    {
        r = Resolve(tokens);

        for (auto it = tokens.begin(); it != tokens.end(); ++it)
            delete (*it);

        return r;
    }
    catch (invalid_argument& e)
    {
        for (auto it = tokens.begin(); it != tokens.end(); ++it)
            delete (*it);

        throw e;
    }
}