【发布时间】:2016-07-28 21:39:04
【问题描述】:
我正在使用一个程序来更新输入的日期并更新它。考虑到当月的天数以及是否是闰年等。
我正在尝试在“C 语言编程”的指针章节中进行练习:
“给定本章中定义的date 结构的定义,编写一个名为dateUpdate() 的函数,该函数将指向date 结构的指针作为其参数,并将结构更新到第二天(参见程序 8.4)。”
你能告诉我我是否按照要求完成了吗?
这是原始代码:
#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 中使用指针的尝试:
#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;
}
现在两个程序都可以编译并且运行正常。
【问题讨论】:
-
我不完全理解你的问题。但是要创建一个指向结构的指针,你明天会做 struct data* ;在任何你有点运算符的地方,只要把它变成箭头运算符->。示例:不要让明天.day 让它成为明天-> 天;
-
我的错,让我重写这个车祸的一个问题。
-
你试过了吗?
标签: c pointers data-structures