万年历系统

#include<iostream>
#include<fstream>
#include<iomanip>//包含setw()函数 设置域宽
#include<string>
using namespace std;
ofstream fout("万年历.txt");
int monthday(int year,int month);//各个月份的天数 
int newdays(int year);//年份的第一天
int outp(int year) //两月一行万历表的打印
{      
int i,j,k,n;      
int firstmonthday[12];      
char month[12][10]={{"Jan"},{"Feb"},{"Mar"},{"Apr"},{"May"},{"Jun"},{"Jul"},{"Agu"},{"Sep"},{"Oct"},{"Nov"},{"Dec"}};     
char weekday[]={" sun mon tue wed thu fri sat"};     
for(i=0;i<12;i++)     
{          
if(i==0)          
firstmonthday[i]=newdays(year);         
else          
firstmonthday[i]=(firstmonthday[i-1]+monthday(year,i))%7;     
}
int month1[6][7],month2[6][7];  
cout<<year<<"年"<<endl;
fout<<year<<"年"<<endl;
for(i=0;i<12;i+=2)     
{         
cout<<month[i]<<setw(25)<<'\t'<<month[i+1]<<endl;//<<setw(4)<<setw(4)
fout<<month[i]<<setw(25)<<'\t'<<month[i+1]<<endl;  //<<setw(4) <<setw(4)
cout<<weekday<<setw(4)<<"\t"<<weekday<<endl;  
fout<<weekday<<setw(4)<<"\t"<<weekday<<endl; 
for(j=0;j<6;j++)         
{
for(k=0;k<7;k++)             
{                  
month1[j][k]=k+1-firstmonthday[i]+j*7;   //日期的计算               
month2[j][k]=k+1-firstmonthday[i+1]+j*7;             
}               
for(n=0;n<7;n++)             
{                  
if(month1[j][n]<1||month1[j][n]>monthday(year,i+1))//如果日期小于1或大于当月天数则打印3个空格                
{
cout<<"    ";   
fout<<"    "; 
}
else                  
{
cout<<setw(4)<<month1[j][n]; 
fout<<setw(4)<<month1[j][n];
}
}              
cout<<'\t';
fout<<'\t';
for(n=0;n<7;n++)             
{                  
if(month2[j][n]<1||month2[j][n]>monthday(year,i+2))                 
{
cout<<"    "; 
                    fout<<"    "; 
}
else                  
{
cout<<setw(4)<<month2[j][n];
fout<<setw(4)<<month2[j][n]; 
}
}              
cout<<'\n';
fout<<"\n";
}     
}     
return 0; 
}
/*void printday(int md, int week)//如要求一月一行打印出,可以用这个函数
{
 int line=1;
 for( int blank=week; blank; --blank,++line )
  cout << setw(7) << " ";
 for( int d=1; d<=md; ++d,++line )
 {
  cout << setw(7) << d;
  if( line%7==0 )
   cout << endl;
 }
 cout << endl;
}*/
void printyear(char a[4])//用*打印出相应的年份
{
int i = 0;
    int j = 0;
    char output[4][5][6] = {{{0}}};//三维数组来储存
    char ZERO[5][6]    = {"*****","*   *","*   *","*   *","*****"};//将每个数字各个行放在一个字符串里
    char ONE[5][6]     = {"    *","    *","    *","    *","    *"};
    char TWO[5][6]     = {"*****","    *","*****","*    ","*****"};
    char THREE[5][6]   = {"*****","    *","*****","    *","*****"};
    char FOUR[5][6]    = {"*   *","*   *","*****","    *","    *"};
    char FIVE[5][6]    = {"*****","*    ","*****","    *","*****"};
    char SIX[5][6]     = {"*****","*    ","*****","*   *","*****"};
    char SEVEN[5][6]   = {"*****","    *","    *","    *","    *"};
    char EIGHT[5][6]   = {"*****","*   *","*****","*   *","*****"};
    char NINE[5][6]    = {"*****","*   *","*****","    *","*****"};
 for (i = 0; i < 4; i++)
 {
  switch (a[i])
  {
   case '0':
    memcpy(output[i], ZERO, 5*6*sizeof(char));//用memcpy()函数拷贝对应数字
    break;
   case '1':
    memcpy(output[i], ONE, 5*6*sizeof(char));
    break;
   case '2':
    memcpy(output[i], TWO, 5*6*sizeof(char));
    break;
   case '3':
    memcpy(output[i], THREE, 5*6*sizeof(char));
    break;
   case '4':
    memcpy(output[i], FOUR, 5*6*sizeof(char));
    break;
   case '5':
    memcpy(output[i], FIVE, 5*6*sizeof(char));
    break;
   case '6':
    memcpy(output[i], SIX, 5*6*sizeof(char));
    break;
   case '7':
    memcpy(output[i], SEVEN, 5*6*sizeof(char));
    break;
   case '8':
    memcpy(output[i], EIGHT, 5*6*sizeof(char));
    break;
   case '9':
    memcpy(output[i], NINE, 5*6*sizeof(char));
    break;
   default:
    break;
  }
 }


 for (i = 0; i < 5; i++)
 {
cout<<"                  ";
fout<<"                  ";
  for (j = 0; j < 4; j++)
  {
 
   cout<<output[j][i];
   fout<<output[j][i];
   cout<<" ";
   fout<<" ";
  }
  cout<<"\n";
  fout<<"\n";
 }
}
//数字转化为汉字
string change(int x)
{
switch(x)
{
case 1:return "一";break;
case 2:return "二";break;
case 3:return "三";break;
case 4:return "四";break;
case 5:return "五";break;
case 6:return "六";break;
case 0:return "日";break;
}
return "1";
}
int monthday(int year,int month) //计算每月的天数
{      
int d;      
switch(month)     
{          
case 1:case 3:case 5:case 7:case 8:case 10:case 12:d=31;break;         
case 4:case 6:case 9:case 11:d=30;break;         
case 2:          
if ((year%4==0)&&(year%100!=0)||(year%400==0)) d=29;
else d=28;     
}      
return d; 
}
int newdays(int year) //判断某年的1月1日是星期几
{      
int day;      
if((year%4==0)&&(year%100!=0)||(year%400==0))     
day=(year-1+(year/4)-(year/100)+(year/400))%7;     
else      
day=(year+(year/4)-(year/100)+(year/400))%7;     
return day; 
}  //主函数实现 
void menu()
{
cout<<"          -----------------------------------------\n";  
cout<<"          -         欢迎进入万年历查询系统        -\n";  
cout<<"          -----------------------------------------\n";  
cout<<"           请输入你所要查询的年份:  "; 
    fout<<"          -----------------------------------------\n";  
fout<<"          -         欢迎进入万年历查询系统        -\n";  
fout<<"          -----------------------------------------\n";  
fout<<"           请输入你所要查询的年份:  ";
}
void main() 
{
int i,year;
char a[10];//用字符数组来储存
menu();
cin>>a;
year=((int)(a[0])-48)*1000+((int)(a[1])-48)*100+((int)(a[2])-48)*10+(int)a[3]-48;//转化成int类型,方便下面对数据的判断
for(i=0;i<4;i++)
{
while(a[4]||(int)a[0]==0||(int)a[i]<48||(int)a[i]>57||year/1000>=10||year/1000<1)//对输入年份进行判断 非数字,非4位数字,以0开头的数字
{
   cout<<"Wrong input! Please enter the year again:";
       fout<<"Wrong input! Please enter the year again:";
       cin>>a;
year=((int)(a[0])-48)*1000+((int)(a[1])-48)*100+((int)(a[2])-48)*10+(int)a[3]-48;
       fout<<a;
       fout<<"\n";
}
}
     cout<<""<<year<<"年1月1日是星期"<<change(newdays(year))<<endl;
     fout<<""<<year<<"年1月1日是星期"<<change(newdays(year))<<endl;
     printyear(a);
outp(year); 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值