Skip to content

Instantly share code, notes, and snippets.

@prehistoricpenguin
Last active December 10, 2015 01:49
Show Gist options
  • Save prehistoricpenguin/4362996 to your computer and use it in GitHub Desktop.
Save prehistoricpenguin/4362996 to your computer and use it in GitHub Desktop.
day calculating.... 3 days fishing,2 days netting.....
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
const int STARTYEAR = 1990,
STARTMONTH = 1,
STARTDAY = 1,
ENDYEAR = 2100;
const int DAYSOFCOMMONYEAR = 365;
const int tbl[][13] =
{
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
inline bool isLeapYear (int year)
{
return (year % 400 == 0) ||
(year % 4 == 0 && year % 100 != 0);
}
bool navieQuery (int year, int month, int day)
{
int i, daysPast = day -1;
bool isLeap = isLeapYear(year);
/*
*count total years,example:
*1990.01.01 ~ 2012.01.01
*/
for (i = STARTYEAR; i <= year; ++i)
daysPast += DAYSOFCOMMONYEAR + isLeapYear(i);
for (i = 0; i < month; ++i)
daysPast += tbl[isLeap][i];
/*
* judging
*/
if (daysPast % 5 <= 2)
printf ("Fishing ...\n");
else
printf ("Netting ...\n");
}
/*
*@brief: check whether the year month day will make
* up a valid date time
*/
bool checkInput (int year, int month, int day)
{
bool yearErr = false,
monthErr = false,
dayErr = false;
yearErr = year < STARTYEAR || year > ENDYEAR;
monthErr = month < 1 || month > 12;
dayErr = day < 1 || day > tbl[isLeapYear(year)][month];
if (yearErr)
fprintf(stderr, "Error :invalid year specified!\n");
if (monthErr)
fprintf(stderr, "Error :invalid month specified!\n");
if (dayErr)
fprintf(stderr, "Error :invalid day specified!\n");
if (yearErr || monthErr || dayErr)
return false;
else
return true;
}
/*
*@brief: check whether the command paramters
* containes three digits
*/
bool checkParameter (const char* str1,
const char* str2,
const char* str3)
{
int year, month, day;
if ( isdigit(*str1) &&
isdigit(*str2) &&
isdigit(*str3))
return true;
else
{
fprintf (stderr, "Error :invalid paramter!\n"
"input year month day again!\n");
return false;
}
}
/*
*@brief:get year month day,then check its' Legitimacy
* if not valid, get again.If meet end-of-file,return false
*/
bool getInput (int* year, int* month, int* day)
{
do
{
if (scanf("%d%d%d", year, month, day) == EOF)
return false;
}while (!checkInput(*year, *month, *day));
return true;
}
int main (int argc, char* argv[])
{
int year, month, day;
/*
* input manually
*/
if (argc != 4)
{
printf("This simple application will calculate the day after 1990:01:01\n"
"end up to 2100:01:01,\n"
"what the fishman will being doing\n");
while (true)
{
printf("please input (year month day)[EOF to terminate]:\n");
if (!getInput(&year, &month, &day))
return EXIT_SUCCESS;
navieQuery(year, month, day);
}
}
/*
* input from progarm paramter
*/
else
{
if (!checkParameter(argv[1], argv[2], argv[3]))
getInput(&year, &month, &day);
else
{
year = strtol(argv[1], NULL, 10);
month = strtol(argv[2], NULL, 10);
day = strtol(argv[3], NULL, 10);
if (!checkInput(year, month, day))
if (!getInput(&year, &month, &day))
return EXIT_SUCCESS;
}
navieQuery(year, month, day);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment