输出万年历

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了输出万年历相关的知识,希望对你有一定的参考价值。

一、目的和要求
1.综合应用C语言的基本语句,编写应用程序。
2.通过程序设计进一步熟悉C语言循环语句、if语句,switch-case语句。灵活应用格式输出函数pringf,编写一定难度的应用程序。
3.计算的结果必须正确。
二、实验内容
输入任一年号、输出这一年的年历。
三、算法
首先在一行按等距离输出星期的英文单词或缩写,再第2题的方法确定该年的元旦是星期几,就确定了1月1日输出在星期几的位置,12个月循环,再每个月的天数循环按等距离输出日,每行输出完星期六后换行,一个月输出完了也换行。

以当前电脑时间为基准,显示当前年月的日历
可以按左右方向键,翻到上一月或下一月
可以按上下方向键,翻到上一年或下一年
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

int leap(int year )

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

return 1;

return 0;


void show(int year,int month)

const char month_str[][4]="","Jan","Feb","Mar","Apl",
"May","Jun","Jul","Aug","Sep","Oct","Nov","Dec";
const int month_day[]=0,31,28,31,30,31,30,31,31,30,31,30,31;
int i,j,wdays,mdays,days;

for(i=1,days=0;i<year;i++)

if(leap(i))

days += 366;

else

days += 365;


for(i=1;i<month;i++)

if(i==2 && leap(year))

days+=29;

else

days+=month_day[i];



printf(" %s (%d)\n",month_str[month],year);
printf(" Mon Tue Wed Thu Fri Sat Sun\n");
wdays = days % 7;
for( j = 0; j < wdays; j++)

printf(" ");

if(month == 2 && leap(year))

mdays=29;

else

mdays= month_day[month];

for(i=1;i<=mdays;i++)

if( i > 1 && days % 7 == 0 )

printf("\n");

printf("%4d",i);
days=days+1;

printf("\n---------------------------\n");


main()

time_t rawtime;
struct tm *info;
int year,month;
char ch;

time ( &rawtime );
info = localtime ( &rawtime );
year =info->tm_year + 1900;
month =info->tm_mon + 1;
while(1)

show(year,month);
printf("Left....Prev Month\n");
printf("Right...Next Month\n");
printf("Up......Prev Year\n");
printf("Down....Next Year\n");
printf("Esc.....Exit\n");
ch=getch();
switch(ch)

case 27://Ecs
exit(0);
case -32://Navigator
ch=getch();
if(ch==77)
//Right
year+=(month==12)?1:0;
month=month%12+1;

else if(ch==75)
//Left
year-=(month==1)?1:0;
month=(month-2+12)%12+1;

else if(ch==72)
//Up
year--;

else if(ch==80)
//Down
year++;

system("cls");


参考技术A #include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define X " Sun Mon Tue Wed Thu Fri Sat"

void menu()

system("cls");
printf("\nTHIS IS THE MENU OF THE PROGRAM !");
printf("\nYOU CAN CHOOSE THE NUMBER FOR THE FUNCTIOM:");
printf("\n\n");
printf("1 Find the day by year-month-date.\n");
printf("2 Find out whether the year you input is a leap year.\n");
printf("3 Print the calendar of the year you input.\n");
printf("4 Exit.\n\n");
printf("Input your choice:");


int getday(int year,int month,int date)

int isleap(int year);
int flag,s,i;
int a[13]=0,31,28,31,30,31,30,31,31,30,31,30,31;
int cont=0;
flag=isleap(year);
if(flag==1)
a[2]++;
for(i=1;i<month;i++)

cont=cont+a[i];

cont=cont+date;
s=year+1+(year-1)/4+(year-1)/100+(year-1)/400+cont;
return s%7;

int isleap(int year)

if(year%4==0&&year%100||year%400==0)
return 1;
else
return 0;

void print(int n)

int i;
for(i=0;i<n;i++) printf(" ");

