【问题标题】:Date calculation program that adds 7 days to date entered by user日期计算程序,将用户输入的日期加上 7 天
【发布时间】:2017-09-07 20:50:13
【问题描述】:

我目前正在创建一个日期计算 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;
}

【问题讨论】:

  • // February else { 部分中的if() ... else ... 缺少addSev.month == 2 条件。
  • 在输入日期后执行newDate = addSeven (origDate);printf("\n%d/%d/%d\n", origDate); :需要origDate 的每个成员的参数。
  • 例如,您在哪里检查您的用户是否在关注并且没有输入2017/09/071 作为日期?实际上,您在哪里检查他们没有输入2017-09-07 作为日期?输入验证很难。

标签: c function


【解决方案1】:

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

// 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);

// February else { 部分中的if() ... 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;
}

顺便说一句:addSev.year % 4 == 0 是对leap year 的过于简单的检测。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 2017-07-17
    相关资源
    最近更新 更多