ページ 11

無題

Posted: 2009年12月15日(火) 17:43
by お願いします!
問題文
以下の出力例のように、月を入力して、その日数を返すプログラムを作成しなさい。

出力例:
月を入力してください:
9
9月は30日あります。


解答
#include <stdio.h>
int main(void)
{
int month, days;
puts("月を入力してください:");
scanf("%d",&month);

//
(* ここに解答を書き加える *)


printf("%d月は%d日あります。\n", month, days);
return(0);
}
//

この問題も答え教えてください!

Re:無題

Posted: 2009年12月15日(火) 17:49
by softya
規約と注意事項をよく読んで、補足を願います。
http://dixq.net/board/board.html

Re:無題

Posted: 2009年12月15日(火) 18:02
by non
質問ですが、2月は何日ありますか?

Re:無題

Posted: 2009年12月16日(水) 10:52
by たかぎ
規約を守らない質問者のことはどうでもよいですが、他に読んでいる人にとって、もしかすると役に立つかもしれませんので...
#include <stdio.h>
int main(void)
{
int month, days;
puts("月を入力してください:");
scanf("%d",&month);

//
    sub(month);
}

#include <time.h>

int sub(month)
    int month;
{
    time_t timer = time(NULL);
    struct tm t = *localtime(&timer);
    t.tm_mon = month;
    t.tm_mday = 0;
    mktime(&t);
    int days = t.tm_mday;

printf("%d月は%d日あります。\n", month, days);
return(0);
}
//
こんな感じでどうでしょうか?
閏年かどうかは、実行した年について判断しています。

Re:無題

Posted: 2009年12月16日(水) 11:17
by non
t.tm_mon = month;
t.tm_mday = 0;
なるほど、こんなテクニックがあるのですね。勉強になります。

Re:無題

Posted: 2009年12月16日(水) 11:50
by sizuma
>t.tm_mon = month;
>t.tm_mday = 0;
>mktime(&t);

なるほど、tmの仕様でmktime関数を使えばこんなことができるんですね。