鱼C论坛

 找回密码
 立即注册
查看: 1018|回复: 2

[已解决]解决问题

[复制链接]
发表于 2023-12-7 17:08:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这个月份算出来是15,哪里出错了。
以下是计算月份的代码
  1. void calculate_mouth(int time_s,int *mouth)
  2. {
  3.     int mouth_all[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  4.     int day_remainder = (time_s % (3 * 365 * 86400 + 366 * 86400)) / 86400;//get the remainder day of four year
  5.     if (day_remainder >= 365 * 2 + 366)
  6.     {
  7.         mouth_all[1] = 29;
  8.     }
  9.     int day;
  10.     calculate_day(time_s,&day);
  11.     int i;
  12.     for (i = 0;i < 12;i++)
  13.     {
  14.         int n = day - mouth_all[i];
  15.         if (n > 0)
  16.         {
  17.             continue;
  18.         }
  19.         else
  20.         {
  21.             *mouth = i + 1;
  22.             break;
  23.         }
  24.     }
  25. }
复制代码

以下是完整代码
  1. #include <stdio.h>
  2. #include <time.h>
  3. void leap_years(int time_s,int *leap)
  4. {
  5.     int day_remainder = (time_s % (3 * 365 * 86400 + 366 * 86400)) / 86400;//get the remainder day of four year
  6.     if (day_remainder >= 0 && day_remainder < 365)
  7.     {
  8.         *leap = 0;
  9.     }
  10.     else if (day_remainder >= 365 && day_remainder < (365 * 2))
  11.     {
  12.         *leap = 1;
  13.     }
  14.     else if (day_remainder >= (365 * 2) && day_remainder < (365 * 2 + 366))
  15.     {
  16.         *leap = 2;
  17.     }
  18.     else
  19.     {
  20.         *leap = 3;
  21.     }   
  22. }

  23. void cal_second_remainder(int time_s,int *second_remainder)
  24. {
  25.     int leap;
  26.     leap_years(time_s,&leap);
  27.     switch(leap)
  28.     {
  29.         case 0:*second_remainder = time_s % (3 * 365 * 86400 + 366 * 86400);break;
  30.         case 1:*second_remainder = time_s % (3 * 365 * 86400 + 366 * 86400) - 365 * 86400;break;
  31.         case 2:*second_remainder = time_s % (3 * 365 * 86400 + 366 * 86400) - 365 * 2 * 86400;break;
  32.         case 3:*second_remainder = time_s % (3 * 365 * 86400 + 366 * 86400) - (365 * 2 + 366) * 86400;break;
  33.     }
  34. }

  35. void calculate_year(int time_s,int *year)
  36. {
  37.     int leap;
  38.     leap_years(time_s,&leap);
  39.     *year = 4 * (time_s / (3 * 365 * 86400 + 366 * 86400)) + 1970 + leap;
  40. }

  41. void calculate_day(int time_s,int *day)
  42. {
  43.     int second_remainder;
  44.     cal_second_remainder(time_s,&second_remainder);
  45.     *day = second_remainder / (24 * 60 * 60);
  46. }   

  47. void calculate_hour_minute(int time_s,int *hour,int *minute)
  48. {
  49.     int day,second_remainder,second;
  50.     calculate_day(time_s,&day);
  51.     cal_second_remainder(time_s,&second_remainder);
  52.     second = second_remainder - day * 24 * 60 * 60;  
  53.     *hour = second / (60 * 60);
  54.     *minute = (second % (60 * 60)) / 60;
  55. }

  56. void calculate_mouth(int time_s,int *mouth)
  57. {
  58.     int mouth_all[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  59.     int day_remainder = (time_s % (3 * 365 * 86400 + 366 * 86400)) / 86400;//get the remainder day of four year
  60.     if (day_remainder >= 365 * 2 + 366)
  61.     {
  62.         mouth_all[1] = 29;
  63.     }
  64.     int day;
  65.     calculate_day(time_s,&day);
  66.     int i;
  67.     for (i = 0;i < 12;i++)
  68.     {
  69.         int n = day - mouth_all[i];
  70.         if (n > 0)
  71.         {
  72.             continue;
  73.         }
  74.         else
  75.         {
  76.             *mouth = i + 1;
  77.             break;
  78.         }
  79.     }
  80. }

  81. int prnitf_time(int time_s,int year,int day,int hour,int minute)
  82. {
  83.     printf("senconds:%i\n",time_s);
  84.     printf("year:%i\n",year);
  85.     printf("day:%i\n",day);
  86.     printf("hour:%i\n",hour);
  87.     printf("minute:%i\n",minute);
  88. }


  89. int main()
  90. {
  91.     int time_s = time(NULL) + 8 * 60 * 60;
  92.     int year,day,hour,minute,mouth;

  93.     calculate_year(time_s,&year);
  94.     calculate_day(time_s,&day);
  95.     calculate_hour_minute(time_s,&hour,&minute);
  96.     calculate_mouth(time_s,&mouth);
  97.    
  98.     prnitf_time(time_s,year,day,hour,minute);
  99.     printf("%i",mouth);

  100.     return 0;
  101. }
复制代码
最佳答案
2023-12-7 17:09:14
在代码中,你定义了一个名为`mouth_all`的数组,并尝试将初始值赋给它。然而,你使用了错误的语法来初始化数组。正确的写法是使用大括号`{}`来赋值,而不是使用等号`=`。以下是修改后的代码:
  1. int month_all[] = {31,28,31,30,31,30,31,31,30,31,30,31};
复制代码

另外,在循环中,你需要更新`day`的值,以便计算剩余天数。你可以通过减去已经过去的月份的天数来实现。以下是修改后的`calculate_mouth`函数的完整代码:
  1. void calculate_mouth(int time_s,int *mouth)
  2. {
  3.     int month_all[] = {31,28,31,30,31,30,31,31,30,31,30,31};  // 修改了语法错误
  4.     int day_remainder = (time_s % (3 * 365 * 86400 + 366 * 86400)) / 86400; // 获取四年的余下天数
  5.     if (day_remainder >= 365 * 2 + 366)
  6.     {
  7.         month_all[1] = 29;  // 修改了变量名错误
  8.     }
  9.     int day;
  10.     calculate_day(time_s,&day);
  11.     int i;
  12.     for (i = 0; i < 12; i++)
  13.     {
  14.         day -= month_all[i]; // 更新剩余天数
  15.         if (day <= 0)  // 找到月份后立即停止循环
  16.         {
  17.             *mouth = i + 1;
  18.             break;
  19.         }
  20.     }
  21. }
复制代码

通过以上修改,你应该能够正确计算出月份。希望对你有所帮助!

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-12-7 17:09:14 | 显示全部楼层    本楼为最佳答案   
在代码中,你定义了一个名为`mouth_all`的数组,并尝试将初始值赋给它。然而,你使用了错误的语法来初始化数组。正确的写法是使用大括号`{}`来赋值,而不是使用等号`=`。以下是修改后的代码:
  1. int month_all[] = {31,28,31,30,31,30,31,31,30,31,30,31};
复制代码

另外,在循环中,你需要更新`day`的值,以便计算剩余天数。你可以通过减去已经过去的月份的天数来实现。以下是修改后的`calculate_mouth`函数的完整代码:
  1. void calculate_mouth(int time_s,int *mouth)
  2. {
  3.     int month_all[] = {31,28,31,30,31,30,31,31,30,31,30,31};  // 修改了语法错误
  4.     int day_remainder = (time_s % (3 * 365 * 86400 + 366 * 86400)) / 86400; // 获取四年的余下天数
  5.     if (day_remainder >= 365 * 2 + 366)
  6.     {
  7.         month_all[1] = 29;  // 修改了变量名错误
  8.     }
  9.     int day;
  10.     calculate_day(time_s,&day);
  11.     int i;
  12.     for (i = 0; i < 12; i++)
  13.     {
  14.         day -= month_all[i]; // 更新剩余天数
  15.         if (day <= 0)  // 找到月份后立即停止循环
  16.         {
  17.             *mouth = i + 1;
  18.             break;
  19.         }
  20.     }
  21. }
复制代码

通过以上修改,你应该能够正确计算出月份。希望对你有所帮助!

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-7 17:34:56 | 显示全部楼层
本帖最后由 jackz007 于 2023-12-7 18:02 编辑

          简单的问题竟被你搞得如此复杂,参考此贴(你自己发的求助帖):
https://fishc.com.cn/forum.php?m ... 407&pid=6406392
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-19 23:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表