如何解决C程序设计:使用结构和函数来确定两个日期之间的天数
我已经做了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,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撰写的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) = 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,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.year);
month = date1.month;
year = date1.year;
N1 = N (date1);
printf("\nEnter second date (dd mm yyyy): ");
scanf(" %i%i%i",&date2.year);
month = date2.month;
year = date2.year;
N2 = N (date2);
diff = N2 - N1;
printf("Number of elapsed days are: %i.\n",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;
}
}
解决方法
违规行是:
Message=Attempt to invoke virtual method 'void android.support.design.internal.BottomNavigationItemView.setTextColor(android.content.res.ColorStateList)' on a null object reference
但是,这是函数 N = (((1461 * (f) / 4) + ((153 * (g))) / 5) + d.day);
中的 。
您不能分配给函数名称。
要么更改函数名称(例如N
-> N
),要么为包含返回值的变量使用其他名称(例如Ncalc
)。
在ret
[IIRC]中,将N
包含为包含函数的赋值给N
来设置函数的返回值。在fortran
中不能很好地工作。
而且,您必须提供变量的定义(例如):
c
一些样式项...
按照惯例,在int ret;
中,使用所有大写字母通常保留给常量(例如):
c
尽管通过 value 传递#define X 12345
是完全合法的,但是大多数代码将 pointer 传递给结构(可能是struct
),因为更快。
const
想象一下,如果您的int
N(struct date *d)
{
}
是:
struct
它将把大约40,000个字节压入堆栈。
此外,使用(更长)更具描述性的名称比使用单个字符的名称会有所帮助。您已经有了一些函数来“计算” struct date {
...
int array[10000];
};
,f
和g
,分别名为n
,F
和G
。
嗯,什么是[an] N
[或f
或g
] [其他]阅读您的代码的人都想知道。
使用更具描述性的评论,等同于:
n
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。