曜日の判定

課題 Zellarの公式を用いて、入力された年月日の曜日を判定する

program youbi;
{$APPTYPE CONSOLE}
uses SysUtils;
label 10;
label 20;
label 30;
var year,month,day,week:integer;
begin
10:writeln('入力された年月日の曜日を判定します。');
write('年を西暦で入力してください。:');
readln(year);
20:write('月を入力してください。:');
readln(month);
while(month < 1) or (month > 12) do       //月の吟味
begin
writeln('月は1から12までしかありませんよ。');
goto 20;
end;
30:write('日を入力してください。:');
readln(day);
while(month = 1) and (day > 31) or       //日の吟味
(month = 2) and (day > 29) or
(month = 3) and (day > 31) or
(month = 4) and (day > 30) or
(month = 5) and (day > 31) or
(month = 6) and (day > 30) or
(month = 7) and (day > 31) or
(month = 8) and (day > 31) or
(month = 9) and (day > 30) or
(month = 10) and (day > 31) or
(month = 11) and (day > 30) or
(month = 12) and (day > 31) or
(day < 1) do
begin
writeln('',month,'月に',day,'日はありませんよ。');
goto 30;
end;
while((month = 2) and (day = 29) and (year mod 4<>0)) or   //閏年の吟味
((month = 2) and (day = 29) and (year mod 100=0) and (year mod 400<>0))do
begin
writeln('',year,'年は閏年ではありませんよ。');
goto 30;
end;

if (month=1)or(month=2) then begin            //月が1,2月なら前年の
year:=year-1;                     //13月、14月と考える。
month:=month+12;
end;
week:=((year+year div 4-year div 100+year div 400)+     //曜日の判定
((13*month+8)div 5)+day)mod 7;
case week of                       //判定した曜日の出力
0 :writeln('その日は日曜日です。');
1 :writeln('その日は月曜日です。');
2 :writeln('その日は火曜日です。');
3 :writeln('その日は水曜日です。');
4 :writeln('その日は木曜日です。');
5 :writeln('その日は金曜日です。');
6 :writeln('その日は土曜日です。');
end;
writeln('');                        //何度でも実行可能な
goto 10;                         //ようにした。
readln
end.

プログラムの説明
Zellarの公式を用いて曜日を判定するプログラムを作成した。
まず年、月を入力するように指示し、月が不適当でないか吟味を行う。
さらに日を入力するよう指示し、日が不適当でないか判定を行う。
次にZellarの公式により曜日を判定し、算出した数字によって「その日は○曜日です。」と表示するようにした。また、再度実行しなくても、別の年月日の曜日が判定できるように、曜日を表示し終わってから、プログラムの最初に戻るように設定した。

レポートを作成しての感想
はじめ「その日は○曜日です。」を出力するプログラムにif文を使っていたが、case文を用いればずっと簡単に書けることに気づいて、書き換えた。ただ、日の吟味をする部分が、条件だけで13行にもなってしまい、やや不満が残った。

もどる

SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送