int day(int year)

long a,b;
if(year<=2000)

a=2000-year;
b=6-(a+a/4-a/100+a/400)%7;
return b;

else

a=year-2000;
b=(a+1+(a-1)/4-(a-1)/100+(a-1)/400)%7+6;
return b%7;


void printcalendar(int year)

int i,j,k,m,n,f1,f2,d;
int a[13]=0,31,28,31,30,31,30,31,31,30,31,30,31;
printf("\nThe calendar of the year %d.\n\n",year);
d=day(year);
if(isleap(year)==1)
a[2]++;
for(i=1;i<=12;i+=2)

m=0; n=0; f1=0; f2=0;
switch(i)

case 1:printf(" Januray 1 ");break;
case 3:printf(" March 3 ");break;
case 5:printf(" May 5 ");break;
case 7:printf(" July 7 "); break;
case 9:printf(" September 9 ");break;
case 11:printf(" Nevember 11 ");break;

print(21);
switch(i+1)

case 2:printf(" February 2 "); break;
case 4:printf(" April 4 "); break;
case 6:printf(" June 6 "); break;
case 8:printf(" August 8 "); break;
case 10:printf(" October 10 "); break;
case 12:printf(" December 12"); break;

printf("\n");
printf(X);
print(6);
printf(X);
printf("\n");
for(j=0;j<6;j++)

if(j==0)

print(d*4);
for(k=0;k<7-d;k++)

printf("%4d",++m);

print(6);
d+=a[i]%7;
d%=7;
print(d*4);
for(k=0;k<7-d;k++)

printf("%4d",++n);

printf("\n");

else

for(k=0;k<7;k++)

if(m<a[i])

printf("%4d",++m);

else

print(4);

if(m==a[i]) f1=1;

print(6);
for(k=0;k<7;k++)

if(n<a[i+1])

printf("%4d",++n);

else

print(4);

if(n==a[i+1]) f2=1;

printf("\n");
if(f1&&f2) break;


d+=a[i+1]%7;
d%=7;
printf(" ");
for(k=0;k<27;k++)

printf("=");

print(6);
printf(" ");
for(k=0;k<27;k++)

printf("=");

printf("\n");
if(i==5)

printf("Press any key to others!");
getch();
system("cls");



void main()

int choice;
int year,month,date;
int day,flag;
char con;
menu();
scanf("%d",&choice);
if(choice==1)

r1:system("cls");
printf("\nPlease input the year-month-date(XXXX,XX,XX):");
scanf("%d,%d,%d",&year,&month,&date);
day=getday(year,month,date);
if(day==0)
printf("\n%d-%d-%d is Sunday!\n",year,month,date);
if(day==1)
printf("\n%d-%d-%d is Monday!\n",year,month,date);
if(day==2)
printf("\n%d-%d-%d is Tuesday!\n",year,month,date);
if(day==3)
printf("\n%d-%d-%d is Wednesday!\n",year,month,date);
if(day==4)
printf("\n%d-%d-%d is Thursday!\n",year,month,date);
if(day==5)
printf("\n%d-%d-%d is Friday!\n",year,month,date);
if(day==6)
printf("\n%d-%d-%d is Saturday!\n",year,month,date);
printf("\nContinue...(Y/N)");
con=getch();
if((con=='y')||(con=='Y'))
goto r1;
if((con=='n')||(con=='N'))
main();

if(choice==2)

r2:system("cls");
printf("\nPlease input the year(XXXX):");
scanf("%d",&year);
flag=isleap(year);
if(flag==1)
printf("\nThe year %d is leap year!\n",year);
if(flag==0)
printf("\nThe year %d is not leap year!\n",year);
printf("\nContinue...(Y/N)");
con=getch();
if((con=='y')||(con=='Y'))
goto r2;
if((con=='n')||(con=='N'))
main();

if(choice==3)

