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


C++ Token_stream::unget方法代码示例

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


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

示例1: calculate

void calculate() // Performs calculations
{
	while(true) try {
		cout << prompt; // Output '>' symbol
		Token t = ts.get(); // Get a new character
		if (t.kind == help) { // If help was entered open help menu
		cout << "Welcome to Awesome Calculator " << version << " ! Here is the list of the commands:\n"
			"Available operators are '+','-','/','*'. The program is able to work with floating-point numbers\n"
			"'Expression' + ';' - Prints the result (always use it at the end of the statement)\n"
			"'let' + 'name of variable' + '='' + 'value' - defines a variable\n"
			"'const' + 'name of constant' + '='' + 'value' - defines a constant\n"
			"'name of variable' + '=' + 'new value' - assigns a new value to the variable\n"
			"'pow(x,y)' - multiplies x by y times (y must be of type int)\n"
			"'sqrt(x)' - returns square root of x\n"
			"'h' or 'help' - opens this menu\n"
			"'quit' - closes the program\n";
			continue; // Move to next iteration
		}
		while (t.kind == print) t=ts.get(); // Read all ';'
		if (t.kind == quit) return; // Close the program if exit was entered
		ts.unget(t); // Return a character into the stream
		cout << result << statement() << endl; // Output the result
	}
	catch(runtime_error& e) {
		cerr << e.what() << endl; // Cout the error if the exception was thrown
		clean_up_mess(); // Ignores all characters till the ';'
	}
}
开发者ID:thelastpolaris,项目名称:Programming-Principles-and-Practice-Using-C-,代码行数:28,代码来源:ex10.cpp

示例2: func

double func(const char func_kind)
{
	Token t {ts.get()};
	if (t.kind != '(')
        error("function expects a '(' as opening");

    switch (func_kind) {
        case sqrt_kind:
            {
                ts.unget(t);
                double d {expression()};
                if (d < 0)
                    error("negative value for sqrt()");
                return sqrt(d);
            }
        case power_kind:
            {
                double d1 {expression()};
                t = ts.get();
                if (t.kind != ',')
                    error("',' expected");
                double d2 {expression()};
                return pow(d1, d2);
            }
        default:
            return func(func_kind);
    }
}
开发者ID:Tigrolik,项目名称:git_workspace,代码行数:28,代码来源:calculator08buggy.cpp

示例3: term

double term()
{
	double left = primary();
	while(true) {
		Token t = ts.get();
		switch(t.kind) {
			case '*':
				left *= primary();
				break;
			case '/':
				{	double d = primary();
					if (d == 0) error("divide by zero");	// ゼロ除算
					left /= d;
					break;
				}
			case '%':
				{	
					double d = primary();
					int v1 = int(left);
					if (v1 != left) error("left-hand operand is not integer");
					int v2 = int(d);
					if (v2 != d) error("right-hand operand is not integer");
					if (v2 == 0) error("mod: divide by zero");	// ゼロ除算
					left = v1 % v2;
					break;
				}
			default:
				ts.unget(t);
				return left;
			}
	}
}
开发者ID:k-mi,项目名称:Stroustrup_PPP,代码行数:32,代码来源:drill7_11.cpp

示例4: term

double term()
{
  double left = primary();
  while(true) {
    Token t = ts.get();  // get the next Token from the stream
    switch(t.kind) {
    case '*':
      left *= primary();
      break;
    case '/':
    { double d = primary();
      if (d == 0) error("divide by zero");
      left /= d;
      break;
    }
    case '%':
    {
      double d = primary();
      if (d == 0) error("divide by zero");
      left = fmod(left,d); // x%y = x-y*int(x/y); 6.7%3.3=6.7-3.3*int(6.7/3.3) = 0.1
      break;
    }
    default:
      ts.unget(t);  // put the Token back onto the stream (we did not do any action here)
      return left;
    }
  }
}
开发者ID:tol60,项目名称:STROUSTRUP_C11,代码行数:28,代码来源:ch7.e.2.calculator08buggy_fixed.C

示例5: statement

// トークンが"let"であればDecleartion
// それ以外であればExpressionとして処理
double statement()
{
	Token t = ts.get();
	switch(t.kind) {
		case let:
			return declaration();
		default:
			ts.unget(t);
			return expression();
	}
}
开发者ID:k-mi,项目名称:Stroustrup_PPP,代码行数:13,代码来源:drill7_11.cpp

示例6: get_square

double get_square(){
    Token t = ts.get();
    if(t.kind != '(') error ("'(' expected");
    t=ts.get();
    if(t.kind == ')') error("Empty square root function!");
    ts.unget(t);
    double d = expression();
    if(d<0) error("Can\'t take square root of a negative!");
    t = ts.get();
    if(t.kind != ')') error("')' expected");
    return sqrt(d);
}
开发者ID:dracvijja,项目名称:BjarneStroustrupProgrammingPrinciples,代码行数:12,代码来源:calculator08buggy.cpp

