Thinking in C++

My C++ code snips.

Archive for the ‘C Programming Language’ Category

Calendar in C.

Posted by Abhi on July 7, 2012

I just finished writing the code for printing a calendar for a given year in C. Thought it might be useful to someone.

/***************************************
Program to print the calendar of a given
year. You also have to enter the starting
day of that year in words.

Written by Abijeet. 7-7-2012

****************************************/

#include
#include

#define DAYS 7
#define WEEKS 5
#define MAX 30
#define MONTHS 12

int isLeapYear(int);
int getStartingDay(const char*);
int noOfDaysInMonth(int);
int printMonth(int,int,int,int);
char* getNameOfMonth(int);

int main()
{
    int monthNo, currentDay, year, startDay, lastDay;   // startDay is starting day, lastDay is the last day of the month.
    int flagLeap,errNo;
    char startingDay[MAX],ans;
    do
    {
        errNo=0;
        printf("Enter the year: ");
        scanf("%d",&year);
        // Determining whether the year is a leap year.
        flagLeap = isLeapYear(year);

        // Determining the starting day of the week
        do
        {
            if(errNo>0)
                printf("\nWrong day entered, please enter the starting day as Sunday or Monday or Tuesday or any other day of the week.\n\n");

            printf("Please enter the starting day of the year: ");
            scanf("%s",&startingDay);

            startDay = getStartingDay(startingDay);
            ++errNo;

        }while(startDay == -1);
        //printf("%d %d %d",startDay,flagLeap,noOfDaysInMonth(7));

        // Prints the months of the year
        for(monthNo=0;monthNo<MONTHS;++monthNo)
        {
            // For Feburary in which the year is a leap year.
            if((monthNo == 1) && flagLeap)
            {
                lastDay = printMonth(monthNo,startDay,29,year);
            }
            else
            {
                lastDay=printMonth(monthNo,startDay,noOfDaysInMonth(monthNo),year);
            }
            // Calculating the starting day of the next month.
            if(lastDay == 6)
                startDay = 0;
            else
                startDay = lastDay + 1;
        }
        printf("Do you want to display the calendar for another year? (Y/N) : __\b\b");
        scanf("%s",&ans);
    }while(ans=='y'||ans=='Y');

    printf("\nThanks for using..Bye!\n\n");

    return 0;
}

/**********************************
Function to check if the year is a
leap year. Return 1 if it is a leap
year else Returns 0.

**********************************/

int isLeapYear(int year)
{
    if((year%4==0)&&(year%100!=0)||(year%400==0))
        return 1;
    return 0;
}

/***************************************
Function to return the number of the day
in the week when the user enters the
string. For example, will return 0 for
Sunday, 1 for Monday, 2 for Tuesday and
so on.....

****************************************/

int getStartingDay(const char* nameOfDay)
{
    char nameOfDaysOfWeek[DAYS][MAX] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    char nameOfDaysOfWeekInSmall[DAYS][MAX] = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};
    int i;
    for(i=0;i<DAYS;++i)
    {
        if(strcmp(nameOfDay,nameOfDaysOfWeek[i])==0 || strcmp(nameOfDay,nameOfDaysOfWeekInSmall[i])==0)
            return i;
    }
    return -1;
}

/**********************************
Function to return the number of
days in a month. Returns 28 for
Feburary by default.

**********************************/

int noOfDaysInMonth(int monthNum)
{
    int dayMonth[MONTHS][1] = {{31},{28},{31},{30},{31},{30},{31},{31},{30},{31},{30},{31}};

    return *dayMonth[monthNum];
}

/***************************************
Core of the program. This function takes
month number, starting day, and total days
in a month as input and displays the month.
It also returns the name of the last day of
the month so that the calling function can
use it to calculate the first day of the
next month. Also takes the year as input
and prints it.

****************************************/

int printMonth(int month,int startDay,int noOfDays,int year)
{
    int currentDay=1,calendar[WEEKS][DAYS],currentNameOfDay;
    int row,col;
    char nameOfMonth[MAX];

    // Initializing all the array elements to 0

    for(row=0;row<WEEKS;++row)
    {
        for(col=0;col<DAYS;++col)
        {
            calendar[row][col]=0;
        }
    }
    row = 0;
    // Filling up the array with dates
    while(currentDay     {
        for(col = 0;colnoOfDays)
                break;

        }
        ++row; // Moving to the next row of the array.
    }
    currentNameOfDay = col;

    /*
    Stores where the current month ended, and returns it to the calling functions so that it
    can calculate the starting day of the next month!
    */

    // Now printing the calendar!!
    strcpy(nameOfMonth,getNameOfMonth(month));
    printf("\n%s, %d\n__________________________________________________________________\n",nameOfMonth,year);

    // Printing the days of the week....
    printf("\n\tSun\tMon\tTue\tWed\tThu\tFri\tSat\n\n");

    for(row=0; row    {
        for(col=0;col < DAYS;++col)
        {
        /*
        We initialized the calendar array to 0, now when printing it we do not
        want to display the 0's. So if the value is 0, we print a tab space and
        continue.
        */
            if(calendar[row][col] == 0)
            {
                printf(" \t");
                continue;
            }
            printf("\t%d",calendar[row][col]);
        }
        printf("\n");
    }
    printf("\n\n\n");

    return currentNameOfDay;
}

/***************************************
Takes the month number as input and
returns the name of the month.

***************************************/

char* getNameOfMonth(int month)
{
    static char monthNames[MONTHS][MAX]={"January","Feburary","March","April","May","June","July","August","September","October","November","December"};
    return monthNames[month];
}

Advertisements

Posted in C Programming Language | Leave a Comment »