アプリ版:「スタンプのみでお礼する」機能のリリースについて

自分の誕生日を入力して、その日から何日後かを入力すると日付を出力する。なお、「年、月、日」をメンバとする構造体を宣言して、その型のオブジェクトの日付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;

}

質問者からの補足コメント

  • そもそも上のコードで次の日が入力したものと同じものが出力されるのですがなぜでしょうか

    No.1の回答に寄せられた補足コメントです。 補足日時:2021/05/06 17:52
  • tomorrow()内のすべての y,m,d の前に*をつけてmain()のところを
    tomorrow(&year,&month,&date);
    にすれば良いでしょうか

    ポインタの使い方がわかっていなくてすいません

    No.3の回答に寄せられた補足コメントです。 補足日時:2021/05/12 21:03

A 回答 (5件)

No.1、2、3です。



手抜きな回答で済みません。
質問者さんの認識で合っているはずです。

それで、値が同じになってしまう問題は解決すると思います。
このやり方は参照渡しではなく、ポインタ渡しでしたね…。

ポインタの記号の意味についてはこちらを参照してみてください。
https://www.sgnet.co.jp/technology/c/6-3/
    • good
    • 0

> 自分の誕生日を入力して、その日から何日後かを入力すると日付を出力する。

なお、「年、月、日」をメンバとする構造体を宣言して、その型のオブジェクトの日付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;
}

/* ここまで */
    • good
    • 0

済みません、よく見ていませんでした。

tomorrow関数が参照渡しになっていませんでしたね…。

tomorrow内で年月日を操作していますが、これはtomorrow内のローカル変数を弄っているだけです。

呼び元の引数の前に&、tomorrow本体の引数に*をつけてみて下さい。
この回答への補足あり
    • good
    • 0

すみません。


for文のiを初期化してませんでした。

i=1で初期化してください。
    • good
    • 0

万年暦っぽい処理も使って1日後を出力するまではできているような気がするので…。


あと、指定日数分、tomorrowを呼べば良いと思います。

int iDaysLater
scanf("%d",&iDaysLater);

for( i; iDaysLater>=i; i++)
{
tomorrow(year,month,date);
}
この回答への補足あり
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!