日期计算程序,添加用户输入的7天

时间:2017-09-07 20:50:14

标签: c function

我目前正在创建一个日期计算c程序,该程序会将7天添加到用户输入的日期。它必须将数据传入和传出自定义函数。但 当我运行下面的代码时,newDate总是0/7/0。我确定问题出在我的自定义功能上,但我无法找到问题所在。我是编程新手,非常感谢任何帮助。谢谢!

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

struct date {
    int month;
    int day;
    int year;
    };

struct date addSeven(struct date addSev) {
    addSev.day += 7;
 // Months with 31 days
    if ((addSev.month == 1 || // January
        addSev.month == 3 || // March
        addSev.month == 5 || // May
        addSev.month == 7 || // July
        addSev.month == 8 || // August
        addSev.month == 10 || // October
        addSev.month == 12) // December
        && addSev.day > 31) {
            addSev.day -= 31; // Equivalent to addSev.day = addSev.day - 31;
            addSev.month += 1;
            }
    // Months with 30 days
        else if ((addSev.month == 4 || // April
        addSev.month == 6 || // June
        addSev.month == 9 || // September
        addSev.month == 11) // November
        && addSev.day > 30) {
            addSev.day -= 30;
            addSev.month += 1;
            }
    // February
        else {
            if (addSev.year % 4 == 0 && addSev.day > 29) { // Leap year
                addSev.day -= 29;
                addSev.month += 1;
                }
            else if (addSev.day > 28) {
                addSev.day -= 28;
                addSev.month += 1;
                }
            }
    if ((addSev.day == 25)
            && addSev.day > 31) {
            addSev.day -= 31; // Equivalent to addSev.day = addSev.day - 31;
            addSev.month += 1;
            }
        else if ((addSev.day == 24)
                && addSev.day > 30) {
                    addSev.day -= 30;
                    addSev.month += 1;
                    }
        else {
            if (addSev.year % 4 == 0 && addSev.day > 29) { // Leap year
                addSev.day -= 29;
                addSev.month += 1;
                }
            else if (addSev.day > 28) {
                addSev.day -= 28;
                addSev.month += 1;
                }
         if (addSev.month > 12) {
            addSev.month = 1; addSev.year += 1;
            }
        return addSev;
        }
    }

int main () {
    struct date origDate, newDate;
    newDate = addSeven (origDate);
    printf("Please enter a date in mm/dd/yyyy format: ");
    scanf("%d/%d/%d",&origDate.month,&origDate.day,&origDate.year);

    printf("\n%d/%d/%d\n", origDate);
    printf("\n%d/%d/%d\n", newDate);

return 0;
}

1 个答案:

答案 0 :(得分:0)

函数调用在输入数据之前。 @BLUEPIXY。在填充addSeven()后致电origDate

// newDate = addSeven (origDate);
printf("Please enter a date in mm/dd/yyyy format: ");
scanf("%d/%d/%d",&origDate.month,&origDate.day,&origDate.year);
newDate = addSeven(origDate);

if() ... else ...部分中的// February else {缺少addSev.month == 2条件。

代码非常令人费解,因此建议重新编写。

int IsLeapYear(int year) {
  // Left for OP to code leap year detection - see far below link.
}

int DaysPerMonth(int year, int month) {
  // Left for OP to code the more simple task of finding the number of days in a month
  ... if (IsLeapYear(year)) ...
}

struct date addSeven(struct date addSev) {
  addSev.day += 7;

  int dpm = DaysPerMonth(addSev.year, addSev.month);  

  if (addSev.day > dpm) {
    addSev.day -= dpm;
    addSev.month++; // next month
    const int mpy = 12;
    if (addSev.month > mpy) {
      addSev.month -= mpy;
      addSev.year++; // next year
    }
  }
  return addSev;
}

BTW:addSev.year % 4 == 0leap year的检测过于简单。