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


C++ TokenScanner::hasMoreTokens方法代码示例

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


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

示例1: processLine

void processLine(string line, Program & program, EvalState & state) {
   TokenScanner scanner;
   scanner.ignoreWhitespace();
   scanner.scanNumbers();
   scanner.setInput(line);
//   Expression *exp = parseExp(scanner);
//   int value = exp->eval(state);
//   cout << value << endl;
//   delete exp;
   if (!scanner.hasMoreTokens()) {
       return;
   }
   string firstToken = toUpperCase(scanner.nextToken());
   if (firstToken == "RUN") {
       runProgram(program, state);
   }
   else if (firstToken == "QUIT") {
       exit(0);
   }
   else if (firstToken == "HELP") {
       dspHelp();
   }
   else if (firstToken == "CLEAR") {
       program.clear();
   }
   else if (firstToken == "LIST") {
       listCmd(program);
   }
   else if (firstToken == "INPUT" || firstToken == "PRINT" || firstToken == "LET") {
       scanner.saveToken(firstToken);
       Statement * stmt = parseStatement(scanner);
       stmt->execute(state);
       delete stmt;
   }
   else {
       int lineNumber = stringToInteger(firstToken);
       if (!scanner.hasMoreTokens()) {
           program.removeSourceLine(lineNumber);
       }
       else {
           int length = firstToken.length();
           string source = line.substr(length);
           program.addSourceLine(lineNumber, source);
           Statement * stmt = parseStatement(scanner);
           program.setParsedStatement(lineNumber, stmt);
       }
   }
}
开发者ID:mariolew,项目名称:CS106B,代码行数:48,代码来源:Basic.cpp

示例2: sqrtFunction

/* Function: sqrtFunction()
 * -------------------------
 * Library sqrt function execution. Expects for brackets and involves
 * formulaStringScanning process for expression in brackets. Return sqrt function result
 * for obtained value in brackets.
 *
 * @param scanner   TokenScanner for user formula */
double sqrtFunction(TokenScanner& scanner) {
    bool sqrtFailFlag = false;
    string token = "";
    double sqrtParam = 0;

    if (scanner.hasMoreTokens()) {
        token = scanner.nextToken();//waiting for brackets at this place
        if (token == "(") {
            bool bracketsOpened = true;
            /* Calls internal scanFormulaString process for expression in brackets */
            sqrtParam = scanFormulaString(scanner, bracketsOpened);
        } else {
            sqrtFailFlag = true;
        }
    }

    if (sqrtFailFlag) {
        cout << "   - SQRT FUNCTION FAULT" << endl;
        FAIL_FLAG = true;
        return 0;
    }

    /* Returns sqrt function result */
    return sqrt(sqrtParam);
}
开发者ID:shpp-ekuryatenko,项目名称:cs-b-assignment1_Calculator,代码行数:32,代码来源:Calculator.cpp

示例3: processText

/**
 * Receive information from file, processes and displays the result on the screen.
 */
void processText(TokenScanner & scanner){
    int counterSentences = 0;
    int counterWords = 0;
    int counterSyllable = 0;
    double grade = 0.0;

    scanner.ignoreWhitespace(); // ignores spaces
    scanner.addWordCharacters("'"); // does part of the token is the character "'"

    // processes each token
    while (scanner.hasMoreTokens()){
        string token = scanner.nextToken();
        counterSentences += checkSentences(token);
        if(checkWords(token)){
            counterWords ++;
            counterSyllable += colSyllable(token);
        }
    }
    // If the file does not contain any words or sentences, assume that their one
    if(counterWords == 0 || counterSentences == 0) counterSentences = counterWords = 1;

    grade = getGrade(counterSentences, counterWords, counterSyllable);

    cout << "Words : " << counterWords << endl;
    cout << "Syllable : " << counterSyllable << endl;
    cout << "Sentences : " << counterSentences << endl;
    cout << "Grade : " << grade << endl;
}
开发者ID:vlad-alekseev,项目名称:other,代码行数:31,代码来源:FleschKincaid.cpp

示例4: isHaiku

