カレンダー作るのちょろすぎて草

この記事は暫定版です。

…煽り全開のタイトルですみません。

一応課題のカレンダーを表示するプログラムについて,助言と言うか手引ができたら良いかなと思ってこの記事を書いています。

コードを実際に見せることはしません。俺0点になりたくないので…

ですが部分的に「こうすると良いかもね」みたいな例を示すかもしれないのでご参考までにどうぞ。

注意

あくまでこの記事は考え方を提示するものであって,このコードをコピーして作れるように記事は組み立てていませんのでご注意ください。

みんなに落単してほしくないからそうしてます。ご理解よろしくお願いします。

あと僕も落単したくないです。

作りたいもの

最終的にはこのように表示するプログラムを作るのが目標です。

実現方法

考察

まずは実現方法について考察してみましょう。

今回の動作要件としては

・年/月を入力すると,その年のその月のカレンダーを表示する

・月初めの曜日の判定は,西暦1年1月1日を月曜日と定めたことからの加算で行う

・C言語のライブラリや関数などの機能は使ってはいけない

etc..

でした。これらの要件に沿うように,どのようにして実現するのかを考えてみます。

表示に必要な要件

まず,カレンダーを表示するために必要な要件を考えてみましょう。

・1日が何曜日か

・その月は何日までか

この2つがあればカレンダーを表示することができるはずです。

ではそれぞれの判定のしかたについて考えていきます。

1日が何曜日か

これを判定するためには,

・西暦1年1月1日が月曜日であるということ

・表示したい年月の1日が,西暦1年1月1日から数えて何日経過したか

という2つが必要になります。具体的にどのように実現するかというと,

<経過日数> % 7 = <インデントする数>

このようにすることで,頭からいくつ字下げ(インデント)をすればいいかを計算することができます。

経過日数の計算は次章で。

例えば,西暦1年1月1日は,経過日数1日なので,

1 % 7 = 1

よって下図のように,1つインデント状態(月曜日)になることがわかります。

その月が何日までか

基本この判定は毎月ごとに決まっている値(1月…31日 2月…28日 など)を配列に代入しておいて,繰り返し文を使って表示する時に配列から値を取り出すのが良いと思います。

なので,

int date[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

のようにして配列を作成しておくべきでしょう。

しかしこれだと問題があります。そうです。うるう年の存在です。

うるう年の時は,2月が29日までになりますから,1足してあげる必要があります。

配列の値を別に用意するのは面倒なので,① 入力された年がうるう年かを判定 ② 入力された月が2月かどうかを判定 ③ dateの値に1を足す とすると操作が簡単かと思います。

配列の値を直接書き換えるのは好ましくないので,一度配列の値を変数に取り出すべきでしょう。

まずは変数に取り出します。

int dateA = date[month]

そして,①,②の条件を通し,③をします。

if((year%4==0 && year%100!=0) || year%400==0){
      if(month==2){
           dateA++;
      }
}

こうすることで,「いくつまで繰り返して日付を表示すればいいか」という部分の値が完成します。

この後で dateA をfor文の実行条件に使って式を書いていきます。

経過日数を計算してみよう

先程曜日を求めるには,西暦1年1月1日からの経過日数が必要と言いました。

ではその経過日数を求めてみましょう。

経過日数は,以下のように求められます。

( <入力年>-1 ) * 365 + <入力月-1までの合計日数> + 1 + <うるう年の数>

(入力年-1)×365という操作は,入力年の1年前までの日数をカウントしています。

入力月-1までの合計日数はその名の通り。

そこに+1をするのは,例えば7月を入力したとするならば,上まででカウントされているのは6/30までなので,+1をして7/1までの合計日数にしてあげるというわけです。

うるう年の数は前章で紹介した方法を使います。

1, うるう年の数を数える

一つ変数を用意し,入力年-1年までうるう年の条件に適合するかどうかを試し,適合したら変数をインクリメントする,のような動作でカウントできます。

int leap = 0;
for(i=1;i<year;i++){
    if((year%4==0 && year%100!=0) || year%400==0){
    leap++;
    }
}

2, 入力月-1月までの合計日数を数える

先程用意した月の日数が入っている配列を利用して,入力月-1月までの合計日数を足していきます。

配列の要素は,No.0から始まっていることに注意です。

例) 6月の日数ーNo.5の位置にある

