CSE 109  Test 2  Wednesday 19 November 2003
>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<,
1.  Write a template for a class Between.  Instances of Between are created
with two values and respond to the message good() by returning true if and
only if good()'s argument lies between the two values (inclusive).  For
example, given  Between <Word> a("good","bad");  a.good("cool") returns true,
while a.good("man") returns false.  For another example, given
Between <int> b(12,18); b.good(18) returns true, while b.good(21) returns
false.
>>>>>>>>>>>>>>>>>>>>>>>>.
template <class T>
class Between
{public:
  Between(const T &a, const T&b);
  bool good(const T &t);
 private:
  T low,high;
};

template <class T>
Between<T>::Between(const T&a,const T&b)
{if(a<b)
  {low=a;
   high=b;
  }
 else
  {low=b;
   high=a;
  }
}

template <class T>
bool Between<T>::good(const T&t)
{return low<=t && t<=high;}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2. Assume that you are writing a program to read a single line of text from
cin, where the text should have no blanks and should have the syntax given by
 <A>::=<number> { < | <= } <number>
 <number>::= <digit> <digit>*
 <digit>::= 0 | 1
(basically the comparison between two binary numbers).
Write a class Lex to be used to tokenize the text appearing on the line.
Keep it very simple.  Its only public member function is next(), and next()
returns 0 for '<', 1 for '<=', 2 for <number>, 3 for end of line, and 4 for
for anything else, including a blank (junk).
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
class Lex
{public:
  static const int LESS=0, LEQ=1, NUMBER=2,EOLN=3, JUNK=4;
  Lex();
  int next();
 private:
  char ch;
};



Lex::Lex()
{cin.get(ch);}

Lex::next()
{switch(ch)
  {case '<': cin.get(ch);
             if(ch!='=')
              return LESS;
             cin.get(ch);
              return LEQ;
   case '\n': return EOLN;
   case '0' :
   case '1' : while(ch=='0' || ch=='1')
                cin.get(ch);
              return NUMBER;
   default: cin.get(ch);
           return JUNK;
  }
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<,
3. Write a program that uses class Lex from question 2 and that checks
whether a line of text entered via the keyboard obeys the syntax rules
in question 2. For example, if the user enters
1011<==10
the program would respond "Bad syntax"; if the user enters
  10001 < 1001
the program would respond "Bad syntax"; (because blanks are not allowed);
if the user enters
10001<=101
the program would respond "Good syntax".
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include <fstream.h>
int checkSyntax();

int main()
{char *out[]={"Bad","Good"};
cout<<out[checkSyntax()]<<" syntax\n";
}

int checkSyntax()
{//leave early if an error, denoted by 0
 Lex a;
 if(a.next()!=Lex::NUMBER)
   return 0;
 switch(a.next())
  {case Lex::LESS:
   case Lex::LEQ: break;
   default: return 0;
  }
 if(a.next()!=Lex::NUMBER)
  return 0;
 if(a.next()!=Lex::EOLN)
  return 0;
 return 1;  //no errors
}

4. Given the interface for my class Word below, write the declaration(s)
and definition(s) (code), so that the following compiles and creates a
Word representing "a":
  Word f;
  f='a';
(Note that this code will NOT compile, given the current declaration of Word,
because 'a' is not a string.)
class Word
{public:
  Word(char *st="");
  Word(const Word & w);
  ~Word();
  friend bool operator<(const Word &a, const Word &b);
  friend bool operator<=(const Word &a, const Word &b);
  friend bool operator>(const Word &a, const Word &b);
  friend bool operator>=(const Word &a, const Word &b);
  friend bool operator==(const Word &a, const Word &b);
  friend bool operator!=(const Word &a, const Word &b);
  Word operator=(const Word &w);
  friend ostream & operator <<(ostream &out,const Word &w);
 private:
  char str[22];
};
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Add to the public section of the declaration:
 Word(char ch);
Define
 Word::Word(char ch)
 {str[0]=ch;
  str[1]='\0';
 }
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
OR Add to the public section of the declaration
 Word operator=(char ch);
Define
 Word Word::opertaor=(char ch)
 {str[0]=ch;
  str[1]='\0';
  return *this;
 }
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<