Created
November 9, 2016 22:18
-
-
Save shreezan123/9e4d7d2ad658199819666144d9b86067 to your computer and use it in GitHub Desktop.
Bubble sort c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <iomanip> | |
#include <fstream> | |
#include <vector> | |
using namespace std; | |
template<typename ItemType> | |
void Swap(ItemType& V1, ItemType& V2 ) | |
{ | |
ItemType temp; | |
temp = V1; | |
V1 = V2; | |
V2 = temp; | |
} | |
template<typename ItemType> | |
void BubbleUp(vector <ItemType>& values, int startIndex, int endIndex) | |
{ | |
for (int index = endIndex; | |
index > startIndex; index--) | |
if (values[index].personID < values[index-1].personID) | |
Swap(values[index], values[index-1]); | |
} | |
template<typename ItemType> | |
void BubbleSort(vector<ItemType>& values, int numValues) | |
{ | |
int current = 0; | |
while (current < numValues - 1) | |
{ | |
BubbleUp(values, current, numValues-1); | |
current++; | |
} | |
} | |
class DateType | |
{ | |
public: | |
void Initialize(int newMonth, int newDay, int newYear); | |
void setYear(int); | |
void setMonth(int); | |
void setDay(int); | |
int GetYear() const; | |
int GetMonth() const; | |
int GetDay() const; | |
private: | |
int year; | |
int month; | |
int day; | |
}; | |
void DateType::Initialize(int newMonth, int newDay, int newYear) { | |
year = newYear; | |
month = newMonth; | |
day = newDay; | |
} | |
void DateType::setYear(int Yr) | |
{ | |
year = Yr; | |
} | |
void DateType::setMonth(int Month) | |
{ | |
month = Month; | |
} | |
void DateType::setDay(int Day) | |
{ | |
day = Day; | |
} | |
int DateType::GetYear() const | |
{ | |
return year; | |
} | |
int DateType::GetMonth() const | |
{ | |
return month; | |
} | |
int DateType::GetDay() const | |
{ | |
return day; | |
} | |
class monthType | |
{ | |
public: | |
int month_num; | |
int dates[6][7]; | |
monthType(); | |
void printMonth(); | |
void setMonthName(string); | |
string getMonthName(); | |
private: | |
string month_name; | |
}; | |
monthType::monthType() | |
{ | |
for (int row = 0; row<6; row++) | |
{ | |
for (int col = 0; col<7; col++) | |
dates[row][col] = 0; | |
} | |
} | |
void monthType::setMonthName(string mon_name) | |
{ | |
month_name = mon_name; | |
} | |
string monthType::getMonthName() | |
{ | |
return month_name; | |
} | |
void monthType::printMonth() | |
{ | |
cout << endl << setw(12) << month_name << endl << endl; | |
for (int row = 0; row<6; row++) | |
{ | |
for (int col = 0; col<7; col++) | |
{ | |
cout << setw(6) << dates[row][col]; | |
} | |
cout << endl; | |
} | |
} | |
class yearType | |
{ | |
public: | |
monthType months[12]; | |
yearType(); //constructor | |
void printYear() | |
{ | |
for (int month_index = 0; month_index<12; month_index++) | |
{ | |
months[month_index].printMonth(); | |
} | |
} | |
}; | |
yearType::yearType() | |
{ | |
for (int i = 0; i<12; i++) | |
months[i].month_num = i; | |
} | |
class calendar | |
{ | |
public: | |
void printCalendar(yearType); | |
}; | |
class USDollarCents | |
{ | |
private: | |
int dollars; | |
int cents; | |
void normalize(); | |
public: | |
USDollarCents(int dollars = 0, int cents = 0); | |
USDollarCents Length(void) const; // USDollarCents length | |
void CopyUSDollarCents(USDollarCents* t) const; // copy USDollarCents | |
friend ostream& operator<< (ostream& ostr, const USDollarCents& s); | |
// Read USDollarCentss | |
friend istream& operator>> (istream& istr, USDollarCents& s); | |
// ***** relational operator ***** | |
// USDollarCents == USDollarCents | |
int operator== (const USDollarCents& s) const; | |
// USDollarCents < USDollarCents | |
int operator< (const USDollarCents& s) const; | |
// USDollarCents > USDollarCents | |
int operator> (const USDollarCents& s) const; | |
// USDollarCents <= USDollarCents | |
int operator<= (const USDollarCents& s) const; | |
// USDollarCents >= USDollarCents | |
int operator>= (const USDollarCents& s) const; | |
// USDollarCents != USDollarCents | |
int operator!= (const USDollarCents& s) const; | |
// ***** USDollarCents Arithmetic operators **** | |
// ***** USDollarCents addition ***** | |
USDollarCents operator+ (const USDollarCents& s) const; | |
// ***** USDollarCents addition equal ***** | |
void operator+= (const USDollarCents& s); | |
// ***** USDollarCents subtraction ***** | |
USDollarCents operator- (const USDollarCents& s) const; | |
// ***** USDollarCents subtraction equal ***** | |
void operator-= (const USDollarCents& s); | |
// ***** USDollarCents mutiplication ***** | |
USDollarCents operator* (double) const; | |
// ***** USDollarCents mutiplication equal ***** | |
void operator*= (int); | |
// ***** USDollarCents division ***** | |
USDollarCents operator/ (int) const; | |
// ******* USDollarCents absolute value *** | |
USDollarCents absolute() const; | |
// ******* USDollarCents divided by 100 *** | |
USDollarCents divideby100() const; | |
}; | |
USDollarCents::USDollarCents(int dd, int cc) : dollars(dd), cents(cc) | |
{ | |
while (cents >= 100) | |
{ | |
cents = cents - 100; | |
dollars++; | |
} | |
} | |
ostream& operator<< (ostream& ostr, const USDollarCents& s) | |
{ | |
ostr << "$" << s.dollars << "." << s.cents; | |
return ostr; | |
} | |
istream& operator>> (istream& istr, USDollarCents& s) | |
{ | |
char char1; | |
istr >> s.dollars >> char1 >> s.cents; | |
return istr; | |
} | |
int USDollarCents::operator< (const USDollarCents& s) const | |
{ | |
/******YOUR CODE GOES HERE *******/ | |
if (dollars < s.dollars) | |
return 1; | |
else | |
if (dollars == s.dollars) | |
if (cents < s.cents) | |
return 1; | |
else | |
return 0; | |
else | |
return 0; | |
} | |
int USDollarCents::operator> (const USDollarCents& s) const | |
{ | |
if (dollars > s.dollars) | |
return 1; | |
else | |
if (dollars == s.dollars) | |
if (cents > s.cents) | |
return 1; | |
else | |
return 0; | |
else | |
return 0; | |
} | |
int USDollarCents::operator== (const USDollarCents& s) const | |
{ | |
/******YOUR CODE GOES HERE *******/ | |
if (dollars == s.dollars) | |
if (cents == s.cents) | |
return 1; | |
else | |
return 0; | |
else | |
return 0; | |
} | |
USDollarCents USDollarCents::operator+ (const USDollarCents& s) const | |
{ | |
int h = dollars + s.dollars; | |
int m = cents + s.cents; | |
USDollarCents D(h, m); | |
D.normalize(); | |
return D; | |
} | |
void USDollarCents::operator+= (const USDollarCents& s) | |
{ | |
dollars += s.dollars; | |
cents += s.cents; | |
if (cents >= 100) | |
{ | |
cents = cents - 100; | |
dollars++; | |
} | |
normalize(); | |
} | |
USDollarCents USDollarCents::operator- (const USDollarCents& s) const | |
{ | |
int h = dollars - s.dollars; | |
int m = cents - s.cents; | |
if (m < 0) | |
{ | |
m = m + 100; | |
h--; | |
} | |
USDollarCents D(h, m); | |
D.normalize(); | |
return D; | |
} | |
void USDollarCents::operator-= (const USDollarCents& s) | |
{ | |
dollars -= s.dollars; | |
cents -= s.cents; | |
if (cents < 0) | |
{ | |
cents = cents + 100; | |
dollars--; | |
} | |
normalize(); | |
} | |
USDollarCents USDollarCents::absolute() const | |
{ | |
int h = abs(dollars); | |
int m = abs(cents); | |
USDollarCents D(h, m); | |
return D; | |
} | |
USDollarCents USDollarCents::divideby100() const | |
{ | |
int m = cents; | |
int h = dollars; | |
m = h * 100 + m; | |
h = m / 10000; | |
m = ((m / 10000.0) - h + .005) * 100; | |
USDollarCents D(h, m); | |
D.normalize(); | |
return D; | |
} | |
void USDollarCents::normalize() | |
{ | |
if ((dollars != 0) && (cents != 0)) | |
{ | |
if ((dollars < 0) && (cents > 0)) | |
{ | |
if (cents > 0) | |
{ | |
cents = cents - 100; | |
dollars++; | |
} | |
} | |
if ((dollars > 0) && (cents < 0)) | |
{ | |
if (cents < 0) | |
{ | |
cents = cents + 100; | |
dollars--; | |
} | |
} | |
} | |
} | |
class addressType | |
{ | |
public: | |
string number; | |
string streetName; | |
string streetAddress; | |
string streetType; | |
string city; | |
string state; | |
string zipcode; | |
}; | |
class personType | |
{ | |
public: | |
personType(); | |
string lastName; | |
string firstName; | |
int personNum; | |
int personID; | |
char gender; | |
addressType address; | |
/********MEMBER FUNCTIONS********/ | |
string getInterest1(); | |
string getInterest2(); | |
void setInterest1(string); | |
void setInterest2(string); | |
void print(); | |
private: | |
string interest1; | |
string interest2; | |
}; | |
class membershipType :public personType | |
{ | |
public: | |
yearType memberCalendar; | |
USDollarCents memberAccount; | |
char membership_type; | |
char membership_status; | |
membershipType(); | |
membershipType(char, char); | |
void print_member_type(); | |
bool operator<(const membershipType &rhs) const | |
{ | |
return personID < rhs.personID; | |
} | |
}; | |
//constructor for personType class | |
personType::personType() | |
{ | |
firstName = ""; | |
lastName = ""; | |
personNum = 0; | |
personID = 0; | |
gender = ' '; | |
this->address.number = ""; | |
this->address.streetName = ""; | |
this->address.streetAddress = ""; | |
this->address.streetType = ""; | |
this->address.city = ""; | |
this->address.state = ""; | |
this->address.zipcode = ""; | |
} | |
//constructor for membershipType class | |
membershipType::membershipType() { | |
membership_type = '0'; | |
membership_status = '0'; | |
} | |
membershipType::membershipType(char mem_type, char mem_status) { | |
membership_type = mem_type; | |
membership_status = mem_status; | |
} | |
string personType::getInterest1() { //getter for interest1 | |
return interest1; | |
} | |
string personType::getInterest2() { //getter for interest2 | |
return interest2; | |
} | |
void personType::setInterest1(string inter1) { // setter for interest1 | |
interest1 = inter1; | |
} | |
void personType::setInterest2(string inter2) { //setter for interest2 | |
interest2 = inter2; | |
} | |
void membershipType::print_member_type() { //display the output | |
cout << lastName << "" << firstName << "\t" << personNum << " " << personID << " " << address.streetName << " " << address.streetAddress << " " << address.streetType << " " << address.city << " \t" << address.state << "\t" << address.zipcode << "\t" << gender << "\t" << getInterest1() << "\t" << getInterest2() << " " << membership_type << " " << membership_status << " " << memberAccount << endl; | |
} | |
int main() { | |
vector<membershipType> memberList; //declaring vector of membershipType | |
string int1, int2;//interest1 and 2 are private variables so int1 and int2 are representation for those two interests | |
int doll, cent; | |
membershipType members; | |
ifstream myfile;//creating file object | |
myfile.open("infile.txt");//opening file | |
for (int i = 0; i < 6; i++) { | |
myfile >> members.lastName; | |
myfile >> members.firstName; | |
myfile >> members.personNum; | |
myfile >> members.personID; | |
myfile >> members.address.streetName; | |
myfile >> members.address.streetAddress; | |
myfile >> members.address.streetType; | |
myfile >> members.address.city; | |
myfile >> members.address.state; | |
myfile >> members.address.zipcode; | |
myfile >> members.gender; | |
myfile >> int1; | |
myfile >> int2; | |
members.setInterest1(int1); | |
members.setInterest2(int2); | |
myfile >> members.membership_type; | |
myfile >> members.membership_status; | |
myfile >> doll >> cent; | |
members.memberAccount = USDollarCents(doll, cent); | |
memberList.push_back(members); | |
} | |
BubbleSort(memberList, 6); | |
for (int j = 0; j < 6; j++) { | |
memberList[j].print_member_type(); | |
} | |
myfile.close(); | |
return 0; | |
} | |
/* | |
Joan,Wilson 4 1234 12 Georgia Ave. Washington, DC 20019 F romance dining 2 3 $190.10 | |
Herold,Jill 1 2234 123 Main St. Washington, DC 20019 F yoga facebook 1 2 $231.12 | |
Claire,Claude 6 2311 66 32nd Street Woodbridge, VA 44040 F cooking facebook 1 1 $332.99 | |
Smith,Stanley 5 3456 56 D Street Baltimore, MD 30229 M movies dining 2 1 $876.25 | |
Jackson,Stan 2 3748 12 Douglas Ave. Baltimore, MD 30229 M sports movies 2 4 $200.0 | |
Jerry,Francis 3 6666 2345 6th Street Woodbridge, VA 44040 M movies roadtrips 1 1 $611.33 | |
Program ended with exit code: 0 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment