【问题标题】:C programming passing a structure into a function将结构传递给函数的 C 编程
【发布时间】:2015-01-13 03:35:28
【问题描述】:

我的问题是: 编写一个名为 days() 的 C 函数,该函数确定从 1/1/1900 日期算起作为结构传递的任何日期的天数。将 Date 结构变量的地址传递给 days() 函数。在 main() 中编写一个程序,输入用户的月、日和年,将输入写入 Date 结构变量,调用 days() 函数并显示结果。使用以下日期结构:

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

在编写此函数时,请使用所有年份为 360 天且每个月包含 30 天的约定。该函数应返回传递给它的任何日期结构的天数。

这是我目前所拥有的,每次都计算为 0:

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

int main()
{
    int monthMain, dayMain, yearMain;       //declaring the int variables
    int totalDays;
    printf("Enter a Month: ");              //requesting user to input the month
    scanf("%d", &monthMain);                //accepting the user input for month
    printf("Enter a Day: ");                //requesting user to input the day
    scanf("%d", &dayMain);                  //accepting the user input for day
    printf("Enter a Year: ");               //requesting user to input the year
    scanf("%d", &yearMain);                 //accepting the user input for year
    totalDays = days();
    printf("the date you entered = %d days", totalDays);
return 0;
}

int days(struct date *d)
{
    int yearCalc, daysAmount;
    int monthMain, dayMain, yearMain;       //declaring the int variables

    yearCalc = 1900 * 360;
    yearMain = (yearMain * 360) - yearCalc;

    if(monthMain == 1)
    {
        monthMain = 0;
        daysAmount = monthMain + dayMain + yearMain;
    }
    if(monthMain == 2)
    {
        monthMain = 30;
        daysAmount = monthMain + dayMain + yearMain;
    }
    if(monthMain == 3)
    {
        monthMain = 60;
        daysAmount = monthMain + dayMain + yearMain;
    }
    if(monthMain == 4)
    {
        monthMain = 90;
        daysAmount = monthMain + dayMain + yearMain;
    }
    if(monthMain == 5)
    {
        monthMain = 120;
        daysAmount = monthMain + dayMain + yearMain;
    }
    if(monthMain == 6)
    {
        monthMain = 150;
        daysAmount = monthMain + dayMain + yearMain;
    }
    if(monthMain == 7)
    {
        monthMain = 180;
        daysAmount = monthMain + dayMain + yearMain;
    }
    if(monthMain == 8)
    {
        monthMain = 210;
        daysAmount = monthMain + dayMain + yearMain;
    }
    if(monthMain == 9)
    {
        monthMain = 240;
        daysAmount = monthMain + dayMain + yearMain;
    }
    if(monthMain == 10)
    {
        monthMain = 270;
        daysAmount = monthMain + dayMain + yearMain;
    }
    if(monthMain == 11)
    {
        monthMain = 300;
        daysAmount = monthMain + dayMain + yearMain;
    }
    if(monthMain == 12)
    {
        monthMain = 360;
        daysAmount = monthMain + dayMain + yearMain;
    }
    return daysAmount;
}

任何帮助都会很棒:)

【问题讨论】:

  • 由于daysAmount = monthMain + dayMain + yearMain; 的计算每次都相同,您可以将其移到if 语句集之后。在第 11 个月和第 12 个月之间从 300 跳到 360 是错误的。有一个简单的算法给出了一个有效的月份数,用于推断之前一年中的天数(对于这些漂亮的简单统一大小的月份):(month_number - 1) * 30 给出了你想要的答案。您是否添加了任何打印语句来查看为什么会得到 0?

标签: c


【解决方案1】:

你的主要问题是这个电话:

totalDays = days();

您需要将指向日期结构的指针传递给函数。这意味着您需要main() 中的局部变量:

struct date given;

你需要初始化它:

given.day = 11;
given.month = 11;
given.year = 1918;

你需要通过它:

totalDays = days(&given);

并且您需要学习如何设置编译器警告,以便在您未预先声明具有完整原型的函数并且未准确调用函数时从编译器收到错误消息。

