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 r(20); //hold a maximum of 20 entries r+=4.2; r+=12; r+=-5; cout< #include template 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 Range::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 Range::~Range() {delete [] list;} template void Range::assert(bool b, char *mess) {if(!b) {cerr<<"ERROR: "< Range& Range::operator+=(T a) {assert(count T Range::max() {assert(count>0,"(Range::max()) Empty list"); T big; big=list[0]; for(int j=1; jbig) big=list[j]; return big; } template T Range::min() {assert(count>0,"(Range::max()) Empty list"); T small; small=list[0]; for(int j=1; jgood() && (buff==' ' || buff=='\t' || buff=='\n')) buff=fin->get(); if(!fin->good()) ch='$'; else switch(buff) {case '+': case '*': case '^': case '(': case ')': case '1': case '0': ch=buff; break; default: ch='$'; } buff=fin->get(); cout< ---[C]-------------+--> ---[D]-------------+--> ^ | ^ | ^ | |<-[B]<-{+}<-+ |<-[C]<-{*}<-+ |<-[D]<-{^}<-+ D ---------+---{1}------------+ |---{0}------------| +---{(}--[A]--{)}--+------> Write a class Parser for which objects of type Parser respond to the function parse() by returning the value of an expression satisfying the above diagrams. If the expression does not satisfy the diagrams, then you should simply exit the program. The Parser class should assume that the class Token described in question 3 exists. Given the declaration ifstream f; the class Parser would be used as follows: Parser p(f); int x; x=p.parse();//get the value of the expression in the file f ========================================================================= class Parser {public: Parser(istream &in); int parse(); private: int A(); int B(); int C(); int D(); Token t; char sym; istream *fin; }; Parser::Parser(istream &in):t(in) {} int Parser::parse() {sym=t.next(); if(sym!='$') exit(4); return A(); } int Parser::A() {int temp; temp=B(); while(sym=='+') {sym=t.next(); temp=temp+B(); if(temp>=2) temp=1; } return temp; } int Parser::B() {int temp; temp=C(); while(sym=='*') {sym=t.next(); temp=temp*C(); } return temp; } int Parser::C() {int temp; temp=D(); while(sym=='^') {sym=t.next(); temp=(temp+D())%2; } return temp; } int Parser::D() {int temp; switch(sym) {case '0': sym=t.next(); return 0; case '1': sym=t.next(); return 1; case '(': sym=t.next(); temp=A(); if(sym!=')') exit(1); sym=t.next(); return temp; default: exit(2); } return -1; //keep the compiler happy; we should never get here } 5. Given the class A, write a class B which only has the member functions incinc() and incV() and produces the output indicated between the second and third dashed lines when the code between the first and second dashed dashed lines is run. What simple change in the definition of class A would have the same code produce the output between the third and fourth dashed lines? class A {private: int v,w; public: A(int n=0,int m=0); A::A(int n,int m){v=n; w=m;} void incV(int n); void A::incV(int n){v+=n;} void inc(); void A::inc(){w++; incV(1);} void show(); void A::show() }; {cout<<"v="<0,"(Table(int)) Bad argument"); age=new int[n]; assert(age!=NULL,"Heap overflow"); size=n; names=new char*[n]; assert(names!=NULL,"Heap overflow"); for(int j=0;j