(15 points--CS140Fa16-Mid2-List): Write a function named
findCaps that takes a list of words, wordList, and does two things:
1) returns a pointer to a new list of all words in wordList that
start with a capital letter, and 2) deletes all words in wordList that
start with a capital letter. Hence when the function returns, wordList
will consist solely of words that start with lowercase letters. For example,
if wordList starts as <"Brad", "Smiley", "summer", "Bill", "nancy"> then
findCaps will return a pointer to a list containing the words
<"Brad", "Smiley", "Bill">
and wordList will contain the words <"summer", "nancy"> when findCaps
is finished.
findCaps takes an STL list of strings as an argument and returns a pointer
to an STL list of strings. You will need to allocate this new list
in findCaps.
It is okay for either the returned list or the edited wordList to
be empty. For example, if wordList consists only of words that start
with a capital letter, then wordList will be empty when findCaps returns.
Constraints:
- Your algorithm must be O(n), which means that you cannot restart your
search of wordList from the beginning each time that you delete a word
from it. Instead you must remember where you were and pick up your search
from that location.
- wordList could be empty
- Do not print anything. The main that is provided prints the
lists that findCaps creates and manipulates.
Handy Methods From The STL's List API
- .begin(): Returns an iterator to the first element in the list
- .end(): Returns an iterator to the sentinel node
- .push_back(value): Adds a new value to the back of a list
- .erase(iterator): Deletes the value pointed to by iterator and
returns an iterator that points to the next element in the list. For
example, if the iterator points to "Bob" and the node after "Bob"
points to "Nancy", then the return value of .erase() is an iterator
that points to "Nancy".
Test Input and Driver Code:
We have written a main program that reads test input, calls findCaps,
and then prints the resulting lists.
You can provide test input by providing a list of words. For example:
Brad
Smiley
summer
Bill
nancy
(15 points--CS140Fa16-Mid2-Devices)
You have gone to work for a telecommunications company named BTT that
issues devices
to customers. BTT needs to keep track of the devices it has
issued to customers and for each customer it wants to keep track of the
devices that have been issued to that customer.
You are given the following data structures:
class Customer {
public:
string name;
string address;
set<string> devices;
public:
Customer(string n, string a);
};
Customer::Customer(string n, string a): name(n), address(a) {}
class Device {
public:
void addCustomer(string name, string address);
bool addDevice(string deviceName, string customerName);
void printCustomers();
protected:
set<string> devices;
map<string, Customer *> customerMap;
};
You need to write the three member functions for Device:
- addCustomer: This function should create a new customer object and
add the customer to the customerMap.
- addDevice: This function should verify that a device has not
yet been issued. If it has been issued (i.e., it is in the devices
map), then immediately return false. Otherwise add the device to
the devices set and the appropriate customer's device set, and return
true.
- printCustomers: This function should print the customers in
alphabetical order and the list of their devices in alphabetical
order. The format of
the output should be as follows:
- One line for the customer's name and address. This line
should be printed as follows:
- The customer's name left-justified in a field that is 15 characters
wide
- A single space
- The customer's address left-justified in a
field that is 30 characters wide.
- One line for each device name. This line should start with
4 blank spaces and then the device name.
As an example:
Brad 1678 Cardiff Rd.
BradLaptop
BradPhone
BradTablet
Constraints
- A new customer is guaranteed to be unique (i.e., you don't have
to check to see whether or not it's already in the map).
- A new device is not guaranteed to be unique (i.e., you must
verify whether or not it's in the set).
- The customer name associated with a device is guaranteed to already
be in the map.
Test Driver
We have prepared a test driver that will test your member functions.
The test driver accepts two commands:
- ADD_CUSTOMER customer_name customer_address
- ADD_DEVICE device_name customer_name
For example, the input:
ADD_CUSTOMER Brad 1678 Cardiff Rd.
ADD_CUSTOMER Smiley 4391 Redbud Dr.
ADD_DEVICE BradLaptop Brad
ADD_DEVICE BradPhone Brad
ADD_DEVICE BradLaptop Brad
ADD_DEVICE Laptop Smiley
ADD_DEVICE Tablet Smiley
ADD_DEVICE BradPhone Brad
ADD_DEVICE BradTablet Brad
ADD_CUSTOMER Nels 1838 Bluff Rd.
should generate the output
Brad 1678 Cardiff Rd.
BradLaptop
BradPhone
BradTablet
Nels 1838 Bluff Rd.
Smiley 4391 Redbud Dr.
Laptop
Tablet
Handy Methods From The STL's Set API
- .begin(): Returns an iterator to the first element in the set
- .end(): Returns an iterator to the set's sentinel node
- .insert(value): Adds a new value to the set and returns
a pair <set<string>::iterator, bool> element.
The iterator points to the inserted value, if the insert succeeded,
and the bool indicates whether or not the insert succeeded. The
insert succeeds if the value was not previously in the set and fails
otherwise.
Handy Methods From The STL's Map API
- .begin(): Returns an iterator to the first element in the map
- .end(): Returns an iterator to the map's sentinel node
- .find(key): Returns an iterator that points to the node containing
the key/value pair if the key is found in the map and that points to
the map's sentinel node if the key is not found. The specific return
value is a pointer to a
pair<key_type first, value_type second> object.
- .insert(make_pair(key, value)): Adds a new key/value pair to the
map.
- Map_Name[key]: treats the map as an associative map and returns
the value associated with the key if Map_Name[key] is on the right hand side of
the assignment statement and assigns a value to the key if Map_Name[key]
is on the left hand side of the assignment statement.