CSE 109 Test 2 Monday 14 November 2011 ======================SUGGESTED ANSWERS========================= 1. Consider the ADT for a "FlipFlop," an object that is in one of two states. Everytime it is told to "flip" it changes to the alternative state. Write a template for a class FlipFlop. You need only implement those constructors, methods, and functions that enable the code below to compile. FlipFlop a(2, 3); // A flipflop with "states" 2 and 3, initial //value 2 FlipFlop c(1.7, 2.3); cout< template class FlipFlop{ private: X state[2]; int p; public: FlipFlop(const X &a, const X &b); void flip(); template friend ostream&operator<<(ostream&out,const FlipFlop&f); }; template FlipFlop::FlipFlop(const X&a, const X&b){ state[0]=a; state[1]=b; p=0; } template void FlipFlop::flip(){ p=1-p; } template ostream&operator<<(ostream&out,const FlipFlop&f){ return out<<"FF("< #include void check(bool b,const char *mess); int main(int ct,char**arg){ check(ct==2,"Must have two args on command line"); ifstream f(arg[1]); check(f.good(),"Failure to open input file"); char ch; int n; n=0; f.get(ch); while(f.good()){ n++; f.get(ch); } cout<<"The file '"<[b]-->(-)--+ ---------->| |-->[b]--> +--(*)->[b]-->(/)--+ b ---------->[digit]------------------------------> ^ | | | +-----[s]<---(=)<---+ [digit] indicates a single digit int main(){ char ch; cin.get(ch); s(ch); if(ch!='\n') cout<<"not a "; cout<<"successful parse\n"; } ===================================================================== #include using namespace std; void check(bool b,const char *mess){ if(!b){ cerr<<"ERROR: "<