gpt4 book ai didi

c - 日期之间的天数将无法编译

转载 作者:行者123 更新时间:2023-11-30 16:57:14 25 4
gpt4 key购买 nike

我一直在努力让它发挥作用。我需要输入 2 个日期(MM DD),然后让程序告诉我这 2 个日期之间的天数。但出于某种原因,当我尝试使用第 2 个月(二月)时,我认为它没有注册我指示的只有 28 天。另外,当我输入相同的日期时,我无法让它说“0”。请帮忙谢谢

#include <stdio.h>
#include <stdlib.h>

//constructs dates for calculation
struct date{
int month;
int day;
};//end date

int main()
{
struct date first, second; //creates 2 dates to calculate
int finalDays = 0;
int total = 0;
int i = 0;
int valid=0;

printf("Enter first date \n");
scanf("%d %d", &first.month, &first.day); //user input: first date
if (first.month == 1||3||5||7||8||10){
if(first.day > 31){
printf("Invalid Day\n");
valid += 1;
}
}
else if (first.month == 4||6||9||11 ){
if (first.day > 30){
printf("Invalid Day\n");
valid += 1;
}
}
else if (first.month == 2){
if(first.day > 28){
printf("Invalid Day");
valid += 1;
}
}

printf("Enter second date\n");
scanf("%d %d", &second.month, &second.day); // user input: second date
if (second.month == 1||3||5||7||8||10){
if(second.day > 31){
printf("Invalid Day\n");
valid += 1;
}
}
else if (second.month == 4||6||9||11 ){
if (second.day > 30){
printf("Invalid Day\n");
valid += 1;
}
}
else if (second.month ==2){
if(second.day > 28){
printf("Invalid Day");
valid += 1;
}
}

if (first.month == second.month && first.day == second.day){
printf("Days between dates: 0");
valid += 1;
}

//Prints statement if month is invalid
if(first.month > 12 || second.month > 12 || first.month<1 || second.month<1){
printf("Invalid Date: Invalid month");
}

//Prints statement if second date precedes first
if(second.month<first.month){
printf("Invalid. Second date cannot precede first date.");
}
if (second.month==first.month && second.day<first.day){
printf("Invalid. Second date cannot precede first date.");
}

if(first.month==second.month){
finalDays = (second.day - first.day);
printf("Days between dates: %d", finalDays);
valid+=1;
}

if(first.month == 1||3||5||7||8||10||12) // Days remaining in first month
total = 31 - first.day;
else if(first.month == 4||6||9||11)
total = 30 - first.day;
else if(first.month == 2)
total = 28 - first.day;

for(i = first.month + 1; i < second.month; i++)
{
if(i == 3||5||7||8||10||12)
total += 31;
else if(i == 4||6||9||11)
total += 30;
}
total += second.day;

if(valid == 0){
printf("First date: %d %d \n", first.month, first.day);
printf("Second date: %d %d \n", second.month, second.day);
printf("Days between dates: %d", total);
}

return 0;
} //end main

最佳答案

if (first.month == 1||3||5||7||8||10){

这不会做你想做的事。它将被评估为 (first.month == 1)||3||5||7||8||10,这对于所有非零月份都将评估为 true。

最好将其写成案例陈述;

switch (first.month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
/* Handle 31 day months */
...
break;
case 4:
case 6:
case 9:
case 11:
/* Handle 30 day months */
...
break;
case 2:
/* Handle February */
...
break;
default:
/* Handle invalid month */
break;
}

关于c - 日期之间的天数将无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39642550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com