1. 用c语言设计一个简单的万年历怎么写代码
#include"stdio.h"#include"stdlib.h"main(){intYear,Month;//年、月intFirstDay_Year,FirstDay_Month;//某年的第一天是星期几,某年某月的第一天是星期几(范围是0~6,其中0代表星期日)intIsLeapYear;//是否为闰年,0表示不是闰年,1表示是闰年inti,d,y;//临时变量charYN;//YesNo,程序是否要继续intDays[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};//Days[1~12]存储每个月有多少天,其中二月的天数是可变的(闰年29天,平年28天),这里初始化为28天printf("C语言简单万年历\n");//打印标题XunHuan://循环标号(可以通过goto跳转到这里)printf("请输入年份:");//提示输入年份(0~9999)scanf("%d",&Year);//把输入的年份赋值给变量Yearprintf("请输入月份:");//提示输入月份(1~12)scanf("%d",&Month);//把输入的月份赋值给变量Monthy=Year;FirstDay_Year=5*(y/4)+(y%4)-(y/100)+(y/400);//蔡勒公式(计算某年的第一天是星期几)IsLeapYear=(y%4==4&&y%100!=100||y%400==0)?1:0;//判断是否为闰年Days[2]=(IsLeapYear==1)?29:28;//闰年二月29天,非闰年二月28天for(i=1,d=0;i
2. C语言程序设计万年历
#include
<stdio.h>
int
leap
(int
year)
{if(year%4==0&&year%100!=0||year%400==0)
//判断是否是闰年
return
1;
else
return
0;
}
int
days_month
(int
month,int
year)
//判断大月和小月
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
//找出大月
return
31;
if(month==4||month==6||month==9||month==11)
//找出小月
return
30;
if(month==2&&leap(year)==1)
return
29;
//判断二月是29天还是29天
else
return
28;
}
int
firstday(int
month,int
year)
{int
w;
w=(1+2*month+3*(month+1)/5+year+year/4+year/400-year/100)%7+1;
//判断每个月开始的第一天是星期几
return
w;
}
main()
{int
i,j=1,k=1,a,b,month,year;
printf("\n
input
month
and
year:\n");
scanf("%d%d",&month,&year);
//输入月和年
b=days_month(month,year);
a=firstday
(month,year);
printf("
Sun
Mon
Tue
Wed
Thu
Fri
Sat
\n");
//输出对应当月的日历
if(a==7)
{for(i=1;i<=b;i++)
{printf("%4d",i);
if(i%7==0)
{printf("\n");
}
}
}
if(a!=7)
{while
(j<=4*a)
{printf("
");
j++;
}
for(i=1;i<=b;i++)
{printf("%4d",i);
if(i==7*k-a)
{printf("\n");
k++;
}
}
}
printf("\n");
}
3. 万年历C语言
// 万年历.cpp : Defines the entry point for the console application.
// 显示1900年以后任何年份的日历,日历以月份顺序排列,每月以星期顺序排列。
//采用一般日历计算方法,先给出一般年份的每月天数,如果是闰年,第二个月天数为29,再计算出制定年份的1月1日是星期几,然后根据这些数据
//计算出全年日历,边计算边显示。
//函数leap()判定是否闰年
//函数week()计算year年份的1月1日是星期几。计算方法是,已知1900年1月1日是星期一,先计算从1900年到year年份的前一年一共有多少(s)天,
//则(s+1)%7为年份year的1月1日的星期号。
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
static char *week0=" SUN MON TUE WED SUR FRI SAT"; //指针形式定义一个字符串,或者是week0[36]
int leap(int year)
{
if((year%4==0 && year%100!=0)|| year%400==0)
return 1;
else return 0;
}
int week(int year)
{
int i,w;
int s=0;
for(i=1900;i<year;i++)
{
if(leap(i))
s+=366;
else s+=365; //计算从1900年到year年的天数
}
w=(s+1)%7;
return w;
}
void main()
{
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int i,j,k;
int year;
int w;
do
{
cout<<"输入年份:";
cin>>year;
}while(year<=1900);
w=week(year);
if(leap(year))
month[1]=29;
for(i=0;i<12;i++)
{
for(k=0;k<35;k++)
cout<<"_"; //输出一条横线
cout<<endl;
cout<<setw(20)<<i+1<<"月"<<endl;
cout<<endl;
cout<<week0<<endl;
for(k=0;k<w;k++)
cout<<" "; //输出若干空格
for(j=1;j<=month[i];j++)
{
if((j+w)%7==1) //遇到星期日时,换一行输出
cout<<endl;
cout<<setw(5)<<j;
}
cout<<endl;
for(k=0;k<35;k++)
cout<<"_"; //输出一条横线
cout<<endl;
w=(w+month[i])%7; //计算下个月1号是星期几
}
cout<<endl;
system("pause");
}
4. 用C语言怎么编写万年历
C语言编写万年历的源代码如下:
#include <stdio.h>
int week(int y,int m,int d);
int main()
{
char wk[7][3] = {{"天"},{"一"},{"二"},{"三"},{"四"},{"五"},{"六"}};
int year, month, day, w;
scanf("%d %d %d", &year, &month, &day);
w = week(year, month, day);
printf("%d年%d月%d日 星期%s", year, month, day, wk[w]);
}
int week(int y,int m,int d)
{
static int r[13]={0,0,3,3,6,1,4,6,2,5,0,3,5};
int c,w;
y %= 400;
if ((y==0||y%4==0&&y%100!=0)&&m<3)
c = 5;
else
c = 6;
w = (y+y/4-y/100+r[m]+d+c)%7;
return w;
}
(4)c语言万年历节假日扩展阅读
电子时钟C语言版的源代码如下:
#include<stdio.h>
#include<time.h>
#include<string.h>
int main()
{
char str[50];
time_t t;
struct tm *nt;
while(1)
{
system("cls");//清屏函数
t=time(NULL);
nt=localtime(&t);
strftime(str,50,"%Y-%m-%d %H:%M:%S “,nt);
printf("现在时刻:%s ",str);
}
getch();
return 0;
}
5. 用C语言怎么编写这个万年历
核心算法:星期(0~6)=(年份系数+月份系数+日期)%7;
其中,年份系数我以2007年做参照,已知2007年,年份系数是0,往前往后分别是递减和递增。增减规律:非闰年为±1,闰年1~2月-2或+1,闰年3~12月+2或者-1。
其中,12个月份系数对应:0,3,3,6,1,4,6,2,5,0,3,5。
根据以上规律,只要利用循环,就可以的到对应系数和星期。
日历实现翻页功能,按键盘左右方向键可以翻译查看当年其他月份,按向上方向键可以返回菜单。
下面是代码:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <conio.h>
#include <math.h>
#include <time.h>
#define XY 2007
#define X07 0//以2007年年系数0做参照
int isLeapYear(int year);//判断是否是闰年,是返回1,否返回0
int addyx(int yx,int n);//年系数自增,0~6,n:自增的跨度,返回自增后的年系数
int getYX(int year,int month);//获得年系数0~6
int getMX(int month);//获得月系数
int getWeek(int yx,int mx,int day);//通过年月系数(yx:年系数;mx:月系数)及日期获取星期
char *getStrWeek(int yx,int mx,int day);//通过年月系数(yx:年系数;mx:月系数)及日期获取星期对应的中文字符串
int getMaxDay(int year,int month);//通过年月获取当月最大天数
void prCalendar(int year,int month);//打印日历
int showMeun();//打印主菜单
int getDuration(int year,int month,int day);//通过日期获取距今天数
int calendar[6][7];
int main()
{
//int year,month,day,yx,mx;
while(1)
{
if(!showMeun())
break;
}
return 0;
}
int showMeun()//打印主菜单
{
int n,year,month,day,yx,mx;
while(1)
{
system("cls");
printf("1、输入年份,显示该年每个月的日历 ");
printf("2、输入年月,显示该月的天数 ");
printf("3、输入日期,显示距离今天的天数 ");
printf("4、输入日期,查询这一天是星期几 ");
printf("0、结束程序 ");
n=-1,year=0,month=0,day=0;
while(n<0 || n>4)
{
printf("请输入选择的菜单序号:");
scanf("%d",&n);
}
switch(n)
{
case 1:while(year<=0 || year>9999)
printf("请输入年份:"),scanf("%d",&year);
prCalendar(year,1);
break;
case 2:while(year<=0 || year>9999 || month<=0 || month>12)
printf("请输入年份及月份:"),scanf("%d%d",&year,&month);
printf("%4d年%2d月共有%d天 按任意键继续。。。。。。 ",year,month,getMaxDay(year,month)),getch();
break;
case 3:while(year<=0 || year>9999 || month<=0 || month>12 || day<0 || day>getMaxDay(year,month))
printf("请输入日期:"),scanf("%d%d%d",&year,&month,&day);
printf("距离今日有%d天 按任意键继续。。。。。。 ",getDuration(year,month,day)),getch();
break;
case 4:while(year<=0 || year>9999 || month<=0 || month>12 || day<0 || day>getMaxDay(year,month))
printf("请输入日期:"),scanf("%d%d%d",&year,&month,&day);
yx=getYX(year,month);
mx=getMX(month);
printf("%4d年%2d月%2d日是%s 按任意键继续。。。。。。 ",year,month,day,getStrWeek(yx,mx,day)),getch();
break;
case 0:return 0;
}
}
return n;
}
void prCalendar(int year,int month)//打印日历
{
char c1,c2;
int i,j,*p=NULL,yx,mx,cnt,w,maxDay;
while(1)
{
p=&calendar[0][0];
yx=getYX(year,month),mx=getMX(month);
cnt=0,w=getWeek(yx,mx,1);
maxDay=getMaxDay(year,month);
for(i=0;i<6;i++)
for(j=0;j<7;j++)
{
if(cnt<w)
p++,cnt++;
calendar[i][j]=0;
}
for(i=1;i<=maxDay;i++)
*p=i,p++;
system("cls");
printf(" %4d 年%2d月 ",year,month);
printf("日 一 二 三 四 五 六 ");
for(i=0;i<6;i++,printf(" "))
for(j=0;j<7;j++)
if(calendar[i][j]==0)
printf(" ");
else
printf("%2d ",calendar[i][j]);
printf("<- 按方向键向左或向右翻页 -> ");
printf(" 按向上方向键返回主菜单 ");
c1=getch();
c2=getch();
if(c1==-32 && c2==75)//左键
{
if(month==1)
month=12;
else
month--;
}
if(c1==-32 && c2==77)//右键
{
if(month==12)
month=1;
else
month++;
}
if(c1==-32 && c2==72)//上键
{
showMeun();
break;
}
}
}
int getDuration(int year,int month,int day)//通过日期获取距今天数
{
int i,y,m,d,sum=0,sum2=0,minY,maxY,minM,maxM,minD,maxD;
time_t tt;
struct tm *tmp;
time(&tt);
tmp=localtime(&tt);
y=1900+tmp->tm_year,m=1+tmp->tm_mon,d=tmp->tm_mday;//获取当前日期的年月日
if(year<y)
minY=year,maxY=y,minM=month,maxM=m,minD=day,maxD=d;
if(year>y)
minY=y,maxY=year,minM=m,maxM=month,minD=d,maxD=day;
if(year!=y)
{
for(i=minY;i<maxY;i++)//按相差年份累加(不包含右端最大年份)
if(isLeapYear(i))
sum+=366;//闰年
else
sum+=365;
for(i=1;i<minM;i++)//扣除左端年份已过月份对应天数
sum-=getMaxDay(minY,i);
sum-=minD;//扣除左端当月已过天数
for(i=1;i<maxM;i++)//累加右端最大年份已过月份对应天数(不包含最大年份当月)
sum+=getMaxDay(maxY,i);
sum+=maxD;//累加右端当月已过天数
}
else//如果年份相同,累加两边一年内已过天数,求差值
{
for(i=1;i<month;i++)
sum+=getMaxDay(month,i);
sum+=day;
for(i=1;i<m;i++)
sum2+=getMaxDay(m,i);
sum2+=d;
return abs(sum2-sum);
}
return sum;
}
int getMaxDay(int year,int month)//通过年月获取当月最大天数
{
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(year) && month==2)
return days[month-1]+1;
return days[month-1];
}
char *getStrWeek(int yx,int mx,int day)//通过年月系数(yx:年系数;mx:月系数)及日期获取星期对应的中文字符串
{
int w;
static char week[7];
memset(week,0,7);
strcpy(week,"星期");
w=getWeek(yx,mx,day);
switch(w)
{
case 0:strcat(week,"日");break;
case 1:strcat(week,"一");break;
case 2:strcat(week,"二");break;
case 3:strcat(week,"三");break;
case 4:strcat(week,"四");break;
case 5:strcat(week,"五");break;
case 6:strcat(week,"六");break;
}
return week;
}
int getWeek(int yx,int mx,int day)//通过年月系数(yx:年系数;mx:月系数)及日期获取星期对应的数值0~6
{
return (yx+mx+day)%7;
}
int getMX(int month)//获得月系数
{
int xmonth[12]={0,3,3,6,1,4,6,2,5,0,3,5};//月系数:1-12月,每月的系数。
return xmonth[month-1];
}
int getYX(int year,int month)//获得年系数0~6
{
int yx=0,nyear=XY,isly,flag;
if(nyear==year)
return 0;
if(nyear<year)
flag=0,nyear++;
else
flag=1,nyear--;
while(1)
{
isly=isLeapYear(nyear);
if(!isly){//非闰年年系数累加1
if(!flag)
yx=addyx(yx,1);
else
yx=addyx(yx,-1);
}
if(isly && month>=1 && month<=2){//闰年1~2月年系数累加1,3`12月年系数累加2
if(!flag)
yx=addyx(yx,1);
else
yx=addyx(yx,-2);
}
else if(isly && month>=3 && month<=12){
if(!flag)
yx=addyx(yx,2);
else
yx=addyx(yx,-1);
}
if(nyear==year)
break;
if(!flag && nyear<year)
nyear++;
if(flag && nyear>year)
nyear--;
}
return yx;
}
int addyx(int yx,int n)//年系数自增,0~6,n:自增的跨度,返回自增后的年系数
{
if(n>0)
{
if(yx+n<=6)
return yx+n;
if(yx+n>6)
return yx+n-7;
}
if(n<0)
{
if(yx+n>=0)
return yx+n;
if(yx+n<0)
return yx+n+7;
}
return -1;
}
int isLeapYear(int year)//判断是否是闰年,是返回1,否返回0
{
if((year%4==0 && year%100!=0)||(year%400==0))
return 1;
return 0;
}
6. C语言编写万年历~~速度
/*C语言编程万年历
要求输入年月,判断是否闰年;
输入年月日,判断星期几;
输入年份,打出12个月的月历;
输入年份,月份,打印出本月日历;
要求用多个函数实现。*/
#include<stdio.h>
#include<time.h>
#include<string.h>int calendar[12][6][7];/*月历*/
char* week[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Satarday"};
char* Monthname[]={"January","February","March","April","May","June","July",
"August","September","October","November","December"};
int monthday[]={31,28,31,30,31,30,31,31,30,31,30,31};
char* menu[]={/*操作菜单*/
"1.Input a year number,check whether it's a leap year.\n",
"2.Input year,month,day,check the weekday.\n",
"3.Input year,month,output the calendar of that month.\n",
"4.Input year,output all the month calendar.\n",
"0.Exit.\n"
};/*判断参数year传递的年份是否是闰年*/
int IsLeapyear(int year)
{
if(!(year%4)&&year%100||!(year%400)) return 1;
else return 0;
}/*输入年月日,判断星期几,利用Zeller公式w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
w是结果星期数,y是年份的后两位,c是年份的前两位,m是月份,3≤m≤14,也就是当
m≤2时,要算到前一年的13月份和14月份,最后要将w对7取模
*/
int WeekDay(int year,int month,int day)
{
int w,y,c,m,d;
c=year/100;
if(month<3){
m=12+month;
y=year%100-1;
}
else{
m=month;
y=year%100;
}
d=day;
w=y+y/4+c/4-2*c+26*(m+1)/10+d-1;
return (w%7+7)%7;
}/*输入年份,月份,打印出本月的日历*/
void Monthly(int year,int month)
{
int weekday,i,j;
if(month==2)
if(IsLeapyear(year)) monthday[1]+=1;
weekday=WeekDay(year,month,1);
printf("%s\n",Monthname[month-1]);
printf("Sun. Mon. Tue. Wed. Thu. Fri. Sat.\n");
for(i=1,j=weekday;i<=monthday[month-1];i++,j++){
calendar[month-1][j/7][j%7]=i;
}
for(i=0;i<6;i++){
for(j=0;j<7;j++)
{
if(calendar[month-1][i][j]==0) printf("%5c",' ');
else printf("%-5d",calendar[month-1][i][j]);
}
printf("\n");
}
}void allMonth(int year) /*输入年份,打印出12个月的月历*/
{
int i;
for(i=1;i<=12;i++){
Monthly(year,i);
getch();/*按任意键继续执行*/
}
}void main(void)
{
int year,month,day,i,n,weekday;
memset(&calendar,sizeof(calendar),0); /*初始化月历*/
for(i=0;i<5;i++)
printf("%s",menu[i]);
while(1){
printf("Please choose the menu:");
scanf("%d",&n);
printf("\n");
switch(n){
case 1:
printf("Please input year:");
scanf("%d",&year);
if(IsLeapyear(year)) printf("\n%d is leap year.\n",year);
else printf("%d isn't leap year.\n",year);
break;
case 2:
printf("Please input year month day:");
scanf("%d%d%d",&year,&month,&day);
printf("\n");
weekday=WeekDay(year,month,day);
printf("That day is %s\n",week[weekday]);
break;
case 3:
printf("Please input year month,then it output a calendar of that month:");
scanf("%d%d",&year,&month);
printf("\n");
Monthly(year,month);
break;
case 4:
printf("Please input year,then it will output the calendar of that year:");
scanf("%d",&year);
printf("\n");
allMonth(year);
break;
case 0:
return;
default:
printf("The number you input is invalid.\n");
break;
}
}
getch(); /*按任意键,程序退出*/
}这个程序是可用的~~希望能帮到你~~~
7. 用C语言制作万年历
TC 2.0
/* welcome to use the WanNianLi system!
Copyright @ 2005 KongXinCai All rights reserved!:):):)*/
#include<stdio.h>
#include<stdlib.h>
char* month_str[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
char* week[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int IsLeapYear(int year) /*find out the year is leap year or not*/
{
if((year%4==0&&year%100!=0)||(year%400==0))
return 1;
else
return 0;
}
int month_day(int year,int month)
{
int mon_day[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(IsLeapYear(year)&&month==2)
return 29;
else
return(mon_day[month-1]);
}
int DaySearch(int year,int month,int day) /*search what day this day is*/
{
int c=0;
float s;
int m;
for(m=1;m<month;m++)
c=c+month_day(year,m);
c=c+day;
s=year-1+(float)(year-1)/4+(float)(year-1)/100+(float)(year-1)/400-40+c;
return ((int)s%7);
}
int PrintAllYear(int year)/*print the all year*/
{
int temp;
int i,j;
printf("\n\n%d Calander\n",year);
for(i=1;i<=12;i++)
{
printf("\n\n%s(%d)\n",month_str[i-1],i);
printf("0 1 2 3 4 5 6 \n");
printf("S M T W T F S \n\n");
temp=DaySearch(year,i,1);
for(j=1;j<=month_day(year,i)+temp;j++)
{
if(j-temp<=0)
printf(" ");
else if(j-temp<10)
printf("%d ",j-temp);
else
printf("%d ",j-temp);
if(j%7==0)
printf("\n");
}
}
return 0;
}
int main()
{
int option,da;
char ch;
int year,month,day;
printf("Copyright @ 2005 TianQian All rights reserved!:):):)");
printf("\n\nWelcome to use the WanNianLi system!\n");
while(1)
{
printf("\nPlease select the service you need:\n");
printf("\n1 Search what day the day is");
printf("\n2 Search whether the year is leap year or not");
printf("\n3 Print the calander of the whole year");
printf("\n4 Exit\n");
scanf("%d",&option);
switch(option)
{
case 1:
while(1)
{
printf("\nPlease input the year,month and day(XXXX,XX,XX):");
scanf("%d,%d,%d,%c",&year,&month,&day);
da=DaySearch(year,month,day);
printf("\n%d-%d-%d is %s,do you want to continue?(Y/N)",year,month,day,week[da]);
fflush(stdin);
scanf("%c",&ch);
if(ch=='N'||ch=='n')
break;
}
break;
case 2:
while(1)
{
printf("\nPlease input the year which needs searched?(XXXX)");
scanf("%d",&year);
if(IsLeapYear(year))
printf("\n%d is Leap year,do you want to continue?(Y/N)",year);
else
printf("\n%d is not Leap year,do you want to continue(Y/N)?",year);
fflush(stdin);
scanf("%c",&ch);
if(ch=='N'||ch=='n')
break;
}
break;
case 3:
while(1)
{
printf("\nPlease input the year which needs printed(XXXX)");
scanf("%d",&year);
PrintAllYear(year);
printf("\nDo you want to continue to print(Y/N)?");
fflush(stdin);
scanf("%c",&ch);
if(ch=='N'||ch=='n')
break;
}
break;
case 4:
fflush(stdin);
printf("Are you sure?(Y/N)");
scanf("%c",&ch);
if(ch=='Y'||ch=='y')
exit(0);
break;
default:
printf("\nError:Sorry,there is no this service now!\n");
break;
}
}
return 0;
}