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
|
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void selectionSort(vector<string> &);
vector<string>getVector(const string&);
string getName(const string&);
bool search(const string& , vector<string>&);
void displayResult(const string&, const string&, bool);
void writeToFile(const string&, vector<string>&);
void reverseVector(vector<string>& vec);
int main()
{
string boyName, girlName;
bool boyNameFound, girlNameFound;
vector<string> boyNames(getVector("BoyNames.txt"));
vector<string> girlNames(getVector("GirlNames.txt"));
boyName = getName("boy's");
girlName = getName("girl's");
selectionSort(boyNames);
selectionSort(girlNames);
boyNameFound = search(boyName, boyNames);
girlNameFound = search(girlName, girlNames);
displayResult("boy's", boyName, boyNameFound);
displayResult("girl's", girlName, girlNameFound);
writeToFile("Boynames_asc.txt", boyNames);
writeToFile("Girlnames_asc.txt", girlNames);
reverseVector(boyNames);
reverseVector(girlNames);
writeToFile("Boynames_desc.txt", boyNames);
writeToFile("Girlnames_desc.txt", girlNames);
cout<<endl;
//system("PAUSE");
return 0;
}
void selectionSort(vector<string> &arr)
{
int startScan, minIndex;
string minValue;
for (startScan = 0; startScan < (arr.size() - 1); startScan++)
{
minIndex = startScan;
minValue = arr[startScan];
for(int index = startScan + 1; index < arr.size(); index++)
{
if (arr[index] < minValue)
{
minValue = arr[index];
minIndex = index;
}
}
arr[minIndex] = arr[startScan];
arr[startScan] = minValue;
}
}
vector<string>getVector(const string& filename)
{
ifstream file_obj;
string name;
vector<string>vec;
file_obj.open(filename);
while (file_obj >> name)
{
getline(file_obj, name);
vec.push_back(name);
}
file_obj.close();
return vec;
}
string getName(const string& gName)
{
string name;
cout << "Enter a " << gName << " name, or N if you do not wish to enter a name: ";
cin >> name;
return name;
}
bool search(const string& name, vector<string>& vec)
{
for (int i = 0; i < vec.size(); i++)
{
if (vec[i] == name)
return true;
}
return false;
}
void displayResult(const string& gName, const string& inName, bool popular)
{
if (popular)
cout << endl << inName << " is a popular " << gName << " name. ";
else
cout << endl << inName << " is not a popular " << gName << " name. ";
}
void writeToFile(const string& filename, vector<string>&vec)
{
ofstream vecOut;
vecOut.open("Vector_output.txt");
for (int i = 0; i < vec.size(); i++)
{
vecOut << vec[i] << endl;
}
vecOut.close();
}
void reverseVector(vector<string>& vec)
{
string name;
for (int i = 0, j = vec.size() - 1; i < vec.size()/2; i++, j--)
{
name = vec[i];
vec[i] = vec[j];
name = vec[j];
}
}
|