Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

class of date #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
247 changes: 244 additions & 3 deletions server_fork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@

using namespace std;

// share memory for multiprocess operations
int shmid = shmget(IPC_PRIVATE, sizeof(int), 0777|IPC_CREAT);

string* split(string s,char c, int& count) {
string static strar[1000];
count = 0;
Expand All @@ -41,6 +38,250 @@ string* split(string s,char c, int& count) {
return strar;
}

class Date
{
public:
string dateStr;
int day, month, year;

const int max_valid_yr = 9999, min_valid_yr = 1800

Date(string date_str)
{
dateStr = date_str;
int cnt;
string *str = split(date_str, '.', cnt);
day = stoi(str[0]);
month = stoi(str[1]);
year = stoi(str[2]);
}

Date(string date_str, int d, int m, int y)
{
dateStr = date_str;
day = d;
month = m;
year = y;
}

Date(Date date)
{
this->dateStr = date.dateStr;
this->day = date.day;
this->month = date.month;
this->year = date.year;
}


bool operator==(Date lhs, Date rhs)
{
if(lhs.day == rhs.day && lhs.month == rhs.month && lhs.year == rhs.year)
return true;
else
false;
}

bool operator>(Date lhs, Date rhs)
{
if(lhs.year > rhs.year)
return true;
else if(lhs.year < rhs.year)
return false;
else
{
if(lhs.month > rhs.month)
return true;
else if(lhs.month < rhs.month)
return false;
else
{
if(lhs.day > rhs.day)
return true;
return false;
}
}
}

bool operator<(Date lhs, Date rhs)
{
if(lhs.year < rhs.year)
return true;
else if(lhs.year > rhs.year)
return false;
else
{
if(lhs.month < rhs.month)
return true;
else if(lhs.month > rhs.month)
return false;
else
{
if(lhs.day < rhs.day)
return true;
return false;
}
}
}

bool isLeap(int year)
{
return (((year % 4 == 0) &&
(year % 100 != 0)) ||
(year % 400 == 0));
}

bool isValidDate()
{
if (year > max_valid_yr || year < min_valid_yr)
return false;
if (month < 1 || month > 12)
return false;
if (day < 1 || day > 31)
return false;

if (month == 2)
{
if (isLeap(year))
return (day <= 29);
else
return (day <= 28);
}

if (month == 4 || month == 6 || month == 9 || month == 11)
return (day <= 30);

return true;
}

int dateCompare(Date dt)
{
if(*this > dt)
return 1;
else if(*this == dt)
return 0;
else
return -1;
}
};

class record
{
public:
string date, item;
double price;
bool validity;

record(string s)
{
int cnt;
string* str = split(s, ' ', cnt);
date = str[0];
item = str[1];
validity = true;
if(Date(date).isValidDate())
{
validity = false;
}
try
{
price = stod(str[2]);
}
catch(Exception)
{
price = -1;
validity = false;
}
}

record(record rec)
{
date = rec.date;
item = rec.item;
price = rec.price;
}

bool isRecValid()
{
return validity;
}

int recCompare(record rhs, char by)
{
if(by == 'D')
{
Date dt1 = Date(this->date);
Date dt2 = Date(rhs.date);
if(dt1 == dt2)
return 0;
else if(dt1 > dt2)
return 1;
else
return -1;
}
else if(by == 'N')
{
return this->item.compare(rhs.item);
}
else if(by == 'P')
{
if(this->price == rhs.price)
return 0;
else if(this->price > rhs.price)
return 1;
else
return -1;
}
}

void swap_recs(record& bil1, record& bil2)
{
string date = bil1.date, item = bil1.item;
double price = bil1.price;
bil1.date = bil2.date;
bil1.item = bil2.item;
bil1.price = bil2.price;
bil2.date = date;
bil2.item = item;
bil2.price = price;
}
};

class bill
{
public:
record *arr;
int n;
bill(int n)
{
this->n = n;
arr = new record[n];
}
void sort_bills(char by)
{
if(by == 'D')
{

}
else if(by == 'N')
{

}
else if(by == 'P')
{

}
}
void print()
{
for(int i = 0; i < n; i++)
{
cout << arr[i].date << " " << arr[i].item << " " << arr[i].price << "\n";
}
}
};

// share memory for multiprocess operations
// int shmid = shmget(IPC_PRIVATE, sizeof(int), 0777|IPC_CREAT);

int main()
{
Expand Down