首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我是否在updateDate函数中正确地使用了指针?

我是否在updateDate函数中正确地使用了指针?
EN

Stack Overflow用户
提问于 2016-07-28 21:39:04
回答 3查看 167关注 0票数 2

我正在使用一个程序来更新输入的日期并更新它。考虑到月中的日数以及是否是闰年等。

我正在尝试在“C中的编程”一章中做一个练习:

根据本章中定义的date结构的定义,编写一个名为dateUpdate()的函数,它以指向date结构的指针作为参数,并将结构更新到第二天(参见程序8.4)。

你能告诉我我是否做了被要求的事吗?

这是原始代码:

代码语言:javascript
运行
复制
#include <stdio.h>
#include <stdbool.h>

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


struct date dateUpdate (struct date today);
int numberOfDays (struct date d);
bool isLeapYear(struct date d);

int main (void)
{
    struct date thisDay, nextDay;

    printf("Enter today's date (mm dd yyyy) : ");
    scanf("%i%i%i", &thisDay.month, &thisDay.day, &thisDay.year);

    nextDay = dateUpdate(thisDay);

    printf("Tomorrow's date is %i/%i/%.2i.\n", nextDay.month, nextDay.day, nextDay.year % 100);

    return 0;
}


struct date dateUpdate (struct date today)
{
    struct date tomorrow;
    int numberOfDays (struct date d);

    if(today.day != numberOfDays (today))
    {
        tomorrow = (struct date) {today.month, today.day + 1, today.year};
    }
    else if(today.month == 12)
    {
        tomorrow = (struct date) {1, 1, today.year + 1};
    }
    else
    {
        tomorrow = (struct date) {today.month + 1, 1, today.year};
    }

    return tomorrow;
}

