137 lines
10 KiB
Markdown
137 lines
10 KiB
Markdown
---
|
|
title: 研修に行ってきてもらってきた課題
|
|
author: kazu634
|
|
date: 2008-08-24
|
|
url: /2008/08/24/_1084/
|
|
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
|
|
|
|
---
|
|
<div class="section">
|
|
<p>
|
|
Bashシェルスクリプトの研修に行ってきました。そこでシェルスクリプト用の課題を色々ともらってきたのですが、最後のカレンダー作成だけはシェルよりかはCとか他の言語でくんだ方が勉強になるように思ったので、試行錯誤中。ロジックもらったから、後は組んでみるだけなんです。とりあえず途中の状態を貼り付けておく:
|
|
</p>
|
|
|
|
<blockquote>
|
|
<p>
|
|
年と月をキー入力すると、その月のカレンダーが表示されるスクリプトを作成してください。その月の一日の曜日は外部関数を使い求めてください。計算の基準日は2001年1月1日とする。年は2003年から2099年とする。
|
|
</p>
|
|
</blockquote>
|
|
|
|
<p>
|
|
Todos:
|
|
</p>
|
|
|
|
<ul>
|
|
<li>
|
|
カレンダー表示部分を作成する(現状は入力された年が閏年かの判定と何日あるかしか表示していないので)
|
|
</li>
|
|
</ul>
|
|
|
|
<pre class="syntax-highlight">
|
|
<span class="synComment">/* ======================= */</span>
|
|
<span class="synComment">/* === Include library === */</span>
|
|
<span class="synComment">/* ======================= */</span>
|
|
<span class="synPreProc">#include </span><span class="synConstant"><stdio.h></span>
|
|
<span class="synPreProc">#include </span><span class="synConstant"><stdlib.h></span>
|
|
<span class="synComment">/* ==================== */</span>
|
|
<span class="synComment">/* === Declarations === */</span>
|
|
<span class="synComment">/* ==================== */</span>
|
|
<span class="synType">int</span> days_of_month[<span class="synConstant">13</span>] = {<span class="synConstant">31</span>, <span class="synConstant">28</span>, <span class="synConstant">31</span>, <span class="synConstant">30</span>, <span class="synConstant">31</span>, <span class="synConstant">30</span>,
|
|
<span class="synConstant">31</span>, <span class="synConstant">31</span>, <span class="synConstant">30</span>, <span class="synConstant">31</span>, <span class="synConstant">30</span>, <span class="synConstant">31</span>, <span class="synConstant">29</span>};
|
|
<span class="synType">char</span> n_month[<span class="synConstant">13</span>][<span class="synConstant">4</span>] = {<span class="synConstant">"Jan"</span>, <span class="synConstant">"Feb"</span>, <span class="synConstant">"Mar"</span>, <span class="synConstant">"Ape"</span>, <span class="synConstant">"May"</span>, <span class="synConstant">"Jun"</span>, <span class="synConstant">"Jul"</span>,
|
|
<span class="synConstant">"Aug"</span>, <span class="synConstant">"Sep"</span>, <span class="synConstant">"Nov"</span>, <span class="synConstant">"Oct"</span>, <span class="synConstant">"Dec"</span>, <span class="synConstant">"Feb"</span>};
|
|
<span class="synComment">/* ================= */</span>
|
|
<span class="synComment">/* === Functions === */</span>
|
|
<span class="synComment">/* ================= */</span>
|
|
<span class="synType">void</span> d_print (<span class="synType">int</span> arg[], <span class="synType">int</span> num)
|
|
{
|
|
<span class="synType">int</span> i = <span class="synConstant"></span>;
|
|
<span class="synStatement">for</span> (i = <span class="synConstant"></span>; i < num; ++i)
|
|
{
|
|
printf(<span class="synConstant">"Array [</span><span class="synSpecial">%d</span><span class="synConstant">]: </span><span class="synSpecial">%d\n</span><span class="synConstant">"</span>, i, arg[i]);
|
|
}
|
|
}
|
|
<span class="synComment">/* === Function isleap === */</span>
|
|
<span class="synComment">/* this function checks whether the year is leap year or not.*/</span>
|
|
<span class="synComment">/* When the year is a leap year, it returns 0. */</span>
|
|
<span class="synComment">/* When the year is not, it return 1. */</span>
|
|
<span class="synComment">/* If the year is not in the range, it returns 10. */</span>
|
|
<span class="synType">int</span> isleap(<span class="synType">int</span> year)
|
|
{
|
|
<span class="synComment">/* boundary-value check */</span>
|
|
<span class="synStatement">if</span> (year < <span class="synConstant">2003</span> || <span class="synConstant">2099</span> < year) {
|
|
<span class="synStatement">return</span> <span class="synConstant">10</span>;
|
|
}
|
|
<span class="synComment">/* Leap-year check */</span>
|
|
<span class="synStatement">if</span> (<span class="synConstant"></span> == year % <span class="synConstant">4</span> && <span class="synConstant"></span> != year % <span class="synConstant">100</span> || <span class="synConstant"></span> == year % <span class="synConstant">400</span>){
|
|
<span class="synStatement">return</span> <span class="synConstant"></span>;
|
|
} <span class="synStatement">else</span> {
|
|
<span class="synStatement">return</span> <span class="synConstant">1</span>;
|
|
}
|
|
}
|
|
<span class="synComment">/* === function ismonth === */</span>
|
|
<span class="synComment">/* This function checks whether the given month is in range. */</span>
|
|
<span class="synComment">/* In other words, the given month is between 1 - 12. */</span>
|
|
<span class="synComment">/* If it is, this function returns 0. */</span>
|
|
<span class="synComment">/* If it is not, this function returns 1. */</span>
|
|
<span class="synType">int</span> ismonth (<span class="synType">int</span> month)
|
|
{
|
|
<span class="synStatement">if</span> (<span class="synConstant"></span> < month && month < <span class="synConstant">13</span>) {
|
|
<span class="synStatement">return</span> <span class="synConstant"></span>;
|
|
} <span class="synStatement">else</span> {
|
|
<span class="synStatement">return</span> <span class="synConstant">1</span>;
|
|
}
|
|
}
|
|
<span class="synComment">/* ============ */</span>
|
|
<span class="synComment">/* === main === */</span>
|
|
<span class="synComment">/* ============ */</span>
|
|
<span class="synType">int</span> main(<span class="synType">int</span> argc, <span class="synType">char</span> *argv[])
|
|
{
|
|
<span class="synComment">/* === Declaration === */</span>
|
|
<span class="synType">char</span> year_c[<span class="synConstant">256</span>]; <span class="synComment">/* For reading data from stdin */</span>
|
|
<span class="synType">char</span> month_c[<span class="synConstant">256</span>]; <span class="synComment">/* For reading data from stdin */</span>
|
|
<span class="synType">int</span> year = <span class="synConstant"></span>; <span class="synComment">/* For storing year */</span>
|
|
<span class="synType">int</span> month = <span class="synConstant"></span>; <span class="synComment">/* For storing month */</span>
|
|
<span class="synType">int</span> c_leap; <span class="synComment">/* Check for a leap year */</span>
|
|
<span class="synType">int</span> c_month; <span class="synComment">/* check for boundary value */</span>
|
|
<span class="synComment">/* Ask the user calendar of what year he/she wants */</span>
|
|
c_leap = isleap(year);
|
|
<span class="synStatement">do</span> {
|
|
printf(<span class="synConstant">"Which year?(2003-2099): "</span>);
|
|
fgets(year_c, <span class="synConstant">256</span>, <span class="synConstant">stdin</span>);
|
|
sscanf(year_c, <span class="synConstant">"</span><span class="synSpecial">%d</span><span class="synConstant">"</span>, &year);
|
|
c_leap = isleap(year);
|
|
<span class="synComment">/* If the data is incorrect, show the message. */</span>
|
|
<span class="synStatement">if</span> (c_leap == <span class="synConstant">10</span>){
|
|
printf(<span class="synConstant">"The number is incorrect!</span><span class="synSpecial">\n</span><span class="synConstant">"</span>);
|
|
}
|
|
} <span class="synStatement">while</span> (<span class="synConstant">10</span> == c_leap);
|
|
c_month = ismonth(month);
|
|
<span class="synComment">/* Ask the user calendar of what month he/she wants */</span>
|
|
<span class="synStatement">do</span> {
|
|
printf(<span class="synConstant">"Which month?(1-12): "</span>);
|
|
fgets(month_c, <span class="synConstant">256</span>, <span class="synConstant">stdin</span>);
|
|
sscanf(month_c, <span class="synConstant">"</span><span class="synSpecial">%d</span><span class="synConstant">"</span>, &month);
|
|
c_month = ismonth(month);
|
|
<span class="synComment">/* If the data is incorrect, show the message. */</span>
|
|
<span class="synStatement">if</span> (c_month == <span class="synConstant">1</span>){
|
|
printf(<span class="synConstant">"The number is incorrect!</span><span class="synSpecial">\n</span><span class="synConstant">"</span>);
|
|
}
|
|
} <span class="synStatement">while</span> (<span class="synConstant">1</span> == c_month);
|
|
<span class="synComment">/* Show Calendar */</span>
|
|
<span class="synStatement">if</span> (c_leap == <span class="synConstant">1</span>){
|
|
month = month - <span class="synConstant">1</span>;
|
|
printf(<span class="synConstant">"Year </span><span class="synSpecial">%d</span><span class="synConstant"> is NOT a leap year.</span><span class="synSpecial">\n</span><span class="synConstant">"</span>, year);
|
|
} <span class="synStatement">else</span> {
|
|
month = <span class="synConstant">12</span>;
|
|
printf(<span class="synConstant">"Year </span><span class="synSpecial">%d</span><span class="synConstant"> is a leap year.</span><span class="synSpecial">\n</span><span class="synConstant">"</span>, year);
|
|
}
|
|
printf(<span class="synConstant">"</span><span class="synSpecial">%s</span><span class="synConstant"> has </span><span class="synSpecial">%d</span><span class="synConstant"> days.</span><span class="synSpecial">\n</span><span class="synConstant">"</span>, n_month[month], days_of_month[month]);
|
|
<span class="synStatement">return</span> <span class="synConstant"></span>;
|
|
}
|
|
</pre>
|
|
</div> |