Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
selection sort implementation on an int array and my own Date class(with operators overloading)
  • Loading branch information
eyalbi authored May 30, 2020
1 parent 629daeb commit 6c93e6d
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 0 deletions.
159 changes: 159 additions & 0 deletions selction_sort/Date.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"

Date::Date() {
//time_t rawtime;
//struct tm * timeinfo;
//time(&rawtime);
//timeinfo = localtime(&rawtime);
day = rand() % 31 + 1; //timeinfo->tm_mday;
month = rand() % 12 + 1; //timeinfo->tm_mon + 1;
year = 2019; //timeinfo->tm_year + 1900;
}

Date::Date(int day, int month, int year) :day(day), month(month), year(year) {}

void Date::SetDay(int d) {
day = d;
}

void Date::SetMonth(int m) {
month = m;
}

void Date::SetYear(int y) {
year = y;
}

int Date::GetDay() { return day; }

int Date::GetMonth() { return month; }

int Date::GetYear() { return year; }

void Date::PrintDate() {
cout << day << "/" << month << "/" << year << endl;
}

void Date::PrintMonthName() {
switch (month) {
case 1:
cout << "January" << endl;
break;
case 2:
cout << "February" << endl;
break;
case 3:
cout << "March" << endl;
break;
case 4:
cout << "April" << endl;
break;
case 5:
cout << "May" << endl;
break;
case 6:
cout << "June" << endl;
break;
case 7:
cout << "July" << endl;
break;
case 8:
cout << "August" << endl;
break;
case 9:
cout << "September" << endl;
break;
case 10:
cout << "October" << endl;
break;
case 11:
cout << "November" << endl;
break;
case 12:
cout << "December" << endl;
break;
default:
cout << "No month in number: " << month << "." << endl;
}
}

int Date::MonthDaysNum() {
int num;
enum Month {
January = 1, February, March, April, May, June,
July, August, September, October, November, December
};

if ((month == January) || (month == March) || (month == May) || (month == July) || (month == August) || (month == October) || (month == December)) {
num = 31;
}
if ((month == April) || (month == June) || (month == September) || (month == November)) {
num = 30;
}
if (month == February) {
num = 28;
}
return num;
}

bool Date::Meuberet() {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return true;
else
return false;
}

void Date::NextDayDate() {
if (day < MonthDaysNum())
day++;
if (day == MonthDaysNum() && month < 12) {
day = 1;
month++;
}
if (day == MonthDaysNum() && month == 12) {
day = 1;
month = 1;
year++;
}
}

bool Date::operator < (const Date &obj)
{
if (this->year < obj.year) {
return true;
}
else if (this->year > obj.year) {
return false;
}
else if (this->year == obj.year) {
if (month < obj.month)
{
return true;
}
else if(month > obj.month)
{
return false;
}
else if (month == obj.month)
{
if (day < obj.day) {
return true;
}
else if (day > obj.day) {
return false;
}
else if(day == obj.day)
{
return false;
}
}
}
return false;
}

ostream & operator<<(ostream & out, const Date & date)
{
cout << date.day << "/" << date.month << "/" << date.year;
return out;
}
29 changes: 29 additions & 0 deletions selction_sort/Date.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>
#include <time.h>
using namespace std;



class Date { //date class from lab
private:
int day, month, year;
public:
Date();
Date(int, int, int);
void SetDay(int);
void SetMonth(int);
void SetYear(int);
int GetDay();
int GetMonth();
int GetYear();
void PrintDate();
void PrintMonthName();
int MonthDaysNum();
bool Meuberet();
void NextDayDate();
friend class Game;
friend class Ligat_HaAl;
bool operator < (const Date&);
friend ostream& operator << (ostream & out, const Date& date);
};

67 changes: 67 additions & 0 deletions selction_sort/driver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "Date.h"
template <class T> //template func declarations
void SelectionSort(T Array[], int size);
template <class T>
void swap(T Array [], int index1, int index2);
template <class T>
void Print(T Array [] , int size);
int main() { //main intilaize 2 array (int,date) and sorting them with template func
int ArraySize,*array1,tempday,tempmount,tempyear;
cout << "enter int array size" << endl;
cin >> ArraySize;
array1 = new int[ArraySize];
for (int i = 0; i < ArraySize; i++) {
cout << "enter int array number " << i + 1 << " :" << endl;
cin >> array1[i];
}
cout << "array before change: " << endl;
Print(array1, ArraySize);
cout << "array after change: " << endl;
SelectionSort(array1, ArraySize);
Print(array1, ArraySize);
srand(time(NULL));
Date array2[5]; //intiliazie date array with ctor randomly in size = 5.. if you want to change only change size
cout << "array before change: " << endl;
Print(array2, 5);
cout << "array after change: " << endl;
SelectionSort(array2, 5);
Print(array2, 5);

return 0;
}
template <class T>
void SelectionSort(T Array[] , int size) { //template func that sorting array with selction sort method
int i,j,min_index;
for (i = 0; i < size; i++) {
min_index = i;
for (j = i + 1; j < size; j++) {
if (Array[j] < Array[min_index]) {
min_index = j;
}
}
if (min_index != i) {
swap(Array, i, min_index);
}
}
}
template <class T>
void swap(T Array [] , int index1,int index2) { //template func to swap to arrays values with given array and index
T temp;
temp = Array[index1];
Array[index1] = Array[index2];
Array[index2] = temp;
}
template <class T>
void Print(T Array[], int size) { //template func to print arrays
cout << "{";
for (int i = 0; i < size; i++) {
if (i == size - 1) {
cout << Array[i];
}
else {
cout << Array[i] << ", ";

}
}
cout << "}" << endl;
}

0 comments on commit 6c93e6d

Please sign in to comment.