c109f02f - CSE 109 Final Examination Tuesday 12 December...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 109 Final Examination Tuesday 12 December 2002 **************SUGGESTED ANSWSERS*************************** 1. Write a template class for the ADT "Range." Objects of type Range store lists of objects of some type and return the maximum and minimum entry in the list. For example, the following code should produce the output indicated. Range <double> r(20); //hold a maximum of 20 entries r+=4.2; r+=12; r+=-5; cout<<r.max()<<" "<<r.min(); //displays: 12 -5 ============================================================== #include <fstream.h> #include <stdlib.h> template <class T> class Range {public: Range(int m=100); ~Range(); T max(); T min(); Range & operator+=(T a); private: T *list; int size,count; static void assert(bool b,char *mess); }; } template <class T> Range<T>::Range(int m) {assert(m>0,"(Range(int)) Need m>0"); list=new T[m]; assert(list!=NULL,"(Range(int))Heap overflow"); size=m; count=0; } template <class T> Range<T>::~Range() {delete [] list;} { template <class T> void Range<T>::assert(bool b, char *mess) {if(!b) {cerr<<"ERROR: "<<mess<<endl; exit(1); } } template <class T> {assert(count<size,"(Range +=) List overflow"); list[count]=a; count++; return *this; } template <class T> T Range<T>::max()
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
{assert(count>0,"(Range::max()) Empty list"); T big; big=list[0]; for(int j=1; j<size;j++) if(list[j]>big) big=list[j]; return big; } template <class T> T Range<T>::min() {assert(count>0,"(Range::max()) Empty list"); T small; small=list[0]; for(int j=1; j<size;j++) if(list[j]<small) small=list[j]; return small; } 2. Below is the main program from my implementation of asm2f, except that the commented line was not there. One of you discovered a bug when he wrote: 'asm2f input out input' In particular, the file 'input' got wiped out and the program crashed. I could have repaired that and similar problems by rewriting openFiles(). Instead I chose to add the function screen() indicated in the commented line. Write the protoytpe and definition of screen() so that when such problems occur the program terminates. When there is no problem, screen() does nothing and returns. int main(int ct,char **arg) {ifstream fin; ostream fout,fcode; //screen(ct,arg); openFiles(fin,fout,fcode,ct,arg); FullParser p(fin,fout,fcode; p.parse(); } ============================================================= void screen(int ct, char **arg) {if(ct!=4) exit(1); for(int j=0; j<ct; j++) for(int k=j+1; k<ct; k++) if(strcmp(arg[j],arg[k])==0) exit(2); } 3. Your goal is to write a class for parsing and evaluating expressions which satisfy the syntax diagrams and algebraic rules given in question 4. In this question you should write a tokenizer for such expressions; in the
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 8

c109f02f - CSE 109 Final Examination Tuesday 12 December...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online