ECS 110: Data Structures and Programming Discussion Section Notes -- Week 4 Ted Krovetz (tdkrovetz@ucdavis.edu) MIDTERM REVIEW -------------- 1. BUT FIRST, A LITTLE C++ INPUT MANIPULATION In the current assignment, you will be reading characters from the standard input stream, cin. Test cases may be redirected from a file, but redirection in UNIX is transparent to your program and will be received via cin. Some useful functions defined in include: (a) "ws" the whitespace eater. If you put the input stream to the object "ws" all whitespace will be skipped. For example: cin >> ws; // skips current whitespace. (b) The class istream includes a member function, peek(), which returns the next character in the input stream without consuming it. If the end-of-file has been reached, peek() returns the symbolic constant EOF. Caveat: peek() returns an int, not a char. For example: if (cin.peek() == EOF) { // Your program is finished } (c) The class istream includes a member function, get(), which returns the next character in the input stream and consumes it. For example: char next_char = cin.get(); 2. GETTING TOKENS These three functions can be combined to read in tokens for your current programming assignment. Part of parsing your tokens will be getting the string of characters which represent it from standard input. Below is a function that takes into consideration some special cases and returns a token string. This function hasn't been rigorously tested, and may not test all special cases. It is intended to give you a little head-start on the parsing of your input. A few assumptions may make your parsing easier. All tokens (except parentheses) will be separated by whitespace. Thus, you don't have to handle cases like "notnotBimpliesB". The focus of this program should not be the input parser. Just write a simple parser using decent C++ that can handle the sample input file, and everything will be fine. #include int is_delimiter(char ch) { return ((ch == ' ') || (ch == '\t') || (ch == '\n') || (ch == ')') || (ch == '.')); } int get_next_string(char* s) { int next_char; int cur = 0; cin >> ws; // use built-in flush next_char = cin.peek(); if (next_char == EOF) return (0); if (next_char == '(') { s[0] = cin.get(); s[1] = '\0'; return (1); } do { s[cur] = cin.get(); cur++; next_char = cin.peek(); } while ((next_char != EOF) && ( ! is_delimiter(next_char))); s[cur] = '\0'; return (1); } 3. MIDTERM REVIEW Bring your midterm questions. I will be examining the sample midterm and answering any midterm related questions you might have. Because I expect the review to be question driven, I have not prepared any notes for the review.