CSE 109 Test 1 Wednesday 14 November 2007 >>>>>>>>>>>>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 1. Write a program that determines whether the strings entered on the command line are in descending lexicographical order. If the compiled program is stored in a.out, then the following commands would produce the output that appears on the line following the command. a.out In order by default a.out p In order by default a.out bear Bear Ant In order a.out b b ant In order a.out able baker are Out of order >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include "word.cc" /*descending order*/ int main(int ct,char**sym){ if(ct<3){ cout<<"In order by default\n"; return 0; } ct--; while(ct>1){ // if(strcmp(sym[ct],sym[ct-1])>0){ //either of these works if(Word(sym[ct])>Word(sym[ct-1])){ cout<<"Out of order\n"; return 0; } ct--; } cout<<"In order\n"; } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 2. Assume you have available in the lex.h the declaration for the class Lex, a lexical analyzer for the SPL++ language (recall the constants IDENT, NUMBER, HALT, PLUS, MINUS, MUL, DIV, DOLLAR, LT, LTE, GT, GTE, EQ, NEQ,SEQ, END, READ, WRITE, IF, LPAR,RPAR, JUMPTO, EOLN, JUNK). Write a program that determines whether the first line of text entered at the console obeys the following syntax diagram, where parentheses () indicate circles and brackets [] indicated rectangles. Line -------->(<)--->[IDENT]--->(>)---> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include "lex.h void check(bool b,char *mess); int main(){ Lex lex(cin,cout); int token; token=lex.next(); check(token==Lex::LT," '<' expected"); token=lex.next(); check(token==Lex::IDENT," Identifier expected"); token=lex.next(); check(token==Lex::GT," '>' expected"); token=lex.next(); check(token==Lex::EOLN," EOLN expected"); cout<<"Successful parse"; } void check(bool b,char *mess){ if(!b){ cerr<<"ERROR: "<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> friend Word operator + (const Word &a,const Word &b); Word operator + (const Word &a, const Word &b){ Word c; delete []c.str; c.str=new char [strlen(a.str)+strlen(b.str)+1]; c.check(c.str!=NULL,"(+) Heap overflow"); strcpy(c.str,a.str); strcat(c.str,b.str); return c; } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 4. Write a template for a class Safe that does not allow access to a variable until it has been defined (given a value). You need only declare and define those methods and constructors that would be needed for the program below to compile and produce the indicated results. int main(){ Safe u; Safe v; v.set(6); cout<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include using namespace std; template class Safe{ public: Safe(); void set(const X & t); X get()const; template friend ostream & operator<<(ostream &out,const Safe&s); private: X data; bool good; static void check(bool b,char *mess); }; template Safe::Safe(){ good=false; } template void Safe::set(const X &x){ data=x; good=true; } template X Safe::get()const{ check(good,"(get()) Undefined variable"); return data; } template ostream & operator<<(ostream &out,const Safe &s){ out< void Safe::check(bool b,char *mess){ if(!b){ cerr<<"ERROR: "<