r3:system("cls");
printf("\nPlease input the year(XXXX):");
scanf("%d",&year);
printcalendar(year);
printf("Continue...(Y/N)");
con=getch();
if((con=='y')||(con=='Y'))
goto r3;
if((con=='n')||(con=='N'))
main();

if(choice==4)

system("cls");
printf("\nDo you want to Exit?(Y/N)");
con=getch();
if((con=='y')||(con=='Y'))
return;
if((con=='n')||(con=='N'))
main();



/*
【要求】:

1. 程序运行后,首先在屏幕上显示主菜单:

1. 查询某年某月某日是星期几

2. 查询某年是否是闰年

3. 打印某年的全年日历

4. 退出

2. 在主菜单中输入1后,显示:

“请输入年月日(XXXX,XX,XX)”

运行后输出:XXXX年XX月XX日是星期X,是否继续查询(Y/N)?

如果输入Y,则重新显示 “请输入年月日(XXXX,XX,XX)”,否则回到主菜单。

3. 在主菜单中输入2后,显示:

“请输入要查哪一年?(XXXX)”

运行后输出:XXXX年是(否)是闰年,是否继续查询(Y/N)?

如果输入Y,则重新显示,“请输入要查哪一年?(XXXX)”,否则回到主菜单。

4. 在主菜单中输入3后,显示:

“请输入要打印的年份(XXXX)”

运行后输出XXXX年的日历,格式为:

XXXX

X(月数)

0 1 2 3 4 5 6

S M T W T F S

x x x x x x x

x x x xx xx xx xx

xx xx xx xx xx xx xx

xx xx xx xx xx xx xx

xx xx xx

X(月数)

0 1 2 3 4 5 6

S M T W T F S

x x x x

x x x xx xx xx xx

xx xx xx xx xx xx xx

xx xx xx xx xx xx xx

xx xx xx xx xx

.

运行完后显示:“是否继续打印(Y/N)?”

如果输入Y,则重新显示,“请输入要打印的年份(XXXX)”,否则回到主菜单。

5. 在主菜单中输入4后,显示:“是否要真的退出(Y/N)?”

如果输入Y,结束程序运行,否则重新显示主菜单。
*/
参考技术B #include "stdio.h"
long int f(int year,int month)
/*f(年,月)=年-1,如月<3;否则,f(年,月)=年*/
if(month<3) return year-1;
else return year;


long int g(int month)
/*g(月)=月+13,如月<3;否则,g(月)=月+1*/
if(month<3) return month+13;
else return month+1;


long int n(int year,int month,int day)

/*N=1461*f(年、月)/4+153*g(月)/5+日*/
return 1461L*f(year,month)/4+153L*g(month)/5+day;


int w(int year,int month,int day)

/*w=(N-621049)%7(0<=w<7)*/
return(int)((n(year,month,day)%7-621049L%7+7)%7);


int date[12][6][7];
int day_tbl[ ][12]=31,28,31,30,31,30,31,31,30,31,30,31,
31,29,31,30,31,30,31,31,30,31,30,31;
main()
int sw,leap,i,j,k,wd,day;
int year;/*年*/
char title[]="SUN MON TUE WED THU FRI SAT";
clrscr();
printf("Please input the year whose calendar you want to know: ");/*输入年*/
scanf("%d%*c",&year);/*输入年份值和掠过值后的回车*/
sw=w(year,1,1);
leap=year%4==0&&year%100||year%400==0;/*判闰年*/
for(i=0;i<12;i++)
for(j=0;j<6;j++)
for(k=0;k<7;k++)
date[i][j][k]=0;/*日期表置0*/
for(i=0;i<12;i++)/*一年十二个月*/
for(wd=0,day=1;day<=day_tbl[leap][i];day++)
/*将第i+1月的日期填入日期表*/
date[i][wd][sw]=day;
sw=++sw%7;/*每星期七天,以0至6计数*/
if(sw==0) wd++;/*日期表每七天一行,星期天开始新的一行*/


