CSE 109 Final Examination Tuesday 6 May 2008 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<<<<< 1. In this question I ask you to develop a class Scan for use in question 2. Class Scan should read from a textfile and implement (at least) two methods: next() - ignores the character '\r' and returns the next character from the input stream, unless ther is no next character (end of file), in which case it should return '#'. good() - returns true if and only if there is another character to be returned when next() is subsequently called. Below is an example of how an instance of Scan is created: ifstream f("aFile"); check(f!=NULL," Failure to open 'aFile'"); Scan s(f); >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include using namespace std; class Scan{ public: Scan(istream &in); char next(); bool good(); private: char ch; istream *fin; bool firstTime; void getch(); }; Scan::Scan(istream &in):fin(&in),firstTime(true){} char Scan::next(){ char temp; if(firstTime){ getch(); firstTime=false; } temp=ch; getch(); return temp; } void Scan::getch(){ fin->get(ch); while(fin->good() && ch=='\r') fin->get(ch); if(!fin->good()) ch='#'; } bool Scan::good(){ return fin->good(); } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 2. Write the rest of the program that I started below. It is meant to determine whether the contents of a file satisfy the syntax diagrams for P below. In the diagrams I indicate circles with parentheses, (), and rectangles with square braces, []. No blanks, tabs, or newlines are allowed. +----('a')-----------------+ P | | ----->[S]--->('#')--> S-->+----('b')--[T]---------------> | | +---('a')--[T]--('s')--+ +----('q')---[S]---('a')---+ T | | ----+--('b')--('s')--[T]---------> | | +--------('c')---------+ void check(bool b,char *mess){if (!b) {cerr<<"ERROR: "<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include #include using namespace std; void parse(Scan &s); //plays the role of P void S(Scan &s,char &ch); void T(Scan &s,char &ch); void check(bool b,char *mess){if (!b) {cerr<<"ERROR: "<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include using namespace std; class FWord:public Word{ public: FWord(char *st=""); int size()const; char & operator[](int index); char operator[](int index)const; }; FWord::FWord(char *st):Word(st){} int FWord::size()const{return strlen(str);} char& FWord::operator[](int ind){ check(ind>=0 && ind <=size(),"Range error"); return str[ind]; } char FWord::operator[](int ind)const{ check(ind>=0 && ind x; (((x+=5.6)+=2.3)+=1.4)+=1.7; cout<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include using namespace std; template class Sequence{ public: static const int MAX=100; Sequence(); int length()const; Sequence & operator+=(const A &a); template friend ostream & operator<<(ostream &out,const Sequence &s); private: A x[MAX]; int ct; static void check(bool ok,char *mess); }; template Sequence::Sequence():ct(0){} template ostream & operator<<(ostream &out,const Sequence &s){ out<<"["; for(int j=0; j0) out<<", "; out< int Sequence::length()const{return ct;} template Sequence & Sequence::operator+=(const A&a){ ct++; check(ct void Sequence::check(bool ok,char*mess){ if(!ok){ cerr<<"ERROR: "<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include using namespace std; int count(Node * root,int a, int b){ int temp; if(root==NULL) return 0; if(root->jj>b) temp=1; else temp=0; return temp+count(root->child[0],a,b)+count(root->child[1],a,b); } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 6. Assume the struct below, assume hd is of type struct Link*, and assume that hd points to a linked list. Write a C (NOT C++) function (that will be compiled with the C compiler: g++ -xc), list(), such that the call list(hd,0) displays the contents of the linked list from the first link to the last, and the call list(hd,1) displays the contents in reverse order (last to first). struct Link{ int j; struct Link * next; }; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include void list(struct Link * hd,int dir){ if(hd==NULL) printf("%sNULL",dir==0?"-->":""); else{ if(dir==0) printf("-->%d",hd->j); list(hd->next,dir); if(dir==1) printf("<--%d",hd->j); } } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 7. Write a program that reads the entries on the command line and writes them out in upper case. For example, if the executable code is stored in a.out, then the call a.out g++ kungFu would display on the screen A.OUT G++ KUNGFU >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include using namespace std; void upcase(char * ch){ for(int j=0;j<(int)strlen(ch);j++) if(ch[j]>='a' && ch[j]<='z') ch[j]=ch[j]-'a'+'A'; } int main(int ct,char **arg){ for(int j=0;j>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 10025 0 read x 20026 1 load 2 21999 2 push 2 20025 3 load x 21998 4 push x 20027 5 load 3 21997 6 push 3 20998 7 load stk-1 31997 8 x-3 21998 9 pop, popo, push x-3 20025 10 load x 21997 11 push x 20028 12 load 4 21996 13 push 4 20997 14 load stk-1 30996 15 x+4 21997 16 pop, pop, push x+4 20997 17 load x+4 33998 18 (x-3)*(x+4) 21998 19 push (x-3)*(x+4) 20998 20 load (x-3)*(x+4) 30999 21 add 2 21999 22 pop,pop,push result 11999 23 write result 43000 24 quit 0 25 x 2 26 3 27 4 28 END 5 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<