gpt4 book ai didi

c - 代码中有什么错误。?

转载 作者:行者123 更新时间:2023-11-30 20:24:22 25 4
gpt4 key购买 nike

好吧..所以问题是..假设2001年1月1日是星期一所以..以此为引用...通过键盘输入YEAR,找出1号是星期几该(输入)年份的一月。

在我的程序中(如下)..当人们输入大于引用年份(2001)的输入时..答案是正确的,但是..如果输入小于2001..则答案是错误的。 。您能指出并解释我的代码中的错误吗?谢谢..

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

int main()
{
int present_year;
int normal_days;
int normal_year;
int leap_year;
int leap_days;
int check;
int total_days;
int reference_year=2001;
int day;

printf("Enter the year you want to check\n");
scanf("%d",&present_year);
if(reference_year<present_year)

/*if year entered is greater than reference year(2001)*/
{
check=present_year-reference_year;
}

if(present_year<reference_year)
/* if year entered is smaller than reference year*/
{
check=reference_year-present_year;
}

leap_year=check/4;
normal_year=check-leap_year;
normal_days=normal_year*365;
leap_days=leap_year*366;
total_days=leap_days+normal_days;
day=total_days%7;

if(day==0)
printf("January 1 of year %d will be Monday\n",present_year);
if(day==1)
printf("January 1 of year %d will be Tuesday\n",present_year);
if(day==2)
printf("January 1 of year %d will be Wednesday\n",present_year);
if(day==3)
printf("January 1 of year %d will be Thursday\n",present_year);
if(day==4)
printf("January 1 of year %d will be Friday\n",present_year);
if(day==5)
printf("January 1 of year %d will be Saturday\n",present_year);
if(day==6)
printf("January 1 of year %d will be Sunday\n",present_year);

return 0;
}

最佳答案

从哪里开始......你知道什么是闰年吗?我不认为你这样做。 不是每四年一次。

来,试试这个。

#include <stdio.h>
#include <stdbool.h>
#include <math.h>

double mod(double x, double y)
{
return x - y * floor(x / y);
}

bool gregorian_leap_year(int year)
{
return (
mod(year, 4) == 0 &&
!(mod(year, 400) == 100 ||
mod(year, 400) == 200 ||
mod(year, 400) == 300)) ? true : false;
}

int fixed_from_gregorian(int year, int month, int day)
{
int correction, f;
if (month <= 2) correction = 0;
else if (month > 2 && gregorian_leap_year(year)) correction = -1;
else correction = -2;

f = 365 * (year - 1) +
floor((year - 1) / 4.0) -
floor((year - 1) / 100.0) +
floor((year - 1) / 400.0) +
floor((367 * month - 362) / 12.0) +
correction + day;
return f;
}

char *daynames[] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};

int main(int argc, char *argv[])
{
int present_year;
printf("Enter the year you want to check\n");
scanf("%d", &present_year);

int f = fixed_from_gregorian(present_year, 1, 1);
int day = (int)mod(f,7);

printf("January 1 of year %d will be %s\n", present_year, daynames[day]);

return 0;
}

关于c - 代码中有什么错误。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33707106/

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