![](http://oshiete.xgoo.jp/images/v2/pc/qa/question_title.png?5a7ff87)
以下はcalendarを出力するものです。
commandlineで入力します。
出力するとき(例)
./a.exe → 現在のカレンダー出力させる
./a.exe 2015 → 2015年のすべて表示
./a.exe 2015 10 → 2015年10月の表示
上の二つを追加したいです。一番下のは何となくできました。
全て表示は月(12回)をループするのはわかりますが、どこに入れればよいかわかりません。
現在出力はlocaltimeを使うと思うのですが、どのように入れたらよいか、また変換をしなければいけませんので、このプログラムをどう変えれば良いでしょうか?
やるなら、全表示から行うのが楽ですかね?
ご指摘お願いします。
#include <stdio.h>
int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
void calender(int year2, int month2)
{
int days, i;
int wday, year,month,d,a;
year = year2;
month = month2;
wday = year + year / 4 - year / 100 + year / 400 + (26 * month + 16) / 10 + 1;
wday = wday % 7; //チェラーの法則
if (year <= 2) //曜日の計算をする
{
month += 12;
year--;
}
printf(" ---- %d年%d月 ----\n", year2, month2);
printf(" 日 月 火 水 木 金 土\n");
days = mdays[month2 - 1]; //日数を計算する
if (year2 % 4 == 0 && !(year2 % 100 == 0 && year2 % 400 != 0))
{
if (month == 2) // うるう年の処理
{
days++;
}
}
for(i = 0; i < wday; i++) //空白を生成
{
printf(" ");
}
for(d = 1; d <= days; d++) //最終日まで表示
{
printf(" %2d", d);
wday++;
if (wday == 7) //日曜日で改行
{
wday = 0;
printf("\n");
}
}
if (wday != 0)
{
printf("\n");
}
}
int main(int argc, char *argv[])
{
int year2, month2;
if (argc==2)
{
year2=atoi(argv[1]);
//printf("%d年\n", year2);
}
else if (argc==3)
{
year2=atoi(argv[1]);
month2=atoi(argv[2]);
//printf("%d年%d月\n", year2, month2);
}
else
{
printf("エラー\n");
return 0;
}
calender(year2, month2);
return 0;
}
No.5
- 回答日時:
遅くなりました・・・。
#include <stdio.h>
#include <time.h>
#include <locale.h>
#include <stdlib.h>
int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
void calender(int year2, int month2)
{
int days, i;
int wday, d;
wday = year2 + year2 / 4 - year2 / 100 + year2 / 400 + (26 * month2 + 16) / 10 + 1;
wday = wday % 7; //チェラーの法則
printf(" ---- %d年%d月 ----\n", year2, month2);
printf(" 日 月 火 水 木 金 土\n");
days = mdays[month2 - 1]; //日数を計算する
// うるう年の処理
if (month2 == 2)
{
if (year2 % 4 == 0 && !(year2 % 100 == 0 && year2 % 400 != 0)) days++;
}
//空白を生成
for(i = 0; i < wday; i++) printf(" ");
//最終日まで表示
for(d = 1; d <= days; d++)
{
printf(" %2d", d);
wday++;
if (wday == 7) //日曜日で改行
{
wday = 0;
printf("\n");
}
}
if (wday != 0) printf("\n");
}
int main(int argc, char *argv[])
{
int year2 = 0, month2 = 0;
time_t timep;
struct tm *time_inf;
char buff[100];
if(argc == 3) {
month2=atoi(argv[2]);
year2=atoi(argv[1]);
} else if(argc == 2) {
year2=atoi(argv[1]);
} else {
timep = time(NULL);
time_inf = localtime(&timep);
strftime(buff, sizeof(buff), "%Y", time_inf);
year2=atoi(buff);
strftime(buff, sizeof(buff), "%m", time_inf);
month2=atoi(buff);
}
if( month2 == 0 ) {
for(month2 = 1; month2 <= 12; month2++ ) {
calender(year2, month2);
}
} else {
calender(year2, month2);
}
return 0;
}
ありがとうございます。
エラー判定はmain内で行えば大丈夫ですよね?
あと、strftimeをメンバーにアクセスとはなんですかね?
No.4
- 回答日時:
gao57830 の提示されたコードには以下の問題があります。
(1) strftime() の呼び出しで、 " でくくって文字列リテラルにすべきところで ' を使っている
(2) 教えて!goo の文章入力の仕様のためインデントを全角空白で行っているので、コピペ後にタブ等に置換する必要がある
上記を修正をすればたぶん画像のエラーは消えると思います。
time_inf = localtime(&timep);
strftime(buff, sizeof(buff), '%Y', time_inf);
でエラーがでます。
ポインタ型と整数の比較
strftimeが互換性がない沈黙的なもの
No.3
- 回答日時:
頭の中だけでコーディングしています。
どこで、どんなエラーが出てますか?
No.2
- 回答日時:
if(argc == 3) {
month2=atoi(argv[2]);
year2=atoi(argv[1]);
} else if(argc == 2) {
year2=atoi(argv[1]);
} else {
timep = time(NULL);
time_inf = localtime(&timep);
strftime(buff, sizeof(buff), '%Y', time_inf);
year2=atoi(buff);
strftime(buff, sizeof(buff), '%m', time_inf);
month2=atoi(buff);
}
No.1
- 回答日時:
#include <stdio.h>
#include <time.h>
#include <locale.h>
#include <stdlib.h>
int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
void calender(int year2, int month2)
{
int days, i;
int wday, d;
wday = year2 + year2 / 4 - year2 / 100 + year2 / 400 + (26 * month2 + 16) / 10 + 1;
wday = wday % 7; //チェラーの法則
printf(" ---- %d年%d月 ----\n", year2, month2);
printf(" 日 月 火 水 木 金 土\n");
days = mdays[month2 - 1]; //日数を計算する
// うるう年の処理
if (month2 == 2)
{
if (year2 % 4 == 0 && !(year2 % 100 == 0 && year2 % 400 != 0)) days++;
}
//空白を生成
for(i = 0; i < wday; i++) printf(" ");
//最終日まで表示
for(d = 1; d <= days; d++)
{
printf(" %2d", d);
wday++;
if (wday == 7) //日曜日で改行
{
wday = 0;
printf("\n");
}
}
if (wday != 0) printf("\n");
}
int main(int argc, char *argv[])
{
int year2 = 0, month2 = 0;
time_t timep;
struct tm *time_inf;
char buff[100];
switch(argc) {
case 3:
month2=atoi(argv[2]);
case 2:
year2=atoi(argv[1]);
break;
default:
timep = time(NULL);
time_inf = localtime(&timep);
strftime(buff, sizeof(buff), '%Y', time_inf);
year2=atoi(buff);
strftime(buff, sizeof(buff), '%m', time_inf);
month2=atoi(buff);
}
if( month2 == 0 ) {
for(month2 = 1; month2 <= 12; month2++ ) {
calender(year2, month2);
}
} else {
calender(year2, month2);
}
return 0;
}
ありがとうございます。
main内の中がいまいち理解出来ないです。
あと、ブレイクを使わないようにするにはどうしたらよいですか?
よろしくお願い致します!
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- PHP PHPの構文で間違えが分からない 5 2022/07/11 16:38
- PHP アコーディオンPHPが上手くいかない 3 2022/07/15 16:29
- C言語・C++・C# c言語の問題の説明、各所ごとに 5 2023/07/26 11:03
- C言語・C++・C# バイナリファイルをコピーするのにかかる時間を測りたいのですが実行するとFatel error:gli 2 2022/11/03 01:10
- C言語・C++・C# c言語 プログラムのエラー 1 2023/02/11 20:31
- PHP SQLとPHPの連結方法がわからないのでアドバイスお願い致します 1 2022/07/12 12:16
- Visual Basic(VBA) ①ExcelVBAでカレンダーを作り、別のユザーフォームで日付を入力したいのですがエラーになります。 1 2023/02/17 18:39
- WordPress(ワードプレス) wordpressでphpを読み込みたい 1 2022/10/30 23:40
- PHP style.cssのjQuery条件付きcssが機能しない 4 2022/07/17 18:27
- C言語・C++・C# c言語の問題です 3 2023/01/10 16:15
関連するカテゴリからQ&Aを探す
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
2つの日付・時間の差分を求め...
-
1桁の日付に0を付ける
-
先月(YYYYMM)の生成方法
-
JSONで文字列が長い時
-
Application.ScreenUpdating = ...
-
文字の横にプルダウンを表示さ...
-
セレクトメニューで2つの項目...
-
VBA他のブックから値のみ貼付す...
-
formで特定のinputを送信しない...
-
16進の10進変換について
-
<SELECT>タグの折り返し
-
【至急!!!】python言語で本を見...
-
関数「exists」と「defined」の...
-
pythonで演算子を変数に代入す...
-
Net::Telnetでタイムアウト時間...
-
Python - Excel で Webからデー...
-
テキストエリアの改行を取得したい
-
[python] 文字列を変数名として...
-
Net::FTP使用時の取得値をどう...
-
vbaでxmlからNodeListでデータ...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
日付の差分の求め方(日、分)
-
1桁の日付に0を付ける
-
Perlでsprintf("%02d",$month)...
-
2つの日付・時間の差分を求め...
-
日齢計算プログラム
-
a shower.
-
Perlの日付の比較に関して
-
前日の日付取得するには?
-
サマータイムの判定について
-
pythonのプログラミングについ...
-
時間の計算について
-
VBAでオーバーフローが出て困っ...
-
当日の日付をプルダウンメニュ...
-
「型が一致しません」のエラー...
-
perlでcheckboxがうまく整理で...
-
Schemeのプログラミング うる...
-
perl ハノイの塔の解に順番付け...
-
Perlでテキストファイルの読み込み
-
このソースがコンパイルエラー...
-
PerlからMysqlに接続してデータ...
おすすめ情報
#プログラム
#c言語
エラーでます。