留年がかかっている課題】入力した年と月をもとにカレンダー表示

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
yamachan

留年がかかっている課題】入力した年と月をもとにカレンダー表示

#1

投稿記事 by yamachan » 3年前

本関数を完成させよ。という最初の3つはできました。
でも、未完成部分というところが調べてもなかなか出てこなくてわからないです。

ー問題ー
年月を入力すると、その月のカレンダーを表示するプログラムを作成する。以下に示すプログラム
の未完成の関数(ch_input, calc_elapsed_days, get_number_of_days),および,main 関数の一部を
作成し、プログラムを完成させよ。ただし,入力する年月は 1900 年 1 月以降とする。(なお、1900 年
1 月 1 日は月曜日である。) また、1900 年より前、もしくは、1 月から 12 月の範囲外の入力があった
場合、エラーメッセージを出力する。

プログラム
#include <iostream>
#include <iomanip>
#include <vector>
using std::cout, std::cin, std::vector;
using std::endl, std::setw;
/*
書式: bool ch_input(int year, int month)
説明: 引数として入力した年(year)が 1900 年以降、月(month)が、1 月から 12 月の範囲であ
るかをチェックする
戻り値: 正しければ true,不正であれば false を返す。
*/
bool ch_input(int year, int month)
{
// 本関数を完成させよ。
}
/*
書式:int calc_elapsed_days(int year)
説明:1900 年 1 月 1 日から引数とした年(year)の 1 月 1 日までの経過日数を計算
戻り値:経過日数
*/
int calc_elapsed_days(int year)
{
// 本関数を完成させよ。
}
/*
書式: int get_number_of_days(int year, int month)
説明: 年(year)と月(month)から、その月の日数を得る。
戻り値: 日数(例:year が 2020, month が 12 であれば、31 を返す。)
*/
int get_number_of_days(int year, int month)
{
// 本関数を完成させよ。
}
/*
書式: int get_first_week(int year, int month)
説明: 引数として入力した年(year)、月(month)から、その月の最初の曜日を得る
戻り値: 曜日に対応した数を返す。曜日と数の対応は以下のとおり
0->日 1->月 2->火 3->水 4->木 5->金 6->土
例えば、日曜日なら 0 を返す。
*/
int get_first_week(int year,int month)
{
// 1900 年 1 月 1 日から year 年 1 月 1 日までの経過日の計算
int total = calc_elapsed_days(year);

// year 年 1 月 1 日から year 年 month 月 1 日までの経過日の計算
for(int m = 1; m < month; m++) {
total += get_number_of_days(year,m);
}
return (total+1) % 7;
}
int main()
{
int year,month;

// 年月の入力
cout << "年月を入力して下さい。 \n";
cout << "年 : ";
cin >> year;
cout << "月 : ";
cin >> month;

// 入力チェック
if( ch_input(year,month) )
{
// 入力した年、月の最初の曜日を得る
int fw = get_first_week(year,month);
// 入力した月の日数
int md = get_number_of_days(year,month);

const int n{6}, m{7};
vector< vector<int> > v_days(n, vector<int>(m));
// 以下、プログラムを作成せよ

ーーーーーーー未完成部分ーーーーーーー

// カレンダーを表示する
cout << year << "年" << month << "月" << endl;
cout << " 日 月 火 水 木 金 土" << endl;
cout << " --------------------" << endl;
未完成部分
for (int i=0; i < n; i++) {
for (int j=0; j < m; j++) {
if(v_days[j] != 0) {
cout << setw(3) << v_days[j];
}
else {
cout << setw(3) << " ";
}
}
cout << endl;
}

}
else
{
cout << "不正な入力です。\n";
}
return 0;
}

□ 実行例1
年月を入力して下さい。
年 : 2020
月 : 12
2020 年 12 月
日 月 火 水 木 金 土
--------------------
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31


□ 実行例2
年月を入力して下さい。
年 : 2020
月 : 2
2020 年 2 月
日 月 火 水 木 金 土
--------------------
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29


□ 実行例3
年月を入力して下さい。
年 : 1888
月 : 12
不正な入力です。


□ 実行例4
年月を入力して下さい。
年 : 2020
月 : 13
不正な入力です。

補足
1. main関数の未完成部分は,その下にある「カレンダーを表示する」部分を参考に,変数fw と 変
数 md の値を使用して2次元配列 v_days にカレンダーの日を格納し,日を表示しない部分は
0 を入れて下さい.例えば「実行例1」の 2020 年 12 月の場合,v_days には以下のようにデータが
格納されています.

{ { 0, 0, 1, 2, 3, 4, 5 },
{ 6, 7, 8, 9, 10, 11, 12 },
{ 13, 14, 15, 16, 17, 18, 19 },
{ 20, 21, 22, 23, 24, 25, 26 },
{ 27, 28, 29, 30, 31, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 } }

