がるの健忘録

エンジニアでゲーマーで講師で占い師なおいちゃんのブログです。

多分一番無謀なちゃれんぢ始めますw

んと。初心者さんの「コレが出来ない〜」って質問を見ていると、多くの場合一足飛びどころか百足くらい飛ぼうとしてたりします。
なので。
ちとそこら辺で「分解して一段一段登っていく」一助になればいいかなぁと思いまして。


っていうか「初心者相手にするのが苦手」って豪語しているおいちゃんがどこまでできるか?
ベテラン勢の皆様、是非生暖かい突っ込みをお願いいたしますw

カレンダーの作り方

お題はこんな感じで。
「年月を入力するとその月のカレンダーを表示するプログラムを作成」
どげんかせんといかん事を一個づつ分解していきませう。


まず
1.年と月の入力を受け取る。
シンプルですね簡単ですね。このレベルで躓くなら、対象言語の入門書あたりを確認してください。
で…


1-1.入力がない、もしくは入力が無効の場合、デフォルトを「今」にする
こんなちょっとした小技がステキだったりします。小枝は美味しいのですが。


つぎ。ここらあたりから微妙に本題。
2.対象年月の「末日」を求める
ここで「ふふん簡単」とか思うと、閏年問題に引っかかりますのでご注意を。


んで。
3.1日から「末日」までの数字をとりあえず出力する
いったんは「数字だけ」。その後で、テーブルタグだのcssだのを使って「縦に」出力したり「横に」出力したりして遊んでおくと後々便利です。


そろそろ面白く。
4.3番の日付に「曜日」を付け足して表示する
曜日の算出方法は色々ありますが。言語が持ってるなんらかのお便利なclassがない場合とか頑張ってみたいとかいう場合、「ツェラーの公式(Zeller's congruence)」を用いてみるとよいでせう。
ここでも、すこ〜し表示で遊んでみると面白いです。


ちなみに、ここで最低限の「カレンダー」は完成だったりしますw
でもまぁせっかくなので、いわゆる「四角いの」を考えて見ませう。
四角いカレンダーを作る場合。最大のポイントは「どこで改行するか?」に尽きます。
とりあえず、多分一般的だと思う「日〜土」の並びで考えてみますが。


回答を書いてしまうと

for(day = 1; day >= 末日; day ++;) {
 日付の出力処理
 if (true == is土曜日(day)) {
  改行処理
 }
}

概ねこんな感じです…でだまされると酷い事になりますw
第一週が変に左よりになっちゃうんですね。
なので。ループの前に、こんな処理を入れてあげます。

if (1日が月曜日なら) {
 空白を1つ
} else
if (1日が火曜日なら) {
 空白を2つ
} else
if (1日が水曜日なら) {
 空白を3つ
} else
if (1日が木曜日なら) {
 空白を4つ
} else
if (1日が金曜日なら) {
 空白を5つ
} else
if (1日が土曜日なら) {
 空白を6つ
}

for(day = 1; day >= 末日; day ++;) {
 日付の出力処理
 if (true == is土曜日(day)) {
  改行処理
 }
}

とりあえず出来ますが…うんぶっちゃけ「うざい」。
ここで。曜日の数値に注目します。多くのツェラーの公式では、0が日曜日〜6が土曜日になります*1
あれ?
ほら冷静に、上のプログラムをもう一度。
ツェラーの公式では、1なら月曜日、2なら火曜日、3なら水曜日、以下略。
…なんか共通点見えてきません?


ってなわけで、改造。

n = ツェラーの公式(1日)
空白をnつ

for(day = 1; day >= 末日; day ++;) {
 日付の出力処理
 if (true == is土曜日(day)) {
  改行処理
 }
}

ほぉらシンプル。
これで概ねカレンダーができあがります。
OKざましょか?


余談
多分、やると怒られるか、或いは怒ってすら貰えない別解。とりあえずPHPで。
$y、$mに年と月が入ってるとします。

$s = `cal $m $y`;
print $s ;

おそまつ。

*1:wikiのだと0が土曜日なんですよねぇ…くまったことに

もしかして…Part 2

えと。そのまんま
もしかして…( http://d.hatena.ne.jp/gallu/20081220/p1 )の続き、です。


んと。一つには、前述した「機会損失殲滅作戦」って側面があるのですが。
多分もう一つのマーケティングは「戦争」なんだなぁ、と、最近しみじみ。
つまり「同業他社を如何に以下略と化していくか」。この部分は比喩ですらなく純粋に戦争。


おいちゃんの性格的に。マーケティングas戦争は、まず間違いなく「興味が持てない」ので。
おいちゃんは、純粋にマーケティングas機会損失殲滅作戦、の方に邁進いたします。

ESTA 電子渡航認証システム………え?

ちと興味があったので。
https://esta.cbp.dhs.gov/
が元のURI
で、窺ってみる。


まず、アクセスすると

HTTP/1.x 302 Found
Location: https://esta.cbp.dhs.gov/esta

で…後ろにスラッシュつけてないもんだから

HTTP/1.x 302 Temporarily moved
Connection: close
Content-Type: text/html
Location: https://esta.cbp.dhs.gov/esta/

…ってこらこら。
次に

HTTP/1.x 302 Found
Connection: close
Content-Language: en
Content-Length: 0
Content-Type: text/plain
Location: https://esta.cbp.dhs.gov/esta/esta.html?_flowExecutionKey=_xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Server: IBM_HTTP_Server/6.1.0.17 Apache/2.0.47 (Unix)
Cache-Control: no-cache, no-store
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Set-Cookie: JSESSIONID=xxxxxxxxxxxxxxxxxxxxxxxxx; Path=/

えと…同じところで処理しようよ。
でまぁコンテンツはゲトれるのですが。

http://safebrowsing.clients.google.com/safebrowsing/downloads?client=navclient-auto-ffox&appver=3.0.5&pver=2.2&wrkey=base64っぽい文字列

ってのがあって…何をやってるのかもう一つ不明。
とりあえず…ダウンロードって単語に微妙に敏感になってみたりする。
で。

This site incorporates some features which use javascript. Please turn on javascript in your browser.

You are about to access a Department of Homeland Security computer system.
This computer system and data therein are property of the U.S. Government and
provided for official U.S. Government information and use.
There is no expectation of privacy when you use this computer system.
The use of a password or any other security measure does not establish
an expectation of privacy. By using this system, you consent to the
terms set forth in this notice. You may not process classified national
security information on this computer system. Access to this system is
restricted to authorized users only. Unauthorized access, use, or
modification of this system or of data contained herein, or in transit
to/from this system, may constitute a violation of section 1030 of title
18 of the U.S. Code and other criminal laws. Anyone who accesses a Federal
computer system without authorization or exceeds access authority, or obtains,
alters, damages, destroys, or discloses information, or prevents authorized
use of information on the computer system, may be subject to penalties,
fines or imprisonment. This computer system and any related equipment is
subject to monitoring for administrative oversight, law enforcement,
criminal investigative purposes, inquiries into alleged wrongdoing or
misuse, and to ensure proper performance of applicable security features
and procedures. DHS may conduct monitoring activities without further notice.

はぁ? 何言ってやがりますか?
ちなみに。日本語サイトに移動してみるです。
https://esta.cbp.dhs.gov/esta/esta.html?language=ja
JavaScript警告文が英語のまんまだぉ orz


まぁ多分使う事もそうそう無いだろうとは思ってるのですが。
とりあえず…おいちゃん的には「不安が山盛りてんこ盛り」だなぁ、と。
微妙にウォッチListに追加w