blog/content/post/2008-08-26-00001010.md

17 KiB
Raw Blame History

title author date url wordtwit_post_info categories
できた kazu634 2008-08-26 /2008/08/26/_1086/
O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4235;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}
C
Programming

研修に行ってきてもらってきた課題 武蔵の日記」でやっていた課題ができました。

年と月をキー入力すると、その月のカレンダーが表示されるスクリプトを作成してください。その月の一日の曜日は外部関数を使い求めてください。計算の基準日は2001年1月1日とする。年は2003年から2099年とする。

実行結果:

Which year?(2003-2099): 2008

Which month?(1-12): 8

_______2008_Aug

__S__M__T__W__T__F__S

_________________1__2

__3__4__5__6__7__8__9

_10_11_12_13_14_15_16

_17_18_19_20_21_22_23

_24_25_26_27_28_29_30

_31

/* ======================= */
/* === Include library === */
/* ======================= */
#include <stdio.h>
#include <stdlib.h>
/* ==================== */
/* === Declarations === */
/* ==================== */
// days_of_month is an array of the days of the month.
// Note that the Feb of the leap year is indexed as 12.
int days_of_month[13] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31, 29};
// n_month is an array of the names of the months.
// Note that the Feb of a leap year is indexed as 12.
char n_month[13][4] = {"Jan", "Feb", "Mar", "Ape", "May", "Jun", "Jul",
"Aug", "Sep", "Nov", "Oct", "Dec", "Feb"};
// daysofweek is an array of the names of the days of the week.
char daysofweek[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu",
"Sat", "Sun"};
/* ================= */
/* === Functions === */
/* ================= */
/* === Function isleap === */
/* this function checks whether the year is leap year or not.*/
/* When the year is a leap year, it returns 0. */
/* When the year is not, it return 1. */
/* If the year is not in the range, it returns 10. */
int isleap(int year)
{
/* boundary-value check */
if (year < 2003 || 2099 < year) {
return 10;
}
/* Leap-year check */
if ( == year % 4 &&  != year % 100 ||  == year % 400){
return ;
} else {
return 1;
}
}
/* === function ismonth === */
/* This function checks whether the given month is in range. */
/* In other words, the given month is between 1 - 12. */
/* If it is, this function returns 0. */
/* If it is not, this function returns 1. */
int ismonth (int month)
{
if ( < month && month < 13) {
return ;
} else {
return 1;
}
}
/* === function isjanfir === */
/* This function checks what days of the week the given year's  */
/* January the First is.*/
/* This function returns the index of the array, daysofweek. */
int isjanfir (int year)
{
int	res = 1;	/* Result */
int	i;		/* working variable */
/* Jan the first of 2001 is Monday. */
for (i = 2001; i < year; ++i)
{
/* If the given year is a leap year, */
/* result = result + 2. */
/* Otherwise, result = result + 1. */
if (isleap(i) == ){
res = res + 2;
} else {
res = res + 1;
}
}
/* the return value is result % 7. */
return res % 7;
}
/* === function prt_digit === */
/* This function prints the digit on stdout. */
/* If the digit is less than 10, it puts another " ".  */
void prt_digit(int day)
{
if (day < 10){
printf("  %d", day);
} else {
printf(" %d", day);
}
}
/* === function getweek ===*/
/* This function gets the two arguments representing year and month. */
/* And it returns the days of the week on the 1st days of the month */
/* of the year. */
/* Note that it returns the index of the array daysofweek. */
int getweek (int year, int month)
{
int i;			/* working variable */
// first the variable result stores the value of the New Year day.
int result = isjanfir(year);
/* If the month is Jan, it returns result. */
if ( == month) return result;
/* If the month is Feb of the leap year,  */
/* this function returns (result + 31 [since Jan has 31days]) */
if (12 == month) return ((result + 31) % 7);
for (i = ; i < month; ++i)
{
/* If (the year is a leap year) and (month is Feb) */
if (isleap(year) ==  && i == 1) {
result = result + 29;
} else {
result = result + days_of_month[i];
}
}
return (result % 7);
}
/* === function showcal === */
void showcal(int year, int month)
{
int	i;		/* working variable */
int	week;
int	tmp;		/* temporary variable */
week = getweek(year, month);
tmp = week;
printf("  S  M  T  W  T  F  S\n");
for (i = ; i < week; ++i)
{
printf("   ");
}
for (i = 1; i <= days_of_month[month]; ++i)
{
if (tmp!=  &&  == (tmp % 7)) {
printf("\n");
}
tmp++;
prt_digit(i);
}
printf("\n");
}
/* ============ */
/* === main === */
/* ============ */
int main(int argc, char *argv[])
{
/* === Declaration === */
char	year_c[256];	/* For reading data from stdin */
char	month_c[256];	/* For reading data from stdin */
int	year  = ;	/* For storing year */
int	month = ;	/* For storing month */
int	c_leap;		/* Check for a leap year */
int	c_month;	/* check for boundary value */
/* Ask the user calendar of what year he/she wants */
do {
printf("Which year?(2003-2099): ");
fgets(year_c, 256, stdin);
sscanf(year_c, "%d", &year);
c_leap = isleap(year);
/* If the data is incorrect, show the message. */
if (c_leap == 10){
printf("The number is incorrect!\n");
}
} while (10 == c_leap);
c_month = ismonth(month);
/* Ask the user calendar of what month he/she wants */
do {
printf("Which month?(1-12): ");
fgets(month_c, 256, stdin);
sscanf(month_c, "%d", &month);
c_month = ismonth(month);
/* If the data is incorrect, show the message. */
if (c_month == 1){
printf("The number is incorrect!\n");
}
} while (1 == c_month);
/* Show Calendar */
if (c_leap ==  && month == 2) {
month = 12;
} else {
month = month - 1;
}
printf("\n       %d %s\n", year, n_month[month]);
showcal(year, month);
return ;
}