本文整理汇总了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 ';'
}
}
示例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);
}
}
示例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;
}
}
}
示例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;
}
}
}
示例5: statement
// トークンが"let"であればDecleartion
// それ以外であればExpressionとして処理
double statement()
{
Token t = ts.get();
switch(t.kind) {
case let:
return declaration();
default:
ts.unget(t);
return expression();
}
}
示例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);
}
示例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;
}
}
}
示例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();
}
}
示例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 ';'
}
}
示例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();
}
}
示例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();
}
}
示例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();
}
}
示例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();
}
}
示例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'
}
}
}
示例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;
}
}
}