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
|
//header
#ifndef BIRTHDAY_H
#define BIRTHDAY_H
#endif
#include<string>
#include<iostream>
const int MAX_SIZE = 10;
using std::string;
class Birthday
{
private:
int month;
int day;
int year;
string name;
public:
Birthday();
string getName();
bool operator==(Birthday&);
bool operator>(Birthday&);
void setBirthDate(int month, int day, int yr);
void setName(string person);
};
void bsort(int[], int);
void prints(string message, int values[], int size);
//main
#include"Birthdate.h"
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
Birthday bdClub[MAX_SIZE];
int month, day, year;
string name;
int size = 0;
ifstream inFile("data.txt");
if (inFile.fail())
{
cout << "Could not open data.txt\n";
exit(1);
}
inFile >> month; //prime the pump
while (!inFile.eof()){
inFile >> day >> year >> name;
//bdClub[size].setBirthdate(month, day, year);
bdClub[size].setName(name);
size++;
inFile >> month; //try next birthdate
}
cout << "Unsorted\n";
for (int x = 0; x < size; x++) // This loop does not display all of the array.
{
Birthday b = bdClub[x];
}
//bsort(bdClub, size);
cout << "Sorted\n";
for (Birthday b : bdClub){
}
system("pause");
return 0;
//Implementation
#include"Birthdate.h"
Birthday::Birthday()
{
month = day = year = 0;
name = "";
}
void Birthday::setName(string person)
{
name = person;
}
string Birthday::getName()
{
return name;
}
bool Birthday::operator==(Birthday& birthdate)
{
if (day == birthdate.day && month == birthdate.month && year == birthdate.year)
return true;
else
return false;
}
bool Birthday::operator>(Birthday& birthdate2)
{
if (day > birthdate2.day && month > birthdate2.month && year > birthdate2.year)
return true;
else
return false;
}
void Birthday::setBirthDate(int m, int d, int yr)
{
if (m > 0 && m <= 12)
month = m;
if (d > 0 && d <= 31)
day = d;
if (yr > 0)
year = yr;
}
void bsort(int values[], int size)
{
bool swap;
int temp;
do
{
swap = false;
for (int x = 0; x < size - 1; x++)
{ //check for year >
if (values[x] > values[x + 1] ||
//check for year the same but month >
(values[x].getYear() == values[x + 1].getYear() &&
values[x].getMonth() > values.[x + 1].getMonth()) ||
//check for year and month the same but day
(values[x].getYear() == values[x + 1].getYear() &&
values[x].getMonth() == values[x + 1].getMonth()) ||
(values[x].getDay() > values[x + 1].getDay())
{
temp = values[x];
values[x] = values[x + 1];
values[x + 1] = temp;
swap = true;
};
}
} while (swap);
}
|