1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#include "AddressBook.h"
#include <cstring>
#include <iostream>
static int head = 0;
static int tail = -1;
AddressBook::AddressBook() {}
AddressBook::AddressBook(const Person &p) { addPerson(p); }
AddressBook::AddressBook(const Person p[], int size)
{
for (int i = 0; i < size; i++)
addPerson(p[i]);
}
AddressBook::AddressBook(char *fName, char *lName, char *address)
{
Person tmp;
std::strncpy(tmp.firstname, fName, 19);
tmp.firstname[19] = '\0';
std::strncpy(tmp.lastname, lName, 19);
tmp.lastname[19] = '\0';
std::strncpy(tmp.address, address, 19);
tmp.address[49] = '\0';
addPerson(tmp);
}
bool AddressBook::addPerson(const Person p)
{
if (head < MAXIMUM)
{
person[head] = p;
head++;
if (tail == -1) { tail++; }
return true;
}
return false;
}
bool AddressBook::getPerson(Person &p)
{
if (tail >= 0)
{
if (tail >= MAXIMUM) { tail = 0; }
p = person[tail];
tail++;
return true;
}
return false;
}
bool AddressBook::findPerson(char *lName, Person &p)
{
for (int i = 0; i < head; i++)
{
if (!strcmp(person[i].lastname, lName))
{
p = person[i];
return true;
}
}
return false;
}
bool AddressBook::findPerson(char *lastName, char *firstName, Person &p)
{
for (int i = 0; i < head; i++)
{
if ( !strcmp(person[i].lastname, lastName)
&& !strcmp(person[i].firstname, firstName))
{
p = person[i];
return true;
}
}
return false;
}
void AddressBook::printBook()
{
for (int i = 0; i < head; i++)
{
std::cout << person[i].firstname << '\t' << person[i].lastname << '\t'
<< person[i].address << '\n';
}
}
void AddressBook::sort()
{
Person temp;
for (int i = 0; i < head; i++)
{
for (int j = 0; j < head; j++)
{
if (person[j + 1].lastname < person[j].lastname)
{
temp = person[j];
person[j] = person[j + 1];
person[j + 1] = temp;
}
}
}
}
std::ostream& operator<<(std::ostream& output, const Person& p)
{
output << '(' << p.firstname << ", " << p.lastname << ')';
return output;
}
Person& AddressBook::operator() (int i) { return person[i]; }
|