Complete Source Code if needed: https://repl.it/@BackUp/Assignment-1-Q1 #include
ID: 3703117 • Letter: C
Question
Complete Source Code if needed: https://repl.it/@BackUp/Assignment-1-Q1
#include "addressBookType.hpp"
void addressBookType::swap(int m, int n)
{
string str1, str2, str3, str4;
person[m].getPerson(str1, str2);
person[n].getPerson(str3, str4);
person[m].setPerson(str3, str4);
person[n].setPerson(str1, str2);
dateType bd1, bd2;
person[m].getBirthDate(bd1);
person[n].getBirthDate(bd2);
person[m].setBirthDate(bd2);
person[n].setBirthDate(bd1);
addressType add1,add2;
person[m].getAddress(add1);
person[n].getAddress(add2);
person[m].setAddress(add2);
person[n].setAddress(add1);
str1=person[m].getAssociation();
str2=person[n].getAssociation();
person[m].setAssociation(str2);
person[n].setAssociation(str1);
str1=person[m].getPhone();
str2=person[n].getPhone();
person[m].setPhone(str2);
person[n].setPhone(str1);
}
addressBookType::addressBookType()
{
ifstream inFile;
string fileName = " ";
cout<<" Enter input file name:";
cin>>fileName;
inFile.open(fileName.c_str());
if (!inFile.is_open())
{
cout<<" Error reading in input file";
system("pause");
exit(0);
}
string strs [7];
int nums [4];
ind = 0;
while ((inFile>>strs[0]>>strs[1]>>nums[0]>>nums[1]>>nums[2]>>strs[2]>>strs[3]>>strs[4]>>nums[3]>>strs[5]>>strs[6]))
{
person [ind].setExtPerson(strs[0], strs[1], nums[0], nums[1], nums[2], strs[2], strs[3], strs[4], nums[3], strs [5], strs[6]);
ind++;
}
}
void addressBookType::sortByLastName()
{
int i, j, smallestInd;
string str1, str2, temp;
for (i=0; i < (ind-1); i++)
{
smallestInd=i;
for(j = i+1; j {
person[j].getPerson(temp, str1);
person[smallestInd].getPerson(temp, str2);
if (str1.compare(str2)<0)
smallestInd=j;
}
if (smallestInd != i)
swap(i, smallestInd);
}
}
void addressBookType::printAddressBook() const
{
for(int i=0; i {
cout << " ";
person[i].print();
}
}
int addressBookType::searchBYName() const
{
string keyName, name, temp;
int count=0;
cout<<" Please enter the last name to search:";
cin>>keyName;
for(int i=0; i {
person[i].getPerson(temp,name);
if (keyName.compare(name)==0)
return i;
}
return -1;
}
void addressBookType::printAPerson() const
{
int result = searchBYName();
if (result == -1)
cout<<" No person found as specified by you.";
else
{
cout<<" ";
person[result].print();
}
}
void addressBookType::printByBirthday() const
{
dateType bd;
int month, count=0;
cout<<" Search address book for a given month of birthdays."<<" Please remember name is case sensitive.";
cout<<" Enter the month number (1...12):";
cin>>month;
for(int i=0; i {
person[i].getBirthDate(bd);
if(bd.getMonth()==month)
{
cout<<" ";
person[i].print();
count++;
}
}
if(count==0)
cout<<" No person was found.";
}
void addressBookType::printAssociation() const
{
string asso;
string types[3]={"Familymember", "Familyfriend", "BusinessAssociate"};
int choice, count=0;
cout<<" Search address book by association."<<" Please remember name is case sensitive.";
cout<<" Associatin Types";
cout<<" Family member : 1";
cout<<" Family friend : 2";
cout<<" Business Associate : 3";
cout<<" Enter the association type: ";
cin>>choice;
for (int i=0; i {
asso=person[i].getAssociation();
if(asso.compare(types[choice-1])==0)
{
cout<<" ";
person[i].print();
count++;
}
}
if (count==0)
cout<<" No person was found.";
}
.hpp
#ifndef addressBookType_hpp
#define addressBookType_hpp
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include "extPersonType.hpp"
using namespace std;
const int MAX_ENTRIES = 500;
class addressBookType
{
public:
addressBookType();
void sortByLastName();
int searchBYName() const;
void printAPerson () const;
void printAddressBook () const;
void printByBirthday() const;
void printNamesBetween() const;
void printAssociation() const;
private:
extPersonType person [MAX_ENTRIES];
int ind;
void swap (int, int);
};
#endif /* addressBookType_hpp */
Explanation / Answer
//addressBookType.hpp
#ifndef addressBookType_hpp
#define addressBookType_hpp
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include "extPersonType.hpp"
using namespace std;
//Class to handle linked list
class extPersonTypeNode
{
public:
extPersonType person; //To store person info (class extPersonType)
extPersonTypeNode *next; //Hold next data address
};
class addressBookType
{
public:
addressBookType();
void sortByLastName();
extPersonTypeNode* searchBYName() const; //Return type of function changed
void printAPerson () const;
void printAddressBook () const;
void printByBirthday() const;
void printNamesBetween() const;
void printAssociation() const;
private:
extPersonTypeNode *head; //Hold address of root of linked list
int ind;
void swap (extPersonTypeNode *, extPersonTypeNode *); //Swap parameter change
};
#endif /* addressBookType_hpp */
=================================================================
//addressBookType.cpp
#include "addressBookType.hpp"
void addressBookType::swap(extPersonTypeNode *m, extPersonTypeNode *n) //Instead of array using Node pointer for swapping
{
string str1, str2, str3, str4;
m->person.getPerson(str1, str2);
n -> person.getPerson(str3, str4);
m -> person.setPerson(str3, str4);
n -> person.setPerson(str1, str2);
dateType bd1, bd2;
m -> person.getBirthDate(bd1);
n -> person.getBirthDate(bd2);
m -> person.setBirthDate(bd2);
n -> person.setBirthDate(bd1);
addressType add1,add2;
m -> person.getAddress(add1);
n -> person.getAddress(add2);
m -> person.setAddress(add2);
n -> person.setAddress(add1);
str1=m -> person.getAssociation();
str2=n -> person.getAssociation();
m -> person.setAssociation(str2);
n -> person.setAssociation(str1);
str1=m -> person.getPhone();
str2=n -> person.getPhone();
m -> person.setPhone(str2);
n -> person.setPhone(str1);
}
addressBookType::addressBookType()
{
ifstream inFile;
string fileName = " ";
head = NULL;
cout<<" Enter input file name:";
cin>>fileName;
inFile.open(fileName.c_str());
if (!inFile.is_open())
{
cout<<" Error reading in input file";
system("pause");
exit(0);
}
string strs [7];
int nums [4];
ind = 0;
extPersonTypeNode *last; //Hold address of last node
while ((inFile>>strs[0]>>strs[1]>>nums[0]>>nums[1]>>nums[2]>>strs[2]>>strs[3]>>strs[4]>>nums[3]>>strs[5]>>strs[6]))
{
extPersonTypeNode *curr;
curr = new extPersonTypeNode(); //Create new node for new data
curr -> person.setExtPerson(strs[0], strs[1], nums[0], nums[1], nums[2], strs[2], strs[3], strs[4], nums[3], strs [5], strs[6]);
curr -> next = NULL;
ind++;
if(head == NULL) //If head is null assign directly
{
head = curr;
last = head;
}
else //append new node to last of linked list
{
last -> next = curr;
last = curr;
}
}
}
void addressBookType::sortByLastName()
{
string str1, str2, temp;
extPersonTypeNode * smallestInd;
for (extPersonTypeNode *curr = head; curr != NULL; curr = curr -> next)
{
smallestInd=curr;
for(extPersonTypeNode *nxtNode = curr -> next; nxtNode != NULL; nxtNode = nxtNode->next)
{
nxtNode -> person.getPerson(temp, str1);
smallestInd -> person.getPerson(temp, str2);
if (str1.compare(str2)<0)
smallestInd = nxtNode;
}
if (smallestInd != curr)
swap(curr, smallestInd);
}
}
void addressBookType::printAddressBook() const
{
extPersonTypeNode *curr;
curr = head;
while(curr != NULL) //Traverse till end of list
{
cout << " ";
curr -> person.print();
curr = curr -> next;
}
}
extPersonTypeNode * addressBookType::searchBYName() const //Return node if found else return NULL
{
string keyName, name, temp;
cout<<" Please enter the last name to search:";
cin>>keyName;
extPersonTypeNode *curr;
curr = head;
while(curr != NULL)
{
curr -> person.getPerson(temp,name);
if (keyName.compare(name)==0)
return curr;
curr = curr -> next;
}
return NULL;
}
void addressBookType::printAPerson() const
{
extPersonTypeNode *result = searchBYName();
if (result == NULL)
cout<<" No person found as specified by you.";
else
{
cout<<" ";
result -> person.print();
}
}
void addressBookType::printByBirthday() const
{
dateType bd;
int month, count=0;
cout<<" Search address book for a given month of birthdays."<<" Please remember name is case sensitive.";
cout<<" Enter the month number (1...12):";
cin>>month;
extPersonTypeNode *curr;
curr = head;
while(curr != NULL)
{
curr->person.getBirthDate(bd);
if(bd.getMonth()==month)
{
cout<<" ";
curr -> person.print();
count++;
}
curr = curr -> next;
}
if(count==0)
cout<<" No person was found.";
}
void addressBookType::printAssociation() const
{
string asso;
string types[3]={"Familymember", "Familyfriend", "BusinessAssociate"};
int choice, count=0;
cout<<" Search address book by association."<<" Please remember name is case sensitive.";
cout<<" Associatin Types";
cout<<" Family member : 1";
cout<<" Family friend : 2";
cout<<" Business Associate : 3";
cout<<" Enter the association type: ";
cin>>choice;
extPersonTypeNode *curr;
curr = head;
while(curr != NULL)
{
asso=curr -> person.getAssociation();
if(asso.compare(types[choice-1])==0)
{
cout<<" ";
curr -> person.print();
count++;
}
curr = curr -> next;
}
if (count==0)
cout<<" No person was found.";
}
=============================================================
input.txt (prepared based on code and for testing)
Tom BT 4 23 2018 xyz ABC MH 234567 Familymember 1234567
TEST5 BT6 7 2 1991 xyz ABC MH 234567 Familyfriend 1234567
TEST6 BT7 6 30 1992 xyz ABC MH 234567 Familyfriend 1234567
TEST7 BT8 9 15 1993 xyz ABC MH 234567 Familyfriend 1234567
TEST8 BT9 8 4 1995 xyz ABC MH 234567 Familyfriend 1234567
TEST9 BT11 10 22 1987 xyz ABC MH 234567 Familymember 1234567
TEST2 BT3 5 11 1989 xyz ABC MH 234567 BusinessAssociate 1234567
TEST3 BT4 2 12 1979 xyz ABC MH 234567 BusinessAssociate 1234567
TEST4 BT5 1 13 1988 xyz ABC MH 234567 BusinessAssociate 1234567
TEST10 BT10 12 1 1999 xyz ABC MH 234567 Familymember 1234567
TEST BT2 3 3 1990 xyz ABC MH 234567 2 Familymember 234567
-------------------------------------------------
OUTPUT :
Welcome to the address book program.
Enter input file name:input.txt
These are the addresses before sorting:
4-23-2018
Tom BT
xyz
ABC
MH
234567
Familymember, 1234567
7-2-1991
TEST5 BT6
xyz
ABC
MH
234567
Familyfriend, 1234567
6-30-1992
TEST6 BT7
xyz
ABC
MH
234567
Familyfriend, 1234567
9-15-1993
TEST7 BT8
xyz
ABC
MH
234567
Familyfriend, 1234567
8-4-1995
TEST8 BT9
xyz
ABC
MH
234567
Familyfriend, 1234567
10-22-1987
TEST9 BT11
xyz
ABC
MH
234567
Familymember, 1234567
5-11-1989
TEST2 BT3
xyz
ABC
MH
234567
BusinessAssociate, 1234567
2-12-1979
TEST3 BT4
xyz
ABC
MH
234567
BusinessAssociate, 1234567
1-13-1988
TEST4 BT5
xyz
ABC
MH
234567
BusinessAssociate, 1234567
12-1-1999
TEST10 BT10
xyz
ABC
MH
234567
Familymember, 1234567
3-3-1990
TEST BT2
xyz
ABC
MH
234567
2, Familymember
These are the addresses after sorting:
4-23-2018
Tom BT
xyz
ABC
MH
234567
Familymember, 1234567
12-1-1999
TEST10 BT10
xyz
ABC
MH
234567
Familymember, 1234567
10-22-1987
TEST9 BT11
xyz
ABC
MH
234567
Familymember, 1234567
3-3-1990
TEST BT2
xyz
ABC
MH
234567
2, Familymember
5-11-1989
TEST2 BT3
xyz
ABC
MH
234567
BusinessAssociate, 1234567
2-12-1979
TEST3 BT4
xyz
ABC
MH
234567
BusinessAssociate, 1234567
1-13-1988
TEST4 BT5
xyz
ABC
MH
234567
BusinessAssociate, 1234567
7-2-1991
TEST5 BT6
xyz
ABC
MH
234567
Familyfriend, 1234567
6-30-1992
TEST6 BT7
xyz
ABC
MH
234567
Familyfriend, 1234567
9-15-1993
TEST7 BT8
xyz
ABC
MH
234567
Familyfriend, 1234567
8-4-1995
TEST8 BT9
xyz
ABC
MH
234567
Familyfriend, 1234567
Please enter the last name to search:BT9
Person found.
Search address book for a given month of birthdays.
Please remember name is case sensitive.
Enter the month number (1...12):4
4-23-2018
Tom BT
xyz
ABC
MH
234567
Familymember, 1234567
Search address book by association.
Please remember name is case sensitive.
Associatin Types
Family member : 1
Family friend : 2
Business Associate : 3
Enter the association type: 2
7-2-1991
TEST5 BT6
xyz
ABC
MH
234567
Familyfriend, 1234567
6-30-1992
TEST6 BT7
xyz
ABC
MH
234567
Familyfriend, 1234567
9-15-1993
TEST7 BT8
xyz
ABC
MH
234567
Familyfriend, 1234567
8-4-1995
TEST8 BT9
xyz
ABC
MH
234567
Familyfriend, 1234567
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.