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 173 174 175 176 177 178 179 180 181 182 183 184
|
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
using namespace std;
class Person
{
string FirstName;
string LastName;
public:
// default constructor
Person();
// parameterized constructor
Person(string, string);
// displays the person
void display();
// returns first name
string getFirstName() const;
// returns last name
string getLastName() const;
};
Person::Person() {
}
void Person::display()
{
cout << LastName << ", " << FirstName << endl;
}
Person::Person(string fName, string lName)
{
FirstName = fName;
LastName = lName;
}
// returns first name
string Person::getFirstName() const
{
return FirstName;
}
// returns last name
string Person::getLastName() const
{
return LastName;
}
// reads and returns the command
string getCommand()
{
string cmd;
cout << "\nEnter the command (INSERT/VIEW/DELETE or EXIT to end the program):" << endl;
getline(cin, cmd);
while (cmd.compare("INSERT") != 0 && cmd.compare("VIEW") != 0 && cmd.compare("DELETE") != 0 && cmd.compare("EXIT") != 0)
{
cout << "Invalid Command. Please Try Again." << endl;
cout << "\nEnter the command (INSERT/VIEW/DELETE or EXIT to end the program):" << endl;
getline(cin, cmd);
}
return cmd;
}
// displays the persons in the array
void displayPersons(Person persons[], int count)
{
for (int i = 0; i < count; i++)
persons[i].display();
}
// deletes the person
void deletePerson(Person persons[], int count, int index)
{
for (int i = index; i < count; ++i)
persons[i] = persons[i + 1];
}
// find person
int findPerson(Person persons[], string fName, string lName, int count)
{
for (int i = 0; i < count; i++)
{
if (persons[i].getFirstName().compare(fName) == 0 && persons[i].getLastName().compare(lName) == 0)
return i;
}
return -1;
}
// main function
int main()
{
string command;
string fullName;
string fName;
string lName;
string token;
Person persons[100];
int personCount = 0;
int index;
do
{
command = getCommand();
if (command.compare("INSERT") == 0)
{
cout << "Enter the name of Person in LastName, FirstName format" << endl;
getline(cin, fullName);
size_t found = fullName.find(',');
if (found != std::string::npos)
{
istringstream ss(fullName);
getline(ss, token, ',');
lName = token;
getline(ss, token, ',');
fName = token;
// create person
Person p(fName, lName);
// add person to array
persons[personCount] = p;
personCount++;
// sort the array
std::sort(persons, persons + personCount,
[](Person const & a, Person const & b) -> bool
{ return a.getLastName().compare(b.getLastName()) < 0; });
// display the persons
cout << endl;
displayPersons(persons, personCount);
}
else
{
cout << "Invalid Input." << endl;
}
}
else if (command.compare("VIEW") == 0)
{
cout << endl;
displayPersons(persons, personCount);
}
else if (command.compare("DELETE") == 0)
{
cout << "Enter the name of Person in LastName, FirstName format" << endl;
getline(cin, fullName);
size_t found = fullName.find(',');
if (found != std::string::npos)
{
istringstream ss(fullName);
getline(ss, token, ',');
lName = token;
getline(ss, token, ',');
fName = token;
// find if person exist
index = findPerson(persons, fName, lName, personCount);
if (index >= 0)
{
deletePerson(persons, personCount, index);
personCount--;
// display the persons
cout << endl;
displayPersons(persons, personCount);
}
else
{
cout << "Person not Found!" << endl;
}
}
else
{
cout << "Invalid Input." << endl;
}
}
} while (command.compare("EXIT") != 0);
return 0;
}
|