本文整理汇总了C++中Token_stream::get方法的典型用法代码示例。如果您正苦于以下问题:C++ Token_stream::get方法的具体用法?C++ Token_stream::get怎么用?C++ Token_stream::get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Token_stream
的用法示例。
在下文中一共展示了Token_stream::get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: primary
// deal with numbers and parentheses
double primary()
{
Token t = ts.get();
switch (t.kind) {
case '{': // handle '{' expression '}'
{
double d = expression();
t = ts.get();
if (t.kind != '}') error("'}' expected");
return d;
}
case '(': // handle '(' expression ')'
{
double d = expression();
t = ts.get();
if (t.kind != ')') error("')' expected");
return d;
}
case '8': // we use '8' to represent a number
{
Token next_t = ts.get();
if (next_t.kind == '!')
return factorial(narrow_cast<int>(t.value));
else
ts.putback(next_t);
return t.value; // return the number's value
}
default:
error("primary expected");
}
}
示例2: func
double func()
{
Token t = ts.get();
if (ts.get().kind == '(') {
if(t.name == "sqrt"){
double d = expression();
t = ts.get();
if (t.kind != ')') error("')' expected");
if (d < 0) error("sqrt() parameter is negative number ");
return sqrt(d); // 平方根を返す
}
else if(t.name == "pow"){
double d1 = expression();
t = ts.get();
if (t.kind != ',') error("',' expected");
double d2 = expression();
int i2 = int(d2);
if (i2 < 0) error("second parameter is negative number ");
if (i2 != d2) error("second parameter is not integer");
t = ts.get();
if (t.kind != ')') error("')' expected");
return pow(d1, i2); // べき乗を返す
}
}
else
error("'(' expected");
}
示例3: primary
// deal with numbers and parentheses
double primary()
{
Token t = ts.get();
switch (t.kind) {
case '(': // handle '(' expression ')'
{
double d = expression();
t = ts.get();
if (t.kind != ')') error("')' expected");
return d;
}
case number:
return t.value; // return the number's value
case name:
{
Token next = ts.get();
if (next.kind == '=') { // handle name = expression
double d = expression();
set_value(t.name,d);
return d;
}
else {
ts.putback(next); // not an assignment: return the value
return get_value(t.name); // return the variable's value
}
}
case '-':
return - primary();
case '+':
return primary();
default:
error("primary expected");
}
}
示例4: primary
double primary(){
Token t = ts.get();
switch(t.kind){
case '(': // '(' Expression ')'を処理する
{
double d = expression();
t = ts.get();
if(t.kind != ')') error("')' expected");
return d;
}
case number: // 数字を表す
return t.value; // 数字の値を返す
case name:
{
Token t2 = ts.get();
if(t2.kind == '='){ // 変数に値を代入
double d = expression();
st.set_value(t.name, d);
return d;
}
else{
ts.putback(t2);
return st.get_value(t.name); // 変数の値を返す
}
}
case '-':
return -primary();
case '+':
return primary();
default:
error("primary expected");
}
}
示例5: primary
double primary()
{
Token t = ts.get();
switch (t.kind) {
case '(':
{ double d = expression();
t = ts.get();
if (t.kind != ')') error("'(' expected");
return d;
}
case sqrtsymb:
return get_square();
case powsymb:
return get_pow();
case '-':
return - primary();
case name:
{
t2 = ts.get();
cin >>
if(t.kind == '=') return set_value();
return get_value(t.name);
}
case '+':
return primary();
case number:
return t.value;
default:
error("primary expected");
}
}
示例6: main
int main()
try
{
while (cin) {
cout << "> ";
Token t = ts.get();
while (t.kind == ';') t=ts.get(); // eat ';'
if (t.kind == 'q') {
keep_window_open();
return 0;
}
ts.putback(t);
cout << "= " << expression() << endl;
}
keep_window_open();
return 0;
}
catch (exception& e) {
cerr << e.what() << endl;
keep_window_open("~~");
return 1;
}
catch (...) {
cerr << "exception \n";
keep_window_open("~~");
return 2;
}
示例7: term
double term()
{
double left = primary();
Token t = ts.get();
while (true) {
switch (t.kind) {
case '*':
left *= primary();
t = ts.get();
break;
case '/':
{
double d = primary();
if (d==0) error("Division by zero.");
left /= d;
t = ts.get();
break;
}
case '%':
{
int i1 = narrow_cast<int>(left);
int i2 = narrow_cast<int>(term());
if (i2 == 0) error("%: divide by zero");
left = i1 % i2;
t = ts.get();
break;
}
default:
ts.putback(t);
return left;
}
}
}
示例8: factorial
double factorial()
{
double left = primary();
Token t = ts.get(); // get the next token from token stream
while(true) {
switch (t.kind) {
case '!':
{
int x = left;
int count = left;
for (int i = 1; i < count; i++) {
x*=i;
//cout << left << "\n" << i << "\n";
}
if (x == 0) left = 1;
else left = x;
t = ts.get();
break;
}
default:
ts.putback(t); // put t back into the token stream
return left;
}
}
}
示例9: primary
// deal with numbers and parentheses
double primary()
{
Token t = ts.get();
switch (t.kind) {
case '(': // handle '(' expression ')'
{
double d = expression();
t = ts.get();
if (t.kind != ')') error("')' expected");
return d;
}
case '{': // handle '(' expression ')'
{
double d = expression();
t = ts.get();
if (t.kind != '}') error("'}' expected");
return d;
}
case '8': // we use '8' to represent a number
return t.value; // return the number's value
case 'x':
error("x");
default:
error("primary expected");
}
}
示例10: primary
double primary()
{
Token t = ts.get();
if(t.kind == '(')
{
double val = expression();
t = ts.get();
if (t.kind != ')')
error ("Closing parenthesis expected, but not found");
return val;
}
else if(t.kind == '{')
{
double val = expression();
t = ts.get();
if(t.kind != '}')
error ("Closing brace expected, but not found");
return val;
}
else if (t.kind == '8')
return t.value;
error("Primary expected, but found something else");
// can never reach here
cerr << "Should never reach here" << endl;
return 0;
}
示例11: term
// deal with *, /, and %
double term()
{
double left = primary();
Token t = ts.get(); // get the next token from token stream
while (true) {
switch (t.kind) {
case '*':
left *= primary();
t = ts.get();
break; //added break and bracket
case '/':
{
double d = primary();
if (d == 0) error("divide by zero");
left /= d;
t = ts.get();
break;
}
default:
ts.putback(t); // put t back into the token stream
return left;
}
}
}
示例12: primary
double primary(){
Token t = ts.get();
switch(t.kind){
case '(': // '(' Expression ')'を処理する
{
double d = expression();
t = ts.get();
if(t.kind != ')') error("')' expected");
return d;
}
case '{': // '{' Expression '}'を処理する
{
double d = expression();
t = ts.get();
if(t.kind != '}') error("'}' expected");
return d;
}
case '8': // '8'を使って数字を表す
return t.value; // 数字の値を返す
case 'q': // 追加
keep_window_open("~0");
exit(EXIT_SUCCESS);
default:
error("primary expected");
}
}
示例13: calculate
void calculate()
{
const string prompt = "> ";
const string result = "= ";
while (cin) {
try {
bool displayed_help = false;
cout << prompt;
Token t = ts.get();
while (t.kind == print) t = ts.get();
if (t.kind == quit) return;
if (t.kind == help_lower) {
display_help();
displayed_help = true;
}
if (displayed_help == false) {
ts.putback(t);
cout << result << statement() << endl;
}
}
catch (exception& e) {
cerr << e.what() << endl;
clean_up_mess();
}
}
}
示例14: main
int main()
try
{
cout << WELCOME << endl;
cout << INSTRUCT << endl;
double val = 0;
while (cin) {
Token t = ts.get();
if (t.kind == ';') // ';' for "print now"
cout << "=" << val << '\n';
else
ts.putback(t);
t = ts.get();
if (t.kind == 'x') break; // 'x' for quit
else
ts.putback(t);
val = expression();
}
keep_window_open();
}
catch (exception& e) {
cerr << "error: " << e.what() << '\n';
keep_window_open();
return 1;
}
catch (...) {
cerr << "Oops: unknown exception!\n";
keep_window_open();
return 2;
}
示例15: primary
// deal with numbers and parentheses
double primary()
{
Token t = ts.get();
switch (t.kind) {
case '{':
{
double e=expression();
t=ts.get();
if(t.kind!='}') error("'}' expected");
return suffix(e);
}
case '(': // handle '(' expression ')'
{
double d = expression();
t = ts.get();
if (t.kind != ')') error("')' expected");
return suffix(d);
}
case '8': // we use '8' to represent a number
return suffix(t.value); // return the number's value
case END:
case QUIT:
ts.putback(t);
return 0;
default:
ts.putback(t);
error("primary expected");
return t.value;
}
}