int numberOfDays (struct date d)
{
    int days;
    bool isLeapYear (struct date d);
    const int daysPerMonth[12] =
        {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    if(isLeapYear (d) && d.month == 2)
    {
        days = 29;
    }
    else
    {
        days = daysPerMonth[d.month - 1];
    }

    return days;
}

bool isLeapYear(struct date d)
{
    bool leapYearFlag;

    if ( (d.year % 4 == 0 && d.year % d.year % 100 != 0) || d.year % 400 == 0)
    {
        leapYearFlag = true;
    }
    else
    {
        leapYearFlag = false;
    }

    return leapYearFlag;
}

下面是我在updateFunction中利用指针的尝试

代码语言:javascript
运行
复制
#include <stdio.h>
#include <stdbool.h>

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


struct date dateUpdate (struct date* today);
int numberOfDays (struct date d);
bool isLeapYear(struct date d);

int main (void)
{
    struct date thisDay, nextDay, *datePtr;

    printf("Enter today's date (mm dd yyyy) : ");
    scanf("%i%i%i", &thisDay.month, &thisDay.day, &thisDay.year);

    datePtr = &thisDay;

    nextDay = dateUpdate(datePtr);

    printf("Tomorrow's date is %i/%i/%.2i.\n", nextDay.month, nextDay.day, nextDay.year % 100);

    return 0;
}


struct date dateUpdate (struct date* today)
{
    struct date tomorrow;
    int numberOfDays (struct date d);

    if(today->day != numberOfDays (*today))
    {
        tomorrow = (struct date) {today->month, today->day + 1, today->year};
    }
    else if(today->month == 12)
    {
        tomorrow = (struct date) {1, 1, today->year + 1};
    }
    else
    {
        tomorrow = (struct date) {today->month + 1, 1, today->year};
    }

    return tomorrow;
}

int numberOfDays (struct date d)
{
    int days;
    bool isLeapYear (struct date d);
    const int daysPerMonth[12] =
        {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    if(isLeapYear (d) && d.month == 2)
    {
        days = 29;
    }
    else
    {
        days = daysPerMonth[d.month - 1];
    }

    return days;
}

bool isLeapYear(struct date d)
{
    bool leapYearFlag;

    if ( (d.year % 4 == 0 && d.year % d.year % 100 != 0) || d.year % 400 == 0)
    {
        leapYearFlag = true;
    }
    else
    {
        leapYearFlag = false;
    }

    return leapYearFlag;
}

现在,这两个程序都编译并似乎运行正常。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-07-28 22:08:51

为了扩展的评论:它几乎是一样简单。差不多了。

代码语言:javascript
运行
复制
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

// ALL TESTS OMMITTED!

struct date {
  int month;
  int day;
  int year;
};
// add the pointer mark (asterix)
struct date *dateUpdate(struct date *today);
int numberOfDays(struct date *d);
bool isLeapYear(struct date *d);

int main(void)
{
  // again ,just add the pointer marks
  struct date *thisDay, *nextDay;
  // using a pointer means that all you have is a pointer
  // but you need some memory to store the content
  thisDay = malloc(sizeof(struct date));

  printf("Enter today's date (mm dd yyyy) : ");
  // use the "->" notation to get to the respective storages
  scanf("%i%i%i", &thisDay->month, &thisDay->day, &thisDay->year);

  // dateUpdate() has been changed to accept and return a pointer,
  // so no change in notations here 
  nextDay = dateUpdate(thisDay);
  // again: use the "->" notation to get to the respective storages
  printf("Tomorrow's date is %i/%i/%.2i.\n", nextDay->month, nextDay->day,
     nextDay->year % 100);

  // memory allocated by 'alloc() needs to be free'd, too
  free(nextDay);
  free(thisDay);

  return 0;
}

// just added pointer markings
struct date *dateUpdate(struct date *today)
{
  struct date *tomorrow;
  // again, we need to allocated some memory
  // not only to get storage but also to be able to return it
  tomorrow = malloc(sizeof(struct date));
  // again: use the "->" notation to get to the respective storages
  if (today->day != numberOfDays(today)) {
    // the notation of the cast does not change, the target does
    *tomorrow = (struct date) {
    today->month, today->day + 1, today->year};
  } else if (today->month == 12) {
    *tomorrow = (struct date) {
    1, 1, today->year + 1};
  } else {
    *tomorrow = (struct date) {
    today->month + 1, 1, today->year};
  }
  return tomorrow;
}

int numberOfDays(struct date *d)
{
  int days;
  const int daysPerMonth[12] =
      { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

  if (isLeapYear(d) && d->month == 2) {
    days = 29;
  } else {
    days = daysPerMonth[d->month - 1];
  }

  return days;
}

bool isLeapYear(struct date * d)
{
  bool leapYearFlag;
  // you have one "d.year %" too much in your code
  if ((d->year % 4 == 0 && d->year % 100 != 0) || d->year % 400 == 0) {
    leapYearFlag = true;
  } else {
    leapYearFlag = false;
  }

  return leapYearFlag;
}

我希望这能让事情更清楚一些。

票数 1
EN

Stack Overflow用户

发布于 2016-07-28 22:09:53

此代码可以很容易地转换为使用指针。指针很有用,因为它们为每次调用函数时复制结构的数据提供了一种替代方法。你仍然可以在其他地方使用正常的、按值传递的机制。重写代码非常简单,几乎不需要更改。例如,您的struct date dateUpdate();函数可以通过在类型后面添加*符号(例如intint * )来重新定位为接受指针。函数struct date dateUpdate的定义将更改为struct date *dateUpdate (struct date *today);,这意味着日期结构的指针或内存地址正在传递。您的代码也必须在声明中更改。例如,在bool isLeapYear()中,代码行if ( (d.year % 4 == 0 && d.year % d.year % 100 != 0) || d.year % 400 == 0)必须更改为if ( (d->year % 4 == 0 && d->year % d->year % 100 != 0) || d->year % 400 == 0)->操作符是(*pointer).variable的缩写,因为必须告诉编译器在address中获取结构的成员,而不是指针的实际位置。现在传递的类型已经改变了,调用也不同了。调用bool isLeapYear()现在不是bool isLeapYear (d);,而是bool isLeapYear (&d);,因为&运算符获取struct的地址。使用这些信息,下面是您的程序的转换版本:链接。 --然而,一个大问题是,您的许多代码无法工作,但这不在您的问题范围之内。编辑:这个答案是在你编辑问题之前做出的,但仍然很有帮助。

票数 0
EN

Stack Overflow用户

发布于 2016-07-29 01:49:11

下面是我对这个练习的看法,我认为这个练习的目的是使用指针来用第二天的日期更新单个日期结构。

代码语言:javascript
运行
复制
/* Exercise 10.11

   Given the definition of a date structure as defined in this chapter, write
   a function called dateUpdate() that takes a pointer to a date structure as
   its argument and that updates the structure to the following day.
*/

#include <stdio.h>
#include <stdbool.h>

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

void dateUpdate (struct date *ptr)
{
    int numberOfDays (struct date d);

    if ( ptr->day != numberOfDays (*ptr) )
        ++ptr->day;
    else if ( ptr->month == 12 ) {                  // end of year
        ptr->day = 1;
        ptr->month = 1;
        ++ptr->year;
    }
    else {                                          // end of month
        ptr->day = 1;
        ++ptr->month;
    }
}

int numberOfDays (struct date d)
{
    bool isLeapYear (struct date d);

    int days;
    const int daysPerMonth[12] =
        { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    if ( isLeapYear (d) && d.month == 2 )
        days = 29;
    else
        days = daysPerMonth[d.month - 1];

    return days;        
}

bool isLeapYear (struct date d)
{
    bool leapYearFlag;

    if ( (d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0 )
        leapYearFlag = true;                        // leap year
    else
        leapYearFlag = false;                       // not a leap year

    return leapYearFlag;        
}

int main (void)
{
    void dateUpdate (struct date *ptr);

    struct date calendar;

    printf ("Enter a day's date (mm dd yyyy): ");
    scanf ("%i%i%i", &calendar.month, &calendar.day, &calendar.year);

    dateUpdate (&calendar);

    printf ("The next day's date is %i/%i/%.2i.\n", calendar.month,
             calendar.day, calendar.year % 100);

    return 0;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38647175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档