gpt4 book ai didi

c - 知道一个月中的日期,其中一天出现在 c 中

转载 作者:太空宇宙 更新时间:2023-11-04 08:05:41 26 4
gpt4 key购买 nike

我正在尝试打印某一天所在月份的日期。为此,我需要获取日期的整数值,例如星期日为 0,星期一为 1,星期二为 2 等。我如何在 C 中执行此操作?每当我尝试时,我似乎都会出错。

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main(){
int days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int year, day, month, date, a, y, m, s, i, b=0;
char week[7][10] ;

strcpy(week[0], "Sunday") ;
strcpy(week[1], "Monday") ;
strcpy(week[2], "Tuesday") ;
strcpy(week[3], "Wednesday") ;
strcpy(week[4], "Thursday") ;
strcpy(week[5], "Friday") ;
strcpy(week[6], "Saturday") ;
//char *week[]={"Sunday","Monday","Tuesday","Wednesday",
//"Thrusday","Friday","Saturday"};
printf("Enter the day:");
scanf("%s",week[i]);
printf("Enter the month:");
scanf("%d",&month);
printf("Enter the year:");
scanf("%d",year);
if(year%4==0&&year%100!=0||year%400==0)
days[2]==
29;
else
days[2]==28;

//for(i=1;i<7;i++)
//week[i]=i;
//i=i+1;
week[0]==0;
week[1]==1;
week[2]==2;
week[3]==3;
week[4]==4;
week[5]==5;
week[6]==6;

//algorithm from http://www.tondering.dk/claus/cal/chrweek.php#calcdow
a = (14 - month) % 12;
y = year - a;
m = month + (12 * a) - 12;

date = (week[i] + y + (y / 4) - (y / 100) + (y / 400) + ((31 * m) / 12)) % 7 ;
printf("%d",date);

for(day=1; day<=days[month] ;day++)
{
date=date+7;
date++;
}
return 0;
}

最佳答案

在第一个 scanf 中,i 没有值。 week[0]、week[1] 等是字符数组。你想通过将它们与 int 进行比较来做什么? (周[0]==0....)。

  declare a char[10] wordday;
printf("Enter the day:");
scanf("%s",wordday);




for(i=0;i<7;i++)
if(strcmp(wordday,week[i])==0)
{
printf("%d",i);
break;
}

关于c - 知道一个月中的日期,其中一天出现在 c 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43030203/

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