[英]Calculate the number of days between two dates in C – Convert date to days
[英]C programming: Using structures and functions to determine the number of days between two dates
我已经做出了 Craig Estey 建议的更正。 代码现在可以编译并运行。 这是修改后的代码:
/* Programme to determine the number of days between two dates ex8.2.c
This is done with the formula
N = ( ((1461 * (f(year, month))) / 4) + ((153 * (g(month))) / 5) + day )
with:
f(year, month) = year - 1 if month <= 2; otherwise year
g(month) = month + 13 if month <=2; otherwise month + 1
The formula is applicable for dates after 1 March 1900;
add 1 to N for dates between 1 March 1800 to 28 February 1900
add 2 to N for dates between 1 March 1700 to 28 February 1800
ALGORITHMS
N.B.: Use ternary operators to help with different evaluations
Declare structure(s) to store date
Write functions to evaluate f
Write function to evaluate g
Compare date periods to know if modification of formula will be used
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
struct date
{
int day;
int month;
int year;
};
struct date date1, date2;
int month, year, duration1, duration2, diff;
// Function prototypes
int number_of_Days (struct date d);
bool is_Leap_Year (struct date d);
int year_Func (int month, int year);
int month_Func (int month);
int date_Elapsed (struct date d);
int main(void)
{
printf("This is a programme to find the number of days between two dates!\n");
printf("\nEnter first date (dd mm yyyy): ");
scanf(" %i%i%i", &date1.day, &date1.month, &date1.year);
month = date1.month;
year = date1.year;
duration1 = date_Elapsed (date1);
printf("\nEnter second date (dd mm yyyy): ");
scanf(" %i%i%i", &date2.day, &date2.month, &date2.year);
month = date2.month;
year = date2.year;
duration2 = date_Elapsed (date2);
diff = duration2 - duration1;
printf("Number of elapsed days are: %i.\n", diff);
}
// Function to find the number of days in a month
int number_of_Days (struct date d)
{
int days;
bool is_Leap_Year (struct date d);
const int days_Per_Month[13] = {0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
if (is_Leap_Year (d) == true && d.month == 2)
{
days = 29;
}
else
{
days = days_Per_Month[d.month];
}
return days;
}
// Function to determine if it is a leap year
bool is_Leap_Year (struct date d)
{
bool leap_Year_Flag;
if ((d.year % 4 == 0 && d.year % 100 != 0) || (d.year % 400 == 0))
{
leap_Year_Flag = true;
}
else
{
leap_Year_Flag = false;
}
return leap_Year_Flag;
}
// Function to find f in formula
int year_Func (int month, int year)
{
int yrRet;
yrRet = (month <= 2) ? (year - 1) : (year);
return yrRet;
}
// Function to find g in formula
int month_Func (int month)
{
int mntRet;
mntRet = (month <= 2) ? (month + 13) : (month + 1);
return mntRet;
}
// Function to calculate N in formula
int date_Elapsed (struct date d)
{
int number_of_Days (struct date d);
bool is_Leap_Year (struct date d);
int year_Func (int month, int year);
int month_Func (int month);
int yCalc, mCalc, nCalc;
yCalc = year_Func (d.month, d.year);
mCalc = month_Func (d.month);
// Calculates number of elapsed days
nCalc = ( ((1461 * (yCalc) / 4) + ((153 * (mCalc))) / 5) + d.day );
if ((d.day < 1) && (d.month < 3) && (d.year < 1700))
{
printf("Invalid date input!\n");
printf("Date must be from 1 March 1700.\n");
exit (999);
}
else if (((d.day >= 1) && (d.month >= 3) && (d.year >= 1700)) && ((d.day <= 28) && (d.month <= 2) && (d.year <= 1800)))
{
nCalc = nCalc + 2;
}
else if (((d.day >= 1) && (d.month >= 3) && (d.year >= 1800)) && ((d.day <= 28) && (d.month <= 2) && (d.year <= 1900)))
{
nCalc = nCalc + 1;
}
else
{
return nCalc;
}
}
================================================== ==============================
我是一个学习计算机编程的新手。 这是 Stephen G. Kochan 的 Programming in C 练习。 到目前为止,我需要帮助来查明错误。 我已经坚持了大约一个星期。
我的 Code::Blocks 编译器显示以下错误:
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
|In function ‘N’:|
|134|error: lvalue required as left operand of assignment|
|144|error: lvalue required as left operand of assignment|
|148|error: lvalue required as left operand of assignment|
|152|warning: return makes integer from pointer without a cast [-Wint-conversion]|
|154|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 3 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
这是我的代码
/* Programme to determine the number of days between two dates ex8.2.c
This is done with the formula
N = ( ((1461 * (f(year, month))) / 4) + ((153 * (g(month))) / 5) + day )
with:
f(year, month) = year - 1 if month <= 2; otherwise year
g(month) = month + 13 if month <=2; otherwise month + 1
The formula is applicable for dates after 1 March 1900;
add 1 to N for dates between 1 March 1800 to 28 February 1900
add 2 to N for dates between 1 March 1700 to 28 February 1800
ALGORITHMS
N.B.: Use ternary operators to help with different evaluations
Declare structure(s) to store date
Write functions to evaluate f
Write function to evaluate g
Compare date periods to know if modification of formula will be used
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
struct date
{
int day;
int month;
int year;
};
struct date date1, date2;
int month, year, N1, N2, diff;
// Function prototypes
int number_of_Days (struct date d);
bool is_Leap_Year (struct date d);
int F (int month, int year);
int G (int month);
int N (struct date d);
int main(void)
{
//
printf("This is a programme to find the number of days between two dates!\n");
printf("\nEnter first date (dd mm yyyy): ");
scanf(" %i%i%i", &date1.day, &date1.month, &date1.year);
month = date1.month;
year = date1.year;
N1 = N (date1);
printf("\nEnter second date (dd mm yyyy): ");
scanf(" %i%i%i", &date2.day, &date2.month, &date2.year);
month = date2.month;
year = date2.year;
N2 = N (date2);
diff = N2 - N1;
printf("Number of elapsed days are: %i.\n", diff);
}
// Function to find the number of days in a month
int number_of_Days (struct date d)
{
int days;
bool is_Leap_Year (struct date d);
const int days_Per_Month[13] = {0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
if (is_Leap_Year (d) == true && d.month == 2)
{
days = 29;
}
else
{
days = days_Per_Month[d.month];
}
return days;
}
// Function to determine if it is a leap year
bool is_Leap_Year (struct date d)
{
bool leap_Year_Flag;
if ((d.year % 4 == 0 && d.year % 100 != 0) || (d.year % 400 == 0))
{
leap_Year_Flag = true;
}
else
{
leap_Year_Flag = false;
}
return leap_Year_Flag;
}
// Function to find f
int F (int month, int year)
{
int F;
F = (month <= 2) ? (year - 1) : (year);
return F;
}
// Function to find g
int G (int month)
{
int G;
G = (month <= 2) ? (month + 13) : (month + 1);
return G;
}
// Function to calculate N
int N (struct date d)
{
int number_of_Days (struct date d);
bool is_Leap_Year (struct date d);
int F (int month, int year);
int G (int month);
int f, g;
f = F (d.month, d.year);
g = G (d.month);
//N = ( ((1461 * (f(d.month, d.year))) / 4) + ((153 * (g(d.month))) / 5) + d.day );
N = ( ((1461 * (f) / 4) + ((153 * (g))) / 5) + d.day );
if ((d.day < 1) && (d.month < 3) && (d.year < 1700))
{
printf("Invalid date input!\n");
printf("Date must be from 1 March 1700.\n");
exit (999);
}
else if (((d.day >= 1) && (d.month >= 3) && (d.year >= 1700)) && ((d.day <= 28) && (d.month <= 2) && (d.year <= 1800)))
{
N = N + 2;
}
else if (((d.day >= 1) && (d.month >= 3) && (d.year >= 1800)) && ((d.day <= 28) && (d.month <= 2) && (d.year <= 1900)))
{
N = N + 1;
}
else
{
return N;
}
}
违规行是:
N = (((1461 * (f) / 4) + ((153 * (g))) / 5) + d.day);
但是,这是在函数N
内部。
您不能分配给函数名称。
更改函数名称(例如N
--> Ncalc
)或为保存返回值的变量使用不同的名称(例如ret
)。
分配给N
其中N
是包含函数是fortran
[IIRC] 中用于设置函数返回值的事情。 这在c
不太好用。
而且,您必须提供变量的定义(例如):
int ret;
一些风格的项目...
按照惯例,在c
,使用所有大写字母通常是为常量保留的(例如):
#define X 12345
尽管按值传递struct
是完全合法的,但大多数代码会传递一个指向结构体的指针(可能使用const
),因为它更快。
int
N(struct date *d)
{
}
想象一下,如果您的struct
是:
struct date {
...
int array[10000];
};
它会将大约 40,000 个字节推入堆栈。
此外,使用 [更长] 比单字符名称更具描述性的名称也很有帮助。 您有函数来“计算” f
、 g
和n
命名为F
、 G
和N
。
那么,什么是 [an] f
[或g
或n
]??? 任何[其他] 阅读你的代码的人都会想知道。
使用更具描述性的注释,相当于:
// set x to the value of 10
x = 10;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.