QQ网名大全

c语言 万年历的程序设计

#include <windows.h>
#include <winnt.h>
#include<iostream>
#include<iomanip>
using namespace std;
int week(int,int,int); //根据年月日判断星期几
int leap_year(int); //判断闰年
void d**play_year(int ); //显示某年日历
void demand_day(int,int,int); //查询某天
int main()
{
int y,m,d,es=1;
while(es)
{
handle consolehwnd;
consolehwnd = getstdhandle(std_output_handle);
setconsoletextattribute(consolehwnd,12);
cout<<"请选择操作:\n1→显示某年日历\
\n2→查询某天\n0→退出"<<endl;
char tp[20];cin>>tp;
if(tp[1]!='\0'||tp[0]>'2'||tp[0]<'0'){cout<<"输入有误"<<endl;continue;}
switch(tp[0]-48)
{
case 1:{cout<<"请输入年份:";cin>>y;system("cls");d**play_year(y);break;}
case 2:{cout<<"请输入年、月、日,以空格分开:";cin>>y>>m>>d;system("cls");
demand_day(y,m,d);break;}
case 0:{es=0;break;}
}
}
return 0;
}
//-----根据年月日判断星期几-------------------------
int week(int y,int m, int d)
{
int week1,yy=y;
if(m==1) {m=13;yy--;}
if(m==2) {m=14;yy--;}
week1=(d+2*m+3*(m+1)/5+yy+yy/4-yy/100+yy/400)%7;
int s;
switch (week1)
{
case 0: s=1; break;
case 1: s=2; break;
case 2: s=3; break;
case 3: s=4; break;
case 4: s=5; break;
case 5: s=6; break;
case 6: s=0; break;
}
return s;
}
//----判断闰年-------------------------------------
int leap_year(int y)
{
int i;
if((y%4==0&&y%100!=0)||y%400==0)i=1;
else i=0;
return i;
}
//--------显示某年日历------------------------
void d**play_year(int y)
{
int n1,n2,i,j,a[13],c,d;
handle consolehwnd;
consolehwnd = getstdhandle(std_output_handle);
setconsoletextattribute(consolehwnd,5);
cout<<setw(38)<<y<<"年"<<endl;
cout<<setw(28)<<"*********";
for(i=1;i<=27;i++)cout<<'*';
cout<<endl;
a[1]=a[3]=a[5]=a[7]=a[8]=a[10]=a[12]=31;//
a[4]=a[6]=a[9]=a[11]=30; //确定每月天数
if(leap_year(y))a[2]=29;
else a[2]=28; //
for(i=1;i<=11;i+=2) //六次循环
{
setconsoletextattribute(consolehwnd,1);
cout<<setw(14)<<i<<"月"<<setw(42)<<i+1<<"月"<<endl;
setconsoletextattribute(consolehwnd,2);
cout<<setw(4)<<"日"<<setw(4)<<"一"<<setw(4)<<"二"<<setw(4)<<"三"<<setw(4)\
<<"四"<<setw(4)<<"五"<<setw(4)<<"六";
cout<<setw(16)<<' ';
cout<<setw(4)<<"日"<<setw(4)<<"一"<<setw(4)<<"二"<<setw(4)<<"三"<<setw(4)\
<<"四"<<setw(4)<<"五"<<setw(4)<<"六"<<endl;
setconsoletextattribute(consolehwnd,7);
n1=week(y,i,1);n2=week(y,i+1,1);
if(n1) //-----------
{
for(j=1;j<=n1;j++) //
cout<<setw(4)<<' ';
}
for(j=1;j<=7-n1;j++)
cout<<setw(4)<<j;
cout<<setw(16)<<' ';
if(n2)
{ //-----输出每次循环的第一行---
for(j=1;j<=n2;j++)
cout<<setw(4)<<' ';
}
for(j=1;j<=7-n2;j++)
cout<<setw(4)<<j;
cout<<endl; //--------------
c=8-n1;d=8-n2;
for(int m=1;m<6;m++) //每月日历最多占六行
{
if(c>a[i])cout<<setw(4*7)<<' ';//若c>a[i],则该月的这一行全部输出空格
for(j=c;j<=a[i];j++)
{
cout<<setw(4)<<j;
if((j-c+1)%7==0){c=j+1;break;}
if(j==a[i]){cout<<setw((6-week(y,i,a[i]))*4)<<' ';c=j+1;break;}
//如果j是该月最后一天,该行剩下的全部补空格
}
cout<<setw(16)<<' ';
if(d>a[i+1])cout<<setw(4*7)<<' ';
for(j=d;j<=a[i+1];j++)
{ //
cout<<setw(4)<<j;
if((j-d+1)%7==0){d=j+1;break;}
if(j==a[i+1]){cout<<setw((6-week(y,i+6,a[i+1]))*4)<<' ';d=j+1;break;}
}
cout<<endl;
}
cout<<endl;
}
cout<<endl;
}
//--------查询某天------------
void demand_day(int y,int m,int d)
{
int n;
handle consolehwnd;
consolehwnd = getstdhandle(std_output_handle);
setconsoletextattribute(consolehwnd,5);
n=week(y,m,d);
switch(n)
{
case 1:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期一"<<endl;break;
case 2:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期二"<<endl;break;
case 3:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期三"<<endl;break;
case 4:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期四"<<endl;break;
case 5:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期五"<<endl;break;
case 6:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期六"<<endl;break;
case 0:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期日"<<endl;break;
default:break;
}
cout<<endl;
}
佚名
2024-06-05 22:40:03
最佳回答
类似问题(10)
  • 佚名
    2024-06-05 13:18:12

    设计一个万年历查询程序。

    用什么语言开发,j**a方面的话,我可以帮你,你要用什么开发语言呀

  • 佚名
    2024-06-05 08:00:00

    万年历的c语言程序代码包括节假日农历,农历转公历,公历农历查询某

    直接给你整个工程吧,实现了星期计算、公历转农历、农历转公历、节气查询。非常不错的代码,你要的年份范围可以自己决定,支持从公元1600年到公元6400年的四千八百...

  • 佚名
    2024-06-05 08:00:00

    用c语言编写万年历

    写万年历程序,您需要先了解万年历的特点下面是万年历的特点(复制粘贴的):1. 平年365天(52周+1天),闰年366天(52周+2天)。平年2月28天,闰年2...

  • 佚名
    2024-06-05 08:00:00

    万年历的c语言程序

    已经编译运行确认过。#include <stdio.h> #include <stdlib.h> int leap(int); /*判断是否为闰年*...

  • 佚名
    2024-06-05 08:00:00

    单片机万年历的怎样设计的?c语言版

    分析:硬件:需要设计89c51+lcd1602,一般有现成的;再采用 spi 三线接口设计ds1302,一般也有类似的;估计还需要设计键盘,输入时间。程序:功能...

  • 佚名
    2024-06-05 08:00:00
  • 佚名
    2024-06-05 08:00:00

    求用c语言编写打印万年历的程序代码

    #include<stdio.h>int monthday(int,int);int **leapyear(int);main(){ int d...

  • 佚名
    2024-06-05 08:00:00

    c语言万年历问题

    你在第一个for循环里加一个prnt("\n");不就好了、、、我不知道你的month数组里是怎么存的~你得算好每个月第一天是星期几,然后填好空格、建议你的输入...

  • 佚名
    2024-06-05 08:00:00

    万年历查询程序要c++的

    随便写了一个,大概250行代码,是基于自己多年前开发的库来实现的,如果是写来使用,源码工程拿去就行,如果是交作业的,需要自己去加工一下。源码密码:tu0q不带库...

  • 佚名
    2024-06-05 08:00:00

    c语言程序设计万年历

    #include <stdio.h> int leap (int year) {if(year%4==0&&year%100!=0||ye...