CSE 109 Finaly Exam Saturday 5 May 2007 4:00-7:00 PM >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<< 1. Assume the definition of class Link below, meant to be used to construct a linked list ending in a NULL pointer. Write a function remove() that removes the first link it encounters containing a given number. If the number is not in the list, remove() does nothing. Give the declaration Link *hd; the call to remove the first link, if any, containing 10 would look like remove(hd,10). class Link{ public: int j; Link *next; }; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> void remove(Link *&h,int t){ Link * temp; if(h==NULL) return; if(h->j==t){ temp=h; h=h->next; delete temp; //avoid memory leak } else remove(h->next,t); } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 2. Write a template for the class Magnitude that stores an instance of some variable or object for which "<", "==", and "<<" are defined. Instances of the class should respond to the binary operators "<", "==", and "<=". Below is some sample code. Magnitude m(5), n(3); cout<<"("<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> template class Mag{ public: Mag(const C&c=NULL); template friend bool operator<(const Mag&a, const Mag &b); template friend bool operator<=(const Mag&a, const Mag &b); template friend bool operator==(const Mag&a, const Mag &b); template friend ostream & operator<<(ostream &out,const Mag &m); private: C x; }; template Mag::Mag(const A &c):x(c){} template bool operator<(const Mag&a, const Mag &b){ return a.x bool operator<=(const Mag&a, const Mag &b){ return a.x bool operator==(const Mag&a, const Mag &b){ return a.x==b.x; } template ostream & operator<<(ostream &out,const Mag &m){ out<[B]----|--(-)--|--->[C]---> +--(/)--+ B ---------------->(s)->[C]-------> C ---------------->(t)-->[A]-->(u)-------> | +---->(u)-------------------> Write a program that reads a single line from the console and determines whether the characters in the line satisfy the syntax of A. Spaces and tabs are not acceptable characters. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include using namespace std; void a(char&ch); void b(char&ch); void c(char &ch); void check(bool b, char *mess); int main(){ char ch; cout<<"Enter a line of text, and I check its syntax\n"; cin.get(ch); a(ch); check(ch=='\n',"EOLN expected"); cout<<"Good syntax\n"; } void a(char&ch){ b(ch); check(ch=='+' || ch=='-' || ch=='*',"'+', '-', or '*' expected"); cin.get(ch); c(ch); } void b(char&ch){ check(ch=='s',"'s' expected"); cin.get(ch); c(ch); } void c(char &ch){ check(ch=='t' || ch=='u',"'u' or 't' expected"); if(ch=='t'){ cin.get(ch); a(ch); check(ch=='u',"'u' expected"); } cin.get(ch); } void check(bool b, char *mess){ if(!b){ cerr<<"ERROR: "<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 10021 0 I avoid obvious shortcuts and follow 10022 1 the way SAL++ probably generates code 20022 2 load y 21999 3 push y 20021 4 load x 21998 5 push x 20999 6 get stk+1 31998 7 sub stk 21999 8 pop and store 20023 9 load 3 21998 10 push 3 20022 11 load y 21997 12 push y 20998 13 load stk+1 31997 14 sub stk 21998 15 pop and store 20999 16 load stk+1 33998 17 multiply 21999 18 pop and store 11999 19 write stk 43999 20 halt 0 21 x 0 22 y 3 23 const END <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 5. Write a program that reads from two text files specified at the command line and determines whether the two files are identical. If the executable program is stored in "same", the call would be same someFile someOtherFile The output might look something like "The files 'someFile' and 'someOtherFile' are identical." >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include #include using namespace std; void check(bool b, char *a, char *c="", char *d=""); int main(int ct,char **arg){ char cha,chb; check(ct==3,"Usage: ",arg[0]," "); ifstream a(arg[1]), b(arg[2]); check(a.good(),"The file '",arg[1],"' does not exist"); check(b.good(),"The file '",arg[2],"' does not exist"); cha=a.get(); chb=b.get(); while(a.good() && b.good() && cha==chb){ cha=a.get(); chb=b.get(); } check(!a.good() && !b.good(),"The files are not the same"); cout<<"The files are the same\n"; } void check(bool b, char *a, char *c, char *d){ if(!b){ cerr<<"ERROR: "<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include using namespace std; class Account{ public: Account(double j=0); Account & operator+=(double j); Account & operator-=(double j); double balance()const; private: double money; void check(bool b, char *mess); }; Account::Account(double j):money(j){ check(money>=0,"Cannot open with a negative amount"); } Account & Account::operator+=(double d){ check(d>=0,"Cannot deposit a negative amount"); money+=d; return *this; } Account & Account::operator-=(double d){ check(d>=0,"Cannot withdraw a negative amount"); check(money-d>=0,"Cannot overdraw account"); money-=d; return *this; } double Account::balance()const{return money;} void Account::check(bool b, char *mess){ if(!b){ cerr<<"ERROR: "<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> class Savings:public Account{ public: Savings(double intRate=0,double amt=0); void post(int m); friend ostream & operator<<(ostream & out,const Savings & s); private: double rate; }; Savings::Savings(double intRate, double amt):Account(amt),rate(intRate){ check(rate>=0 && rate<=1,"Rate must be between 0 and 1, inclusive"); } void Savings::post(int m){ check(m>0,"Must have positive number of months"); money+=m*rate*money; } ostream & operator<<(ostream & out,const Savings & s){ out<<"Savings("<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include struct Pair{ int a,b; }; void loadList(struct Pair list[],int *n); void printList(struct Pair list[],int n); int main(){ struct Pair list[101]; int n; loadList(list,&n); printList(list,n); return 0; } void loadList(struct Pair list[],int *n){ *n=0; scanf( "%d",&list[*n].a); while(list[*n].a>0 && *n<100){ scanf(" %d",&list[*n].b); (*n)++; scanf(" %d",&list[*n].a); } } void printList(struct Pair list[],int n){ while(n>0){ n--; printf(" %d %d \n",list[n].a,list[n].b); } } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<