当前位置: 首页>>代码示例>>C++>>正文


C++ TokenStream::setToken方法代码示例

本文整理汇总了C++中TokenStream::setToken方法的典型用法代码示例。如果您正苦于以下问题:C++ TokenStream::setToken方法的具体用法?C++ TokenStream::setToken怎么用?C++ TokenStream::setToken使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TokenStream的用法示例。


在下文中一共展示了TokenStream::setToken方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: expression

double expression(TokenStream& tokenStream)
{
    double result = term(tokenStream);       // set result to value of term
    Token token = tokenStream.getToken();  // get next token from tokenStream

    while (true)
    {
        if (token.kind == '+')
        {
            result += term(tokenStream);      // evaluate term and add to result
            token = tokenStream.getToken(); // get next token from tokenStream
        }

        if (token.kind == '-')
        {
            result -= term(tokenStream);      // evaluate term and subtract from result
            token = tokenStream.getToken(); // get next token from tokenStream
        }

        if (token.kind != '+' && token.kind != '-')
        {
            tokenStream.setToken(token);    // reset token into token stream
            return result;                    // return result
        }
    }
}
开发者ID:silverbowen,项目名称:Calculator,代码行数:26,代码来源:calculator01.cpp

示例2: calculate

void calculate()
{
    const char quit = 'q';       // t.kind==quit means that t is a quit Token
    const char print = ';';      // t.kind==print means that t is a print Token
    const std::string prompt = "> ";
    const std::string equals = "= ";  // used to indicate that what follows is an evaluation
    TokenStream tokenStream;
    std::string enterToClose = "~~";

    while (std::cin)
        try
        {
            std::cout << prompt;
            Token token = tokenStream.getToken();

            if (token.kind == print)
                token = tokenStream.getToken();   // eat ';'

            if (token.kind == quit)
                break;

            tokenStream.setToken(token);      // reset token into tokenStream
            std::cout << equals << expression(tokenStream) << '\n';
        }

        catch (std::exception& e)
        {
            std::cerr << e.what() << '\n';                      // write error message
            tokenStream.ignore(print);
        }
}
开发者ID:silverbowen,项目名称:Calculator,代码行数:31,代码来源:calculator01.cpp

示例3: term

double term(TokenStream& tokenStream)
{
    double result = primary(tokenStream);   // set result to value of primary
    Token token = tokenStream.getToken(); // get next token from tokenStream

    while (true)
    {
        if (token.kind == '*')
        {
            result *= primary(tokenStream);   // evaluate primary and multiply with result
            token = tokenStream.getToken(); // get next token from tokenStream
        }

        else if (token.kind == '/')
        {
            double divisor = primary(tokenStream);     // set divisor to value of primary
            if (divisor == 0) error("divide by zero"); // check for error
            result /= divisor;                           // divide result by divisor
            token = tokenStream.getToken();      // get next token from tokenStream
        }

        else if (token.kind == '%') // modulo will only work on ints for this implementation
        {
            int numer = result; // cast result to int
            int denom = primary(tokenStream); // cast denom to int
            if (denom == 0) error("%: divide by zero"); // error check
            result = numer % denom; // get result
            token = tokenStream.getToken();      // get next token from tokenStream
        }

        else
        {
            tokenStream.setToken(token);   // put token back into the Token stream
            return result;                 // return result
        }
    }
}
开发者ID:silverbowen,项目名称:Calculator,代码行数:37,代码来源:calculator01.cpp


注:本文中的TokenStream::setToken方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。