CSE 109 Test 2 Wednesday 11 April 2008 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<<<<<< NAME_______________________________________________________________ 1. Consider the ADT for an interval of some type. An interval has a lower and an upper bound. An object is in the interval if and only if its value is between the two bounds (including the two bounds). Write a template, Interval, that implements this ADT. It need only have those methods necessary for the code below to compile and produce the indicated results. Interval x(2.1,4); //all t such that 2.1<=t<=4 cout<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include using namespace std; template class Interval{ public: Interval(const A&lo,const A &h); Interval & shift(const A & delta); bool contains(const A &a)const; template friend ostream & operator<<(ostream &out,const Interval &b); private: A low,high; static void check(bool b,char *mess); }; template Interval::Interval(const A&lo,const A&h):low(lo),high(h){ check(lo<=h,"Illegal bounds"); } template Interval & Interval:: shift(const A & delta){ low+=delta; high+=delta; return *this; } template bool Interval::contains(const A &a)const{ return a>=low && a<=high; } template void Interval::check(bool b,char *mess){ if(!b){ cerr<<"ERROR[Interval] :"< ostream & operator<<(ostream &out,const Interval &b){ out<<"[ "<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> friend Word operator+(const Word &a,const Word&b); Word operator+(const Word &a,const Word&b){ a.check((int)(strlen(a.wd)+strlen(b.wd))<=a.LENGTHMAX,"String too long"); Word temp(a); strcat(temp.wd,b.wd); return temp; } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< (another version) 2. Below is my start on the declaration of the class Word. Provide the declaration and definition for the operator '+' so that the following code produces the indicated output. ('+' implements concatenation) class Word {public: public static int LENGTHMAX=12; Word(char *st=""); const Word & operator=(const Word &w); friend ostream & operator <<(ostream &out,const Word &w); private: char *str; static void check(bool ok,char *mess); }; Word a; a="Hello"; a=a+"World"; cout<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> friend Word operator+(const Word &a,const Word&b); Word operator+(const Word &a,const Word&b){ Word temp; delete []temp.str; temp.str=new char[strlen(a.str)+strlen(b.str)+1]; check(temp.str!=NULL,"('+') heap overflow"); strcpy(temp.str,a.str); strcpy(temp.str,b.str); return temp; } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 3. Write a program whose sole purpose is to count the number of characters in each of the arguments entered in the command line, including the call to the program itself. For example, if the call is a.out one two three the program should print out (counting 5 for 'a.out', 3 for 'one', etc.) The number of characters is 16. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include using namespace std; int main(int ct,char **mess){ int count,loc; count=0; for(int j=0; j>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 10007 0 read 20007 1 load x 31008 2 sub 3 41006 3 42006 4 11009 5 43000 6 0 7 3 8 1 9 END 2 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<