用C++编写一个程序,判断星期几?
用C++编写一个程序:计算从某年某月某日到今天又多少天,以及那天是星期几(知道今天星期几),建议使用类。感谢...
用C++编写一个程序:计算从某年某月某日到今天又多少天,以及那天是星期几(知道今天星期几),建议使用类。感谢
展开
展开全部
#include "stdio.h"
main()
{
int day,month,year,sum,leap;
printf("\nplease input year,month,day\n");
scanf("%d,%d,%d",&year,&month,&day);
switch(month)/*先计算某月以前月份的总天数*/
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
default:printf("data error");break;
}
sum=sum+day; /*再加上某天的天数*/
if(year%400==0||(year%4==0&&year%100!=0))/*判断是不是闰年*/
leap=1;
else
leap=0;
if(leap==1&&month>2)/*如果是闰年且月份大于2,总天数应该加一天*/
sum++;
printf("It is the %dth day.",sum);}
main()
{
int day,month,year,sum,leap;
printf("\nplease input year,month,day\n");
scanf("%d,%d,%d",&year,&month,&day);
switch(month)/*先计算某月以前月份的总天数*/
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
default:printf("data error");break;
}
sum=sum+day; /*再加上某天的天数*/
if(year%400==0||(year%4==0&&year%100!=0))/*判断是不是闰年*/
leap=1;
else
leap=0;
if(leap==1&&month>2)/*如果是闰年且月份大于2,总天数应该加一天*/
sum++;
printf("It is the %dth day.",sum);}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <conio.h>
#include <iostream>
using namespace std;
class MyDate
{
public:
void Print();
void SetWeek(int w);
int GetWeek();
int Compare(MyDate other);
int Differ(MyDate&other);
int TotalDays();
bool IsLeap();
static bool IsLeap(int Year);
int DayOfYear();
static int TotalDays(int Year);
MyDate(int y,int m,int d,int w);
private:
static int Days[12];
int year;
int mon;
int day;
int week;
};
int MyDate::Days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
MyDate::MyDate(int y,int m,int d,int w)
{
if(y<=0)
{
cerr<<"年份不能为"<<y<<endl
<<"自动修正为1900"<<endl;
y=1900;
}
year=y;
if(m>12 || m<=0)
{
cerr<<"年份不能为"<<m<<endl
<<"自动修正为1"<<endl;
m=1;
}
mon=m;
int maxday=0;
if(m==2)
{
if(IsLeap(y))
{
maxday=29;
}
else
{
maxday=28;
}
}
else
{
maxday=Days[m-1];
}
if(d>maxday || d<=0)
{
cerr<<year<<"年"<<mon<<"月的日期不能为"<<d<<endl
<<"自动修正为1"<<endl;
d=1;
}
day=d;
week=w%7;
}
int MyDate::DayOfYear()
{
int days=0;
for(int i=1;i<mon;i++)
{
if(i==2)
if(IsLeap())
days++;
days+=Days[i-1];
}
days+=day;
return days;
}
bool MyDate::IsLeap()
{
if ((year%400==0) || (year%4==0 && year%100!=0))
return true;
else
return false;
}
bool MyDate::IsLeap(int Year)
{
if ((Year%400==0) || (Year%4==0 && Year%100!=0))
return true;
else
return false;
}
int MyDate::TotalDays()
{
if(IsLeap())
return 366;
else
return 365;
}
int MyDate::TotalDays(int Year)
{
if(IsLeap(Year))
return 366;
else
return 365;
}
int MyDate::Differ(MyDate &other)
{
int days=0;
int day1=this->DayOfYear();
int day2=other.DayOfYear();
if (Compare(other)>=0)
{
for(int i=other.year;i<year;i++)
days+=TotalDays(i);
days+=(day1-day2);
days=-days;
}
else
{
for(int i=year;i<other.year;i++)
days+=TotalDays(i);
days+=(day2-day1);
}
return days;
}
int MyDate::Compare(MyDate other)
{
if(year==other.year)
{
if(mon==other.mon)
{
return day-other.day;
}
else
{
return mon-other.mon;
}
}
else
{
return year-other.year;
}
}
int MyDate::GetWeek()
{
return week;
}
void MyDate::SetWeek(int w)
{
week=w%7;
}
void MyDate::Print()
{
cout<<year<<"年"
<<mon<<"月"
<<day<<"日";
}
main()
{
int a,b,c,d;
int days,week;
char WEEK[7][8]=
{
"星期日",
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六"
};
cout<<"请输入今天的年 月 日 星期几(0表示周日)"<<endl;
cin>>a>>b>>c>>d;
MyDate today(a,b,c,d);
cout<<"请输入要计算的那一天的年 月 日"<<endl;
cin>>a>>b>>c;
MyDate thatday(a,b,c,0);
/*
MyDate today(2010,9,26,0);
MyDate thatday(2010,9,26,0);
*/
days=today.Differ(thatday);
if(days>0)
{
week=(days%7+today.GetWeek())%7;
cout<<"从今天(";
today.Print();
cout<<")到";
thatday.Print();
cout<<")有"
<<days
<<"天。"
<<endl;
}
else if(days<0)
{
week=(7-(-days)%7+today.GetWeek())%7;
cout<<"从";
thatday.Print();
cout<<"到今天(";
today.Print();
cout<<")有"
<<-days
<<"天。"
<<endl;
}
else
{
week=today.GetWeek();
thatday.Print();
cout<<"就是今天!"
<<endl;
}
thatday.SetWeek(week);
thatday.Print();
cout<<"是"
<<WEEK[week]
<<endl;
getch();
return 1;
}
#include <iostream>
using namespace std;
class MyDate
{
public:
void Print();
void SetWeek(int w);
int GetWeek();
int Compare(MyDate other);
int Differ(MyDate&other);
int TotalDays();
bool IsLeap();
static bool IsLeap(int Year);
int DayOfYear();
static int TotalDays(int Year);
MyDate(int y,int m,int d,int w);
private:
static int Days[12];
int year;
int mon;
int day;
int week;
};
int MyDate::Days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
MyDate::MyDate(int y,int m,int d,int w)
{
if(y<=0)
{
cerr<<"年份不能为"<<y<<endl
<<"自动修正为1900"<<endl;
y=1900;
}
year=y;
if(m>12 || m<=0)
{
cerr<<"年份不能为"<<m<<endl
<<"自动修正为1"<<endl;
m=1;
}
mon=m;
int maxday=0;
if(m==2)
{
if(IsLeap(y))
{
maxday=29;
}
else
{
maxday=28;
}
}
else
{
maxday=Days[m-1];
}
if(d>maxday || d<=0)
{
cerr<<year<<"年"<<mon<<"月的日期不能为"<<d<<endl
<<"自动修正为1"<<endl;
d=1;
}
day=d;
week=w%7;
}
int MyDate::DayOfYear()
{
int days=0;
for(int i=1;i<mon;i++)
{
if(i==2)
if(IsLeap())
days++;
days+=Days[i-1];
}
days+=day;
return days;
}
bool MyDate::IsLeap()
{
if ((year%400==0) || (year%4==0 && year%100!=0))
return true;
else
return false;
}
bool MyDate::IsLeap(int Year)
{
if ((Year%400==0) || (Year%4==0 && Year%100!=0))
return true;
else
return false;
}
int MyDate::TotalDays()
{
if(IsLeap())
return 366;
else
return 365;
}
int MyDate::TotalDays(int Year)
{
if(IsLeap(Year))
return 366;
else
return 365;
}
int MyDate::Differ(MyDate &other)
{
int days=0;
int day1=this->DayOfYear();
int day2=other.DayOfYear();
if (Compare(other)>=0)
{
for(int i=other.year;i<year;i++)
days+=TotalDays(i);
days+=(day1-day2);
days=-days;
}
else
{
for(int i=year;i<other.year;i++)
days+=TotalDays(i);
days+=(day2-day1);
}
return days;
}
int MyDate::Compare(MyDate other)
{
if(year==other.year)
{
if(mon==other.mon)
{
return day-other.day;
}
else
{
return mon-other.mon;
}
}
else
{
return year-other.year;
}
}
int MyDate::GetWeek()
{
return week;
}
void MyDate::SetWeek(int w)
{
week=w%7;
}
void MyDate::Print()
{
cout<<year<<"年"
<<mon<<"月"
<<day<<"日";
}
main()
{
int a,b,c,d;
int days,week;
char WEEK[7][8]=
{
"星期日",
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六"
};
cout<<"请输入今天的年 月 日 星期几(0表示周日)"<<endl;
cin>>a>>b>>c>>d;
MyDate today(a,b,c,d);
cout<<"请输入要计算的那一天的年 月 日"<<endl;
cin>>a>>b>>c;
MyDate thatday(a,b,c,0);
/*
MyDate today(2010,9,26,0);
MyDate thatday(2010,9,26,0);
*/
days=today.Differ(thatday);
if(days>0)
{
week=(days%7+today.GetWeek())%7;
cout<<"从今天(";
today.Print();
cout<<")到";
thatday.Print();
cout<<")有"
<<days
<<"天。"
<<endl;
}
else if(days<0)
{
week=(7-(-days)%7+today.GetWeek())%7;
cout<<"从";
thatday.Print();
cout<<"到今天(";
today.Print();
cout<<")有"
<<-days
<<"天。"
<<endl;
}
else
{
week=today.GetWeek();
thatday.Print();
cout<<"就是今天!"
<<endl;
}
thatday.SetWeek(week);
thatday.Print();
cout<<"是"
<<WEEK[week]
<<endl;
getch();
return 1;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询