求助:用C语言编万年历程序

作者&投稿:符肢 (若有异议请与网页底部的电邮联系)
怎么用C语言编写万年历程序~

万年历

#include "stdio.h" /* Required for MS-DOS use */
#define ENTER 0x1C0D /* Enter key */
int year, month, day;
static char *days[8] = {" ","Sunday ","Monday ","Tuesday ",
"Wednesday","Thursday ","Friday ","Saturday "};
struct TIMEDATE {
int year; /* year 1980..2099 */
int month; /* month 1=Jan 2=Feb, etc. */
int day; /* day of month 0..31 */
int hours; /* hour 0..23 */
int minutes; /* minute 0..59 */
int seconds; /* second 0..59 */
int hsecs; /* 1/100ths of second 0..99 */
char dateline[47]; /* date & time together */
};
static struct TIMEDATE today;
main()
{
char cmonth[3];
char cday[3];
char cyear[5];
double getdays();
double daynumb, numbnow;
int weekday, retcode, dayer, i;
dayer = datetime(&today);
clrscn();
for (i=0;i<3;++i)cmonth[i]='\0';
for (i=0;i<3;++i)cday[i]='\0';
for (i=0;i<5;++i)cyear[i]='\0';
putstr(5,8,14,"Enter date in MM DD YYYY format:");
while (retcode != ENTER)
{
retcode = bufinp(5,41,13,2,cmonth);
if (retcode != ENTER) retcode = bufinp(5,44,13,2,cday);
if (retcode != ENTER) retcode = bufinp(5,47,13,4,cyear);
}
year = atoi(&cyear);
month = atoi(&cmonth);
day = atoi(&cday);
daynumb = getdays(year, month, day);
numbnow = getdays(today.year, today.month, today.day);
weekday = weekdays(daynumb);
if (numbnow - daynumb == 0)
printf("

%02d-%02d-%d is",month, day, year);
if (numbnow - daynumb > 0)
printf("

%02d-%02d-%d was",month, day, year);
if (numbnow - daynumb < 0)
printf("

%02d-%02d-%d will be",month, day, year);
printf(" a %s
",days[weekday]);
} /* end MAIN */
/************************************************************
* GETDAYS - From integer values of year (YYYY), month *
* (MM) and day (DD) this subroutine returns a *
* double float number which represents the *
* number of days since Jan 1, 1980 (day 1). *
* This routine is the opposite of GETDATE. *
************************************************************/
double getdays(year, month, day)
int year, month, day;
{
int y,m;
double a,b,d, daynumb;
double floor(),intg();
/**********************************
** make correction for no year 0 **
**********************************/
if (year < 0) y = year + 1;
else y = year;
/*********************************************************
** Jan and Feb are months 13 and 14 in this calculation **
*********************************************************/
m = month;
if (month < 3)
{
m = m + 12;
y = y - 1;
}
/**************************
** calculate Julian days **
**************************/
d = floor(365.25 * y) + intg(30.6001 * (m + 1)) + day - 723244.0;
/**********************************************
** use Julian calendar if before Oct 5, 1582 **
**********************************************/
if (d < -145068.0) daynumb = d;
/*************************************
** otherwise use Gregorian calendar **
*************************************/
else
{
a = floor(y / 100.0);
b = 2 - a + floor(a / 4.0);
daynumb = d + b;
}
return(daynumb);
} /* end GETDAYS */
/********************************************************
* GETDATE - This routine takes a double float number *
* representing the number of days since Jan 1,*
* 1980 (day 1) and returns the year month and *
* day as pointer integers *
* This routine is the opposite of GETDAYS *
********************************************************/
getdate(numb)
double numb;
{
double a,aa,b,c,d,e,z;
double date;

date = numb;
z = intg(date + 2444239.0);
if (date < -145078.0) a = z;
else
{
aa = floor((z - 1867216.25) / 36524.25);
a = z + 1 + aa - floor(aa/4.0);
}
b = a + 1524.0;
c = intg((b - 122.1) / 365.25);
d = intg(365.25 * c);
e = intg((b - d) / 30.6001);
day = b - d - intg(30.6001 * e);
if (e > 13.5) month = e - 13.0;
else month = e - 1.0;
if (month > 2) year = c - 4716.0;
else year = c - 4715.0;
if (year < 1) --year;
return;
} /* end GETDATE */
/********************************************************
* WEEKDAYS - This routine takes a double float number *
* representing the number of days since Jan 1,*
* 1980 (day 1) and returns the day of the week*
* where 1 = Sunday, 2 = Tuesday, etc. *
********************************************************/
int weekdays(numb)
double numb;
{
double dd;
int day;

dd = numb;
while (dd > 28000.0) dd = dd - 28000.0;
while (dd < 0) dd = dd + 28000.0;
day = dd;
day = ((day + 1) % 7) + 1;
return(day);
}
/********************************************************
* FRACT - This routine takes a double float number *
* and returns the fractional part as a double *
* float number *
********************************************************/
double fract(numb)
double numb;
{
int inumb;
double fnumb;

while (numb < -32767) numb += 32767;
while (numb > 32767) numb -= 32767;
inumb = numb;
fnumb = inumb;
return(numb-fnumb);
} /* end FRACT */
/********************************************************
* FLOOR - This routine takes a double float number *
* and returns the next smallest integer *
********************************************************/
double floor(numb)
double numb;
{

double fract(), intg();
double out;
out = intg(numb);
if (numb < 0 && fract(numb) != 0) out -= 1.0;
return(out);
} /* end FLOOR */
/********************************************************
* INTG - This routine takes a double float number *
* and returns the integer part as a double *
* float number *
********************************************************/
double intg(numb)
double numb;
{
double fract();
return(numb - fract(numb));
} /* end INTG */

写万年历程序,您需要先了解万年历的特点
下面是万年历的特点(复制粘贴的):
1. 平年365天(52周+1天),闰年366天(52周+2天)。平年2月28天,闰年2月29天。
由于公元1月1日设为星期六,故3月1日为星期三。 ——注意这个“三”
为使算法达到最简,故本算法以“星期”为计算单位。且选3月1日为基月。
2. 每400年整一闰,或每4年且不为百年的一闰。(原因:地球绕太阳一周的时间是365天5小时46秒,为了使一年的天数为整数,将一年的天数定为365天,余下的时间积累起来,四年就是23小时15分4秒,将近一天,把这一天加在某年的二月而成29天,该年称为闰年,其它年称为平年。但四年加一天又多用了44分56秒,这个数积满400年为三天。因此400年中只能有97个闰年,所以凡能被400整除,或不能被100整除但能被4整除的年份为闰年。)
所以百年%4=0闰或(年%4=0并且年0)闰。
3. 每 4年(3个平年+1个闰年)共208周+5天 ——注意这个“5天”
每百年共100*(208周+5天)-1天=5217周+5天 ——注意这个“5天”(整百年暂设为平年)
每400年共4*(5217周+5天)+1天(整400年闰)=20871周+0天——注意这个“0天”和“1天”(4个整百年只有一个闰年)
即400年一轮回!(原来万年历400年前是一家)
至于你说的日期和星期对齐,给你一点思路参考:每一行是7天,可以设定每一天占10个字符(同时上面的星期也得占满10个字符),假设本月第一天是星期4,那么在输出1之前需要先输出10×3个字符来占位,当输出星期天的日期之后换行,之后的就简单了

# include "stdio.h"
int week(int y,int m,int d);
void main()
{
int monthday[12]={31,28,31,30,31,30,31,31,30,31,30,31};

int y,w,i,m=1,d=1;
printf("请输入一个年份yyyy:\n");
scanf("%d",&y);
if (y%4==0&&y%100!=0) monthday[1]=29;

for(m=1;m<=12;m++)
{

printf("\n %d年,%d月\n",y,m);
printf("S M T w T F S \n");

for (d=1;d<=monthday[m-1];d++)
{
w=week(y,m,d);
if(d==1)
{
for (i=0;i<w;i++) printf(" ");
}

if(d<10) printf("%d ",d);
else printf("%d ",d);
if(w==6) printf(" \n");
}

}
}

int week (int y,int m,int d)
{
int w;
if((m==1)||(m==2))
{
y--;
m+=12;
}
w=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7;
return (w);
}

运行结果如下:

请输入一个年份yyyy:
2001

2001年,1月
S M T w T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
2001年,2月
S M T w T F S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28
2001年,3月
S M T w T F S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

2001年,4月
S M T w T F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
2001年,5月
S M T w T F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
2001年,6月
S M T w T F S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

2001年,7月
S M T w T F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
2001年,8月
S M T w T F S
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
2001年,9月
S M T w T F S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
2001年,10月
S M T w T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
2001年,11月
S M T w T F S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
2001年,12月
S M T w T F S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 请按任意键继续. . .

#include<stdio.h>

int day_of_year(int year,int month,int day,int *pi);
int days_of_year(int year);
int day(int year);
int day_of_month(int year,int month,int *pi);
void print(int year,int month);

static int daytab[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}
};

void main()
{
int year,i;

printf("Please input the year:\n");
scanf("%d",&year);
for(i=1;i<13;i++)
print(year,i);
}

int day_of_year(int year,int month,int day,int *pi)//是某年的第多少天
{
int i,leap;

leap=(year%4==0)&&(year%100!=0)||(year%400==0);

for(i=1;i<month;i++)
day+=*(pi+leap*13+i);
return day;
}

int days_of_year(int year)//某年有多少天
{
int leap=0;
leap=(year%4==0)&&(year%100!=0)||(year%400==0);
return (365+leap);
}

int day(int year)//某年到1901年1月1日的天数
{
int days=0;
if(year>1901)
{ while(year>1901)
days+=days_of_year(--year);
return days;
}
else
if(year==1901)
return days=0;
else
{
while(year<1901)
days+=days_of_year(year++);
return days;
}
}

int day_of_month(int year,int month,int *pi)//某年某月有多少天
{
int leap;
leap=(year%4==0)&&(year%100!=0)||(year%400==0);
return (*(pi+leap*13+month));
}

void print(int year,int month)//输出某月的日历
{
int dayy,dayd,mday,tday;
int i,j,w_day;

dayy=day(year);
dayd=day_of_year(year,month,1,&daytab[0][0]);

mday=day_of_month(year,month,&daytab[0][0]);

if(year>=1901)
{
tday=(dayd+dayy)%7;
}

else
{
tday=(dayy-dayd)%7;
}
w_day=(tday+1)%7;
printf("**************%d月**************\n",month);
printf(" Sum Mon Tus Wen Thu Fri Sat\n");
for(j=0;j<w_day;j++)
printf(" ");
for(i=1;i<=mday;i++)
{
printf("%4d",i);
if((i+w_day)%7==0)
printf("\n");
}
printf("\n");
}
我用的是1901年的1月1日星期二。并且2006年的1月1日不是星期六,这一点很重要,一开始我用2006年1月1日星期六,得到的程序结果老是不对。我后来检查才发现问题所在。希望现在发过去能帮到你。


谁能用C语言编万年历(要与系统时间关联),在线等
谁能用C语言编万年历(要与系统时间关联),在线等 编写一万年历系统要求:模仿现实生活中的挂历.当前页以系统当前日期的月份为准显示当前月的每一天(显示出日及对应的星期几),当系统日期变到下一月时,系统自动翻页到下一月。... 编写一万年历系统要求: 模仿现实生活中的挂历. 当前页以系统当前日期的月份为准...

如何用C语言编写一个万年历系统?
\/*这段是1602的库函数程序*\/ define LCD_DATA P0 \/\/控制口定义 sbit LCD_RS = P2^2;\/\/LCD指令\/数据寄存器选择 sbit LCD_RW = P2^1;\/\/LCD读\/写控制 sbit LCD_EN = P2^0;\/\/LCD使能 sbit LCD_BF = P0^7;\/\/LCD忙 code uchar num[ ]={'0','1','2','3','4','5','6'...

用c语言编写万年历
已经编译运行确认过。INCLUDE <STDIO.H> INCLUDE <STDLIB.H> INT LEAP(INT); \/*判断是否为闰年*\/ INT MD(INT M,INT Y); \/*返回本月的天数*\/ VOID PRTWEEK(VOID); \/*输出表头*\/ INT FW(INT M,INT Y); \/*返回本月一号是星期几*\/ VOID PRTMONTH(INT M); \/*输出月份表头*\/ INT ...

万年历的C语言编法
ww(int a,int b,int c){ int n=0,m=0,i,j,k=0;for(i=1;i<c;i++){ if(leapyear(i)==1)m=m+366;else m=m+365;} for(j=1;j<b;j++){if(leapyear(c)==1) k=k+cc[j-1];else k=k+aa[j-1];} n=(m+k+a)%7;return n;} void yuefen(int m){ cout<<"...

一个用C语言写的万年历程序,看不懂,求详细注释和这个程序的算法思想...
一下是大概的注释,不是特别详细,但是你看了应该能明白是怎么回事了 include<stdio.h> main(){ for(;;) \/\/"万年",就是永远都运行 int nian,yue,a,b,m,c,_m=28; \/\/m, c都是做星期控制的,见下面, _m是2月,2月分平年闰年 long e;printf("请输入年份:");scanf("%d",&nian...

用C语言编写一个万年历时候,怎么确定一年中第一天是星期几?
之后,算出某年与2011年共相差几天(本程序中用sum累计),如果year>2011,先自减一,计算year-1那年有多少天 考虑到365%7=1,366%7=2,所以用sum+=1和sum+=2分别统计平年和闰年应该加多少天 最后,返回(sum+6)%7,(2011年1月1日是星期6)如果year<2011,先减sum,再year++,比如2008年1月1日...

编写c语言万年历程序 要求输入能够查询任意年份的日历或某年某月的...
int countyear,countday=0,weekflag,i,j,k,c;int startmon,endmon,year1;year1=year-1;countyear=(year1-year1%4)\/4-(year1-year10)\/100 (year1-year1@0)\/400;weekflag=(countyear*366 (year-1-countyear)*365 1)%7-1;if(month==0) {startmon=1;endmon=12;} else {startmon...

如何用c语言或者c++编程万年历?
你好!这样的效果可以吗,两个效果,私信说明一下

用c语言编程万年历
printf("%c\\n",x);if(x=='N' || x=='n') { rili(year,month+1); } else if(x=='P' || x=='p') { rili(year,month-1); } else { printf("感谢使用!88\\n"); return;} } int runniansub(int year){ return ((year%4==0 && year%100!=0) || (year%4==0 ...

怎么用c语言编写万年历,要求输入日期显示星期
\/\/算元旦的星期 s=x-1+(x-1)\/4-(x-1)\/100+(x-1)\/400+c x为年 c为从元旦起的总天数 include "iostream.h"include "stdlib.h"void print1(int y){ cout<<y<<"月"<<"\\t\\t"<<"日"<<"\\t"<<"一"<<"\\t"<<"二"<<"\\t"<<"三"<<"\\t"<<"四"<<"\\t"<<"五"<<...

曲阳县19533016066: c语言编写万年历 -
戢甘优尼: #include"stdio.h" #include"stdlib.h" #include"windows.h" void welcome(); void getday(int year,int month); void shuru(); void end(); long year; long month; void welcome() {printf("【万年历程序加载中");for(int i=0;i<30;i++){printf("★"...

曲阳县19533016066: 求用C语言编写打印万年历的程序代码 -
戢甘优尼: #include int monthDay(int,int); int IsLeapYear(int); main() { int days,weekDay,year,month=1,d,i; printf("please input the year:\n"); scanf("%d",&year); days=year-1+(year-1)/400+(year-1)/4-(year-1)/100; while(month

曲阳县19533016066: 跪求万年历的C语言源程序~ -
戢甘优尼: #include <stdio.h>void main() {printf("该程序求某天星期几和打印本年年历,不合法输入便自动退出:\n");printf("请输入年 月 日:");while(1){loop:// printf("请输入年 月 日:");int o,p,q;int w,y,m,d,c,yy,xingqi;scanf("%...

曲阳县19533016066: C语言程序设计万年历怎么编写 -
戢甘优尼: #include int IsLeapYear(int); main() { int i; int day; int year; int temp; int temp_i; long int Year_days = 0; int Year_Start = 1; int Per_Year_Days; int month_day[]={31,28,31,30,31,30,31,31,30,31,30,31,29}; printf("Please enter the year: "); scanf("%d...

曲阳县19533016066: 如何用c语言做一个万年历? -
戢甘优尼: 想要就拿去吧! #include #include char* month_str[]={"January","February","March","April","May","June","July","August","September","October","November","December"}; char* week[]={"Sunday","Monday",...

曲阳县19533016066: 求万年历的c语言程序编法,谢谢谢
戢甘优尼: #include<stdio.h> #include<time.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==...

曲阳县19533016066: c语言 万年历的程序设计 -
戢甘优尼: #include <windows.h> #include <winnt.h> #include<iostream>#include<iomanip> using namespace std; int week(int,int,int); //根据年月日判断星期几 int leap_year(int); //判断闰年 void display_year(int ); //显示某年日历 void demand_day(int,int,...

曲阳县19533016066: 如何用C语言编一个万年历系统 -
戢甘优尼: #include int day,year,month; int isLeap() {int isleap; if(((year%4==0)&&!(year%100==0))||(year%400==0))isleap=1; else isleap=0; return isleap;} int lastdayofmonth() {int lastday,isleap; switch (month) {case 1: case 3: case 5: case 7:case 8: case 10: ...

曲阳县19533016066: 求一个用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...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网