关于C++编写万年历程序10
发布网友
发布时间:2023-09-21 21:36
我来回答
共1个回答
热心网友
时间:2024-11-30 18:39
// abc4.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<stdio.h>
#include<time.h>
#include <iostream>
using namespace std;
void week(int y, int m, int d)//日期对于的星期
{
int c,w;
char *weekstr[] = {
"Sun","Mon","Tue","Wed","Thu","Fri","Sta"
};
c = y / 100 + 1;
if( m <= 2)
{
m = m + 12 ;
y = y - 1;
}
w = ( y + y / 4 + c / 4 - 2 * c + 26 * ( m + 1 ) / 10 + d - 1 ) % 7;
cout << weekstr[w];
//if(w == 0)
//cout << "Sun";
//else if (w == 1)
//cout<<"Mon";
//else if (w == 2)
//cout<<"Tue";
//else if (w == 3)
//cout<<"wed";
//else if (w == 4)
//cout<<"Thu";
//else if ( w == 5)
//cout<<"Fri";
//else
//cout << "Sta";
}
void Initdate(int &y, int &m, int &d, int &h, int &min, int &sec)//根据系统时间初始化万年历
{
time_t t;
time( &t );
struct tm * local_time = localtime( &t );
sec = local_time->tm_sec;
min = local_time->tm_min ;
h = local_time->tm_hour ;
d = local_time->tm_mday ;
m = local_time->tm_mon+1;
y = local_time->tm_year + 1900;
}
void printmonth(int y, int m, int d)//打印出给定时间对于月历
{
int ym[2][13]={
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
int leap,i;
leap = ( y % 4 == 0 && y % 100 != 0 || y % 400 == 0);
for( i = 1 ; i < d ; i++ )
{
cout << i << " ";
week( y, m, i);
cout << endl;
}
cout << "★" ;
for(i = d; i <= ym[leap][m]; i++ )
{
cout << i << " ";
week( y, m, i);
cout << endl;
}
}
int judge(int y, int m, int d)//判断给定时间是否存在
{
int ym[2][13]={
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
int leap, i;
if( y <= 0 || m < 1 || m > 12 )
return 0;
leap = ( y % 4 == 0 && y % 100 != 0 || y % 400 == 0 );
if( d < 1 || d > ym[leap][m] )
return 0;
else
return 1;
}
int search(int y, int m, int d)//查找打印给定时间对应阅历
{
if ( judge( y, m, d ) == 0 )
cout << "error date" ;
else
printmonth( y, m, d );
return 0;
}
int main()
{
int y, m, d, h, min, sec;//系统时间参数
int cho;//用户选项
int yt, mt, dt;//拥护输入的时间参数
while(1)
{
cout<<"≡≡≡≡≡≡万年历菜单选项≡≡≡≡≡"<<endl;
cout<<"请按编号选择功能"<<endl;
cout<<" ⑴查询当前时间"<<endl;
cout<<" ⑵显示本月月历"<<endl;
cout<<" ⑶查找给定日期对应的月历"<<endl;
cout<<" ⑷退出万年历"<<endl;
cin>>cho;
Initdate(y,m,d,h,min,sec);
switch(cho){
case 1: cout << "当前时间为" << y << "." << m << "." << d << "." << " " << h << ":" << min << ":" << sec << " ";
week(y,m,d);
break;
case 2: printmonth(y,m,d);break;
case 3: cout<<"请输入所要查找的日期,格式如 2000 7 5";
cin>>yt;
cin>>mt;
cin>>dt;
search(yt,mt,dt);
break;
case 4: exit(1);break;
default :cout<<"输入错误,请重新输入";
}
}
return 0;
}