平成6年度 秋期 第二種 午後 問7

                        [更新日]1994.11.01
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ 次の問7から問10までの4問については、この中から2問を選択し、答案用紙の┃ ┃選択欄の[選]を黒くマークして解答してください。              ┃ ┃ なお、2問以上選択した場合には、はじめの1問について採点します。     ┃ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
問7 次のCプログラムの説明及びプログラムを読んで、設問に答えよ。 〔プログラムの説明〕 生年月日と今日の日付を読み込み、生年月日から今日までの日数を計算して出力す るプログラムである。 (1) 標準入力から生年月日と今日の日付を入力する。生年月日、今日の日付はそれぞ れ、年、月、日の順に並んだ、空白で区切った整数の組である。     ┌────────────────────────┐     │ **  生年月日を入力してください  **  │     │ 1972 1 1               │     │ **  今日の日付を入力してください  ** │      │ 1994 10 16             │     └────────────────────────┘            図1 入力データの例 (2) 年月日は西暦とし、入力できる年月日は西暦1800年1月1日以降とする。 年月日が正しい範囲内にないときは、入力データ誤りとみなしメッセージを出力 して終了する。生年月日が今日の日付より後のときも、誤りとみなしメッセージを 出力して終了する。 (3) 生年月日から今日の日付までの日数を、次の式によって計算する。 (生年月日から今日の日付までの日数)= (西暦1年1月1日から今日の日付までの日数) −(西暦1年1月1日から生年月日までの日数) (4) 日数の計算及び入力データの誤りの検出では、うるう年を考慮する。うるう年は 次のように判定する。 ・4で割り切れかつ100で割り切れない年、及び400で割り切れる年はうるう年で ある。 (5) 生年月日から今日までの日数を標準出力へ出力する。      ┌────────────────────────┐      │ 1972年1月1日から今日までの日数は    │           │ 8324日です                │      └────────────────────────┘ 図2 結果の出力例 〔プログラム〕 #include < stdio.h > #define leap(y) ( [ a ] ) typedef struct{ int year; int month; int day; } Tdate; long calcd (Tdate *); int check (Tdate *); int table[24] = {31,28,31,30,31,30,31,31,30,31,30,31, 31,29,31,30,31,30,31,31,30,31,30,31}; main() { long total; Tdate today, birthday; printf ("** 生年月日を入力してください **\n"); scanf ("%d %d %d" ,&birthday.year,&birthday.month, &birthday.day); printf ("** 今日の日付を入力してください **\n"); scanf ("%d %d %d" , &today.year,&today.month,&today.day); if ([ b ]) if ((total = calcd(&today) - calcd(&birthday)) >= 0 ) printf(" %d年%d月%d日から今日までの日数は\n" " %ld日です\n" , birthday.year,birthday.month,birthday.day,total); else printf("** 生年月日が今日の日付より後になっています **\n"); else printf ("** 入力した年月日が正しい範囲内にありません **\n"); } /* 西暦1年1月1日からの日数を計算する */ long calcd(Tdate *dayp) { int i; int offset; long years=[ c ]; long days=dayp->day; days += years * 365; days += years / 4 - years / 100 + years / 400; /* 前年までのうるう年の数の合計を加える */ if (leap(dayp->year)) offset = 12; else offset = 0; for (i = 0;[ d ]; i++) days +=[ e ]; return days; } /* 入力データの誤りを検出する */ int check (Tdate *dayp) { int offset; int dlimit; if (leap(dayp->year)) offset = 12; else offset = 0; dlimit = table[dayp->month + offset - 1]; if (dayp->year < 1800 || dayp->month < 1 || dayp->month > 12 || dayp->day < 1 || dayp->day > dlimit) return 1; /* 年月日が正しい範囲内にないとき1を返す */ else return 0; /* 年月日が正しい範囲内のとき0を返す */ } 設問 プログラムの中の[ ]に入れる正しい答えを、解答群の中から選べ。 a に関する解答群 ア y % 4 == 0 && y % 100 != 0 || y % 400 == 0 イ y % 4 == 0 && y % 100 != 0 && y % 400 == 0 ウ y % 4 == 0 || y % 100 != 0 || y % 400 == 0 エ y % 4 != 0 || y % 100 == 0 && y % 400 != 0 b に関する解答群 ア check(birthday) == 0 && check(today) == 0 イ check(birthday) != 0 || check(today) != 0 ウ check(&birthday) == 0 && check(&today) == 0 エ check(&birthday) != 0 || check(&today) != 0 c に関する解答群 ア dayp->year + 1 イ dayp->year ウ dayp->year - 1 エ dayp->year - 4 d に関する解答群 ア i < dayp->month - 1 イ i < dayp->month ウ i <= dayp->month エ i < 12 e に関する解答群 ア table[i] イ table[i + offset - 1] ウ table[i - 1] エ table[i + offset]
戻る 次頁:問8