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

274 lines
17 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: できた
author: kazu634
date: 2008-08-26
url: /2008/08/26/_1086/
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:4235;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- C
- Programming
---
<div class="section">
<p>
<a href="http://d.hatena.ne.jp/sirocco634/20080824/1219586071" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20080824/1219586071', ' 研修に行ってきてもらってきた課題 &#8211; 武蔵の日記');" target="_blank"> 研修に行ってきてもらってきた課題 &#8211; 武蔵の日記</a>」でやっていた課題ができました。
</p>
<blockquote>
<p>
年と月をキー入力すると、その月のカレンダーが表示されるスクリプトを作成してください。その月の一日の曜日は外部関数を使い求めてください。計算の基準日は2001年1月1日とする。年は2003年から2099年とする。
</p>
</blockquote>
<p>
実行結果:
</p>
<blockquote>
<p>
Which year?(2003-2099): 2008
</p>
<p>
Which month?(1-12): 8
</p>
<p>
_______2008_Aug
</p>
<p>
__S__M__T__W__T__F__S
</p>
<p>
_________________1__2
</p>
<p>
__3__4__5__6__7__8__9
</p>
<p>
_10_11_12_13_14_15_16
</p>
<p>
_17_18_19_20_21_22_23
</p>
<p>
_24_25_26_27_28_29_30
</p>
<p>
_31
</p>
</blockquote>
<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">&#60;stdio.h&#62;</span>
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdlib.h&#62;</span>
<span class="synComment">/* ==================== */</span>
<span class="synComment">/* === Declarations === */</span>
<span class="synComment">/* ==================== */</span>
<span class="synComment">// days_of_month is an array of the days of the month.</span>
<span class="synComment">// Note that the Feb of the leap year is indexed as 12.</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="synComment">// n_month is an array of the names of the months.</span>
<span class="synComment">// Note that the Feb of a leap year is indexed as 12.</span>
<span class="synType">char</span> n_month[<span class="synConstant">13</span>][<span class="synConstant">4</span>] = {<span class="synConstant">&#34;Jan&#34;</span>, <span class="synConstant">&#34;Feb&#34;</span>, <span class="synConstant">&#34;Mar&#34;</span>, <span class="synConstant">&#34;Ape&#34;</span>, <span class="synConstant">&#34;May&#34;</span>, <span class="synConstant">&#34;Jun&#34;</span>, <span class="synConstant">&#34;Jul&#34;</span>,
<span class="synConstant">&#34;Aug&#34;</span>, <span class="synConstant">&#34;Sep&#34;</span>, <span class="synConstant">&#34;Nov&#34;</span>, <span class="synConstant">&#34;Oct&#34;</span>, <span class="synConstant">&#34;Dec&#34;</span>, <span class="synConstant">&#34;Feb&#34;</span>};
<span class="synComment">// daysofweek is an array of the names of the days of the week.</span>
<span class="synType">char</span> daysofweek[<span class="synConstant">7</span>][<span class="synConstant">4</span>] = {<span class="synConstant">&#34;Sun&#34;</span>, <span class="synConstant">&#34;Mon&#34;</span>, <span class="synConstant">&#34;Tue&#34;</span>, <span class="synConstant">&#34;Wed&#34;</span>, <span class="synConstant">&#34;Thu&#34;</span>,
<span class="synConstant">&#34;Sat&#34;</span>, <span class="synConstant">&#34;Sun&#34;</span>};
<span class="synComment">/* ================= */</span>
<span class="synComment">/* === Functions === */</span>
<span class="synComment">/* ================= */</span>
<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 &#60; <span class="synConstant">2003</span> || <span class="synConstant">2099</span> &#60; 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> &#38;&#38; <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> &#60; month &#38;&#38; month &#60; <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">/* === function isjanfir === */</span>
<span class="synComment">/* This function checks what days of the week the given year's */</span>
<span class="synComment">/* January the First is.*/</span>
<span class="synComment">/* This function returns the index of the array, daysofweek. */</span>
<span class="synType">int</span> isjanfir (<span class="synType">int</span> year)
{
<span class="synType">int</span> res = <span class="synConstant">1</span>; <span class="synComment">/* Result */</span>
<span class="synType">int</span> i; <span class="synComment">/* working variable */</span>
<span class="synComment">/* Jan the first of 2001 is Monday. */</span>
<span class="synStatement">for</span> (i = <span class="synConstant">2001</span>; i &#60; year; ++i)
{
<span class="synComment">/* If the given year is a leap year, */</span>
<span class="synComment">/* result = result + 2. */</span>
<span class="synComment">/* Otherwise, result = result + 1. */</span>
<span class="synStatement">if</span> (isleap(i) == <span class="synConstant"></span>){
res = res + <span class="synConstant">2</span>;
} <span class="synStatement">else</span> {
res = res + <span class="synConstant">1</span>;
}
}
<span class="synComment">/* the return value is result % 7. */</span>
<span class="synStatement">return</span> res % <span class="synConstant">7</span>;
}
<span class="synComment">/* === function prt_digit === */</span>
<span class="synComment">/* This function prints the digit on stdout. */</span>
<span class="synComment">/* If the digit is less than 10, it puts another &#34; &#34;. */</span>
<span class="synType">void</span> prt_digit(<span class="synType">int</span> day)
{
<span class="synStatement">if</span> (day &#60; <span class="synConstant">10</span>){
printf(<span class="synConstant">&#34; </span><span class="synSpecial">%d</span><span class="synConstant">&#34;</span>, day);
} <span class="synStatement">else</span> {
printf(<span class="synConstant">&#34; </span><span class="synSpecial">%d</span><span class="synConstant">&#34;</span>, day);
}
}
<span class="synComment">/* === function getweek ===*/</span>
<span class="synComment">/* This function gets the two arguments representing year and month. */</span>
<span class="synComment">/* And it returns the days of the week on the 1st days of the month */</span>
<span class="synComment">/* of the year. */</span>
<span class="synComment">/* Note that it returns the index of the array daysofweek. */</span>
<span class="synType">int</span> getweek (<span class="synType">int</span> year, <span class="synType">int</span> month)
{
<span class="synType">int</span> i; <span class="synComment">/* working variable */</span>
<span class="synComment">// first the variable result stores the value of the New Year day.</span>
<span class="synType">int</span> result = isjanfir(year);
<span class="synComment">/* If the month is Jan, it returns result. */</span>
<span class="synStatement">if</span> (<span class="synConstant"></span> == month) <span class="synStatement">return</span> result;
<span class="synComment">/* If the month is Feb of the leap year, */</span>
<span class="synComment">/* this function returns (result + 31 [since Jan has 31days]) */</span>
<span class="synStatement">if</span> (<span class="synConstant">12</span> == month) <span class="synStatement">return</span> ((result + <span class="synConstant">31</span>) % <span class="synConstant">7</span>);
<span class="synStatement">for</span> (i = <span class="synConstant"></span>; i &#60; month; ++i)
{
<span class="synComment">/* If (the year is a leap year) and (month is Feb) */</span>
<span class="synStatement">if</span> (isleap(year) == <span class="synConstant"></span> &#38;&#38; i == <span class="synConstant">1</span>) {
result = result + <span class="synConstant">29</span>;
} <span class="synStatement">else</span> {
result = result + days_of_month[i];
}
}
<span class="synStatement">return</span> (result % <span class="synConstant">7</span>);
}
<span class="synComment">/* === function showcal === */</span>
<span class="synType">void</span> showcal(<span class="synType">int</span> year, <span class="synType">int</span> month)
{
<span class="synType">int</span> i; <span class="synComment">/* working variable */</span>
<span class="synType">int</span> week;
<span class="synType">int</span> tmp; <span class="synComment">/* temporary variable */</span>
week = getweek(year, month);
tmp = week;
printf(<span class="synConstant">&#34; S M T W T F S</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
<span class="synStatement">for</span> (i = <span class="synConstant"></span>; i &#60; week; ++i)
{
printf(<span class="synConstant">&#34; &#34;</span>);
}
<span class="synStatement">for</span> (i = <span class="synConstant">1</span>; i &#60;= days_of_month[month]; ++i)
{
<span class="synStatement">if</span> (tmp!= <span class="synConstant"></span> &#38;&#38; <span class="synConstant"></span> == (tmp % <span class="synConstant">7</span>)) {
printf(<span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
}
tmp++;
prt_digit(i);
}
printf(<span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</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>
<span class="synStatement">do</span> {
printf(<span class="synConstant">&#34;Which year?(2003-2099): &#34;</span>);
fgets(year_c, <span class="synConstant">256</span>, <span class="synConstant">stdin</span>);
sscanf(year_c, <span class="synConstant">&#34;</span><span class="synSpecial">%d</span><span class="synConstant">&#34;</span>, &#38;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">&#34;The number is incorrect!</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</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">&#34;Which month?(1-12): &#34;</span>);
fgets(month_c, <span class="synConstant">256</span>, <span class="synConstant">stdin</span>);
sscanf(month_c, <span class="synConstant">&#34;</span><span class="synSpecial">%d</span><span class="synConstant">&#34;</span>, &#38;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">&#34;The number is incorrect!</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</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"></span> &#38;&#38; month == <span class="synConstant">2</span>) {
month = <span class="synConstant">12</span>;
} <span class="synStatement">else</span> {
month = month - <span class="synConstant">1</span>;
}
printf(<span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant"> </span><span class="synSpecial">%d</span><span class="synConstant"> </span><span class="synSpecial">%s\n</span><span class="synConstant">&#34;</span>, year, n_month[month]);
showcal(year, month);
<span class="synStatement">return</span> <span class="synConstant"></span>;
}
</pre>
<blockquote>
<ul>
<li>
「c」に関連する最近のエントリ <ul>
<li>
<a href="http://d.hatena.ne.jp/sirocco634/20080825/1219673282" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20080825/1219673282', ' Flymakeが便利 &#8211; 武蔵の日記');" target="_blank"> Flymakeが便利 &#8211; 武蔵の日記</a>
</li>
<li>
<a href="http://d.hatena.ne.jp/sirocco634/20080824/1219586071" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20080824/1219586071', ' 研修に行ってきてもらってきた課題 &#8211; 武蔵の日記');" target="_blank"> 研修に行ってきてもらってきた課題 &#8211; 武蔵の日記</a>
</li>
<li>
<a href="http://d.hatena.ne.jp/sirocco634/20080820/1219236503" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20080820/1219236503', ' なんかもっと簡単なところからやりたい &#8211; 武蔵の日記');" target="_blank"> なんかもっと簡単なところからやりたい &#8211; 武蔵の日記</a>
</li>
<li>
<a href="http://d.hatena.ne.jp/sirocco634/20080819/1219153913" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20080819/1219153913', ' 第13章 文字と文字列処理 &#8211; 武蔵の日記');" target="_blank"> 第13章 文字と文字列処理 &#8211; 武蔵の日記</a>
</li>
</ul>
</li>
</ul>
</blockquote>
</div>