计算 2 个给定日期之间天数的函数出现意外输出

问题描述 投票:0回答:1
//A function that counts days from a given date to a given date
int count_days_from(int start_day, int start_month, int day, int month, int year){
    
    //DECLARATION AND INITIALIZATION SEGMENT
    int month_days[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int days = 0;

    //CALCULATIONS
    for(int i = start_month; i < month; i++)
        days = days + month_days[i];
    days = days + (month_days[month] - day);  
    
    
    return days;
}

你好。这是我用 C 编写的代码,用于计算 2 个用户给出的日期之间的天数。

我尝试使用输入来测试它

printf("%d\n", count_days_from(1, 1, 31, 12, 2024);

虽然有意想不到的输出:

1915424856

有什么问题吗?

function output
1个回答
0
投票

欢迎! 我更喜欢您计算该月的天数,然后计算两个日期之间的天数。

以下代码显示了获取两个日期之间的天数的最佳方法:

#include <stdio.h>

int getDaysInMonth(int year, int month) {
  if (month < 1 || month > 12) {
    printf("Invalid month: %d\n", month);
    return -1; // Indicate error
  }

  // Months with 31 days
  if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
    return 31;
  }

  // February: check for leap year
  if (month == 2) {
    if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
      return 29;
    } else {
      return 28;
    }
  }

  // Months with 30 days (April, June, September, November)
  return 30;
}

bool isLeapYear(int year) {
  return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}

int countDays(int startDate, int startMonth, int endDate, int endMonth, int year) {
  if (year < 1 || startMonth < 1 || startMonth > 12 || endMonth < 1 || endMonth > 12) {
    printf("Invalid year or month values\n");
    return -1; // Indicate error
  }

  if (startDate < 1 || startDate > getDaysInMonth(year, startMonth)) {
    printf("Invalid start day for month %d\n", startMonth);
    return -1; // Indicate error
  }

  if (endDate < 1 || endDate > getDaysInMonth(year, endMonth)) {
    printf("Invalid end day for month %d\n", endMonth);
    return -1; // Indicate error
  }

  int totalDays = 0;

  // Handle cases where start and end month are the same
  if (startMonth == endMonth) {
    totalDays = endDate - startDate + 1;
  } else {
    // Add days from start month
    totalDays += getDaysInMonth(year, startMonth) - startDate + 1;

    // Add days from all months between start and end month (excluding start and end month)
    for (int month = startMonth + 1; month < endMonth; month++) {
      totalDays += getDaysInMonth(year, month);
    }

    // Add days from end month
    totalDays += endDate;
  }

  return totalDays;
}

int main() {
  // Example usage: Calculate days between January 1st and December 31st, 2024 (should be 366)
  int totalDays = countDays(1, 1, 31, 12, 2024);
  printf("Total days between January 1st and December 31st, 2024: %d\n", totalDays);

  // You can call this function with different start and end dates within the same year
  int daysBetween = countDays(10, 5, 20, 8, 2024); // Should be 112
  printf("Total days between May 10th and August 20th, 2024: %d\n", daysBetween);

  return 0;
}

© www.soinside.com 2019 - 2024. All rights reserved.