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

Update Calender.cpp #1516

Merged
merged 1 commit into from
Oct 19, 2024
Merged
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
225 changes: 100 additions & 125 deletions Desktop Application/Basic/C++/Calender/Calender.cpp
Original file line number Diff line number Diff line change
@@ -1,136 +1,111 @@
#include<iostream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
int a, c, s, i, x, b, l, r, y, m, p;
//Year
std::cout << "Enter the year (from 0 to 1000000000) :";
std::cin >> b;
if (b<0 || b>1000000000)
{
std::cout<<"ERROR::The input year is not in range range.\n"<<"EXIT";
return 1;
}
//Month for which the calender is required
std::cout << "Enter the number of the month(from 1 to 12) :";
std::cin >> a;
if (a<1 || a>12)
{
std::cout<<"ERROR::The input month is not in range range.\n"<<"EXIT";
return 2;
}
//Aplying Julian Calender Algorithm
r = b % 100;
y = (r + (r / 4)) % 7;
//With the help of month number moth name is getting detected
switch (a)
{
case 1:std::cout << "\t\tJanuary" << std::endl;
m = 0;
break;
case 2:std::cout << "\t\tFebruary" << std::endl;
m = 3;
break;
case 3:std::cout << "\t\tMarch" << std::endl;
m = 3;
break;
case 4:std::cout << "\t\tApril" << std::endl;
m = 6;
break;
case 5:std::cout << "\t\tMay" << std::endl;
m = 1;
break;
case 6:std::cout << "\t\tJune" << std::endl;
m = 4;
break;
case 7:std::cout << "\t\tJuly" << std::endl;
m = 6;
break;
case 8:std::cout << "\t\tAugust" << std::endl;
m = 2;
break;
case 9:std::cout << "\t\tSeptember" << std::endl;
m = 5;
break;
case 10:std::cout << "\t\tOctober" << std::endl;
m = 0;
break;
case 11:std::cout << "\t\tNovember" << std::endl;
m = 3;
break;
case 12:std::cout << "\t\tDecember" << std::endl;
m = 5;
break;
// Constants for year limits
const int MIN_YEAR = 0;
const int MAX_YEAR = 1000000000;

// Function to get the month name
string getMonthName(int month) {
switch (month) {
case 1: return "January";
case 2: return "February";
case 3: return "March";
case 4: return "April";
case 5: return "May";
case 6: return "June";
case 7: return "July";
case 8: return "August";
case 9: return "September";
case 10: return "October";
case 11: return "November";
case 12: return "December";
default: return ""; // Should never reach here
}
if (b >= 1700 && b < 1800)
c = 4;
if (b >= 1800 && b < 1900)
c = 2;
if (b >= 1900 && b < 2000)
c = 0;
if (b >= 2000 && b < 2100)
c = 6;
if (b >= 2100 && b < 2200)
c = 4;
if (b >= 2200 && b < 2300)
c = 2;
if (b >= 2300 && b < 2400)
c = 0;
l = 0;
if (b % 4 == 0 && b % 100 != 0 || b % 400 == 0)
{
p = 1;
if (a <= 2)
l = 1;
}

// Function to check if the year is a leap year
bool isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

// Function to get the century code based on the year
int getCenturyCode(int year) {
if (year >= 1700 && year < 1800) return 4;
if (year >= 1800 && year < 1900) return 2;
if (year >= 1900 && year < 2000) return 0;
if (year >= 2000 && year < 2100) return 6;
if (year >= 2100 && year < 2200) return 4;
if (year >= 2200 && year < 2300) return 2;
return 0; // Default for years >= 2300
}

// Function to get the month code for day calculation
int getMonthCode(int month) {
switch (month) {
case 1: return 0; // January
case 2: return 3; // February
case 3: return 3; // March
case 4: return 6; // April
case 5: return 1; // May
case 6: return 4; // June
case 7: return 6; // July
case 8: return 2; // August
case 9: return 5; // September
case 10: return 0; // October
case 11: return 3; // November
case 12: return 5; // December
default: return 0; // Should never reach here
}
else
p = 0;
//Displaying the calender
i = (y + m + c + 1 - l) % 7;
std::cout << "Sun\tMon\tTue\tWed\tThu\tFri\tSat\n";
for (x = 1; x <= i; x++)
{
std::cout << "\t";
}

int main() {
int year, month;

// Input for year
cout << "Enter the year (from " << MIN_YEAR << " to " << MAX_YEAR << "): ";
cin >> year;
if (year < MIN_YEAR || year > MAX_YEAR) {
cout << "ERROR: The input year is not in range.\nPlease try again.\n";
return 1;
}
if (a == 1 || a == 3 || a == 5 || a == 7 || a == 8 || a == 10 || a == 12)
{
for (x = 1; x <= 31; x++)
{
if (((x + i - 1) % 7 == 0) && (x != 1))
std::cout << std::endl;
std::cout << x << "\t";
}

// Input for month
cout << "Enter the number of the month (from 1 to 12): ";
cin >> month;
if (month < 1 || month > 12) {
cout << "ERROR: The input month is not in range.\nPlease try again.\n";
return 2;
}
if (a == 4 || a == 6 || a == 9 || a == 11)
{
for (x = 1; x <= 30; x++)
{

if (((x + i - 1) % 7 == 0) && x != 1)
std::cout << std::endl;
std::cout << x << "\t";
// Calculating the day of the week
int centuryCode = getCenturyCode(year);
int yearCode = year % 100; // Last two digits of the year
int leapAdjustment = (isLeapYear(year) && month <= 2) ? 1 : 0; // Adjust if it's a leap year and before March
int dayOfWeek = (yearCode + (yearCode / 4) + getMonthCode(month) + centuryCode - leapAdjustment) % 7;

}
// Displaying the calendar
cout << "\n\t\t" << getMonthName(month) << " " << year << endl;
cout << "Sun\tMon\tTue\tWed\tThu\tFri\tSat\n";

for (int x = 0; x < dayOfWeek; x++) {
cout << "\t"; // Leading spaces for the first week
}
if (a == 2)
{
if (p == 0)
{
for (x = 1; x <= 28; x++)
{
if (((x + i - 1) % 7 == 0) && (x != 1))
std::cout << std::endl;
std::cout << x << "\t";
}
}
else
{
for (x = 1; x <= 29; x++)
{
if (((x + i - 1) % 7 == 0) && (x != 1))
std::cout << std::endl;
std::cout << x << "\t";
}

int daysInMonth;
if (month == 2) {
daysInMonth = isLeapYear(year) ? 29 : 28; // February days
} else {
daysInMonth = (month == 4 || month == 6 || month == 9 || month == 11) ? 30 : 31; // Days in other months
}

for (int day = 1; day <= daysInMonth; day++) {
cout << day << "\t";
if ((day + dayOfWeek) % 7 == 0) {
cout << endl; // New line after Saturday
}
}
cout << endl; // New line at the end
return 0;
}
Loading