2. calc_elapsed_days 関数と get_number_of_days 関数では閏年(1年は366日,2月は29日)に注
意して下さい。なお,閏年の条件は以下になります.
・ 西暦年が 4 で割り切れる年は閏年
・ ただし,西暦年が 100 で割り切れる年は閏年ではない
・ ただし、西暦年が 400 で割り切れる年は閏年
3. get_number_of_days 関数では、2 月は 28 日(閏年ならば 29 日)、4 月、 6 月、9 月、11 月は 30
日、それ以外は 31 日です。

アバター
あたっしゅ
記事: 663
登録日時: 13年前
住所: 東京23区
連絡を取る:

Re: 留年がかかっている課題】入力した年と月をもとにカレンダー表示

#2

投稿記事 by あたっしゅ » 3年前

とりあえず、見やすくしてみた。

コード:

#include <iostream>
#include <iomanip>
#include <vector>

using std::cout, std::cin, std::vector;
using std::endl, std::setw;
/*
書式: bool ch_input(int year, int month)
説明: 引数として入力した年(year)が 1900 年以降、月(month)が、1 月から 12 月の範囲であ
るかをチェックする
戻り値: 正しければ true,不正であれば false を返す。
*/
bool ch_input(int year, int month)
{
// 本関数を完成させよ。
}
/*
書式:int calc_elapsed_days(int year)
説明:1900 年 1 月 1 日から引数とした年(year)の 1 月 1 日までの経過日数を計算
戻り値:経過日数
*/
int calc_elapsed_days(int year)
{
// 本関数を完成させよ。
}
/*
書式: int get_number_of_days(int year, int month)
説明: 年(year)と月(month)から、その月の日数を得る。
戻り値: 日数(例:year が 2020, month が 12 であれば、31 を返す。)
*/
int get_number_of_days(int year, int month)
{
// 本関数を完成させよ。
}
/*
書式: int get_first_week(int year, int month)
説明: 引数として入力した年(year)、月(month)から、その月の最初の曜日を得る
戻り値: 曜日に対応した数を返す。曜日と数の対応は以下のとおり
0->日 1->月 2->火 3->水 4->木 5->金 6->土
例えば、日曜日なら 0 を返す。
*/
int 
get_first_week(int year,int month)
{
    // 1900 年 1 月 1 日から year 年 1 月 1 日までの経過日の計算
    int total = calc_elapsed_days(year);

    // year 年 1 月 1 日から year 年 month 月 1 日までの経過日の計算
    for(int m = 1; m < month; m++) {
        total += get_number_of_days(year,m);
    }
    return (total+1) % 7;
}


int 
main()
{
    int year,month;

    // 年月の入力
    cout << "年月を入力して下さい。 \n";
    cout << "年 : ";
    cin >> year;
    cout << "月 : ";
    cin >> month;

    // 入力チェック
    if( ch_input(year,month) )
    {
        // 入力した年、月の最初の曜日を得る
        int fw = get_first_week(year,month);
        // 入力した月の日数
        int md = get_number_of_days(year,month);

        const int n{6}, m{7};
        vector< vector<int> > v_days(n, vector<int>(m));
        // 以下、プログラムを作成せよ

        //ーーーーーーー未完成部分ーーーーーーー

        // カレンダーを表示する
        cout << year << "年" << month << "月" << endl;
        cout << " 日 月 火 水 木 金 土" << endl;
        cout << " --------------------" << endl;

        //未完成部分

        for (int i=0; i < n; i++) {
            for (int j=0; j < m; j++) {
                if(v_days[j] != 0) {
                    cout << setw(3) << v_days[j];
                }
                else {
                    cout << setw(3) << " ";
                }
            }
            cout << endl;
        }
    }
    else
    {
         cout << "不正な入力です。\n";
    }

    return 0;
}


// end.
問題を up しただけか ? 留年すればいいだろ。

https://torisky.com/c%E8%A8%80%E8%AA%9E ... %E3%82%8B/
C言語でカレンダーを表示する | 人生は読めないブログ(ja)
VTuber:
東上☆海美☆(とうじょう・うみみ)
http://atassyu.php.xdomain.jp/vtuber/index.html
レスがついていないものを優先して、レスするみみ。時々、見当外れなレスしみみ。

中の人:
手提鞄あたッしュ、[MrAtassyu] 手提鞄屋魚有店
http://ameblo.jp/mratassyu/
Pixiv: 666303
Windows, Mac, Linux, Haiku, Raspbery Pi, Jetson Nano, 電子ブロック 持ち。

アバター
usao
記事: 1887
登録日時: 11年前

Re: 留年がかかっている課題】入力した年と月をもとにカレンダー表示

#3

投稿記事 by usao » 3年前

teratailで同じのを見た.マルチポスト.
同じ課題を課せられた奴に飯でもおごって,パクらせてもらえばよかろうに.

返信

“C言語何でも質問掲示板” へ戻る