gpt4 book ai didi

C 语言的日历编程项目。对于新手来说太复杂了

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

    #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>

void main()
{
int numdays, month, day, year = 1;


while (year > 0)`enter code here`
{
printf("Enter Month: ");
scanf("%d", &month);
printf("Enter Day: ");
scanf("%d", &day);
printf("Enter Year: ");
scanf("%d", &year);

numdays = ((year - 1) * 365 + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400)); // how many days including exceptions

if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) //check if leapyear
{
if (month = 1) // January
numdays = numdays;
if (month = 2) // February
numdays = numdays + 31;
if (month = 3) // March
numdays = numdays + 28 + 31 + 1;
if (month = 4) // April
numdays = numdays + 31 + 28 + 31 + 1;
if (month = 5) // May
numdays = numdays + 30 + 31 + 28 + 31 + 1;
if (month = 6) // June
numdays = numdays + 31 + 30 + 31 + 28 + 31 + 1;
if (month = 7) // July
numdays = numdays + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month = 8) // August
numdays = numdays + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month = 9) // September
numdays = numdays + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month = 10) // October
numdays = numdays + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month = 11) // November
numdays = numdays + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month = 12) // December
numdays = numdays + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
}
else
{
if (month = 1) // January
numdays = numdays;
if (month = 2) // February
numdays = numdays + 31;
if (month = 3) // March
numdays = numdays + 28 + 31;
if (month = 4) // April
numdays = numdays + 31 + 28 + 31;
if (month = 5) // May
numdays = numdays + 30 + 31 + 28 + 31;
if (month = 6) // June
numdays = numdays + 31 + 30 + 31 + 28 + 31;
if (month = 7) // July
numdays = numdays + 30 + 31 + 30 + 31 + 28 + 31;
if (month = 8) // August
numdays = numdays + 31 + 30 + 31 + 30 + 31 + 28 + 31;
if (month = 9) // September
numdays = numdays + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
if (month = 10) // October
numdays = numdays + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
if (month = 11) // November
numdays = numdays + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
if (month = 12) // December
numdays = numdays + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
{
printf("%d %d %d\n", month, day, year);
}
int daycode = numdays % 7;
switch (daycode)
{
case 0:
printf("Sunday\n");
break;

case 1:
printf("Monday\n");
break;

case 2:
printf("Tuesday\n");
break;

case 3:
printf("Wednesday\n");
break;

case 4:
printf("Thursday\n");
break;

case 5:
printf("Friday\n");
break;

case 6:
printf("Saturday\n");
break;

default: printf("unexpected error (daycode case) daycode = %d", daycode);
break;`enter code here`
}

}

}

}

我需要做的是获取输出以读取用户输入的给定日期的日期。但是,我不明白为什么我的月份输入被锁定在 12 点。任何帮助表示赞赏。

最佳答案

正如 MrPickles 的评论中提到的,主要问题是您混淆了赋值运算符 = 与比较运算符 ==。前者 (=) 将左侧的变量设置为右侧的变量,而 == 检查左右两侧是否相等。您的最后一条 if 语句将月份设置为 12,而不是检查它是否为 12。

另一个大问题是打印解决方案的代码位于 else 语句内,因此如果是闰年,它根本不会运行。确保将其移出。

最后,您实际上从未在代码中使用月份的。您需要输入类似 numdays+=day; 的内容。

除此之外,您可以使代码更短,但我认为除此之外这是正确的。

关于C 语言的日历编程项目。对于新手来说太复杂了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35738648/

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