Page 1 of 1

#include #include #include #include #include#include #include
Posted: Thu May 26, 2022 9:48 am
by answerhappygod
#include <vector>#include <iostream>#include <cstdlib>#include <iomanip>#include<string>#include <fstream>#include <ctime>using namespace std;class Date{public:int year;int month;int day;Date();int GetYear();int GetMonth();int GetDay();};Date::Date(){time_t t = time(0);tm* now = localtime(&t);year = now->tm_year + 1900;month = now->tm_mon + 1;day = now->tm_mday;}int Date::GetYear(){return year;}int Date::GetMonth(){return month;}int Date::GetDay(){return day;}class Event{public:Date date;string description;Event(Date d, string desc){date = d;description = desc;}};class Calendar{public:Date currentDate;vector<Event> events;void Run();void Draw();void NextMonth();void PreviousMonth();void ChangeDate();void ChangeDay(int d);void ChangeMonth(int m);void ChangeYear(int y);bool isLeapYear(int year);string dayOfTheWeek(int month, int year);int numberOfDays(int monthNumber, int year);int dayNumber(int day, int month, int year);string getMonthName(int monthNumber);void CreateEvent();void PrintEvents();void DeleteEvent();char Menu();void Save();void Load();};void Calendar::DeleteEvent(){int id, index=0;cout << "Enter the id:" << endl;cin >> id;id -= 1;vector <Event>::iterator it;for (it = events.begin(); it != events.end(); it++){if (index == id){events.erase(events.begin() + id);cout << "Event deleted sucessfully";return;}else{index++;}}cout << "No event found!" << endl;}void Calendar::Load(){string filename, desc;ifstream myReadFile;cout << "filename: ";cin >> filename;myReadFile.open("filename.txt");char date[50];char descFile[100];Date getDate;if (myReadFile.is_open()){while (!myReadFile.eof()){myReadFile >> date;getDate.day = (((int)date[0]-48) * 10) + ((int)date[1] - 48);getDate.month = (((int)date[3] - 48) * 10) + ((int)date[4] - 48);getDate.year = (((int)date[6] - 48) * 1000) + (((int)date[7] - 48)*100)+ (((int)date[8] - 48) * 10)+ ((int)date[9] - 48);myReadFile >> desc;Event eobj(getDate, desc);events.push_back(eobj);}}myReadFile.close();cout << "Events added in the disk successfully!" << endl;}void Calendar::Save(){string filename;ofstream myfile;cout << "Enter the file name: ";cin >> filename;myfile.open("filename.txt");for (int i = 0; i < events.size(); i++){Date dt = events.at(i).date;myfile << dt.day << "-" << dt.month << "-" << dt.year << " ";myfile << events.at(i).description;myfile << "\n";}myfile.close();cout << "Events saved in a file successfully!" << endl;}void Calendar::PrintEvents(){if (events.size() == 0){cout << "No events in the list" << endl;return;}for (int i = 0; i < events.size(); i++){Date dt = events.at(i).date;cout << (i + 1) << ". " << dt.day << "-" << dt.month << "-" << dt.year << "\t";cout << events.at(i).description << endl;}}void Calendar::CreateEvent(){int y, m, d;string desc;cout << "Enter year: ";cin >> y;cout << "Enter month: ";cin >> m;cout << "Enter day: ";cin >> d;cout << "Enter description: ";getline(cin,desc);Date gDate;gDate.day = d;gDate.month = m;gDate.year = y;Event aobj(gDate, desc);events.push_back(aobj);cout << "Event added successfully!" << endl;}void Calendar::ChangeDate(){int y, m;cout << "Enter year: ";cin >> y;cout << "Enter month: ";cin >> m;ChangeYear(y);ChangeMonth(m);Draw();}void Calendar::Run(){Draw();while (true){char operation = Menu();switch (operation){case 'N':case 'n':NextMonth();break;case 'P':case 'p':PreviousMonth();break;case 'C':case 'c':ChangeDate();break;case 'A':case 'a':CreateEvent();break;case 'L':case 'l':PrintEvents();break;case 'D':case 'd':DeleteEvent();break;case 'S':case 's':Save();break;case 'R':case 'r':Load();break;case 'Q':case 'q':cout << "Thank you for using this app!" << endl;exit(0);break;default:cout << "Invalid Input!";break;}}}void Calendar::NextMonth(){if (currentDate.month < 11){ChangeMonth(currentDate.month + 1);Draw();}else{cout << "This month is the ending month of the year." << endl;Draw();}}void Calendar::PreviousMonth(){if (currentDate.month > 2){ChangeMonth(currentDate.month - 1);Draw();}else{cout << "This month is the starting month of the year." << endl;Draw();}}int Calendar::dayNumber(int day, int month, int year){static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };year -= month < 3;return (year + year / 4 - year / 100 + year / 400 + t[month - 1] + day) % 7;}int Calendar::numberOfDays(int monthNumber, int year){// January // March // May // July // August // October // Decemberif (monthNumber == 1 || monthNumber == 3 || monthNumber == 5 ||monthNumber == 7 || monthNumber == 8 || monthNumber == 10 ||monthNumber == 12)return (31);// Februaryif (monthNumber == 2){// If the year is leap then February has// 29 daysif (isLeapYear(year))return (29);elsereturn (28);}// remaining monthsif (monthNumber == 4 || monthNumber == 6 ||monthNumber == 9 || monthNumber == 11)return (30);}string Calendar::getMonthName(int monthNumber){string months[] = { "January", "February", "March","April", "May", "June","July", "August", "September","October", "November", "December"};return (months[monthNumber]);}void Calendar::Draw(){cout << endl;cout << "\t" << getMonthName(currentDate.month - 1).c_str() << "\t" << currentDate.year << "\t" << endl;int days;int current = dayNumber(currentDate.day, currentDate.month, currentDate.year);days = numberOfDays(currentDate.month, currentDate.year);cout << " **************************" << endl;cout << " SU MO TU WE TH FR SA" << endl;int k;for (k = 0; k < current; k++)printf(" ");for (int j = 1; j <= days; j++){printf("%4d", j);if (++k > 6){k = 0;cout << endl;}}if (k)cout << endl;current = k;cout << endl;return;}void Calendar::ChangeDay(int d){currentDate.day = d;}void Calendar::ChangeMonth(int m){currentDate.month = m;}void Calendar::ChangeYear(int y){currentDate.year = y;}string Calendar::dayOfTheWeek(int month, int year){int y, c, m;if (month == 1 || month == 2){y = (year - 1) % 100;c = (year - 1) / 100;m = month + 12;}else{y = year % 100;y = year / 100;m = month;}int week = ((y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 % 7 + 7) % 7);switch (week){case 0:return "Sunday";case 1:return "Monday";case 2:return "Tuesday";case 3:return "Wednesday";case 4:return "Thursday";case 5:return "Friday";case 6:return "Saturday";}}bool Calendar::isLeapYear(int year){return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);}char Calendar::Menu(){char choice;cout << "Press 'N': Show the calendar for the next month" << endl;cout << "Press 'P': Show the calendar for the previous month" << endl;cout << "Press 'C': Input year and month, the program will show the calendar as you inputted" << endl;cout << "Press 'A': Input year/month/day, then input a description, it will add an event to the calendar" << endl;cout << "Press 'L': Show all events you have created, each one with an ID" << endl;cout << "Press 'D': Input a event ID, delete the event with this ID" << endl;cout << "Press 'S': Input a file name, save the events you have created in this calendar program to a file on the disk" << endl;cout << "Press 'R': Input a file name, load the events you have saved" << endl;cout << "Press 'Q': Exit the program." << endl;cout << endl << "Enter choice: ";cin >> choice;while (choice != 'N' && choice != 'P' &&choice != 'C' && choice != 'A' &&choice != 'L' && choice != 'D' &&choice != 'S' && choice != 'R' &&choice != 'Q' && choice != 'q' &&choice != 'n' && choice != 'p' &&choice != 'c' && choice != 'a' &&choice != 'l' && choice != 'd' &&choice != 's' && choice != 'r'){cout << "Invalid choice!" << endl;cout << "Please enter the choice again: " << endl;cin >> choice;}return choice;}int main(){Calendar calObj;calObj.Run();return 0;}
#include <vector>
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include<string>
#include <fstream>
#include <ctime>
using namespace std;
class Date
{
public:
int year;
int month;
int day;
Date();
int GetYear();
int GetMonth();
int GetDay();
};
Date::Date()
{
time_t t = time(0);
tm* now = localtime(&t);
year = now->tm_year + 1900;
month = now->tm_mon + 1;
day = now->tm_mday;
}
int Date::GetYear()
{
return year;
}
int Date::GetMonth()
{
return month;
}
int Date::GetDay()
{
return day;
}
class Event
{
public:
Date date;
string description;
Event(Date d, string desc)
{
date = d;
description = desc;
}
};
class Calendar
{
public:
Date currentDate;
vector<Event> events;
void Run();
void Draw();
void NextMonth();
void PreviousMonth();
void ChangeDate();
void ChangeDay(int d);
void ChangeMonth(int m);
void ChangeYear(int y);
bool isLeapYear(int year);
string dayOfTheWeek(int month, int year);
int numberOfDays(int monthNumber, int year);
int dayNumber(int day, int month, int year);
string getMonthName(int monthNumber);
void CreateEvent();
void PrintEvents();
void DeleteEvent();
char Menu();
void Save();
void Load();
};
void Calendar::DeleteEvent()
{
int id, index=0;
cout << "Enter the id:" << endl;
cin >> id;
id -= 1;
vector <Event>::iterator it;
for (it = events.begin(); it != events.end(); it++)
{
if (index == id)
{
events.erase(events.begin() + id);
cout << "Event deleted sucessfully";
return;
}
else
{
index++;
}
}
cout << "No event found!" << endl;
}
void Calendar::Load()
{
string filename, desc;
ifstream myReadFile;
cout << "filename: ";
cin >> filename;
myReadFile.open("filename.txt");
char date[50];
char descFile[100];
Date getDate;
if (myReadFile.is_open())
{
while (!myReadFile.eof())
{
myReadFile >> date;
getDate.day = (((int)date[0]-48) * 10) + ((int)date[1] - 48);
getDate.month = (((int)date[3] - 48) * 10) + ((int)date[4] - 48);
getDate.year = (((int)date[6] - 48) * 1000) + (((int)date[7] - 48)*100)+ (((int)date[8] - 48) * 10)+ ((int)date[9] - 48);
myReadFile >> desc;
Event eobj(getDate, desc);
events.push_back(eobj);
}
}
myReadFile.close();
cout << "Events added in the disk successfully!" << endl;
}
void Calendar::Save()
{
string filename;
ofstream myfile;
cout << "Enter the file name: ";
cin >> filename;
myfile.open("filename.txt");
for (int i = 0; i < events.size(); i++)
{
Date dt = events.at(i).date;
myfile << dt.day << "-" << dt.month << "-" << dt.year << " ";
myfile << events.at(i).description;
myfile << "\n";
}
myfile.close();
cout << "Events saved in a file successfully!" << endl;
}
void Calendar::PrintEvents()
{
if (events.size() == 0)
{
cout << "No events in the list" << endl;
return;
}
for (int i = 0; i < events.size(); i++)
{
Date dt = events.at(i).date;
cout << (i + 1) << ". " << dt.day << "-" << dt.month << "-" << dt.year << "\t";
cout << events.at(i).description << endl;
}
}
void Calendar::CreateEvent()
{
int y, m, d;
string desc;
cout << "Enter year: ";
cin >> y;
cout << "Enter month: ";
cin >> m;
cout << "Enter day: ";
cin >> d;
cout << "Enter description: ";
getline(cin,desc);
Date gDate;
gDate.day = d;
gDate.month = m;
gDate.year = y;
Event aobj(gDate, desc);
events.push_back(aobj);
cout << "Event added successfully!" << endl;
}
void Calendar::ChangeDate()
{
int y, m;
cout << "Enter year: ";
cin >> y;
cout << "Enter month: ";
cin >> m;
ChangeYear(y);
ChangeMonth(m);
Draw();
}
void Calendar::Run()
{
Draw();
while (true)
{
char operation = Menu();
switch (operation)
{
case 'N':
case 'n':
NextMonth();
break;
case 'P':
case 'p':
PreviousMonth();
break;
case 'C':
case 'c':
ChangeDate();
break;
case 'A':
case 'a':
CreateEvent();
break;
case 'L':
case 'l':
PrintEvents();
break;
case 'D':
case 'd':
DeleteEvent();
break;
case 'S':
case 's':
Save();
break;
case 'R':
case 'r':
Load();
break;
case 'Q':
case 'q':
cout << "Thank you for using this app!" << endl;
exit(0);
break;
default:
cout << "Invalid Input!";
break;
}
}
}
void Calendar::NextMonth()
{
if (currentDate.month < 11)
{
ChangeMonth(currentDate.month + 1);
Draw();
}
else
{
cout << "This month is the ending month of the year." << endl;
Draw();
}
}
void Calendar::PreviousMonth()
{
if (currentDate.month > 2)
{
ChangeMonth(currentDate.month - 1);
Draw();
}
else
{
cout << "This month is the starting month of the year." << endl;
Draw();
}
}
int Calendar::dayNumber(int day, int month, int year)
{
static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
year -= month < 3;
return (year + year / 4 - year / 100 + year / 400 + t[month - 1] + day) % 7;
}
int Calendar::numberOfDays(int monthNumber, int year)
{
// January // March // May // July // August // October // December
if (monthNumber == 1 || monthNumber == 3 || monthNumber == 5 ||
monthNumber == 7 || monthNumber == 8 || monthNumber == 10 ||
monthNumber == 12)
return (31);
// February
if (monthNumber == 2)
{
// If the year is leap then February has
// 29 days
if (isLeapYear(year))
return (29);
else
return (28);
}
// remaining months
if (monthNumber == 4 || monthNumber == 6 ||
monthNumber == 9 || monthNumber == 11)
return (30);
}
string Calendar::getMonthName(int monthNumber)
{
string months[] = { "January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
return (months[monthNumber]);
}
void Calendar::Draw()
{
cout << endl;
cout << "\t" << getMonthName(currentDate.month - 1).c_str() << "\t" << currentDate.year << "\t" << endl;
int days;
int current = dayNumber(currentDate.day, currentDate.month, currentDate.year);
days = numberOfDays(currentDate.month, currentDate.year);
cout << " **************************" << endl;
cout << " SU MO TU WE TH FR SA" << endl;
int k;
for (k = 0; k < current; k++)
printf(" ");
for (int j = 1; j <= days; j++)
{
printf("%4d", j);
if (++k > 6)
{
k = 0;
cout << endl;
}
}
if (k)
cout << endl;
current = k;
cout << endl;
return;
}
void Calendar::ChangeDay(int d)
{
currentDate.day = d;
}
void Calendar::ChangeMonth(int m)
{
currentDate.month = m;
}
void Calendar::ChangeYear(int y)
{
currentDate.year = y;
}
string Calendar::dayOfTheWeek(int month, int year)
{
int y, c, m;
if (month == 1 || month == 2)
{
y = (year - 1) % 100;
c = (year - 1) / 100;
m = month + 12;
}
else
{
y = year % 100;
y = year / 100;
m = month;
}
int week = ((y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 % 7 + 7) % 7);
switch (week)
{
case 0:
return "Sunday";
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
}
}
bool Calendar::isLeapYear(int year)
{
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
char Calendar::Menu()
{
char choice;
cout << "Press 'N': Show the calendar for the next month" << endl;
cout << "Press 'P': Show the calendar for the previous month" << endl;
cout << "Press 'C': Input year and month, the program will show the calendar as you inputted" << endl;
cout << "Press 'A': Input year/month/day, then input a description, it will add an event to the calendar" << endl;
cout << "Press 'L': Show all events you have created, each one with an ID" << endl;
cout << "Press 'D': Input a event ID, delete the event with this ID" << endl;
cout << "Press 'S': Input a file name, save the events you have created in this calendar program to a file on the disk" << endl;
cout << "Press 'R': Input a file name, load the events you have saved" << endl;
cout << "Press 'Q': Exit the program." << endl;
cout << endl << "Enter choice: ";
cin >> choice;
while (choice != 'N' && choice != 'P' &&
choice != 'C' && choice != 'A' &&
choice != 'L' && choice != 'D' &&
choice != 'S' && choice != 'R' &&
choice != 'Q' && choice != 'q' &&
choice != 'n' && choice != 'p' &&
choice != 'c' && choice != 'a' &&
choice != 'l' && choice != 'd' &&
choice != 's' && choice != 'r')
{
cout << "Invalid choice!" << endl;
cout << "Please enter the choice again: " << endl;
cin >> choice;
}
return choice;
}
int main()
{
Calendar calObj;
calObj.Run();
return 0;
}