示例7: power

double power()
{
	double left = primary();
	while(true) {
		Token t = ts.get();
		if(t.kind=='^') {
			left=pow(left,primary());
		} else {
			ts.unget(t);
			return left;
		}
	}
}
开发者ID:dreamaddict,项目名称:Stroustrup-book-exercises,代码行数:13,代码来源:ch7calcexercises1-9.cpp

示例8: statement

double statement() // Divides definition of variable and expressions
{
	Token t = ts.get();
	switch(t.kind) {
	case let: // User wants to declare the variable
		return  names.define_name(let);
	case constant:
		return names.define_name(constant);
	default: // User wants another operation
		ts.unget(t);
		return expression();
	}
}
开发者ID:thelastpolaris,项目名称:Programming-Principles-and-Practice-Using-C-,代码行数:13,代码来源:ex10.cpp

示例9: calculate

void calculate() // Performs calculations
{
	while(true) try {
		cout << prompt; // Output '>' symbol
		Token t = ts.get(); // Get a new character
		while (t.kind == print) t=ts.get(); // Read all ';'
		if (t.kind == quit) return; // Close the program if exit was entered
		ts.unget(t); // Return a character into the stream
		cout << result << statement() << endl; // Output the result
	}
	catch(runtime_error& e) {
		cerr << e.what() << endl; // Cout the error if the exception was thrown
		clean_up_mess(); // Ignores all characters till the ';'
	}
}
开发者ID:thelastpolaris,项目名称:Programming-Principles-and-Practice-Using-C-,代码行数:15,代码来源:ex6.cpp

示例10: calculate

void calculate()
{
  while(true) try {
    cout << prompt;
    Token t = ts.get();
    while (t.kind == print) t=ts.get();
    if (t.kind == quit) return;
    ts.unget(t);
    cout << result << statement() << endl;
  }
  catch(runtime_error& e) {
    cerr << e.what() << endl;
    clean_up_mess();
  }
}
开发者ID:tol60,项目名称:STROUSTRUP_C11,代码行数:15,代码来源:ch7.e.2.calculator08buggy_fixed.C

示例11: calculate

void calculate()
{
	while(true) try {
		cout << PROMPT;
		Token t = ts.get();
		while (t.kind == PRINT) t=ts.get();
		if (t.kind == QUIT) return;
		if (t.kind == HELP) help();
		ts.unget(t);
		cout << RESULT << statement() << endl;
	}
	catch(runtime_error& e) {
		cerr << e.what() << endl;
		clean_up_mess();
	}
}
开发者ID:dreamaddict,项目名称:Stroustrup-book-exercises,代码行数:16,代码来源:ch7calcexercises1-9.cpp

示例12: calculate

void calculate()
{
	while(true) try {
		cout << prompt;
		Token t = ts.get();
		while (t.kind == print) t=ts.get();	// 出力トーンを飲み込む
		if (t.kind == quit) return;			// 終了トークンの場合は終了する
		ts.unget(t);
		cout << result << statement() << endl;
	}
	catch(runtime_error& e) {
		cerr << e.what() << endl;
		if(!cin)							// cinエラー後の復帰
			cin.clear();
		clean_up_mess();
	}
}
开发者ID:k-mi,项目名称:Stroustrup_PPP,代码行数:17,代码来源:drill7_11.cpp

示例13: statement

double statement()
{
    Token t = ts.get();
    switch(t.kind) {
        case sqrt_kind:
            return func(sqrt_kind);
        case power_kind:
            return func(power_kind);
        case let:
            return declaration();
        case name:
            return assign(t.name);
        default:
            ts.unget(t);
            return expression();
    }
}
开发者ID:Tigrolik,项目名称:git_workspace,代码行数:17,代码来源:calculator08buggy.cpp

示例14: expression

double expression() // Performs '+' and '-' operations
{
	double left = term(); // Get the number or the result of calculations in term
	while(true) {
		Token t = ts.get();
		switch(t.kind) {
		case '+':
			left += term(); // Addition
			break;
		case '-':
			left -= term(); // Subtraction
			break;
		default:
			ts.unget(t); // If nothing was done return character to the stream
			return left; // Return the new or unchanged value of 'left'
		}
	}
}
开发者ID:thelastpolaris,项目名称:Programming-Principles-and-Practice-Using-C-,代码行数:18,代码来源:ex10.cpp

示例15: expression

double expression()
{  // deal we A+B and A-B
  double left = term();
  while(true) {
    Token t = ts.get(); // get the next Token from the stream
    switch(t.kind) {
    case '+':
      left += term();
      break;
    case '-':
      left -= term();
      break;
    default:
      ts.unget(t); // put the Token back onto the stream (we did not do any action here)
      return left;
    }
  }
}
开发者ID:tol60,项目名称:STROUSTRUP_C11,代码行数:18,代码来源:ch7.e.2.calculator08buggy_fixed.C


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