Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从C中的开始日期和天数计算结束日期_C_Date_Console Application - Fatal编程技术网

从C中的开始日期和天数计算结束日期

从C中的开始日期和天数计算结束日期,c,date,console-application,C,Date,Console Application,在过去的几个小时里,我一直在想如何用C编写一个程序,根据开始日期和天数计算结束日期。(我还没有找到解决这个问题的论坛)。 假设您输入2021年1月27日作为开始日期,然后输入380天。该计划现在应计算并向您显示2022年11月2日的结束日期。 我不知道如何前进,如果能得到帮助,我将不胜感激 #include <stdio.h> #include <stdlib.h> int day, month, year, numberDays; int leapYear(int

在过去的几个小时里,我一直在想如何用C编写一个程序,根据开始日期和天数计算结束日期。(我还没有找到解决这个问题的论坛)。 假设您输入2021年1月27日作为开始日期,然后输入380天。该计划现在应计算并向您显示2022年11月2日的结束日期。 我不知道如何前进,如果能得到帮助,我将不胜感激

#include <stdio.h>
#include <stdlib.h>

int day, month, year, numberDays;

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

int monthYear[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main(void) {

    printf("Enter starting date: ");
    scanf("%d %d %d", &day, &month, &year);

    printf("Enter number of days: ");
    scanf("%d", &numberDays);

    leapYear(year);

    int wholeYears, rest;
    if (leapYear(year)) {
        wholeYears = numberDays / 366;
        rest = numberDays % 366;
    }
    else {
        wholeYears = numberDays / 365;
        rest = numberDays % 365;
    }
    int resultYears = year + wholeYears;
    int midDays = day + rest;
    int resultMonths;

    return 0;
}
#包括
#包括
整数天、月、年、数天;
整数年(整数年){
回报率(第%4年==0&(第%100年!=0 | |第%400年==0));
}
int monthYear[12]={31,28,31,30,31,31,31,30,31,31};
内部主(空){
printf(“输入开始日期:”);
scanf(“%d%d%d”、&day、&month、&year);
printf(“输入天数:”);
scanf(“%d”、&numberDays);
年份(年);;
全耳休息;
如果(年){
Wholeyear=天数/366;
剩余=天数%366;
}
否则{
Wholeyear=天数/365;
剩余=天数%365;
}
int resultYears=年+全年;
正午=白天+休息;
int-resultMonths;
返回0;
}
我不能再往前走了。我需要帮助

假设您输入2021年1月27日作为开始日期,然后输入380天。该计划现在应计算并向您显示2022年11月2日的结束日期

一种简单的方法是使用
mktime()
将日期带入其标准范围

#include <stdio.h>
#include <time.h>

int main(void) {
  struct tm start = {.tm_year = 2021 - 1900, .tm_mon = 1 - 1, .tm_mday = 27,
      .tm_hour = 12};  // Midday to avoid DST issues.
  start.tm_mday += 380;
  printf("%d/%d/%d\n", start.tm_mday, start.tm_mon + 1, start.tm_year + 1900);
  time_t t = mktime(&start);
  printf("%d/%d/%d %s\n", start.tm_mday, start.tm_mon + 1, start.tm_year + 1900, 
      t == -1 ? "failed" : "OK");
}

否则,使用离散代码时,将有效地重新写入
mktime()
的年/月/日部分

假设您输入2021年1月27日作为开始日期,然后输入380天。该计划现在应计算并向您显示2022年11月2日的结束日期

一种简单的方法是使用
mktime()
将日期带入其标准范围

#include <stdio.h>
#include <time.h>

int main(void) {
  struct tm start = {.tm_year = 2021 - 1900, .tm_mon = 1 - 1, .tm_mday = 27,
      .tm_hour = 12};  // Midday to avoid DST issues.
  start.tm_mday += 380;
  printf("%d/%d/%d\n", start.tm_mday, start.tm_mon + 1, start.tm_year + 1900);
  time_t t = mktime(&start);
  printf("%d/%d/%d %s\n", start.tm_mday, start.tm_mon + 1, start.tm_year + 1900, 
      t == -1 ? "failed" : "OK");
}

否则,使用离散代码时,将有效地重新写入
mktime()
的年/月/日部分

我不知道如何前进,如果能得到帮助,我将不胜感激

#include <stdio.h>
#include <stdlib.h>

int day, month, year, numberDays;

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

int monthYear[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main(void) {

    printf("Enter starting date: ");
    scanf("%d %d %d", &day, &month, &year);

    printf("Enter number of days: ");
    scanf("%d", &numberDays);

    leapYear(year);

    int wholeYears, rest;
    if (leapYear(year)) {
        wholeYears = numberDays / 366;
        rest = numberDays % 366;
    }
    else {
        wholeYears = numberDays / 365;
        rest = numberDays % 365;
    }
    int resultYears = year + wholeYears;
    int midDays = day + rest;
    int resultMonths;

    return 0;
}
将380添加到
y,m,d
d
。在其他情况下,可能会为
m
添加一些值。然后执行基本范围缩小。每年有12个月的时间。月份范围[1…12]。每400年有365*400+97天。每月的日期至少为1

最后一步是处理月范围之外的一天。 一种简单的,虽然效率不高,但方法是:测试数据是否超过当月的天数。如果是,则减去该月的天数,并提前到下个月

我遗漏了一些代码细节,因为OP的目标是一些前进的想法

#define DAYSPER400YEARS (365 * 400 + 97)
#define JANUARY 1
#define FEBRUARY 2
#define DECEMBER 12
#define MONTHPERYEAR 12

static int is_leap_year(long long year) {
  // If not divisible by 4 ...
  // If not divisible by 100 ...
  // If not divisible by 400 ...
}

static int days_per_month(long long year, int month) {
  static const signed char dpm[] = { 0, //
      31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  if (month != FEBRUARY) {
    return ....;
  }
  return .... + is_leap_year(year);
}

/*
 * Bring a date into it primary range.
 * https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar
 * The year, month, day may be any value INT_MIN ... INT_MAX.
 * Return error flag.
 */
bool ymd_to_primary_range(int *year, int *month, int *day) {
  long long y = *year;
  y += *month / 12;
  *month %= 12; // month now in -11 to 11 range
  while (*month < JANUARY) {
    *month += MONTHPERYEAR;
    year--;
  }
  y += (*day / DAYSPER400YEARS) * 400;
  *day %= DAYSPER400YEARS;
  while (*day < 1) {
    *day += DAYSPER400YEARS;
    y -= 400;
  }
  int dpm;
  while (*day > (dpm = days_per_month(y, *month))) {
    *day -= dpm; 
    (*month)++;
    if (*month > ...) {
      *month -= ....;
      y++;
    }
  }
  if (y < INT_MIN) {
    *year = INT_MIN;
    return true;
  }
  if (y > INT_MAX) {
    *year = INT_MAX;
    return true;
  }
  *year = (int) y;
  return false;
}
输出

407/1/2021
11/2/2022 OK
Error: 0
Date (dmy): 11/02/2022
我不知道如何前进,如果能得到帮助,我将不胜感激

#include <stdio.h>
#include <stdlib.h>

int day, month, year, numberDays;

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

int monthYear[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main(void) {

    printf("Enter starting date: ");
    scanf("%d %d %d", &day, &month, &year);

    printf("Enter number of days: ");
    scanf("%d", &numberDays);

    leapYear(year);

    int wholeYears, rest;
    if (leapYear(year)) {
        wholeYears = numberDays / 366;
        rest = numberDays % 366;
    }
    else {
        wholeYears = numberDays / 365;
        rest = numberDays % 365;
    }
    int resultYears = year + wholeYears;
    int midDays = day + rest;
    int resultMonths;

    return 0;
}
将380添加到
y,m,d
d
。在其他情况下,可能会为
m
添加一些值。然后执行基本范围缩小。每年有12个月的时间。月份范围[1…12]。每400年有365*400+97天。每月的日期至少为1

最后一步是处理月范围之外的一天。 一种简单的,虽然效率不高,但方法是:测试数据是否超过当月的天数。如果是,则减去该月的天数,并提前到下个月

我遗漏了一些代码细节,因为OP的目标是一些前进的想法

#define DAYSPER400YEARS (365 * 400 + 97)
#define JANUARY 1
#define FEBRUARY 2
#define DECEMBER 12
#define MONTHPERYEAR 12

static int is_leap_year(long long year) {
  // If not divisible by 4 ...
  // If not divisible by 100 ...
  // If not divisible by 400 ...
}

static int days_per_month(long long year, int month) {
  static const signed char dpm[] = { 0, //
      31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  if (month != FEBRUARY) {
    return ....;
  }
  return .... + is_leap_year(year);
}

/*
 * Bring a date into it primary range.
 * https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar
 * The year, month, day may be any value INT_MIN ... INT_MAX.
 * Return error flag.
 */
bool ymd_to_primary_range(int *year, int *month, int *day) {
  long long y = *year;
  y += *month / 12;
  *month %= 12; // month now in -11 to 11 range
  while (*month < JANUARY) {
    *month += MONTHPERYEAR;
    year--;
  }
  y += (*day / DAYSPER400YEARS) * 400;
  *day %= DAYSPER400YEARS;
  while (*day < 1) {
    *day += DAYSPER400YEARS;
    y -= 400;
  }
  int dpm;
  while (*day > (dpm = days_per_month(y, *month))) {
    *day -= dpm; 
    (*month)++;
    if (*month > ...) {
      *month -= ....;
      y++;
    }
  }
  if (y < INT_MIN) {
    *year = INT_MIN;
    return true;
  }
  if (y > INT_MAX) {
    *year = INT_MAX;
    return true;
  }
  *year = (int) y;
  return false;
}
输出

407/1/2021
11/2/2022 OK
Error: 0
Date (dmy): 11/02/2022

提示:1月32日-->2月1日,因为1月只有31天。所以1月27日+380天就是1月407日。现在正常化。“然后380天”-->添加的潜在范围是多少?+/-1000,+/-1000000?提示:1月32日-->2月1日,因为1月只有31天。所以1月27日+380天就是1月407日。现在正常化。“然后380天”-->添加的潜在范围是多少?+/-1000,+/-1000000?很好地利用了正午时间-避免了在冬季和夏季(标准和夏令时)之间时区变化的潜在问题。谢谢,但问题是-我对在没有任何预编码库的情况下这样做很感兴趣。@w4nnacry这样的条件“在没有任何预编码库的情况下这样做”还有其他的限制在原来的帖子里,否则我们的目标会移动。@JonathanLeffler好眼力-我确实漏掉了一条关于
.tm_hour=12
的评论,看看人们对代码的看法有多近。我会加上的。@chux,Monica,我还有一个问题,请回答。当输入中的天数很好地利用了正午时,它可以正常工作-这避免了在冬季和夏季(标准和夏令时)之间时区变化的潜在问题。谢谢,但问题是-我对在没有任何预编码库的情况下这样做感兴趣。@w4nnacry这样的条件“在没有任何预编码库的情况下这样做”还有其他的限制在原来的帖子里,否则我们的目标会移动。@JonathanLeffler好眼力-我确实漏掉了一条关于
.tm_hour=12
的评论,看看人们对代码的看法有多近。我会加上的。@chux,Monica,我还有一个问题,请回答。当输入中的天数为