bool isHaiku(string filename) {
    fstream infile;
    infile.open(filename.c_str());
    string temp;
    int count = 0;
    TokenScanner scanner;
    while(getline(infile, temp)){
        count++;
        int syllables = 0;
        scanner.setInput(temp);
        while (scanner.hasMoreTokens()){
            syllables += syllablesIn(scanner.nextToken());
        }
        if((count == 1 || count ==3)&&syllables!=5){
            infile.close();
            return false;
        }
        if (count == 2 && syllables !=7){
            infile.close();
            return false;
        }
        if (count >3){
            infile.close();
            return false;
        }
            
    }
    return true;
}
开发者ID:sungjuc,项目名称:Study,代码行数:29,代码来源:main.cpp

示例5: powFunction

/* Function: powFunction()
 * Usage: is called by formulaStringScanning() function if it detect pow token in formula
 * -----------------------------------------------------------------------------------------//
 * Library pow function execution. Believe that it's normalized function -
 * without any variables like "x" or "y" - just pow(<value>, <power for value>):
 * pow(2, 4) = 16
 * Expects brackets, and return pow function result for values in brackets.
 *
 * @param scanner      TokenScanner for user formula */
double powFunction(TokenScanner& scanner) {
    bool powFail = false;
    string token = "";
    string num1 = "";
    string num2 = "";
    while(scanner.hasMoreTokens()) {
        token = scanner.nextToken();//waiting for brackets at this place
        if(token == "(") {
            num1 = scanner.nextToken();//pow function param1
            token = scanner.nextToken();//waiting coma at this place
            if(token == ",") {
                num2 = scanner.nextToken();//pow function param2
                token = scanner.nextToken();//waiting for brackets at this place
                if(token == ")") {
                    break;
                } else {
                    powFail = true;
                }
            } else {
                powFail = true;
            }
        } else {
            powFail = true;
        }

        if(powFail) {
            cout <<  "   - POW FUNCTION INPUT FAULT!" << endl;
            failFlag = true;
            return 0;
        }
    }
    /* Returns pow function result */
    return pow(stringToDouble(num1), stringToDouble(num2));
}
开发者ID:ekuryatenko,项目名称:cs-b-assignment3_Calculator,代码行数:43,代码来源:Calculator.cpp

示例6: sqrtFunction

/* Function: sqrtFunction()
 * Usage: is called by formulaStringScanning() function if it detect sqrt token in formula
 * -----------------------------------------------------------------------------------------//
 * Library sqrt function execution. Expects for brackets and involves
 * formulaStringScanning process for expression in brackets. Return sqrt function result
 * for obtained value in brackets.
 *
 * @param scanner   TokenScanner for user formula
 * @param xValue    Value for x variable in user equation */
double sqrtFunction(TokenScanner& scanner, double xValue) {
    bool sqrtFail = false;
    string token = "";
    double num1 = 0;
    if(scanner.hasMoreTokens()) {
        token = scanner.nextToken();
        if(token == "(") { //waiting for brackets at this place
            bool bracketsOpened = true;
            /* Calls internal formulaStringScanning process for expression in brackets */
            num1 = formulaStringScanning(scanner,
                                         bracketsOpened,
                                         xValue);
        } else {
            sqrtFail = true;
        }
    }

    if(sqrtFail) {
        cout << "   - SQRT FUNCTION FAULT" << endl;
        failFlag = true;
        return 0;
    }

    /* Returns sqrt function result */
    return sqrt(num1);
}
开发者ID:ekuryatenko,项目名称:cs-b-assignment3_Calculator,代码行数:35,代码来源:Calculator.cpp

示例7: error

/*
 * Implementation notes: the InputStmt subclass
 * ----------------------------------------------
 * Instances of this class read the identifier from the scanner and notify
 * the user if its format is illegal. In the execute method, it records
 * the input of the user as the value of the identifier in "state".
 */
InputStmt::InputStmt(TokenScanner & scanner){
    string nextToken = scanner.nextToken();
    if (isdigit(nextToken[0]))
        error("Invalid identifier for Input Statement -- can't start with an integer.");
    if (scanner.hasMoreTokens())
        error("Unexpected token: " + scanner.nextToken());
    identifier = nextToken;
}
开发者ID:abielg,项目名称:BASIC-interpreter,代码行数:15,代码来源:statement.cpp

示例8: readE

LetStmt::LetStmt(TokenScanner &scanner) {
    identifier = scanner.nextToken();
    op = scanner.nextToken();
    exp = readE(scanner, 0);
    if (scanner.hasMoreTokens()) {
        error("Extraneous token " + scanner.nextToken());
    }
}
开发者ID:mariolew,项目名称:CS106B,代码行数:8,代码来源:statement.cpp