int mdaysum = 0;
for (i=0;i<month-1;i++){
    mdaysum += date[i];
}

3, 今年がうるう年かどうかを判定する

先程のうるう年判定の部分では,今年を含まずに条件を作っていました。

もし今年がうるう年なら,日数に1を加えてあげなければなりません。

そのためにまずはうるう年かどうかを判定しておきます。True(1),False(0)の形で良いと思います。

int thisLeap = 0;
if((year%4==0 && year%100!=0) || year%400==0){
    thisLeap = 1;
}

カレンダー作るのちょろすぎて草” に対して14件のコメントがあります。

  1. 255 より:

    First of all I want to say awesome blog! I had a quick question in whijch I’d llike to ask if youu do nott mind.
    I waas cuhrious to know how youu cenmter yourself and cclear your thoughts before writing.
    I’ve had trouble cearing my thuoughts in getting my thoughts
    out. I do taqke pleasure in weiting however iit just seems lkke the first 10 to 15 minutes tend to be wassted simpl just trying to figure out how to begin.
    Anyy ideeas or hints? Cheers!

  2. Cumshot!!! より:

    Hi there i am kavin, its myy first time tto commenting anywhere,
    when i reaqd this plst i tyought i could also make comment due to thiss brilliant post.

  3. Its not mmy first time tto pay a visit thijs web site, i am browsing this webb page
    daijlly annd geet nice facts from here daily.

  4. I bllog ooften and I seriously hank you for your information.This
    article hass really peaked my interest. I’m
    gong to bookmark youjr website and kerep chhecking foor nnew detail about once per week.
    I opted in for yoir RSS feed too.

  5. xxxtube2022.com より:

    Wow, superb blo format! How leengthy hafe yoou ever been running a bllog for?
    youu mazde rrunning a bloog glance easy. Thee total look of ykur web site is excellent, llet alone
    tthe content!

  6. Having rread this I thouht it was rather informative. I appreciate
    you takibg the time and effort to puut thks informative article together.I once again fin myself perssonally
    spendfing a signifiant amount of time bpth reading annd leaving comments.
    Butt so what, iit wass still worthwhile!

  7. Hi there, There’s no diubt tyat yopur weeb siite may be having brtowser compatibilit issues.
    When I look at yor website in Safari, iit lokks fine however,
    iif opening iin Intedrnet Explorer, itt has some overlapping issues.

    I just wantyed to give youu a quick heads up! Aside
    froom that, great website!

  8. heey there andd thank you for your info – I have definiyely
    picked uup something neww from right here. I did however expertise some
    teechnical iswsues using this webb site, as I experienced too reload thee website lots off tiimes previouus tto I could gget iit too load correctly.

    I hadd een wondering if yojr weeb host iss OK? Nott
    that I’m complaining, but sluggish loading
    instanxes times will sometimes affect your placement in googe andd ccan damage youur qualiyy score if advertising and marketing with Adwords.
    Well I am adding this RSS tto mmy e-mail and could look oout
    foor a llot mmore off your rerspective intriguig content.
    Ensure thwt youu updsate this agaiin soon.

  9. Hi there, You’ve donee a fantastic job. I will definitely digg it and personbally recommend tto my friends.

    I am shre they’ll bee bbenefited frolm this website.

  10. Cyrus より:

    Hello just wanted to give you a quick heads up. The words
    inn youjr content swem to bbe running off the screen in Firefox.
    I’m nnot suree iff this is a formatting isseue oor
    something to doo woth wweb browser compatibility bbut I
    thought I’d pot tto llet yoou know. Thhe dewign look grsat though!
    Hope you gett the probleem resokved soon. Cheers

  11. Jung より:

    Everything is very open with a clear explanation oof thee issues.
    It was definiely informative. Your ite iss very helpful.Thamk you for sharing!

  12. Luther より:

    alll the time i used to read smaller articles or reviews that also clear theeir motive, and that iis also happening wih this piece of writing which I
    am reading at thbis place.

  13. Do yoou mind if I quyote a couple oof yourr polsts as long as I provide credit aand sourcfes back to your webpage?
    My website iss in thhe vry same nichee aas yours andd mmy uswrs wold definitely benevit from a lot off the information yyou provide here.
    Please llet me know if this ok with you. Cheers!

  14. Thanks vewry interesting blog!

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です