今天比較無聊,就隨便找了個程序設計來作,下面是源代碼,以及效果圖...不喜請噴!
/*題目1:年曆顯示。 功能要求: (1) 輸入一個年份,輸出是在屏幕上顯示該年的日曆。假定輸入的年份在1940-2040年之間。 (2) 輸入年月,輸出該月的日曆。 (3) 輸入年月日,輸出距今天還有多少天,星期幾,是不是公曆節日。 */
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(¤t); //獲得目前的時間 67 t=localtime(¤t); 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(¤t); //獲得目前的時間 94 t=localtime(¤t); 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 }
效果圖:ios