如果您的编译器是 GCC,请使用:

gcc -O3 -std=c11 -g -Wall -Wextra -Werror …

作为起点。您可能还需要-Wstrict-prototypes-Wold-style-declaration-Wold-style-definition-Wmissing-prototypes;我将其中的大部分用作例行程序,以确保我不会犯愚蠢的错误。

此外,由于计算 daysAmount = monthMain + dayMain + yearMain; 每次都是相同的,您可以将其移到 if 语句集之后。在第 11 个月和第 12 个月之间从 300 跳到 360 是错误的。有一个简单的算法给出了一个有效的月份数来推断前一年的天数(对于这些漂亮的简单统一大小的月份):(month_number - 1) * 30 给出了你想要的答案。

您是否添加了任何打印语句来查看为什么得到 0?例如,如果您在函数开头添加了如下语句:

 printf("Date: %.4d-%.2d-%.2d\n", d->year, d->month, d->day);

您将以 ISO 8601 格式打印日期。如果这没有向您显示您认为输入的答案,您就会知道有问题。打印值以确保计算机看到的就是您认为计算机应该看到的,这是一种基本的调试技术。

【讨论】:

  • 非常感谢,我不知道为什么我没有想到要进行您向我展示的计算。我是一个糟糕的程序员,但我的专业需要这门课。这就像一个魅力:)
  • +1 表示%.4d-%.2d-%.2d。它似乎比我以前使用的 %04d-%02d-%02d 好一点(带有负数)。
【解决方案2】:
struct date
{
     int month;
     int day;
     int year;
}; // unsigned int would serve this purpose better, as negative values are not valid here

int days(struct date *d)
{
    int daysTotal; // Declaring variable to hold total number of days since 1900-01-01

    // Adding days from date::day
    if (d->day > 0 && d->day < 31){ // Only execute if day is valid
        daysTotal = d->day - 1;
    }
    // Adding days from date::month
    if (d->month > 0 && d->month < 13){ // Only execute if month is valid
        daysTotal += 30 * (d->month-1);
    }
    // Adding days from date::year
    if (d->year > 1899){ // Only execute if year is valid
        daysTotal += 360 * (d->year-1900); // subtracting 1900 years
    }

    return daysTotal; // This may still be a negative value, but is accepted here for simplicity
};

int main()
{
    date myDate; // Declaring an object of struct date

    printf("Enter a Month: ");              //requesting user to input the month
    scanf("%d", &myDate.month);             //recieving the user input for month
    printf("Enter a Day: ");                //requesting user to input the day
    scanf("%d", &myDate.day);               //recieving the user input for day
    printf("Enter a Year: ");               //requesting user to input the year
    scanf("%d", &myDate.year);              //recieving the user input for year

    printf("the date you entered = %d days", days(&myDate)); // Passing object pointer to function
return 0;
};

您最初根本没有使用该结构。你没有为此目的制作结构的对象。最后有一大块我可以折叠成一个 if 语句。其他结构成员没有应用验证。有不必要的变数。我进行了更改以简化事情。将days()函数放在main()之前,防止出现“function days(struct date *d) not declared in this scope”错误。

编辑:我的实现现在返回0 days for 1900-01-0131 days for 1900-02-02。这是通过从日和月中减去 1 来完成的,将其转换为索引,而不是传递单位的数量,因为这是日和月的实际含义(年份不是这样,它们从 0 年或此处开始, 1900 年)。

【讨论】:

  • 您需要只计算有效月份的前几天吗?目前,您的代码将报告 1900-01-01 的 31 天,而不仅仅是 1(或者,实际上是 0)。
  • 谢谢乔恩,我现在改变了我的实现。需要通过减一将日期和月份值转换为索引,因此它们从 0 开始,因为那是自它们中的第一个以来通过的单位数。年份不是这样,在本例中它们从 0 或 1900 开始。
猜你喜欢
  • 1970-01-01
  • 2015-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-12
  • 2015-01-03
相关资源
最近更新 更多