[cpp] dec1
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- /*
- * ContactList.cpp
- *
- * Interviewer:
- * Candidate:
- */
- #include <string>
- #include <iostream>
- #include "console.h"
- #include "simpio.h"
- #include "strlib.h"
- #include "hashcode.h"
- using namespace std;
- class ContactList {
- public:
- /*
- * Contructor for ContactList
- */
- ContactList();
- /*
- * Destructor for ContactList
- */
- ~ContactList();
- /*
- * Method: putContact
- * Usage: list.putContact("NAME", "###-####", "BIRTHDAY");
- * ---------------------------
- * Places a contact with a name, phone number, and birthday, into the ContactList.
- * If a phone number is not in the format ###-####, insert “unknown” as the phone
- * number for the contact, instead of the inputted number.
- * Assume that all birthday strings entered are valid. Also, assume that no two users
- * in the ContactList will ever have the same name, and that names are case sensitive.
- */
- void putContact(string name, string number, string birthday);
- /*
- * Method: printContact
- * Usage: list.printContact("NAME");
- * ---------------------------
- * Prints and returns the birthday and phone number of a contact.
- * Format must look like this:
- * "Contact: CONTACT, Phone Number: NUMBER, Birthday: BIRTHDAY"
- *
- * If contact not in list return the following string: "Contact not found."
- */
- string printContact(string name);
- /*
- * Method: changePhoneNumber
- * Usage: list.changePhoneNumber("NAME", ###-####);
- * ---------------------------
- * Changes the phone number associated with a contact.
- * If a phone number is not in the format ###-####, insert “unknown” as the phone
- * number for the contact, instead of the inputted number.
- * If the contact is not in the ContactList, throw any string literal exception.
- */
- void changePhoneNumber(string name, string number);
- private:
- // TODO add private members and function declarations here
- struct Node {
- string name;
- string birthday;
- string number;
- Node* next;
- };
- Node** table;
- int NUM_BUCKETS = 1000;
- void deleteLL(Node* head);
- bool checkPhoneNumber(string number);
- };
- // TODO implement public (and optional private) functions below
- ContactList::ContactList() {
- table = new Node*[NUM_BUCKETS]();
- for (int i = 0; i < NUM_BUCKETS; i++) {
- table[i] = nullptr;
- }
- /* TODO: Your implementation here */
- }
- ContactList::~ContactList() {
- for (int i = 0; i < NUM_BUCKETS; i++) {
- deleteLL(table[i]);
- }
- /* TODO: Your implementation here */
- }
- void ContactList::deleteLL(Node* head) {
- Node* temp = head;
- while (temp) {
- head = head->next;
- temp = head;
- delete temp;
- }
- }
- bool ContactList::checkPhoneNumber(string number) {
- if (number.size() == 8 && number[3] == '-') {
- for (int i = 0; i < 8; i++) {
- if (i != 3 && !isdigit(number[i])) {
- return false;
- }
- return true;
- }
- }
- return false;
- }
- void ContactList::putContact(string name, string number, string birthday){
- if (checkPhoneNumber(number)) {
- number = "unknown";
- }
- Node* toAdd = new Node();
- toAdd->name = name;
- toAdd->number = number;
- toAdd->birthday = birthday;
- Node* temp = table[hashCode(name) % NUM_BUCKETS];
- table[hashCode(name) % NUM_BUCKETS] = toAdd;
- toAdd->next = temp;
- }
- string ContactList::printContact(string name){
- /* TODO: Your implementation here */
- string output = "Contact not found.";
- Node* ref = table[hashCode(name) % NUM_BUCKETS];
- output = "Contact: ";
- while (ref) {
- if (ref->name == name) {
- output += ref->name;
- output += ", Phone Number: ";
- output += ref->number;
- output += ", Birthday: ";
- output += ref->birthday;
- cout << output << endl;
- break;
- }
- }
- return output;
- }
- void ContactList::changePhoneNumber(string name, string number){
- if (!checkPhoneNumber(number)) {
- number = "unknown";
- }
- Node* ref = table[hashCode(name) % NUM_BUCKETS];
- while (ref) {
- ref = ref->next;
- if (ref->name == name) {
- ref->number = number;
- }
- }
- }
- /* ------------------------------------------------------------------------------
- * Code beyond this line is a bug-free test harness. You may edit code below this line
- * if desired to add tests, but there are no bugs that need to be fixed in the section
- * below.
- * ------------------------------------------------------------------------------
- */
- void confirmPrint(ContactList& list, string expectedName, string expectedNumber, string expectedBirth, bool found) {
- bool passed;
- if (!found) {
- cout << "Expected output: " << "Contact not found." << endl << "Student output: ";
- passed = list.printContact(expectedName) == "Contact not found.";
- } else {
- stringstream ss;
- ss << "Contact: " << expectedName;
- ss << ", Phone Number: " << expectedNumber;
- ss << ", Birthday: " << expectedBirth;
- cout << "Expected output: " << ss.str() << endl << "Student output: ";
- passed = list.printContact(expectedName) == ss.str();
- }
- if (passed) {
- cout << "TEST CASE PASSED!" << endl;
- } else {
- cout << "TEST CASE FAILED!" << endl;
- }
- }
- int main() {
- string testnames [6] = {"Lisa", "Shubham", "David", "Jeremiah", "Clyde", "Kate"};
- string testnumbers [6] = {"666-7777", "408-625-4897", "443-8856", "494-89565", "563-8425", "i forgot"};
- string testbirths [6] = {"1/23/1997", "8/21/1965", "4/7/1983", "8/21/1993", "9/21/2000", "5/4/1995"};
- ContactList list;
- cout << "Test Case 1: Add one contact." << endl;
- list.putContact(testnames[0], testnumbers[0], testbirths[0]);
- confirmPrint(list, testnames[0], testnumbers[0], testbirths[0], true);
- cout << endl << endl;
- cout << "Test Case 2: Add one contact with an invalid phone number." << endl;
- list.putContact(testnames[1], testnumbers[1], testbirths[1]);
- confirmPrint(list, testnames[1], "unknown", testbirths[1], true);
- cout << endl << endl;
- cout << "Test Case 3: Checking for a nonexistent contact." << endl;
- confirmPrint(list, "Jerry", "", "", false);
- cout << endl << endl;
- cout << "Test Case 4: Adding some more contacts." << endl;
- for (int i = 2; i < 6; i++) {
- list.putContact(testnames[i], testnumbers[i], testbirths[i]);
- }
- for (int i = 2; i < 6; i++) {
- confirmPrint(list, testnames[i], i % 2 == 0 ? testnumbers[i] : "unknown", testbirths[i], true);
- }
- cout << endl << endl;
- cout << "Test Case 5: Changing one phone number to something valid." << endl;
- list.changePhoneNumber(testnames[5], testnumbers[0]);
- confirmPrint(list, testnames[5], testnumbers[0], testbirths[5], true);
- cout << endl << endl;
- cout << "Test Case 6: Changing one phone number to something invalid." << endl;
- list.changePhoneNumber(testnames[4], testnumbers[1]);
- confirmPrint(list, testnames[4], "unknown", testbirths[4], true);
- cout << endl << endl;
- cout << "Test Case 7: Changing every phone number." << endl;
- for (int i = 2; i < 6; i++) {
- list.changePhoneNumber(testnames[5-i], testnumbers[i]);
- }
- for (int i = 2; i < 6; i++) {
- confirmPrint(list, testnames[5-i], i % 2 == 0 ? testnumbers[i] : "unknown", testbirths[5-i], true);
- }
- cout << endl << endl;
- return 0;
- }
Editor
You can edit this paste and save as new: