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
|
#include <iostream>
#include <string>
#include <ctime>
#include <fstream>
#include <cstdlib>
#include <stdio.h>
using namespace std;
struct Point {
string x;
int y;
bool operator<(const Point& b) {
if (this->x < b.x)
return true;
else if (this->x == b.x && this->y <= b.y)
return true;
return false;
}
};
void quickSort(Point [], int, int);
int partition(Point [], int, int);
void swap(Point &, Point &);
int main()
{
ifstream inputFile;
string filename;
cout << "Please enter the filename: ";
cin >> filename;
inputFile.open(filename.c_str());
Point array[32];
int arraySize = 0;
int num=0;
string name;
int year;
string data;
while (inputFile)
{
if ((num%2)==0)
{
inputFile >> data;
array[arraySize].x=data;
}
else
{
inputFile >> data;
year=atoi(data.c_str());
array[arraySize].y=year;
}
arraySize++;
num++;
}
inputFile.close();
time_t now = time(0);
struct tm* today = localtime(&now);
int curr_year=today->tm_year+1900;
int count;
cout << "Names" << " " << "Year Born" << endl;
for (count=0; count < arraySize; count++)
cout << array[count].x << " " << array[count].y << endl;
quickSort(array, 0, arraySize-1);
cout << "Names" << " " << "Ages" << endl;
for (count = 0; count <arraySize; count++)
cout << array[count].x << " " << curr_year - array[count].y << endl;
return 0;
}
void quickSort (Point set[], int start, int end)
{
int pivotPoint;
if (start < end)
{
pivotPoint = partition (set, start, end);
quickSort(set, start, pivotPoint-1);
quickSort(set, pivotPoint+1, end);
}
}
int partition(Point set[], int start, int end)
{
Point pivotValue;
int pivotIndex, mid;
mid = (start + end)/2;
swap(set[start], set[mid]);
pivotIndex=start;
pivotValue = set[start];
for (int scan = start +1; scan<=end; scan++)
{
if (set[scan] < pivotValue)
{
pivotIndex++;
swap(set[pivotIndex], set[scan]);
}
}
swap(set[start], set[pivotIndex]);
return pivotIndex;
}
void swap(Point &value1, Point &value2)
{
Point temp = value1;
value1= value2;
value2 = temp;
}
|