首页 > 电脑 > 用·c语言求输入未来某天的日期,输出距今天还有多少天?是星期几?

用·c语言求输入未来某天的日期,输出距今天还有多少天?是星期几?

电脑 2022-07-21

用c语言输入日期输出星期几

#include
intmain(intargc,charconst*argv[])
{
intdata;
scanf("%d",&data);
printf("星期%d\n",(data+1)%7+1);
return0;
}

C语言编程,输入年月日,输出距今天还有多少天,星期几,是否是公历节日。

#include #include #include #include /*这段是注释,放在此处用以说明各个成员的含义,在time.h中定义*/#if 0struct tm { int tm_sec; /* seconds after the minute - [0,59] */ int tm_min; /* minutes after the hour - [0,59] */ int tm_hour; /* hours since midnight - [0,23] */ int tm_mday; /* day of th

用C语言输入年月日,输出距今天还有多少天,星期几,是否是公历节日

试试这个吧,GetNextTime函数 /*-----------------------------------------------------------------*\ author: wanfustudio mail : wanfu.liu@163.com Rights: 任意拷贝,修改,传播包括商业使用 Q & A : 如何疑问,以及想法,不满足的需求 欢迎来函!!! \*-----------------------------------------------------------------*/ #include #include 问:c++程序设计: 1,输入年月日,输出距离今天还有多少天,星期几,是否是公历日。假定输入的年份
#define_CRT_SECURE_NO_WARNINGS
#include
#include
usingnamespacestd;
//判断星期几
intgetWeekDay(constintyear,constintyday)
{
intn=year-1+(int)((year-1)/4)-(int)((year-1)/100)+(int)((year-1)/400)+yday;
returnn%7;
}
//是否闰年
boolisLeapYear(constintyear)
{
return(year%4==0&&year%100!=0)||year%400==0;
}
//获取当天是一年中的第几天
intgetYearDay(constintyear,constintmonth,constintmday)
{
intyday=0;
for(inti=1;i {
switch(i)
{
case1:
case3:
case5:
case7:
case8:
case10:
case12:
yday+=31;
break;
case4:
case6:
case9:
case11:
yday+=30;
break;
case2:
yday+=(isLeapYear(year)?29:28);
break;
}
}
yday+=mday;
returnyday;
}
//计算两个日期的天数差(假设date1小于date2)
intgetDayCount(conststructtm&date1,conststructtm&date2)
{
intnday=0;
intyday1=getYearDay(date1.tm_year+1900,date1.tm_mon+1,date1.tm_mday);
intyday2=getYearDay(date2.tm_year+1900,date2.tm_mon+1,date2.tm_mday);
for(inti=date1.tm_year;i {
if(isLeapYear(i+1900))
nday+=366;
elsenday+=365;
}
nday+=(yday2-yday1);
returnnday;
}
//公历节日,随便弄15个。
constchar*festivals[]={
"1,1","元旦",
"2,14","情人节",
"3,8","妇女节",
"3,12","植树节",
"3,15","消费者权益日",
"4,1","愚人节",
"4,5","清明节",
"5,1","劳动节",
"5,4","青年节",
"6,1","儿童节",
"7,1","建党节",
"8,1","建军节",
"9,10","教师节",
"10,1","国庆节",
"12,25","圣诞节"
};
char*weekdays[]={"日","一","二","三","四","五","六"};
voidsetDate(structtm&date,constintyear,constintmonth,constintday)
{
date.tm_year=year;
date.tm_mon=month;
date.tm_mday=day;
}
voidtestDate1()
{
intmonth,year,day;
cout<<"请输入你想知道的年月日(用逗号隔开):";
scanf("%d,%d,%d",&year,&month,&day);
//cin>>year>>month>>day;
//距离元旦的天数
intyday=getYearDay(year,month,day);
//将日期赋值到tm结构体中,方便函数调用
structtmdate;
setDate(date,year-1900,month-1,day);
//取得今天的时间,并转换为tm结构体
time_tnow=time((time_t*)NULL);
structtm*today=localtime(&now);
intnday;
//将年月日换算成如20160412的整数然后进行比较,简单直观
if(year*10000+(month-1)*100+day>=
(today->tm_year+1900)*1000+today->tm_mon*100+today->tm_mday)
nday=getDayCount(*today,date);
elsenday=getDayCount(date,*today);
printf("%d,%d,%d离今天有%d天!\n",year,month,day,nday);
intwday=getWeekDay(year,yday);
cout<<"是星期"< //看看今天是什么节日
charszDate[10]={'\0'};
sprintf(szDate,"%d,%d",month,day);
for(inti=0;i<30;i+=2)
{
if(strcmp(szDate,festivals[i])==0)
{
cout<<"是"< break;
}
}
}
voidtestDate2()
{
structtmdate1900;
setDate(date1900,0,0,1);
intyear,month,day;
cout<<"请输入你想知道的年月日(用逗号隔开):";
scanf("%d,%d,%d",&year,&month,&day);
structtmdate;
setDate(date,year-1900,month-1,day);
//求出所输入日期和1900年1月1日的日期差就能判断
intndays=getDayCount(date1900,date);
//三天打渔两天晒网,即每5天一个周期。
intcycle=ndays%5;
//对5求模,则结果是0-3的话就是打渔,否则就是晒网
if(cycle<3)
cout<<"这天是在打渔!"< else
cout<<"这天是在晒网!"<}
intmain()
{
cout<<"1.离今天还有多少天!\n"<<
"2.是打渔还是晒网!\n"<<
"3.退出!"< intop;
do
{
cout<<"请选择1,2,3!\n";
cin>>op;
if(op<1||op>3)
continue;
switch(op)
{
case1:
testDate1();
break;
case2:
testDate2();
break;
default:
break;
}
}while(op!=3);
system("pause");
return0;
}

用c语言设定一个纪念日时间,输入一个日期,显示当天距离纪念日的天数。。大佬们求帮忙,小弟明天有急用

#include
#include
#defineYear2018
#defineMonth1
#defineDay8//纪念日时期,可自行更改
intyear_alldays(intyear);
intyear_sumday(intyear,intmonth);
intget_alldays(intyear,intmonth,intday);
intmain()
{
intyear,month,day;
printf("纪念日日期为:%d-%02d-%02d\n",Year,Month,Day);

printf("输入一个日期(如2017-03-21):");
scanf("%d-%d-%d",&year,&month,&day);

intday1=get_alldays(year,month,day);
intday2=get_alldays(Year,Month,Day);

printf("距离纪念日天数为:%d\n",abs(day1-day2));
return0;
}
//获取公历年的天数
intyear_alldays(intyear)
{
if((year%4==0&&year%100!=0)||year%400==0)return366;elsereturn365;
}
//获取公历年初至某整月的天数
intyear_sumday(intyear,intmonth)
{
intsum=0;
intrui[12]={31,29,31,30,31,30,31,31,30,31,30,31};
intping[12]={31,28,31,30,31,30,31,31,30,31,30,31};
intruiflag=0;
if((year%4==0&&year%100!=0)||year%400==0)ruiflag=1;
for(intindex=0;index{
if(ruiflag==1)sum+=rui[index];elsesum+=ping[index];
}
returnsum;
}
//获取从公历1800年1月25日至当前日期的总天数
intget_alldays(intyear,intmonth,intday)
{
inti=1800,days=-24;
while(i{
days+=year_alldays(i);
i++;
}
intdays2=year_sumday(year,month);
returndays+days2+day;
}

标签:C(编程语言) 编程语言 编程 CC++ 信息技术

大明白知识网 Copyright © 2020-2022 www.wangpan131.com. Some Rights Reserved. 京ICP备11019930号-18