#include <iostream>
#include <stdio.h>
//#include <cstdlib>
using namespace std;
int thatday(int,int);
int today(int,int,int,int);
int month_cal(int tod,int d,int m,int y);
void bsort(int *w);
int week_variable=-1;
int main(int argc, char** argv)
{int d,m,y,tatd,tod;
string s;
start:
printf("\033[37menter todays date(as dd/mm/yy):");
for(int i=0;i<8;i++)
cin>>s[i];
s[i]='\0'
for(int i=0;s[i]!=0;i++)
if
cin>>d>>m>>y;
int month_days[]={31,29,31,30,31,30,31,31,30,31,30,31};
//1 2 3 4 5 6 7 8 9 10 11 12
if(m>12||d>month_days[m-1]||y<1980)
{cout<<"wrong input"<<endl;
goto start;
}
if((y%4)!=0&&m==2&&d>28)
{cout<<"wrong input"<<endl;
goto start;
}
tatd=thatday(d,m);
if(y==1980){tod=d;month_cal(tod,d,m,y);goto start;}
tod=today(tatd,y,m,d);
cout<<endl;
month_cal(tod,d,m,y);
return 0;
}
int thatday(int d,int m)
{
int tat_day;
int sum_month[]={0,31,60,91,121,152,182,213,244,274,305,335,366};
// 1 2 3 4 5 6 7 8 9 10 11 12
tat_day=(sum_month[m-1]+(d-1))%7;
cout<<"tat day is";
switch(tat_day)
{
case 0:cout<<"tuesday";week_variable=2;break;
case 1:cout<<"wednesday";week_variable=3;break;
case 2:cout<<"thursday";week_variable=4;break;
case 3:cout<<"friday";week_variable=5;break;
case 4:cout<<"saturday";week_variable=6;break;
case 5:cout<<"sunday";week_variable=7;break;
case 6:cout<<"monday";week_variable=1;break;
}
return week_variable;
}
int today(int tatd,int y,int m,int d)
{
int no_of_leaps,tisday,diff_days;
//int month_days[]={31,28,31,30,31,30,31,31,30,31,30,31};
int month_days_l[]={31,29,31,30,31,30,31,31,30,31,30,31};
int sum_month[]={0,31,59,90,120,151,181,212,243,273,304,334,365};
int sum_month_l[]={0,31,60,91,121,152,182,213,244,274,305,335,366};
no_of_leaps=0;
diff_days=month_days_l[m-1]-d;
//cout<<endl<<diff_days<<endl;
for(int i=m;i<12;i++)
{
diff_days+=month_days_l[i];
// cout<<month_days[i]<<" "<<diff_days<<endl;
}
for(int i=1981;i<y;i++)
{if(!(i%4))
{//cout<<"366"<<i<<" "<<diff_days<<endl;
diff_days+=366;
}
else
{//cout<<"365"<<" "<<diff_days<<endl;
diff_days+=365;
}
}
if(y%4==0&&m>2)
{
diff_days+=sum_month_l[m-1];
//cout<<sum_month_l[m-1]<<" "<<diff_days<<endl;
}
if(y%4==0&&m<=2)
{
diff_days+=sum_month_l[m-1];
//cout<<sum_month_l[m-1]<<" "<<diff_days<<endl;
}
if(y%4!=0)
{
diff_days+=sum_month[m-1];
//cout<<sum_month[m-1]<<" "<<diff_days<<endl;
}
diff_days+=d;//d-1;
//cout<<d<<" "<<diff_days<<endl;
//cout<<diff_days<<" "<<diff_days<<endl;
tisday=((diff_days)%7);
//cout<<tisday<<endl;
cout<<"today is";
switch((tisday+week_variable)%7)
{
case 1:cout<<"monday";break;
case 2:cout<<"tuesday";break;
case 3:cout<<"wednesday";break;
case 4:cout<<"thursday";break;
case 5:cout<<"friday";break;
case 6:cout<<"saturday";break;
case 7:cout<<"sunday";break;
case 0:cout<<"sunday";break;}
return ((tisday+week_variable)%7);}
//<------------------------------------Curses implementation part------->
int month_cal(int tod,int d,int m,int y)
{printf("\033[34m");
int i;//static int no_of_iterations=0;
int month_days_l[]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int month_days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int x[7][5];
for(int i=0;i<7;i++)
for(int j=0;j<6;j++)
x[i][j]=32;
int copy=tod;
//this for loop grades the dates into corresponding week arrays
//what it does is it compares every day in that month using
//tod(today 1,....,7) with the index variable k which takes each day
//value(if k+1==1 then mon) and places that date 'i'(another index
//variable which iterate among dates with initial value 'd' ,in corres-
//-ponding dimension of x[][]
if(y%4==0)
for(i=0;i<12;i++)
month_days[i]=month_days_l[i];
for(int k=0;k<7;k++)
{ tod=copy;
int p=0;
for(i=d;i>0;i--,tod--)
{
if(tod==k+1)
{x[k][p]=i;p++;}
if(tod==1)
tod=8;
}
for(i=d+1,tod=copy+1;i<=month_days[m];i++,tod++)
{
if(tod==k+1)
{x[k][p]=i;p++;}
if(tod==7)
tod=0;
}
}
for(i=0;i<7;i++)
bsort(x[i]);
//<-----------------pritnting of calender--------------------------->
printf("MN TU WD TH FR ST SN");
for(i=0;i<7;++i)
for(int j=0;j<5;++j)
{
if(x[i][j]<32)
// printf("\033[%d;%dH%2d ",no_of_iterations*9+j+4,3*i+1,x[i][j]);
printf("\033[%d;%dH%2d ",j+4,3*i+1,x[i][j]);
}
getchar();
//no_of_iterations++;
//printf("\n\n\n");
printf("\033[37m");
return 0;
}
/*<-------------------sorting of dates in a week--------------------->*/
void bsort(int w[])
{ int n=5;
int i,k;
for(i=0;i<n-1;i++)
{
for(k=0;k<n-i-1;k++)
{
if(w[k]>w[k+1])
{
int temp=w[k];
w[k]=w[k+1];
w[k+1]=temp;
}
}
}
}
Refactorings
No refactoring yet !
Ants
August 14, 2009, August 14, 2009 09:08, permalink
The requested input in line 17 contradicts the check in line 27... specifically year has to be entered as yy, but the comparison is again 1980 as the minimum year. Additionally, there is no check to ensure the year is less than 2100. (Note that 2100 is not a leap year, but the code above will treat it as if were a leap year.)
Why does the program loop back to start when the year is 1980, but not otherwise? Is this a bug or intentional?
What's the point of lines 110-124? The first if checks to see if it's a leap year and the month is after Feb, and the second if checks to see if it's a leap year and the month is Jan or Feb, but both end up doing the same thing anyway.
vhar.myopenid.com
August 14, 2009, August 14, 2009 16:09, permalink
its intentional its disturbing the generality of the alogritm it works fine from 1981 so i turned back if it's 1980.
i implemented that for debugging purpose (to the extent I remember:)).You can try removing it the calender can easily be extended simply making a little additions I leave it to your choice,I am no more interested in it (it ate of 7 days of my time)
Ants
August 18, 2009, August 18, 2009 11:09, permalink
This code cheats a little bit by using mktime() to determine the day of the week that a particular date falls on, but on the other hand, why reinvent the wheel.
Note that on old 32-bit systems, mktime() will fail for dates after Jan 19, 2038. Should not be a problem with the newer C-Runtimes and current 64-bit systems.
#include <stdio.h>
#include <tchar.h>
#include <time.h>
#include <memory.h>
#include <iostream>
#include <iomanip>
using namespace std;
enum Month
{
January = 0,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
};
enum WeekDay
{
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
};
bool IsLeapYear(int year)
{
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
int GetDaysInAMonth(int year, Month month)
{
static const int monthDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (year < 1852)
return 0;
int days = monthDays[month];
if (month == February && IsLeapYear(year))
++days;
return days;
}
bool IsValidDate(int year, Month month, int day)
{
if (year < 1900 || year > 3000)
return false;
if (day < 1 || day > GetDaysInAMonth(year, month))
return false;
return true;
}
void OutputDate(ostream & ostr, int year, int month, int day)
{
ostr << month << "/" << day << "/" << year;
}
WeekDay GetMonthFirstWeekDay(int year, Month month, int day)
{
struct tm date = { 0 };
date.tm_year = year - 1900;
date.tm_mon = month;
date.tm_mday = 1;
if (mktime(&date) == (time_t) -1)
throw -1;
return (WeekDay) date.tm_wday;
}
void DisplayMonthCalendar(int year, Month month, int day)
{
if (!IsValidDate(year, month, day))
{
cout << "Invalid date: ";
OutputDate(cout, year, month, day);
cout << endl;
return;
}
cout << "Month Calendar for ";
OutputDate(cout, year, month, day);
cout << endl;
cout << endl;
cout << "Sun Mon Tue Wed Thu Fri Sat" << endl;
int weekDay = GetMonthFirstWeekDay(year, month, day);
for(int leadingDay = 0; leadingDay < weekDay; ++leadingDay)
cout << " ";
const int daysInAMonth = GetDaysInAMonth(year, month);
for(int day = 1; day <= daysInAMonth; ++day)
{
cout << setw(3) << day << " ";
if (weekDay == Saturday)
cout << endl;
weekDay = (weekDay + 1) % 7;
}
cout << endl;
}
prints months calenders from 1980 to 2099