zoukankan      html  css  js  c++  java
  • 2.2打印日历实现输出某年某月

    //打印日历实现输出某年某月
    #include<iostream>
    #include<iomanip>
    #include<string>
    using namespace std;
    //判断平闰年
    int doyear(int year)
    {
    if((year%4==0)&&(year%100!=0)||(year%400==0))
    return 1;
    else
    return 0;
    }
    //判断某年的第一天是星期几
    int doweek(int year)
    {
     int days=(year-1)*365+(year-1)/4-(year-100)/100+(year-1)/400;
     return days%7;
     }
     //打印星期
     void printweek()
     {
     int i;//计数
     string week[7]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
     for(i=0;i<7;i++)
     cout<<setw(5)<<week[i];
     cout<<endl;
     }
     //打印月份
     void printmonth(int m)
     {
     //int i;
     string month[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dev"};
     //for(i=0;i<12;i++)
     //if(m=i+1)
     cout<<setw(5)<<month[m-1];
     cout<<endl;
     }
     //打印日历
     void printall(int year,int month)
     {
     int i,d;
     int monthday[12]={31,28,31,30,31,30,31,31,30,31,30,31};
     if(doyear(year))
     monthday[1]=29;
     int sum=0;
     for(i=0;i<month-1;i++)
     sum+=monthday[i];
     int weekday=((doweek(year)+sum)%7);
     cout<<setw(5)<<year<<" "<<month<<"月"<<endl<<endl;
     cout<<setw(5)<<"Calendar";
     cout<<setw(20)<<year<<"-"<<month<<endl;
     for(i=1;i<=5*7;i++)
     cout<<"*";
     cout<<endl;
     printmonth(month);
     printweek();
     for(d=1;d<=monthday[month-1];d++)
     {
     if(d==1){
     for(i=1;i<=weekday;i++)
     cout<<setw(5)<<" ";
     cout<<setw(5)<<d;
     } 
     else
    	 cout<<setw(5)<<d;
     if((weekday+d-1)%7==6)
     cout<<endl;
     }
     }
     //主函数
     int main() 
     {
        int year,month;
    	cout<<"please input which year do you want"<<endl;
    	cin>>year;
    	cout<<"please input which month do you want"<<endl;
    	cin>>month;
        printall(year,month);
    	cout<<endl;
    	return 0;
    }
    

      

    作者:这些年读过的书
    出处: http://www.cnblogs.com/chenzinumber1/
    本文版权归作者与博客园所有,欢迎转载,但未经作者同意必须保留此段声明,文末要留有原文链接,否则保留追究法律责任的权利。

  • 相关阅读:
    Solution 16: 树的层次遍历
    Solution 15: 树的镜像
    Solution 14: Two Sum
    Solution 13: 链表的倒数第K个节点
    Solution 10: 翻转句子中的单词
    Solution 11: 二叉树中节点的最大距离
    Solution 9: 判断序列是否为BST的后续遍历结果
    Solution 7: 判断两链表是否相交
    估算Baidu和Google的网页索引数量之比
    主元素问题
  • 原文地址:https://www.cnblogs.com/chenzinumber1/p/8085440.html
Copyright © 2011-2022 走看看