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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
//sorted by Last Name & First Name
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
/**************************************************************
Defining my Person class with an Array for LastName & FirstName
**************************************************************/
class Person
{
public:
// char szLastName[20], szFirstName[20];
string szLastName;
string szFirstName;
bool IdenticalPerson(string firstName, string lastName) {
if (szLastName.compare(lastName) == 0) {
if (szFirstName.compare(firstName) == 0) {
return true;
}
}
return false;
}
};
/**************************************************************
This function Inserts the Person name
**************************************************************/
bool insertPerson(Person people[], int nCount, Person newPerson)
{
for (int index = 0;index < nCount; index++)
{
if (people[index].IdenticalPerson(newPerson.szFirstName, newPerson.szLastName)) {
// Do not insert
cout << "Person Exists, this person was not added";
return false;
}
}
people[nCount].szFirstName = newPerson.szFirstName;
people[nCount].szLastName = newPerson.szLastName;
return true;
}
Person getPerson()
{
Person person;
cout << "\nEnter another Person\n"
<< "First name: ";
cin >> person.szFirstName;
cout << "Last Name: ";
cin >> person.szLastName;
// cout << "Follow this format:LastNamecommaspaceFirstName: ""Ex. Doe, Jane" ;
// cin >> person.szLastName, person.szFirstName;
return person;
}
/**************************************************************
This function asks if user wants to input another Person
**************************************************************/
int getPeople(Person people[], int cMaxSize)
{
int index;
for(index = 0; index < cMaxSize; index++)
{
char cAnswer; //Needs to stay a char
cout << "Enter another name? (Y or N): ";
cin >> cAnswer;
if(cAnswer != 'Y' && cAnswer != 'y')
{
break;
}
Person newPerson = getPerson();
if(!insertPerson(people, index, newPerson))
{
index = index - 1;
}
}
return index;
}
/**************************************************************
This function Requests Last Name and First Name from User
**************************************************************/
void displayPerson(Person person)
{
// cout << "First name: " << person.szFirstName << endl;
cout << person.szLastName << "," << person.szFirstName << endl; //Input info on same line LastName,FirstName (no spaces)
}
/**************************************************************
This function Displays the Person name
**************************************************************/
void displayPeople(Person people[], int nCount)
{
for(int index = 0;index < nCount; index++)
{
displayPerson(people[index]);
}
}
/**************************************************************
This function sorts by Last Name, then by First Name (NOT SORTING!!!)
**************************************************************/
void sortPeople(Person people[], int nCount)
{
int nSwaps = 1;
while(nSwaps != 0)
{
nSwaps = 0;
for(int n = 0; n < (nCount - 1); n++)
{
if(people[n].szLastName > people[n+1].szLastName)
{
Person temp = people[n+1];
people[n+1] = people [n];
people[n] = temp;
nSwaps++;
}
}
}
}
/**************************************************************
Main
**************************************************************/
int main(int nNumberofArgs,char**pszArgs[])
{
Person people[20];
cout << "This program stores names by LastName, FirstName \n";
int nCount = getPeople(people, 4);
sortPeople(people, nCount);
cout << "\nHere is the list sorted by "
<< "Person's Last Name, then First Name" << endl;
displayPeople(people,nCount);
system("PAUSE");
return 0;
}
|