怎样用C++设计一个输入年月日来确定星期几的程序啊?
2个回答
展开全部
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int year;
int month;
int monthcodearr[12]={1,4,4,7,2,5,7,3,6,1,4,6};
char week[7][10]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
int day;
printf("input the date(YYYY-MM-DD)\n");
scanf("%d-%d-%d",&year,&month,&day); //这里主要是格式化输入。2011-6-20要按照这个格式输入。
int yearcode;
int monthcode;
if(year > 2000)
{
yearcode = 4-2*((year-2000)/4)+(year-2000)%4+1;
}
else if(year == 2000)
{
yearcode = 4;
}
else
{
yearcode = 4+2*(2000-year)/4-(2000-year)%4;
}
monthcode = monthcodearr[month-1];
int weekcode = (yearcode+monthcode+day)%7;
cout<<yearcode<<endl;
cout<<monthcode<<endl;
cout<<day<<endl;
cout<<weekcode<<endl;
cout<<week[weekcode]<<endl;
}
#include<stdio.h>
using namespace std;
int main()
{
int year;
int month;
int monthcodearr[12]={1,4,4,7,2,5,7,3,6,1,4,6};
char week[7][10]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
int day;
printf("input the date(YYYY-MM-DD)\n");
scanf("%d-%d-%d",&year,&month,&day); //这里主要是格式化输入。2011-6-20要按照这个格式输入。
int yearcode;
int monthcode;
if(year > 2000)
{
yearcode = 4-2*((year-2000)/4)+(year-2000)%4+1;
}
else if(year == 2000)
{
yearcode = 4;
}
else
{
yearcode = 4+2*(2000-year)/4-(2000-year)%4;
}
monthcode = monthcodearr[month-1];
int weekcode = (yearcode+monthcode+day)%7;
cout<<yearcode<<endl;
cout<<monthcode<<endl;
cout<<day<<endl;
cout<<weekcode<<endl;
cout<<week[weekcode]<<endl;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询