CSc 109 Final Examination Sunday 12 May 2002 >>>>>>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 1. (25 pts) I am interested in a class, CheckList, whose instances keep track of whether instances of some class have been added or removed from the CheckList. By default, the CheckList can hold up to 100 items, but an instances of CheckList of other sizes can be created. One can add an item to a CheckList with the += operator (which does nothing if the item has been previously added, one can remove an item with the -= operator (which does nothing if the item is not in the CheckList), and one can ask whether an item is in the CheckList with the inList() function. Below is some code using the class. Write a template for class CheckList. int main() { CheckList a(200),b; CheckList d; a+=3; a+=2000; a+=2000; if(a.inList(2000)) cout<<"2000 is in the list\n";//should print b+=5; a-=2000; if(!a.inList(2000)) cout<<"2000 is not in the list\n";//should print return 0; } ========================== #include template class CheckList {public: CheckList(int j=100); ~CheckList(); CheckList & operator+=(const T &j); CheckList& operator-=(const T &j); bool inList(const T &j)const; private: T **table; int size,count; void err(bool b, char *mess); }; template CheckList::CheckList(int j) {err(j<1,"Bad size"); size=j; count=0; table=new T*[size]; err(table==NULL,"Heap overflow"); for(int j=0;j CheckList::~CheckList() {for(int j=0;j CheckList & CheckList::operator+=(const T &j) {int loc=0; while(loc=size,"CheckList overflow"); table[loc]=new T(j); err(table[loc]==NULL,"Heap overflow"); count++; return *this; } template CheckList & CheckList::operator-=(const T &j) {int loc=0; while(loc=count) return *this; delete table[loc]; for(int j=loc;j bool CheckList::inList(const T &t)const {for(int j=0;j void CheckList::err(bool b, char *mess) {if(b) {cerr<<"ERROR: "< a(200),b(0); a+=3; a+=2000; a+=2000; if(a.inList(2000)) cout<<"2000 is in the list\n";//should print b+=5; a-=2000; if(!a.inList(2000)) cout<<"2000 is not in the list\n";//should print return 0; } 2. (35 pts) Write a class Parse, which checks whether a line of text entered at the console satisfies the syntax of the diagram for EXP below, diagnoses the first error, if any, and, if the syntax is correct, generates a StakMach program which reads in int values to x and y and then writes the resulting value when evaluating the line of text. An error free line of text should have no blanks or tabs and should terminate with a '\n'. Below I show a sample line of text and the StakMach code generated. Below that I show how Parse is used, and then I show the syntax diagram. (Hints: you only need space for x and y. This space can be allocated at the beginning of the code, as I did in the example below. This means no program counter is needed, and only one pass is needed. Octal output can be handled with / and % by 8, 64, etc.) Text: x*y Code: -5003 0000 0000 3000 -4001 3000 -4002 4001 4002 2000 -3000 0000 E Prog: int main() {ofstream code("out.code"); Parse p(code); p.parse(); code.close(); return 0; } ( () indicates a circle, [] indicates a rectangle ) EXP ---------[TERM]--------------------+-------> ^ | | |--(*)---| [TERM]<----+--(+)---+ TERM ----------(x)---------------->+ |---(y)---------------->| +----(()-->[EXP]-->())--+----------> HALT=0,ADD=1,SUB=-1,MUL=2,DIV=-2,READ=3,WRITE=-3,PUSH=4,POP=-4,PUSHI=5, JMP=-5,JMPL=6,JMPG=-6,JMPE=7,JMPNE=-7 ================================================================== #include #include class Parse {public: Parse(ostream &out=cout); void parse(); void exp(); private: static const int HALT=0,ADD=1,SUB=-1,MUL=2,DIV=-2,READ=3,WRITE=-3,PUSH=4,POP=-4,PUSHI=5, JMP=-5,JMPL=6,JMPG=-6,JMPE=7,JMPNE=-7; char ch; ostream *code; void genCode(int op,int addr); void term(); void errCheck(bool b,char *mess); void getch(); }; int main() {ofstream code("out.code"); Parse p(code); p.parse(); code<<"E"; code.close(); return 0; } Parse::Parse(ostream &out) {code=&out; } void Parse::getch() {cin.get(ch); cout< //assume that if half the table is probed and item is not found //the item is not there bool inTable(int table[],int size,int item) {int probe; probe=0; while(probe < size/2 && table[(item+probe*probe)%size]!=item && table[(item+probe*probe)%size]!=-1) probe++; return table[(item+probe*probe)%size]==item; } int main() {int table[]={101, -1, 23, 46, -1, 79, 57, 68, -1, -1, -1}; cout<