前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c++课程设计(日历)

c++课程设计(日历)

作者头像
Gxjun
发布2018-03-22 13:17:41
1.1K0
发布2018-03-22 13:17:41
举报
文章被收录于专栏:mlml
代码语言:javascript
复制
今天比较无聊,就随便找了个程序设计来做,下面是源代码,以及效果图...不喜请喷!
/*题目1:年历显示。
功能要求:
(1)    输入一个年份,输出是在屏幕上显示该年的日历。假定输入的年份在1940-2040年之间。
(2)    输入年月,输出该月的日历。
(3)    输入年月日,输出距今天还有多少天,星期几,是否是公历节日。
*/
代码语言:javascript
复制
  1 //日历
  2 // Coder @Gxjun 2014/3/30/10:35
  3 #include<iostream>
  4 #include<ctime>
  5 #include<string>
  6 #include<iomanip>
  7 using namespace std;
  8 namespace isprimer
  9 {
 10     int ren[13]={0,31,60,91,131,162,192,223,254,284,315,335,366} ; //是闰年
 11     int pin[13]={0,31,59,90,130,161,191,222,253,283,314,334,365} ;
 12 }
 13 namespace mon
 14 {
 15     int ren_month[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
 16     int pin_month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
 17 }
 18 bool isprim(int year)
 19 {
 20   if(year%400==0||(year%4==0&&year%100!=0))
 21      return true;
 22    return false;
 23 }
 24 void print(int month,int weekly,int tol_day)
 25 {
 26 
 27    int k=1;
 28    cout.width(3);
 29    cout<<month<<"  SUN MON TUE WED THU FRI SAT"<<endl;
 30    cout<<"     ";
 31    int kongge=weekly%7;
 32    while(kongge)
 33    {
 34      cout<<"    ";
 35      kongge--;
 36    }
 37    for(k=1;k<=tol_day ;k++)
 38    {
 39        cout<<setw(3)<<setiosflags(ios::right)<<k<<" ";
 40        if(weekly%7==6)
 41            cout<<endl<<"     ";
 42          weekly++;
 43    }
 44    cout<<endl;
 45 }
 46 class Calender
 47 {
 48   public :
 49     Calender(){};
 50     Calender(int y):year(y){};
 51     Calender(int y,int m): year(y),month(m){};
 52     Calender(int y,int m,int d): year(y),month(m),day(d){};
 53     ~Calender(){};
 54     void show1();
 55     void show2();
 56     void show3();
 57   private:
 58   int year;
 59   int month;
 60   int day;
 61 };
 62 void Calender::show1()
 63 {
 64     time_t current;
 65     struct tm *t;
 66     time(&current); //得到目前的时间
 67      t=localtime(&current);
 68      t->tm_year+=1900;
 69      t->tm_mon+=1;
 70      cout<<"今天的时间为:"<<t->tm_year<<" 年 ";
 71      cout<<t->tm_mon<<" 月 "<<t->tm_mday<<" 日"<<endl;
 72      cout<<"您要查询的时间为: "<<year<<" 年 "<<month<<" 月 "<<day<<"日"<<endl;
 73      //判断是否闰年
 74      using namespace isprimer ;
 75        int en,start;
 76      if(isprim(t->tm_year)) //这一天为闰年
 77         start=ren[t->tm_mon-1]+t->tm_mday ;
 78      else
 79         start=pin[t->tm_mon-1]+t->tm_mday ;
 80      if(isprim(year))
 81           en=ren[month-1]+day;
 82      else
 83           en=pin[month-1]+day;
 84         int  year_cha=year - t->tm_year;
 85         int ans=(year_cha/4)*366+(year_cha - year_cha/4)*365+en-start;
 86         cout<<"相隔:"<<ans<<" 天"<<endl;
 87         cout<<"那一天是:"<<t->tm_wday+ans%7<<endl;
 88 };
 89 void Calender::show2()
 90 {
 91     time_t current;
 92     struct tm *t;
 93     time(&current); //得到目前的时间
 94     t=localtime(&current);
 95        int en,start,sday=1;
 96      using namespace isprimer ;
 97      t->tm_year+=1900;
 98      t->tm_mon++;
 99      if(isprim(t->tm_year)) //这一天为闰年
100         start=ren[t->tm_mon-1]+t->tm_mday ;
101      else
102         start=pin[t->tm_mon-1]+t->tm_mday ;
103      using namespace mon ;
104      if(isprim(year))
105           en=ren[month-1]+sday;
106      else
107           en=pin[month-1]+sday;
108         int  year_cha=year - t->tm_year;
109         int ans=(year_cha/4)*366+(year_cha - year_cha/4)*365+en-start;
110         //得到本月初一是星期几
111         int weekly=t->tm_wday+ans%7;
112         if(weekly<0)weekly+=7;
113       if(isprim(year))
114           print(month,weekly,ren_month[month]);
115      else
116          print(month,weekly,pin_month[month]);
117 };
118 void Calender::show3()
119 {
120     int start,ans;
121     using namespace mon;
122     time_t k;
123     time(&k);
124     struct tm *t;
125     t=localtime(&k);
126     //推出其为星期几即可
127     int sday,mon;
128     sday=mon=1;
129     t->tm_year+=1900;
130     t->tm_mon++;
131     using namespace isprimer;
132     if(isprim(t->tm_year))
133     start=ren[t->tm_mon-1]+t->tm_mday;
134     else
135     start=pin[t->tm_mon-1]+t->tm_mday;
136     int year_cha=(year-t->tm_year);
137     ans=(year_cha/4)*366+(year_cha - year_cha/4)*365+1-start;
138     int weekly=t->tm_wday+ans%7;
139     if(weekly<0)weekly+=7;
140     if(isprim(year))
141       {
142           for(int i=1;i<=12 ;i++ )
143           print(i,(weekly+ren[i-1])%7,ren_month[i]);
144       }
145     else
146          for(int i=1;i<=12 ;i++ )
147           print(i,(weekly+pin[i-1])%7,pin_month[i]);
148 
149 };
150 int main()
151 {
152     int tem,aa,bb,cc;
153     cout<<"你想要查找什么?";
154     cout<<"1.某年某月某日距现在的天数以及是星期几?"<<endl;
155     cout<<"2.某年某月的一个月的日历"<<endl;
156     cout<<"3.某年的日历"<<endl;
157     cin>>tem;
158     switch(tem)
159     {
160      case 1:
161      {
162          cout<<"输入年 月 日"<<endl;
163          cin>>aa>>bb>>cc;
164          Calender stu(aa,bb,cc);
165          stu.show1();
166      } ;break;
167      case 2:
168      {
169           cout<<"输入年 月"<<endl;
170          cin>>aa>>bb;
171          Calender tt(aa,bb);
172          cout<<"this  is "<<aa<<" Calender !"<<endl;
173          tt.show2();
174          break;
175      }
176      case 3:
177      {
178          cout<<"输入 年"<<endl;
179          cin>>aa;
180          Calender stu(aa);
181         stu.show3();
182      }
183     }
184       return 0;
185 }

效果图:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-03-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档