2013年10月7日月曜日

開発環境

C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)の6章(条件式と制御文)、6.10(プログラミング実習)、実習 6-5を解いてみる。

その他参考書籍

実習 6-5.

コード

sample.c

#include <stdio.h>
int main(int argc, char *argv[])
{
    int year;
    char line[100];
    printf("西暦を入力: ");
    fgets(line, sizeof(line), stdin);
    sscanf(line, "%d", &year);
    if (year % 400 == 0) {
        printf("うるう年\n");
    } else if (year % 4 == 0 && year % 100 != 0) {
        printf("うるう年\n");
    } else {
        printf("うるう年ではない\n");
    }
    return (0);
}

入出力結果(Terminal)

$ cc -g -o sample sample.c
$ ./sample
西暦を入力: 1984
うるう年
$ ./sample
西暦を入力: 2004
うるう年
$ ./sample
西暦を入力: 1800
うるう年ではない
$ ./sample
西暦を入力: 1900
うるう年ではない
$ ./sample
西暦を入力: 1600
うるう年
$ ./sample
西暦を入力: 2000
うるう年
$ ./sample
西暦を入力: 2004
うるう年
$ ./sample
西暦を入力: 2005
うるう年ではない
$

0 コメント:

コメントを投稿