示例9: listProgram

/*
 * Function: listProgram
 * Usage: listProgram(program, scanner);
 * -------------------------------------------
 * Displays the lines of the program.  It calls either version of program.toString()
 * depending on whether the user specifies line numbers.
 */
void listProgram(Program & program, TokenScanner & scanner) {
    if (!scanner.hasMoreTokens()) {
        program.toString();
    }
    else {
        int startLine = stringToInteger(scanner.nextToken());
        scanner.nextToken();
        int endLine = stringToInteger(scanner.nextToken());
        program.toString(startLine, endLine);
    }
}
开发者ID:Thunder1989,项目名称:CS106B,代码行数:18,代码来源:Basic.cpp

示例10: processLine

void processLine(string line, Program & program, EvaluationContext & context) {
   TokenScanner scanner;
   scanner.ignoreWhitespace();
   scanner.setInput(line);
    
    string firstToken = toUpperCase(scanner.nextToken());
    if (firstToken == "RUN") {
        runProgram(program, context);
    }
    else if (firstToken == "LIST") {
        listProgram(program, scanner);
    }
    else if (firstToken == "CLEAR") {
        program.clear();
    }
    else if (firstToken == "HELP") {
        if (!scanner.hasMoreTokens()) {
            displayHelp();
        }
        else {
            specificHelp(scanner);
        }
    }
    else if (firstToken == "QUIT") {
        exit(0);
    }
    else {
        int lineNumber = stringToInteger(firstToken);
        if (!scanner.hasMoreTokens()) {
            program.removeSourceLine(lineNumber);
        }
        else {
            Statement *stmt = parseStatement(scanner);
            program.addSourceLine(lineNumber, line);
            program.setParsedStatement(lineNumber, stmt);
        }
    }
}
开发者ID:Thunder1989,项目名称:CS106B,代码行数:38,代码来源:Basic.cpp

示例11: toAlpha

void toAlpha(std::string word, Map<std::string, std::string>& symbolTable) {
	TokenScanner scanner;
	scanner.ignoreWhitespace();

	for(char i = 'A'; i < 'Z'; i++)
		scanner.addWordCharacters(symbolTable.get(charToString(i)));

	scanner.setInput(word);

	while(scanner.hasMoreTokens()) {
		std::string symbol = scanner.nextToken();
		cout << symbolTable.get(symbol);
	}
	cout << endl;
}
开发者ID:mahmoudmheisen91,项目名称:Programing_Abstractions,代码行数:15,代码来源:chapterFourProblems.cpp

示例12: interpreter

void interpreter(void) {
	std::string line;
	TokenScanner scanner;
	scanner.ignoreWhitespace();
	scanner.scanNumbers();
	Map<std::string, std::string> symbolTable;

	cout << "Interpreter is running: " << endl;

	// Main loop:
	std::string key, value;
	while(true) {
		cout << "> ";
		line = getLine();

		if(line == "quit")
			break;

		else if(line == "list") {
			for(std::string key : symbolTable) {
				cout << key << " = " << symbolTable.get(key) << endl;
			}
		}

		else if(symbolTable.containsKey(line))
			cout << symbolTable.get(line) << endl;

		// Parse statements:
		else {
			scanner.setInput(line);
			bool isValue = false;
			while(scanner.hasMoreTokens()) {
				std::string token = scanner.nextToken();
				if(isValue)
					value = token;
				else if(token == "=")
					isValue = true;
				else
					key = token;
			}
			symbolTable.add(key, value);
		}
	}

	cout << "Interpreter is stopped." << endl;
}
开发者ID:mahmoudmheisen91,项目名称:Programing_Abstractions,代码行数:46,代码来源:chapterFourProblems.cpp

示例13: powFunction

/* Function: powFunction()
 * ------------------------
 * Library pow function execution. Expects sequentally:
 * - brackets, param1, coma, param2, brackets.
 *
 * @param scanner      TokenScanner for user formula */
