一、系统概述
在日常生活中经常使用各种日历,现用C语言编写一个日历小程序,它提供了一下的几个功能:
(1)显示系统当前的时间,包括年、月、日以及时、分、秒等一些信息。
(2)判断输入的日期是星期几。
(3)判断输入的年份是否为闰年。
(4)输出某年某月的日历。
(5)输出某年的完整日历。
二、功能模块
功能模块图如下:(功能1、2、3、4、5参照系统概述)
![在这里插入图片描述](https://img-blog.csdnimg.cn/45c46bfa6b704455b68592e8d058fe27.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeV94ampi,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
三、问题分析
(1)判断输入的日期是否合法。
(2)判断输入的年份是否是闰年。
(3)判断输入的日期是星期几。
(4)输出日历。
四、函数设计
(1)主函数main();
(2)IsLeapYear(int year);
(3)Time();
(4)month_day(int year,int month);
(5)DaySearch(int year,int month,int day);
(6)PrintAllMonth(int year);
(7)SearchMonth(int year,int month);
(8)menu()
五、运行代码
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
char *month_str[]={"January","February","March","April","May","June",
"July","August","September","October","November","Devember"};
char *week[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
void Time(){ //获取并显示系统时间
time_t t; //时间值time_t为长整型的别名
struct tm *tp; //定义tm结构指针变量来获得日期和时间
t=time(NULL); //获得的日历时间
tp=localtime(&t); //localtime()函数是将日历时间转化为本地时间
printf("\t----------------------------------------------------------\n");
printf("\t\t 现在是:%d年%d月%d日",tp->tm_year+1900,tp->tm_mon+1,tp->tm_mday);
printf(" %d:%d:%d ",tp->tm_hour,tp->tm_min,tp->tm_sec);
printf(" \t%s\n",week[(tp->tm_wday)]);
printf("\t-----------------------------------------------------------\n");
}
int IsLeapYear(int year){ //判断闰年
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){ //判断是星期几
int m,c=0;
float s;
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){ //输出整年的日历
int temp,i,j;
printf("\n\n\t\t<-- %d年的日历 -->\n",year);
for(i=1;i<=12;i++){
printf("\n\n\t\t===\t%s(%d)\t===\n\n",month_str[i-1],i);
printf(" \t日 \t一 \t二 \t三 \t四 \t五 \t六 \n");
printf(" \tS \tM \tT \tW \tT \tF \tS \n\n");
temp=DaySearch(year,i,1);
for(j=1;j<=month_day(year,i)+temp;j++){
if(j-temp<=0){
printf("\t");
}else if(j-temp<10){
printf("\t%d",j-temp);
}else{
printf("\t%d",j-temp);
}
if(j%7==0)
printf("\n");
}
}
return 0;
}
int SearchMonth(int year,int month){ //输出某月的日历
int temp,j;
printf("\n\n\t %d年%月的日历\n\n",year,month);
printf(" \t日 \t一 \t二 \t三 \t四 \t五 \t六 \n");
printf(" \tS \tM \tT \tW \tT \tF \tS \n\n");
temp=DaySearch(year,month,1);
for(j=1;j<=month_day(year,month)+temp;j++){
if(j-temp<=0) printf("\t");
else if(j-temp<10) printf("\t%d",j-temp);
else printf("\t%d",j-temp);
if(j%7==0)
printf("\n");
}
return 0;
}
menu(){ //主菜单
int option,da,year,month,day;
char ch,c;
printf("\n\n\t\t <== 欢迎进入kpz万年历系统 ==>\n\n\n");
while(1){
printf("\n\t\t\t 请选择以下功能--> \n\n");
printf("\t\t\t 1、判断某个日期是星期几\n");
printf("\t\t\t 2、判断某年是否为闰年\n");
printf("\t\t\t 3、输出某年某月的日历\n");
printf("\t\t\t 4、输出某年的完整日历\n");
printf("\t\t\t 5、退出系统\n");
printf("\t\t ----------------------------------------\n");
printf("\t\t\t请选择(1,2,3,4,5):");
while(scanf("%d",&option)!=1){
while((c=getchar())!='\n');
printf("\n\n\n\n\n\t\t\t【X】Error:输入有误,请再次输入!");
getch(); //在window平台下从控制台无回显地取一个字符
system("cls");
Time();
menu();
}
switch(option){
case 1:while(1){
system("cls");
nod: printf("请按年月日的顺序分别输入年、月、日,如2008.09\n");
printf("请输入年:");
while(scanf("%d",&year)!=1){
while((c=getchar())!='\n');
printf("\n\n\t\t\t[X]Error:输入有误,请重新输入!");
getch();
system("cls");
goto nod;
}
first: printf("请输入月份:");
while(scanf("%d",&month)!=1){
while((c=getchar())!='\n');
printf("[X]Error:输入有误,请重新输入!\n");
goto first;
}
if(month<1||month>12){
printf("有误!输入的月份必须在1-12之间,请重新输入......\n");
goto first;
}
second: printf("其输入日期:");
while(scanf("%d",&day)!=1){
while((c=getchar())!='\n');
printf("[X]Error:输入有误,请重新输入!\n");
goto second;
}
if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&
(day<1||day>31)){
printf("有误!请输入%d月的%d日应在1-31之间,请重新输入......\n",month,day);
goto second;
}
else if((month==4||month==6||month==9||month==11)&&(day<1||day>30)){
printf("有误!请输入%d月的%d日应在1-30之间,请重新输入......\n",month,day);
goto second;
}
else if((day<1||day>28)&&(!IsLeapYear(year)&&month==2)){
printf("有误!请输入%d月的%d日应在1-28之间,请重新输入......\n",month,day);
goto second;
}
else if((day<1||day>29)&&(IsLeapYear(year)&&month==2)){
printf("有误!请输入%d月的%d日应在1-29之间,请重新输入......\n",month,day);
goto second;
}
da=DaySearch(year,month,day);
printf("\n%d-%d-%d is %s\n\n是否继续?(Y/N)",year,month,day,week[da]);
fflush(stdin); //刷新标准输入缓冲区,把输入缓冲区里的东西丢弃
scanf("%c",&ch);
if(ch=='N'||ch=='n'){
system("cls");
break;
}
}
break;
case 2:while(1){
system("cls");
loop: printf("请输入年份(如2007年):");
while(scanf("%d",&year)!=1){
while((c=getchar())!='\n');
printf("\n\n\n\n\t\t\t[X]Error:输入有误,请重新输入!");
getch();
system("cls");
goto loop;
}
if(year<1900||year>2500){
printf("\n\n\n\t有误!输入的年份应该在1900-2500之间,请再次输入!");
getch();
system("cls");
goto loop;
}
if(IsLeapYear(year)){
printf("\n\n%d是闰年,你是否想继续?(Y/N)",year);
}
else{
printf("\n\n%d不是闰年,你是否想继续?(Y/N)",year);
}
fflush(stdin);
scanf("%c",&ch);
if(ch=='N'||ch=='n'){
system("cls");
break;
}
}
break;
case 3:while(1){
system("cls");
year: printf("请输入年份(如2007年):");
while(scanf("%d",&year)!=1){
while((c=getchar())!='\n');
printf("\n\n\n\n\t\t\t[X]Error:输入有误,请重新输入!");
getch();
system("cls");
goto year;
}
if(year<1900||year>2500){
printf("\n\n\n\t有误!输入的年份应该在1900-2500之间,请再次输入!");
getch();
system("cls");
goto year;
}
mon: printf("请输入月份:");
while(scanf("%d",&month)!=1){
while((c=getchar())!='\n');
printf("[X]Error:输入有误,请重新输入!\n");
goto mon;
}
if(month<1||month>12){
printf("有误!输入的年份应该在1-12之间,请再次输入!\n");
goto mon;
}
SearchMonth(year,month);
printf("\n\n是否想继续?(Y/N)");
fflush(stdin);
scanf("%c",&ch);
if(ch=='N'||ch=='n'){
system("cls");
break;
}
}
break;
case 4:while(1){
system("cls");
tag: printf("请输入您想要打印的年份(如2007年):");
while(scanf("%d",&year)!=1){
while((c=getchar())!='\n');
printf("\n\n\n\n\t\t\t[X]Error:输入有误,请重新输入!");
getch();
system("cls");
goto tag;
}
if(year<1900||year>2500){
printf("\n\n\n\t有误!输入的年份应该在1900-2500之间,请再次输入!");
getch();
system("cls");
goto tag;
}
PrintAllYear(year);
printf("\n\n是否想继续?(Y/N)");
fflush(stdin);
scanf("%c",&ch);
if(ch=='N'||ch=='n'){
system("cls");
break;
}
}
break;
case 5:fflush(stdin);
printf("\n\n\n\t\t\t是否真的确定!(Y/N)");
scanf("%c",&ch);
if(ch=='Y'||ch=='y'){
system("cls");
printf("\n\n\n\t\t\t 谢谢使用!");
exit(0);
}
else{
system("cls");
Time();
menu();
break;
}
default:printf("\n\n\t\t\t Error:***对不起,没有这个选项!***");
getch();
system("cls");
break;
}
}
return 0;
}
int main(){
Time();
menu();
return 0;
}
六、结束语
**
如出现错误,请谅解;
如有疑惑,请提出,看见了会及时回答
谢谢!
**