自分の誕生日を入力して、その日から何日後かを入力すると日付を出力する。なお、「年、月、日」をメンバとする構造体を宣言して、その型のオブジェクトの日付dateを引数とすると1日後の日付を返す関数を作成して用いること
教えてください
以下、自分でやった途中です
#include <stdio.h>
int day[2][13] = {{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,3130,31,30,31}};
int leap(int year){
if((year%400)==0 || (year%4)==0 && (year%100)!=0){
return 1;
}
else{
return 0;
}
}
void tomorrow(int y,int m,int d){
if(d != day[leap(y)][m]){
d++;
}
else{
if(m != 12){
m++;
}
else{
y++;
m = 1;
}
d = 1;
}
}
int main(void){
int year,month,date;
printf("Enter your birthday\n");
scanf("%d %d %d",&year,&month,&date);
printf("誕生日は %d年%d月%d日 です\n\n",year,month,date);
tomorrow(year,month,date);
printf("Tomorrow is %d年%d月%d日\n",year,month,date);
return 0;
}
A 回答 (5件)
- 最新から表示
- 回答順に表示
No.5
- 回答日時:
No.1、2、3です。
手抜きな回答で済みません。
質問者さんの認識で合っているはずです。
それで、値が同じになってしまう問題は解決すると思います。
このやり方は参照渡しではなく、ポインタ渡しでしたね…。
ポインタの記号の意味についてはこちらを参照してみてください。
https://www.sgnet.co.jp/technology/c/6-3/
No.4
- 回答日時:
> 自分の誕生日を入力して、その日から何日後かを入力すると日付を出力する。
なお、「年、月、日」をメンバとする構造体を宣言して、その型のオブジェクトの日付dateを引数とすると1日後の日付を返す関数を作成して用いること何か問題が良く分からんな・・・。
いずれにせよ、構造体使え、って言われてるんだから構造体使わなアカンと思うんだけどね。
ありきたりだけど、
typedef struct {
int year;
int month;
int day;
} ymd_t;
を定義する事からはじめる。
そしてここでユーザー定義型を作った以上、関数tomorrowは例えば
ymd_t tomorrow(ymd_t date)
みたいにユーザー定義した型(構造体)を受け取るようにすべきじゃないかしらん。
コード例は例えば次のような感じ。
/* ここから */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int year;
int month;
int day;
} ymd_t;
int* is_leap(int year) {
static int months[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (year % 4 == 0 || (year % 4 == 0 && year % 100 != 0)) {
months[2] = 29;
return months;
} else {
months[2] = 28;
return months;
}
}
ymd_t tomorrow(ymd_t date) {
ymd_t t = date;
int* months = is_leap(date.year);
if (date.day == months[date.month]) {
if (date.month == 12) {
t.year += 1;
t.month = 1;
} else {
t.month++;
}
t.day = 1;
} else {
t.day++;
}
return t;
}
int main(void) {
ymd_t birth, the_date;
printf("Enter your birthday : ");
char str[11];
scanf("%10[^\n]%*c", str);
birth.year = strtol(strtok(str, " "), NULL, 10);
birth.month = strtol(strtok(NULL, " "), NULL, 10);
birth.day = strtol(strtok(NULL, " "), NULL, 10);
printf("誕生日は%d年%d月%d日です\n", birth.year, birth.month, birth.day);
scanf("%14s%*[^\n]%*c", str);
the_date = birth;
for (int i = strtol(str, NULL, 10); i != 0; i--) {
the_date = tomorrow(the_date);
}
printf("The day is %d年%d月%d日\n", the_date.year, the_date.month, the_date.day);
return EXIT_SUCCESS;
}
/* ここまで */
No.3
- 回答日時:
済みません、よく見ていませんでした。
tomorrow関数が参照渡しになっていませんでしたね…。tomorrow内で年月日を操作していますが、これはtomorrow内のローカル変数を弄っているだけです。
呼び元の引数の前に&、tomorrow本体の引数に*をつけてみて下さい。
No.1
- 回答日時:
万年暦っぽい処理も使って1日後を出力するまではできているような気がするので…。
あと、指定日数分、tomorrowを呼べば良いと思います。
int iDaysLater
scanf("%d",&iDaysLater);
for( i; iDaysLater>=i; i++)
{
tomorrow(year,month,date);
}
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・人生のプチ美学を教えてください!!
- ・10秒目をつむったら…
- ・あなたの習慣について教えてください!!
- ・牛、豚、鶏、どれか一つ食べられなくなるとしたら?
- ・【大喜利】【投稿~9/18】 おとぎ話『桃太郎』の知られざるエピソード
- ・街中で見かけて「グッときた人」の思い出
- ・「一気に最後まで読んだ」本、教えて下さい!
- ・幼稚園時代「何組」でしたか?
- ・激凹みから立ち直る方法
- ・1つだけ過去を変えられるとしたら?
- ・【あるあるbot連動企画】あるあるbotに投稿したけど採用されなかったあるある募集
- ・【あるあるbot連動企画】フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
- ・映画のエンドロール観る派?観ない派?
- ・海外旅行から帰ってきたら、まず何を食べる?
- ・誕生日にもらった意外なもの
- ・天使と悪魔選手権
- ・ちょっと先の未来クイズ第2問
- ・【大喜利】【投稿~9/7】 ロボットの住む世界で流行ってる罰ゲームとは?
- ・推しミネラルウォーターはありますか?
- ・都道府県穴埋めゲーム
- ・この人頭いいなと思ったエピソード
- ・準・究極の選択
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
「指定されたキャストは有効で...
-
C言語での引数の省略方法
-
複数桁10進数の*桁目だけを抽出...
-
#define _CRT_SECURE_NO_WARNIN...
-
実数の整数部,小数部の取得
-
std::set<int> で、ある値が何...
-
return 1L
-
c言語の配列を使ってサイコロを...
-
Win32APIで作るコンボボックス...
-
構造体の勉強中です 合計点の高...
-
C言語で三目並べをするプログラ...
-
(int *)の意味
-
【C++】関数ポインタの使い方
-
int型の変数値をバイト列として...
-
HANDLEて何ですか?
-
if と配列の組み合わせ
-
C2601 ローカル関数の定義について
-
PowerShellがうまくいかない
-
構造体にたいして、ビット演算...
-
メルセンヌ・ツイスタの乱数生...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
「指定されたキャストは有効で...
-
C言語での引数の省略方法
-
#define _CRT_SECURE_NO_WARNIN...
-
AtCoderABC135の問題Cについて
-
C言語 エラーの原因がわからな...
-
複数桁10進数の*桁目だけを抽出...
-
【C++】関数ポインタの使い方
-
実数の整数部,小数部の取得
-
ラップ関数とはどんなものですか?
-
if と配列の組み合わせ
-
return 1L
-
read関数をノンブロッキングで...
-
(int *)の意味
-
std::set<int> で、ある値が何...
-
Win32APIで作るコンボボックス...
-
C++でvectorにテキストファイル...
-
「{ } で囲むだけ」は正しい?
-
足して100になるような乱数のア...
-
Arduinoのプログラムにエラーが...
-
課題でつまってます・・・
おすすめ情報
そもそも上のコードで次の日が入力したものと同じものが出力されるのですがなぜでしょうか
tomorrow()内のすべての y,m,d の前に*をつけてmain()のところを
tomorrow(&year,&month,&date);
にすれば良いでしょうか
ポインタの使い方がわかっていなくてすいません