C或C++编程实现万年历程序,按每行两个月的格式
发布网友
发布时间:2022-05-17 15:20
我来回答
共3个回答
热心网友
时间:2023-10-26 16:40
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
// 打印当年的日历,输出到以自己学号命名的文件夹下面的文件名称
const char OUTPUT_FILE_NAME[] = "calendar.txt";
/*表示星期的常量*/
const int SUN = 0;
const int MON = 1;
const int TUE = 2;
const int WED = 3;
const int THU = 4;
const int FRI = 5;
const int SAT = 6;
const char WEEK_NAME_SHORT[][4]={"SUN","MON","TUE","WED","THU","FRI","SAT"};
const char WEEK_NAME[][10]={"SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY"};
// 表示月份的常量
const int JAN = 0;
const int FEB = 1;
const int MAR = 2;
const int APR = 3;
const int MAY = 4;
const int JUN = 5;
const int JUL = 6;
const int AUG = 7;
const int SEP = 8;
const int OCT = 9;
const int NOV = 10;
const int DEC = 11;
const char MONTH_NAME[][10]=
{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
/*
设定一个最小的系统日期,并且指定改天是星期几
*/
/*最小的系统日期年份,默认是 2000 年*/
const int START_YEAR = 2000;
/*日最小的系统日期月份,默认是 1 月*/
const int START_MONTH = 1;
/*最小的系统日期,默认是 1 号*/
const int START_DAY = 1;
/*最小的系统日期所在的星期,默认是星期六(2000年1月1日是星期六)*/
const int START_WEEK_DAY = SAT;
/*
功能:计算一年中指定月份的天数
*/
int getDaysOfMonth(int year ,int month)
{
const int daysOfMonth[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
if( month == 2 && ( (year%4==0 && year%100!=0) || year %400==0 ))
{
return daysOfMonth[month] +1;
}
else
{
return daysOfMonth[month];
}
}
/*
功能:
计算 year 年 month 月 day 日
距离
START_YEAR 年 START_MONTH 月 START_DAY 日
的天数
*/
int getDiffDays(int year ,int month , int day)
{
int diffDays = 0 ;
int y,m,d;
// 同年
if(year == START_YEAR)
{
// 同年同月
if(month == START_MONTH)
{
diffDays = day - START_DAY;
}
// 同年不同月
else
{
/*加上当月剩余天数*/
diffDays += getDaysOfMonth(START_YEAR,START_MONTH) - START_DAY + 1;
/*加上整月的天数*/
for(m = START_MONTH + 1 ; m < month ; m ++)
{
diffDays += getDaysOfMonth(START_YEAR,m);
}
/*加上当月剩余天数*/
diffDays += day -1;
}
}
// 不同年
else
{
/*加上当月剩余天数*/
diffDays += getDaysOfMonth(START_YEAR,START_MONTH) - START_DAY + 1;
/*加上整月的天数*/
for(m = START_MONTH + 1 ; m <= 12 ; m ++)
{
diffDays += getDaysOfMonth(START_YEAR,m);
}
/*加上整年的天数*/
for(y = START_YEAR+1 ; y < year ; y ++)
{
/*如果是闰年*/
if((y%4==0%y%100!=0) || y%400==0)
{
diffDays += 366;
}
else
{
diffDays += 365;
}
}
/*加上整月的天数*/
for(m = 1 ; m < month ; m ++)
{
diffDays += getDaysOfMonth(year,m);
}
/*加上当月的天数*/
diffDays += ( day - 1 );
}
return diffDays;
}
/*
功能:计算 year 年 month 月第一天所在的星期
*/
int getFirstDayOfMonthInWeekDay(int year ,int month)
{
int days = getDiffDays(year,month,1);
return (days % 7 + START_WEEK_DAY) % 7;
}
/*
功能:输出 year 年 month 月日历
*/
void printMonthCalendar(int year,int month)
{
int firstDayOfMonthInWeekDay = 1;
int daysOfMonth;
int daysCounter;
int d,i;
/*检查输入的年份*/
if(year < START_YEAR || year > 9999)
{
printf("Illegal year : %d ! The year must range [%d,9999]!\n",year,START_YEAR);
return ;
}
/*检查输入的月份*/
if(month < 0 || month > 12)
{
printf("Illegal month : %d ! The month must range [0,12]");
return;
}
/********************开始打印日历********************/
/*日历年月头部*/
printf("Calendar %d-%02d\n",year,month);
/*日历星期头部*/
for(i=0;i<7;i++)
{
printf("%s ",WEEK_NAME_SHORT[i]);
}
printf("\n");
for(i=0;i<4*7;i++)
{
printf("_");
}
printf("\n");
/*月中每日星期排列*/
//每月的第一天所在的星期
firstDayOfMonthInWeekDay = getFirstDayOfMonthInWeekDay(year,month);
//当月的天数
daysOfMonth = getDaysOfMonth(year,month);
//输出每月开头空出的星期,并统计空白的星期
for(d = 0 ,daysCounter = 0; d < firstDayOfMonthInWeekDay ; d ++)
{
printf("%4s"," ");
daysCounter ++;
}
//输出每月的天,根据所在的星期
for( d = 1 ; d <= daysOfMonth; d ++)
{
if(daysCounter % 7 ==0)
{
printf("\n");
}
printf("%3d ",d);
daysCounter++;
}
printf("\n");
for(i=0;i<4*7;i++)
{
printf("_");
}
printf("\n");
}
/*
功能:输出 year 年历 ,每行最多显示 maxShowMonthNumPerLine 个月份
*/
void printYearCalendar(int year,int maxShowMonthNumPerLine)
{
int *firstDayOfMonthInWeekDay = (int*)malloc(sizeof(int)*maxShowMonthNumPerLine);
int *daysOfMonth = (int*)malloc(sizeof(int)*maxShowMonthNumPerLine);
int *daysCounter = (int*)malloc(sizeof(int)*maxShowMonthNumPerLine);
int i,d,month,startMonth,showMonthNumPerLine;
int showMonthCounterPerLine;
/*检查输入的年份*/
if(year < START_YEAR || year > 9999)
{
printf("Illegal year : %d ! The year must range [%d,9999]!\n",year,START_YEAR);
return ;
}
/********************开始打印日历********************/
/*日历年头部*/
printf("Calendar %d\n",year);
for(startMonth=1;startMonth<=12;startMonth+=showMonthNumPerLine)
{
showMonthNumPerLine = (startMonth+maxShowMonthNumPerLine )<=12 ? maxShowMonthNumPerLine : (12 - startMonth + 1);
/*【1】年历的月头部*/
for(month=startMonth;month<(startMonth+showMonthNumPerLine);month++)
{
printf("%-28s ",MONTH_NAME[month-1]);
}
printf("\n");
/*【2】年历的星期头部*/
for(month=startMonth;month<(startMonth+showMonthNumPerLine);month++)
{
for(i=0;i<7;i++)
{
printf("%s ",WEEK_NAME_SHORT[i]);
}
printf(" ");
}
printf("\n");
// 分割线
for(month=startMonth;month<(startMonth+showMonthNumPerLine);month++)
{
for(i=0;i<4*7;i++)
{
printf("_");
}
printf(" ");
}
printf("\n");
/* 数据初始化 */
for(month=startMonth;month<(startMonth+showMonthNumPerLine);month++)
{
//每月的第一天所在的星期
firstDayOfMonthInWeekDay[month-startMonth] = getFirstDayOfMonthInWeekDay(year,month);
//当月的天数
daysOfMonth[month-startMonth] = getDaysOfMonth(year,month);
// 已经输出的天计数器
daysCounter[month-startMonth] = -firstDayOfMonthInWeekDay[month-startMonth]+1;
}
/*【3】日历每天排列*/
showMonthCounterPerLine = 0;
while(showMonthCounterPerLine < showMonthNumPerLine)
{
for(month=startMonth;month<(startMonth+showMonthNumPerLine);month++)
{
for(i=0;i<7;i++)
{
if(daysCounter[month-startMonth]<=0 || daysCounter[month-startMonth] > daysOfMonth[month-startMonth])
{
printf("%4s"," ");
}
else
{
printf("%3d ",daysCounter[month-startMonth]);
}
daysCounter[month-startMonth] ++;
if(daysCounter[month-startMonth] > daysOfMonth[month-startMonth] )
{
showMonthCounterPerLine++;
}
}
printf(" ");
}
printf("\n");
}
// 分割线
for(month=startMonth;month<(startMonth+showMonthNumPerLine);month++)
{
for(i=0;i<4*7;i++)
{
printf("_");
}
printf(" ");
}
printf("\n");
}
}
int main(int argc, char *argv[])
{
int year,month;
time_t t = time(NULL);
struct tm* now = localtime(&t);
int choice;
/*
【1】
显示任意年月的月历
每行两个月的格式输出当年的年历
每行两个月的格式输出任意年的年历
*/
while(1)
{
printf("×××××××××××××××\n");
printf("【1】输出当月月历 【1】\n");
printf("【2】输出当年年历 【2】\n");
printf("【3】指定任意年月的月历 【3】\n");
printf("【4】指定任意年的年历 【4】\n");
printf("【5】退出 【5】\n");
printf("×××××××××××××××\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printMonthCalendar(now->tm_year+1900,now->tm_mon+1);
break;
case 2:
printYearCalendar(now->tm_year+1900,2);
break;
case 3:
printf("输入年和月(YYYY-MM):");
scanf("%d-%d",&year,&month);
printMonthCalendar(year,month);
break;
case 4:
printf("输入年(YYYY):");
scanf("%d-%d",&year);
printYearCalendar(year,2);
break;
case 5:
exit(0);
default:
printf("选择错误,请重新选择\n");;
};
}
return 0;
}追问好多错,不行啊!
追答angelfish_business@163,com 已发送,请查收
热心网友
时间:2023-10-26 16:40
#include<iostream>
#include<iomanip>
using namespace std;
bool leap_year(int year)
{
return year%4==0;
}
int loadmark(int year)
{
int mark=2;
int sum=0;
for(int i=1980;i<year;++i)
sum+=(leap_year(i)?366:365);
mark+=sum;
return mark%7;
}
void show_calendar(int year)
{
if(year<=0)return ;
int a[13]=
{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(leap_year(year))a[2]=29;
int mark=loadmark(year);
for(int month=1;month!=13;++month)
{
cout<<" year/"<<year;
cout<<"\tmonth/"<<month<<endl;
cout<<setw(4)<<"Sun";
cout<<setw(4)<<"Mon";
cout<<setw(4)<<"Tue";
cout<<setw(4)<<"Wed";
cout<<setw(4)<<"Thu";
cout<<setw(4)<<"Fri";
cout<<setw(4)<<"Sat";
cout<<endl;
for(int i=1;i<=mark;++i)
cout<<setw(4)<<' ';
for(int day=1;day<=a[month];++day)
{
cout<<setw(4)<<day;
++mark;
if(mark%7==0)
{
cout<<endl;
mark=0;
}
}
cout<<endl;
}
}
int main()
{
int year;
cout<<"input year :";
cin>>year;
if(year<1980)
{
cout<<"can not display "
<<"the recards of the year."
<<endl;
return 0;
}
show_calendar(year);
return 0;
}
热心网友
时间:2023-10-26 16:41
http://ftp.de.debian.org/debian/pool/main/c/cal/cal_3.5.orig.tar.gz
解压后cal-3.5.orig/source/cal.c追问不知道怎么弄啊,能不能把代码直接发邮箱里啊935125296◎qq.com