お願いします。
Posted: 2015年12月08日(火) 20:53
s.c: 関数 ‘main’ 内:
s.c:62:32: 警告: 複数文字からなる文字定数 [-Wmultichar]
strftime(buff, sizeof(buff), '%Y', time_inf);
^
s.c:62:3: エラー: プログラム内に逸脱した ‘\357’ があります
strftime(buff, sizeof(buff), '%Y', time_inf);
^
s.c:62:3: エラー: プログラム内に逸脱した ‘\274’ があります
s.c:62:3: エラー: プログラム内に逸脱した ‘\214’ があります
s.c:62:40: エラー: expected ‘)’ before ‘time_inf’
strftime(buff, sizeof(buff), '%Y', time_inf);
^
s.c:62:32: 警告: 3 番目の ‘strftime’ の引数へ渡すときに整数からキャスト無しにポインタを作成しています
strftime(buff, sizeof(buff), '%Y', time_inf);
^
In file included from /usr/include/stdio.h:29:0,
from s.c:1:
/usr/include/time.h:61:11: 備考: expected ‘const char *’ but argument is of type ‘int’
size_t _EXFUN(strftime, (char *__restrict _s,
^
s.c:62:3: エラー: 関数 ‘strftime’ へ渡す引数が少なすぎます
strftime(buff, sizeof(buff), '%Y', time_inf);
以下はcalendarを出力するものです。
commandlineで入力します。
出力するとき(例)
./a.exe → 現在のcalendaを出力させる
./a.exe 2015 → 2015年のすべて表示
./a.exe 2015 10 → 2015年10月の表示
上の二つを追加したいです。一番下のは何となくできました。
全て表示は月(12回)をループするのはわかりますが、どこに入れればよいかわかりません。
現在出力はlocaltimeを使うと思うのですが、どのように入れたらよいか、また変換をしなければいけませんので、このプログラムをどう変えれば良いでしょうか?
やるなら、全表示から行うのが楽ですかね?
ご指摘お願いします。
指摘されたプログラム
s.c:62:32: 警告: 複数文字からなる文字定数 [-Wmultichar]
strftime(buff, sizeof(buff), '%Y', time_inf);
^
s.c:62:3: エラー: プログラム内に逸脱した ‘\357’ があります
strftime(buff, sizeof(buff), '%Y', time_inf);
^
s.c:62:3: エラー: プログラム内に逸脱した ‘\274’ があります
s.c:62:3: エラー: プログラム内に逸脱した ‘\214’ があります
s.c:62:40: エラー: expected ‘)’ before ‘time_inf’
strftime(buff, sizeof(buff), '%Y', time_inf);
^
s.c:62:32: 警告: 3 番目の ‘strftime’ の引数へ渡すときに整数からキャスト無しにポインタを作成しています
strftime(buff, sizeof(buff), '%Y', time_inf);
^
In file included from /usr/include/stdio.h:29:0,
from s.c:1:
/usr/include/time.h:61:11: 備考: expected ‘const char *’ but argument is of type ‘int’
size_t _EXFUN(strftime, (char *__restrict _s,
^
s.c:62:3: エラー: 関数 ‘strftime’ へ渡す引数が少なすぎます
strftime(buff, sizeof(buff), '%Y', time_inf);
以下はcalendarを出力するものです。
commandlineで入力します。
出力するとき(例)
./a.exe → 現在のcalendaを出力させる
./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;
}
指摘されたプログラム
#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;
}