CSE 109 Test 2 Wednesday 19 November 2003 >>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<, 1. Write a template for a class Between. Instances of Between are created with two values and respond to the message good() by returning true if and only if good()'s argument lies between the two values (inclusive). For example, given Between a("good","bad"); a.good("cool") returns true, while a.good("man") returns false. For another example, given Between b(12,18); b.good(18) returns true, while b.good(21) returns false. >>>>>>>>>>>>>>>>>>>>>>>>. template class Between {public: Between(const T &a, const T&b); bool good(const T &t); private: T low,high; }; template Between::Between(const T&a,const T&b) {if(a bool Between::good(const T&t) {return low<=t && t<=high;} <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 2. Assume that you are writing a program to read a single line of text from cin, where the text should have no blanks and should have the syntax given by ::= { < | <= } ::= * ::= 0 | 1 (basically the comparison between two binary numbers). Write a class Lex to be used to tokenize the text appearing on the line. Keep it very simple. Its only public member function is next(), and next() returns 0 for '<', 1 for '<=', 2 for , 3 for end of line, and 4 for for anything else, including a blank (junk). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> class Lex {public: static const int LESS=0, LEQ=1, NUMBER=2,EOLN=3, JUNK=4; Lex(); int next(); private: char ch; }; Lex::Lex() {cin.get(ch);} Lex::next() {switch(ch) {case '<': cin.get(ch); if(ch!='=') return LESS; cin.get(ch); return LEQ; case '\n': return EOLN; case '0' : case '1' : while(ch=='0' || ch=='1') cin.get(ch); return NUMBER; default: cin.get(ch); return JUNK; } } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<, 3. Write a program that uses class Lex from question 2 and that checks whether a line of text entered via the keyboard obeys the syntax rules in question 2. For example, if the user enters 1011<==10 the program would respond "Bad syntax"; if the user enters 10001 < 1001 the program would respond "Bad syntax"; (because blanks are not allowed); if the user enters 10001<=101 the program would respond "Good syntax". >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include int checkSyntax(); int main() {char *out[]={"Bad","Good"}; cout<(const Word &a, const Word &b); friend bool operator>=(const Word &a, const Word &b); friend bool operator==(const Word &a, const Word &b); friend bool operator!=(const Word &a, const Word &b); Word operator=(const Word &w); friend ostream & operator <<(ostream &out,const Word &w); private: char str[22]; }; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Add to the public section of the declaration: Word(char ch); Define Word::Word(char ch) {str[0]=ch; str[1]='\0'; } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< OR Add to the public section of the declaration Word operator=(char ch); Define Word Word::opertaor=(char ch) {str[0]=ch; str[1]='\0'; return *this; } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<