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
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Function prototypes
void sort(string[], string[], int[], int[], int[], int);
bool test(int, int, int, int, int, int);
ostream& writeEntry(ostream&, string, string, int, int, int);
const int MAX_SIZE = 1000;
//**********************************************************************
int main(int argc, char* argv[])
{
string lastName[MAX_SIZE],
firstName[MAX_SIZE],
fileName;
int area[MAX_SIZE],
prefix[MAX_SIZE],
number[MAX_SIZE],
count;
ifstream fin;
if (argc > 1)
fileName = argv[1];
else
{
cout << "Name of File: ";
cin >> fileName;
}
fin.open(fileName);
if (!fin)
{
perror(fileName.data());
exit(EXIT_FAILURE);
}
count = 0;
while (count < MAX_SIZE &&
fin >> lastName[count] >> firstName[count] >> area[count]
>> prefix[count] >> number[count])
count++;
sort(lastName, firstName, area, prefix, number, count);
for (int i = 0; i < count; i++)
writeEntry(cout, lastName[i], firstName[i],
area[i], prefix[i], number[i]) << endl;
return EXIT_SUCCESS;
}
//**********************************************************************
ostream& writeEntry(ostream& fout, string lastName, string firstName,
int area, int prefix, int number)
{
fout << lastName << ", " << firstName;
int len = lastName.size() + firstName.size() + 2;
if (len % 2 == 1)
{
fout << ".";
len++;
}
while ((len += 2) <= 30)
fout << " .";
fout << area << "-" << prefix << "-" << number;
return fout;
}
//**********************************************************************
// Insertion sort
void sort(string lastName[], string firstName[],
int area[], int prefix[], int number[], int size)
{
int i,
j;
string saveLastName, saveFirstName;
int saveArea, savePrefix, saveNumber;
for (i = 1; i < size; i++)
{
// insert array[i] in ordered list
// running from array[0] through array[i-1],
j = i - 1;
saveLastName = lastName[i]; saveFirstName = firstName[i];
saveArea = area[i]; savePrefix = prefix[i]; saveNumber = number[i];
while (0 <= j && test(saveArea, area[j], savePrefix, prefix[j],
saveNumber, number[j]))
{
lastName[j + 1] = lastName[j];
firstName[j + 1] = firstName[j];
area[j + 1] = area[j];
prefix[j + 1] = prefix[j];
number[j + 1] = number[j];
j--;
}
lastName[j + 1] = saveLastName;
firstName[j + 1] = saveFirstName;
area[j + 1] = saveArea;
prefix[j + 1] = savePrefix;
number[j + 1] = saveNumber;
}
}
//**********************************************************************
// less-than test for insertion sort by phone number
bool test(int saveArea, int area, int savePrefix, int prefix,
int saveNumber, int number)
{
bool retVal = false;
if (saveArea < area)
retVal = true;
else if ((saveArea == area)
&& (savePrefix < prefix))
retVal = true;
else if ((saveArea == area) && (savePrefix == prefix)
&& (saveNumber < number))
retVal = true;
return retVal;
}
|