printf("\n|==================The Calendar of Year %d =====================|\n|",year);
for(i=0;i<6;i++)
/*先测算第i+1月和第i+7月的最大星期数*/
for(wd=0,k=0;k<7;k++)/*日期表的第六行有日期,则wd!=0*/
wd+=date[i][5][k]+date[i+6][5][k];
wd=wd?6:5;
printf("%2d %s %2d %s |\n|",i+1,title,i+7,title);
for(j=0;j<wd;j++)

printf(" ");/*输出四个空白符*/
/*左栏为第i+1月,右栏为第i+7月*/
for(k=0;k<7;k++)
if(date[i][j][k])
printf("%4d",date[i][j][k]);
else printf(" ");
printf(" ");/*输出十个空白符*/
for(k=0;k<7;k++)
if(date[i+6][j][k])
printf("%4d",date[i+6][j][k]);
else printf(" ");
printf(" |\n|");

/*scanf("%*c");/*键入回车输出下一个月的日历*/


puts("=================================================================|");
puts("\n Press any key to quit...");
getch();

万年历

打印万年历(输入年份,月份,输出该月的日历,已知1900年1月1日是星期一),要求:

(1)编写一个方法判断闰年;

(2)编写一个方法判断某年某月有多少天;

(3)编写一个方法计算某年某月前距离1900年1月1日的总天数;(4)编写一个输出某年某月日历的方法;

(5)编写一个测试方法。


package test2; import java.util.*; public class Calendar { public static void main(String[] args){ Mainmenu(); } public static int Mainmenu(){ Scanner in = new Scanner(System.in);//使用Sanner类定义对象 int flag1 = 1 ; while(flag1 == 1 ) { System.out.println("\n菜单"); System.out.println("1.使用万年历"); System.out.println("2.退出万年历"); int i = in.nextInt(); switch(i){ case 1: Menu(); break; default: exit(); return 1; } } return 1; } public static int Menu(){ Scanner in = new Scanner(System.in);//使用Sanner类定义对象 System.out.println("使用万年历"); System.out.println("1.输出某年某月日历"); System.out.println("2.返回"); int i = in.nextInt(); switch(i){ case 1: System.out.print("请输入要输出日历所在年月(例:201709):"); int year_month = in.nextInt(); int year = year_month / 100 ; int month = year_month % 100 ; printcalendar( year , month); return 1; default: return 1; } } public static int intercalarly_year( int year ){ if( year % 4 == 0){ if( year%100==0){ if( year%400==0) return 1; else return -1; } else return 1; } else return -1; } public static int days_year( int year){ return intercalarly_year(year)==1?366:365; } public static int days_month( int year , int month ){ return month==2?(intercalarly_year(year)==1?29:28):( (month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12) ? 31 : 30 ); } //某年某月前距离1900年1月1日的总天数 public static int count_days(int year , int month ){ int num = 0 ; for(int y = 1900 ; y < year ; y++) num += days_year( y ); for(int m = 1 ; m < month ; m++) num += days_month( year , m ); return num; } //某年某月一号为周几 public static int weekday( int year , int month ){ int week = count_days( year , month ) % 7 + 1 ; return week; } public static void printcalendar(int year ,int month){ int w = weekday( year , month ) ;//1号为周几 int m = days_month( year , month ) ; for( int i = 0 ; i < w ; i++) System.out.print("\t"); for( int j = 1 ; j <=m ; j++){ int k = ( j + w ) % 7 ; if(k==0) System.out.println(j+"\t"); else System.out.print(j+"\t"); } } public static void exit(){ System.out.println("谢谢使用,再见!"); } };

  

 

以上是关于输出万年历的主要内容,如果未能解决你的问题,请参考以下文章

万年历

万年历

万年历

小小万年历

制作一个php万年历

java实验之打印万年历