Latest Update: 04/09/2000
Week 09 Notes for CS 2500 -- Spring 2000
(this is draft material)
* Take roll
* Schedule Highlights
+ level-three program due today: check out
instructions on shar
* We have to start discussing pointers and
linked lists.
* discuss static array lists versus dynamic
linked lists.
* examine the implementation of the linked list.
* I put another gotcha in the list. It is a
warning about doing indentation, headers, and
testing.
=========================
MONDAY
=========================
=========================
WEDNESDAY
=========================
* Final version of program #3 is due Monday, April 17
* Tentative date for Quiz #2 is Friday, April 21.
* Everybody OK with that?
* I will try to put a program #4 assignment in
the web space soon. People who want to get
started over Spring break will have the
opportunity
* continue studying implementation of linked
list.
* code fragment of the day:
/* ================================================== */
/* FUNCTION doUnMatch */
/* ================================================== */
/* Get the args and execute the UNMATCH command */
void doUnMatch(clientData clientD, ifstream & comndFile)
{
if (CHECKING)
cout << " Entering \"doUnMatch.\"" << endl ;
string lastName, firstName ;
/* Read in the names and echo them */
comndFile >> lastName >> firstName ;
cout << " " << lastName << " " << firstName << endl ;
bool success ; int genderID, listPos ;
findClient(clientD, lastName, firstName, genderID, listPos, success) ;
if (success)
{
clientItemType theClient ;
/* Retrieve theClient from listPos in the list
identified by genderID */
/* We must not merely zero out theClient.matchPos
now. We need the current value to locate the
person to whom this client is matched. We need
to zero out the match field of that person's
record. */
int posOfMatch=theClient.matchPos ;
if (posOfMatch>0) /* if there really is a match */
{
clientItemType matchOfClient;
/* Note: it is a useful "trick" that when
MEN==0 and WOMEN==1, 1-MEN == WOMEN and
1-WOMEN == MEN */
/* Retrieve the matchOfClient from position
posOfMatch in the list with number
1-genderID and zero out the match field of
matchOfClient. */
/* Next delete the (old) element in the same
list at the same position position. Now
insert matchOfClient into the same list at
same position (or you could insert at the
end of the list.)
/* Now zero out the match field of the
theClient. */
/* Next delete the (old) version of theClient
from the first list, and then insert the new
version of theClient, which has match field
equal to zero.
}
}
/* After all the output of the command has been
written, we place this separator in the output so
it will be easy to find the boundaries between
outputs for different commands. This version of
the function should be improved a little -- write
a message if the client is successfully
unmatched. Otherwise print a message saying what
went wrong. This is useful for the user of the
program and also can help the programmer while
testing and debugging this function. */
cout << "==================================================" << endl ;
cout << endl ;
}
=========================
FRIDAY
=========================
* code piece du jour: A run-down on how to match-up two
clients.
/* ================================================== */
/* FUNCTION isMatched */
/* ================================================== */
/*
Return true if the string s is in "list" else return
false.
*/
bool IsMatched(string s, stringListClass list)
{
bool success=false, opSuccess ;
int posInList ;
string mayMatch ;
/* Make a for-loop that ranges from posInList=1
to posInList=list.ListLength() */
for (/* whatever */)
{
/* Retrieve into mayMatch the list item
in position posInList. */
/* If s equals mayMatch then set success to true */
}
return success ;
}
/* ================================================== */
/* FUNCTION interestsMatch */
/* ================================================== */
/*
Return true if "theClient" and "candidate" have
three or more matching interests, else return false.
Two interests match if they are exactly equal as
strings.
*/
bool interestsMatch( clientItemType theClient,
clientItemType candidate )
{
string curInterest ;
int posInClient, numMatches=0;
bool success ;
/* Make a for-loop that ranges from posInClient=1
to posInClient=theClient.numInterests */
for (/* whatever */)
{
/* Retrieve into curInterest the element
of theClient.interests in position posInClient. */
/* If IsMatched(curInterest, candidate.interests)
returns true then increment numMatches. */
}
if (numMatches>=3) return true ;
else return false ;
}
/* ================================================== */
/* FUNCTION tryMatch */
/* ================================================== */
/*
Attempt to match the client in position place of
clientD[genderID] with a member of the opposite sex.
A match occurs if and when this function finds a
previously unmatched client with three or more of the
same interests.
Two interests are "the same" if they are exactly
equal as strings.
This function only attempts to find the first person
who matches the client.
PRECONDITION: all interest lists are sorted to make the
match process easier. (This turned out not to be useful --
it is not "easier" if the interests are sorted. It is
just that one can be more "efficient" if they are sorted.)
If a match is found this function designates both
persons as matched, prints the name of the client,
the name of his or her match, and both of their phone
numbers.
If no match is found, this function prints an
appropriate message. */
void tryMatch(clientData clientD, int genderID, int place)
{
int opSex = 1-genderID;
int listLength=clientD[opSex].ListLength() ,
listPos=0 ;
clientItemType theClient, candidate ;
bool success=false, fnSuccess ;
/* Retrieve into "theClient" the element in position "place" in
clientD[genderID]. */
while (!success && listPos < listLength)
{
listPos++ ;
/* Retrieve into "candidate" the element in position
"listPos" in clientD[opSex]. */
success = (candidate.matchPos == 0) &&
(interestsMatch(theClient, candidate) ) ;
}
/* If success is true, call a function that takes care of
changing the match fields of the two people's records,
and writes the names and phone numbers to the standard
output. If success is false then just output a message
saying that no match was found for this client. */
}