--- title: 研修に行ってきてもらってきた課題 author: kazu634 date: 2008-08-24 wordtwit_post_info: - '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:4231;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}' categories: - C - Programming ---

Bashシェルスクリプトの研修に行ってきました。そこでシェルスクリプト用の課題を色々ともらってきたのですが、最後のカレンダー作成だけはシェルよりかはCとか他の言語でくんだ方が勉強になるように思ったので、試行錯誤中。ロジックもらったから、後は組んでみるだけなんです。とりあえず途中の状態を貼り付けておく:

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

Todos:

/* ======================= */
/* === Include library === */
/* ======================= */
#include <stdio.h>
#include <stdlib.h>
/* ==================== */
/* === Declarations === */
/* ==================== */
int days_of_month[13] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31, 29};
char n_month[13][4] = {"Jan", "Feb", "Mar", "Ape", "May", "Jun", "Jul",
"Aug", "Sep", "Nov", "Oct", "Dec", "Feb"};
/* ================= */
/* === Functions === */
/* ================= */
void d_print (int arg[], int num)
{
int i = ;
for (i = ; i < num; ++i)
{
printf("Array [%d]: %d\n", i, arg[i]);
}
}
/* === 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;
}
}
/* ============ */
/* === 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 */
c_leap = isleap(year);
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 == 1){
month = month - 1;
printf("Year %d is NOT a leap year.\n", year);
} else {
month = 12;
printf("Year %d is a leap year.\n", year);
}
printf("%s has %d days.\n", n_month[month], days_of_month[month]);
return ;
}