Our program eats two out of three inputs how come

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 12 1 an answer 4 an answer 6 an answer 8 an answer 10 an answer Aha! Our program “eats” two out of three inputs How come? Let’s have a look at expression() Stroustrup/Programming 28 Dealing with + and Dealing Expression: Term Expression ‘+’ Term Expression ‘-’ Term // Note: every Expression starts with a Term // Note: double expression() // read and evaluate: 1 1+2.5 1+2+3.14 etc. // read { double left = term(); // get the Term double get while (true) { Token t = get_token(); // get the next token… // get switch (t.kind) { // … and do the right thing with it // and case '+': left += term(); break; case '-': left -= term(); break; default: return left; // <<< doesn’t use “next token” return // <<< } } Stroustrup/Programming 29 } Dealing with + and Dealing So, we need a way to “put back” a token! Back into what? “the input,” of course; that is, we need an input stream of tokens double expression() // deal with + and // deal { double left = term(); double while (true) { while Token t = ts.get(); // get the next token from a “token stream” stream” switch (t.kind) { case '+': left += term(); break; case '-': left -= term(); break; default: ts.putback(t); // put the unused token back put return left; return } Stroustrup/Programming } 30 Dealing with * and / Dealing Now make the same change to term() term() Now double term() // deal with * and / // deal { double left = primary(); double while (true) { while Token t = ts.get(); // get the next Token from input // get switch (t.kind) { case '*': // deal with * // deal case '/': // deal with / // deal default: ts.putback(t); // put unused token back into input stream // put return left; } } Stroustrup/Programming 31 } The program The It “sort of works” That’s not bad for a first try Well, second try Well, really, the fourth try; see the book But “sort of works” is not good enough When the program “sort of works” is when the work (and When fun) really start fun) Now we can get feedback! Stroustrup/Programming 32 Another mystery Another 2 3 4 2+3 2*3 2 3 4 5 an answer an answer an answer an answer What! No “6” ? The program looks ahead one token It’s waiting for the user So, we introduce a “print result” command While we’re at it, we also introduce a “quit” command Stroustrup/Programming 33 The main() program The int main() { double val = 0; while (cin) { Token t = ts.get(); // rather than get_token() // rather if (t.kind == 'q') break; // ‘q’ for “quit” // ‘q’ if (t.kind == ';') // ‘;’ for “print now” // ‘;’ cout << val << '\n'; // print result // print else ts. putback(t); // put a token back into the input stream ts. putback // put val = expression(); // evaluate // evaluate } keep_window_open(); } // … exception handlingStroustrup/Programming … // exception 34 Now the calculator is minimally useful Now 2; 2 2+3; 5 3+4*5; 23 q an answer an answer an answer Stroustrup/Programming 35 Next lecture Next Completing a program Tokens Recovering from errors Cleaning up the code Code review Testing Stroustrup/Programming 36...
View Full Document

This note was uploaded on 02/18/2012 for the course CSCE 121 taught by Professor Walter daugherity during the Fall '09 term at Texas A&M.

Ask a homework question - tutors are online