c - 将用户输入的日期加上 7 天的日期计算程序

标签 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;
}

最佳答案

函数调用在输入数据之前。 @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 的过于简单的检测.

关于c - 将用户输入的日期加上 7 天的日期计算程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46104887/

相关文章:

c - 定时 Release模式(优化)功能

android - 制作一个静态链接的库

javascript - 在 JavaScript 中递归地转换对象

javascript - 刷新谷歌地图会阻止添加标记的能力

c - 将一行读取到 s 上并返回其长度的函数是好习惯吗?

php - 显示来自 WordPress 帖子的所有图像

c - 如何创建和访问二维链表元素

C trie节点重新分配导致段错误

c - 使用报警功能

JavaScript:给一个函数和函数表达式同名可以吗?