double powFunction(TokenScanner& scanner) {
    bool powFailFlag = false;
    string token = "";
    string powParam_1 = "";     //pow function param1
    string powParam_2 = "";     //pow function param2
    while (scanner.hasMoreTokens()) {
        token = scanner.nextToken();//waiting for brackets at this place
        if (token == "(") {
            powParam_1 = scanner.nextToken();
            token = scanner.nextToken();//waiting for coma at this place
            if (token == ",") {
                powParam_2 = scanner.nextToken();
                token = scanner.nextToken();//waiting for brackets - end of pow params input
                if (token == ")") {
                    break;
                } else {
                    powFailFlag = true;
                }
            } else {
                powFailFlag = true;
            }
        } else {
            powFailFlag = true;
        }

        if (powFailFlag) {
            cout << "   - POW FUNCTION INPUT FAULT!" << endl;
            FAIL_FLAG = true;
            return 0;
        }
    }

    /* Returns pow function result */
    double param1 = stringToDouble(powParam_1);
    double param2 = stringToDouble(powParam_2);
    return pow(param1, param2);
}
开发者ID:shpp-ekuryatenko,项目名称:cs-b-assignment1_Calculator,代码行数:43,代码来源:Calculator.cpp

示例14: scanFormulaString

/* Function: scanFormulaString()
 * -----------------------------
 * Recursively scanns formula string and sorts each token through stacks due to shunting-yard
 * algorithm. If "()" appear in this recursion  it controls brackets condition. Detects two
 * pow, sqrt - additional library function in user  formula. Breaks process due to global
 * failFlag. Returns double of calculated result. In case of process faults zero result
 * is returned, and fail message is shown.
 *
 * @param scanner               Scanner for main formula string
 * @param bracketsOpenedBefore  Brackets were opened before this recursion invocation */
double scanFormulaString(TokenScanner& scanner, bool& bracketsOpenedBefore) {
    if (FAIL_FLAG) {
        return 0;  //Global flag apearance
    }

    Stack<double> numbersStack; //Stacks for Shunting-Yard process
    Stack<string> operatorsStack; //in current recursion invocation
    string token = "";
    bool bracketsOpenedHere = false;//Rises if "(" appear in this recursion

    /* Main string scanning. It stops due to conditions:
     * - end of formula string
     * - closed brackets condition
     * - FAIL_FLAG appearance                       */
    while (scanner.hasMoreTokens()) {
        token = scanner.nextToken();

        if (token == "pow") {
            /* Lunches library pow function process  */
            double powResult = powFunction(scanner);
            numbersStack.push(powResult);
        }else if (token == "sqrt") {
            /* Lunches library sqrt function process  */
            double sqrtResult = sqrtFunction(scanner);
            numbersStack.push(sqrtResult);
        }else{//Numbers, brackets or garbage symbol occur cases
            if (token == "(") {
                bracketsOpenedHere = true;
                /* Calls new formula recursion for this scanner */
                numbersStack.push(scanFormulaString(scanner, bracketsOpenedHere));
            }else if (token == ")") {
                if (bracketsOpenedBefore) { //Brackets are closed correctly
                    bracketsOpenedBefore = false;//it is end of this recursion
                    break;
                }else  {//Token is ")" and no bracketsOpenedBefore flag
                    FAIL_FLAG = true; //Brackets weren't opened
                    cout << "   - NOT OPENED BRACKETS! " << endl;
                    break;   //Break to show  error to user
                }
            }else {
                if (FAIL_FLAG) {
                    break;//Break if some faults appear in this recursion
                }
                /* Call Shunting-Yard sorting for this token  */
                sortTokenByStacks(token, numbersStack, operatorsStack);
            }
        }
    }//End of while(scanner.hasMoreTokens())

    /* Shunting-yard final calculation */
    if (bracketsOpenedBefore) {//If brackets haven't been closed in this recursion
        cout << "   - NOT CLOSED BRACKETS! " << endl;
        FAIL_FLAG = true;
        return 0;
    } else {
        if (FAIL_FLAG) {//Main scanning cycle was breaked by some faults
            return 0;
        } else {
            /* Final stacks processing */
            return getFinalStacksResult(numbersStack, operatorsStack);
        }
    }
}
开发者ID:shpp-ekuryatenko,项目名称:cs-b-assignment1_Calculator,代码行数:73,代码来源:Calculator.cpp

示例15: error

InputStmt::InputStmt(TokenScanner &scanner) {
    identifier = scanner.nextToken();
    if (scanner.hasMoreTokens()) {
        error("Extraneous token " + scanner.nextToken());
    }
}
开发者ID:mariolew,项目名称:CS106B,代码行数:6,代码来源:statement.cpp


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