c++ program You are to write a program that will read in names and phone numbers
ID: 3911098 • Letter: C
Question
c++ program
You are to write a program that will read in names and phone numbers, rearrange each person’s name, and create a sorted phone directory in a link list format. The list has been collected form different places, placed in a file where the names are not in some standard format. A person’s last name may appear last or it may appear first on the input. A label has been associated with each name to identify where the last name appears. Either “surname” or “lastname” appears just before a person’s last name. The other will be his first name followed by optionally middle initial or name. Capitalize the first letters in each part of the name. Below are examples of inputs. Input file: ‘NamesAndPhoneV2.txt’
Read in the names, and arrange them with the first name first, followed by his middle name or initial if it exist followed by the last name. You must use a link list for you data structure and the list must be a sorted list, sorted on the last name. You must insert a new name into the link list and maintain a sorted order link list. Print out the resulting names and phone directory.
Hint: Read input as ‘strings’ even the phone number. Look for ‘ ’ for the end of line character if reading one char at a time, or optionally if the last string you read had a digit in it (ie looking for a digit might be the better way).
Example input
tom archer lastname jones 555-1234
lastname dewy luewey 288-1324
See More surname Or-less 332-6133
Example output (Sorted):
Luewey Dewy 288-1324
Tom Archer Jones 555-1234
See More Or-less 332-6133
Explanation / Answer
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
struct node
{
string firstName;
string lastName;
node *next;
};
class List
{
private:
node *head, *tail;
public:
List()
{
head = NULL;
tail = NULL;
}
void create_contact(string first, string last) // Create node and add it onto the linked list
{
node *temp = new node;
temp->firstName = first;
temp->lastName = last;
temp->next = NULL;
if (head == NULL)
{
head = temp;
tail = temp;
temp = NULL;
}
else
{
tail->next = temp;
tail = temp;
}
}
void display_all() // Prints out current trip
{
int contactNum = 1;
node *temp = new node;
temp = head;
while (temp != NULL) // Loop through the list while the temporary node is not empty
{
cout << " Contact Number: " << contactNum << endl;
cout << "First Name: " << temp->firstName << endl;
cout << "Last Name: " << temp->lastName << endl;
++contactNum;
temp = temp->next;
}
}
void delete_position(int pos) // delete a stop by using the position in the list
{
node *current = new node;
node *previous = new node;
node *next = new node;
current = head;
for (int i = 1;i<pos;i++) // Loop through the link list while the current node is not empty
{
if (current == NULL)
return;
previous = current;
current = current->next;
}
next = current->next;
previous->next = current->next;
delete current;
}
void delete_head() // delete head node
{
node *temp = new node;
temp = head;
head = head->next;
delete temp;
}
};
int main()
{
List Contacts; // create a Contacts item for the List class
int choice, position;
string firstName;
string lastName;
while (1) {
cout << " What would you like to do?: " << endl;
cout << "1. Show All Contacts" << endl;
cout << "2. Add A Contact" << endl;
cout << "3. Remove A Contact" << endl;
cout << "4. Search Contacts (Coming Soon)" << endl;
cout << "5. Exit The Program" << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << endl;
Contacts.display_all(); // display all contacts
cout << endl;
break;
case 2:
cout << " Enter your first name: ";
cin >> firstName;
cout << " Enter your last name: ";
cin >> lastName;
Contacts.create_contact(firstName, lastName); // create the contact in the linked list
cout << endl;
break;
case 3:
cout << "Enter the contact number of the contact you would like to remove: ";
cin >> position;
if (position == 1)
Contacts.delete_head();
else
Contacts.delete_position(position); // delete contact from list
break;
case 5:
exit(1);
break;
default:
cout << " " << choice << " is not an option. Please select a valid option." << endl;
break;
}
}
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.