Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment you will extend your phonebook program from A 5 . If you are not confident in your solution from A 5 you

In this assignment you will extend your phonebook program from A5. If you are not confident
in your solution from A5 you may reference and use parts of the example solution provided.
However, attribution of use of the example solution must be provided, you should
document what you have used, and indicate changes you have made
You may either copy your A5 phonebook.c and Makefile, or branch from A4 in your git
repo. (git rebase may be helpful here).
Question 1[10 points]
Write a Makefile that builds a C program phonebook.c and produces the executable object
file phonebook. Your Makefile should use the compiler flags discussed in class, and have the
all and clean targets as shown previously. You should also include the flag -Wextra as
discussed in the lab.
Question 2[70 points]
In this question, you are going to extend the functionality of phonebook.c.
(a)(20 points) It is often useful for a phonebook to be kept in alphabetical order for easy
look up. We want to modify our phonebook add such that the new entry is inserted
at the correct position1. The API remains otherwise the same.
(b)(20 points) Sometimes mistakes happen in entry, like the same name getting misspelled
repeatedly, or the phone numbers all missing area codes. We want create a method
called phonebook map. It should take 2 arguments:
(1) a pointer to struct phonebook list
(2) a function pointer of type void (*func ptr)(struct phonebook entry *item)
This function should iterate the entire list and apply the passed function to each item.
This applies an in place change to each item. The function has no return value. Define
the following functions that could be passed to phonebook map:
(a) void respell(struct phonebook entry *item)- Which changes the spelling
of John to Jon
(b) void addAreaCode(struct phonebook entry *item)- Which adds 1306 to
the front of all phone numbers (remember these are unsigned long ints, you
can assume they are only 7 digit numbers though.)
1strncmp doenst actually return alphabetical in terms we would normally think, but well use its -/0/+
sorting
2
(c)(20 points) Sometimes we need to select all the entries from list which match a criteria.
Create a method called phonebook filter. It should take 3 arguments:
(1) a pointer to a source struct phonebook list
(2) a pointer to a destination struct phonebook list
(3) a function pointer of type struct phonebook entry *(*func ptr)(struct phonebook entry
*item)
This function should iterate the entire source list, apply the passed function, and then
if a non-NULL value is returned it should add that value to the destination list. The
function has no return value. Define the following functions that could be passed to
phonebook filter:
(a)(struct phonebook entry *) noHomers(struct phonebook entry *item)- Which
removes (returns NULL) any name containing Homer(you can expect case to
match, but should search the entire string that is MrHomer would still match).
(b)(struct phonebook entry *) torontoOnly(struct phonebook entry *item)
- Which only keeps numbers starting with 1416/
(d)(10 points) Add Map and Filter call to your command line. These will take a number:
1 or 2, which corresponds to the functions above. i.e. for map, 2 is addAreaCode.
a for add; e.g.,
a
Jon
11234567890
This should add a phonebook entry with the given name and phone number.
f for find; e.g.,
f
Jon
In this example, the output should be the value corresponding to Jon if the key
exists in the phonebook, or Key not found if the key is not in the phonebook.
r for remove; e.g.,
r
Jon
3
The output should be success or fail, depending on whether the key was found
and removed.
m for map; e.g.,
m
1
No output expected.
l for filter; e.g.,
l
1
Should output the filtered list (note this function should work more than once).
p for print all; e.g.,
p
The output should be all phonebook entries.
q to quit the program.
Question 3[20 points]
In this part, you will test your phonebook program. But we will use a unittest approach.
Recall from the labs that unittests evaluate individual functions programmatically. For this
section you will create multiple unittest functions of the following functions:
phonebook init -1 test
phonebook add -5 tests
phonebook find -5 test
phonebook remove -5 test
phonebook map -2 tests
phonebook filter -2 tests
Each of these functions (20 total) should initialize data it needs and preform a single test
action. i.e. inserting to an empty list, as well as programmatically validate the result,
returning 0 on success, and -1 on failure. Your unittests should not require outside verification
of your code.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions