Calcalute Months difference

Here is the code which calculates the Entered dates month difference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* split the given string with given dilimiter */
int splitString(const char *string,const char *delimiter ,char *bufs[]);
#define MAX_COL_SIZE 1024
#define NUL 0
#define MAX_CHAR_DATE 10
#define true 1
enum TIME
{
    eDATE,
    eMONTH,
    eYEAR,
    eMONTHS_IN_YEAR = 12,
};

int main()
{
    char datePresent[MAX_CHAR_DATE]; //= {"31/6/2011"};
    char datePast[MAX_CHAR_DATE]; // = {"30/12/2010"};
    char *parsedOutputPresent[MAX_COL_SIZE];
    char *parsedOutputPast[MAX_COL_SIZE];
    int yearPre = 0,monthPre = 0,datePre = 0;
    int yearPst = 0,monthPst = 0,datePst = 0;
    const char *delimiter = "/";
    int monthsDifference = 0;
    while(true)
    {

        printf("Enter Present Date:(say 31/6/2011) :");
        scanf("%s",datePresent);
        printf("Enter Past Date :(say 30/12/2010): ");
        scanf("%s",datePast);
        splitString(datePresent,delimiter,parsedOutputPresent);
        splitString(datePast,delimiter,parsedOutputPast);
        /* FOR PRESENT TIME */
        yearPre  = atoi(parsedOutputPresent[eYEAR]);
        monthPre = atoi(parsedOutputPresent[eMONTH]);
        datePre  = atoi(parsedOutputPresent[eDATE]);
        /* FOR PAST TIME */
        yearPst  = atoi(parsedOutputPast[eYEAR]);
        monthPst = atoi(parsedOutputPast[eMONTH]);
        datePst  = atoi(parsedOutputPast[eDATE]);
        if((yearPre-yearPst)>= NUL && (monthPre - monthPst) >= NUL)
        {
        /* THE TIME DIFFERENCE */
        /* CASE 1:
             past month is gt  pre month */
        monthsDifference = ((yearPre - yearPst)* eMONTHS_IN_YEAR) + (monthPre - monthPst);
        printf("Past:%s  - Present:%s = %d months\n",datePast,datePresent,monthsDifference);

        /* THIS CASE OUTPUTS DIFF 18
         CASE 2:
            past month is lt  pre month
        strcpy(datePast,"31/12/2010");
        strcpy(datePresent,"30/6/2011");
        monthsDifference = ((yearPst - yearPre)* eMONTHS_IN_YEAR) + (monthPst - monthPre);
        printf("Past:%s  - Present:%s = %d months\n",datePast,datePresent,monthsDifference);
        */

        }
        else
        {
            printf("Invalid dates !..");
        }
    }
    return(getchar());
}

/***********************************************************************
* Name  : SplitString                                                  *
*                                                                      *
* input : input string to be splitted,delimiter                        *
*                                                                      *
* output: the splitted output strings                                  *
*                                                                      *
* Modification History :                                               *
*                                                                      *
*                                                                      *
************************************************************************/
int splitString(const char *string,const char *delimiter ,char *bufs[])
{
    char *token,*inLine;
    int bufCnt = 0;
    inLine = _strdup(string);
    token = strtok(inLine,delimiter);
    bufs[bufCnt] = _strdup(token);
    while (token != NULL)
    {
        bufs[bufCnt++] = _strdup(token);
        token = strtok(NULL,delimiter);
    }
    return bufCnt;
}


No comments: