0 GP
書要看不完囉~~
作者:杏仁飯│2008-11-02 02:18:37│巴幣:0│人氣:327
從2點寫到6點
從6點寫到10點
總共寫了8個小時
才寫完三題
第一次寫到破百行的程式
除錯除到要死掉了
還有大概6題要寫
我看我這周末要被程設淹沒了
我還有500頁全部都是英文的普通物理
這學期還沒開始看的微積分跟難到爆了的工程數學
真累 ~~ = = ~~~
最後附上沒學過程式設計的人應該覺得是亂碼的程式
這是我這次的作業最後一題
他的功能是你輸入幾年幾月~
他就會幫你把那個月份的月曆印出來給你看~
為了以後可以複製~
雖然題目規定1583年到3000年
不過我寫的程式是西元1年到9999999年都可以用的
裡面穿插中文的原因是 不會的人看這個像看火星文
但是自己寫完了不看~ 過了一個月兩個月再來看
我們也會覺得自己在寫火星文
所以要寫註解~ 這樣別人跟以後的自己才知道在寫甚麼
#include<iostream>
#include<iomanip>
using namespace std;
int calendar(int,int);
int main(){
while(1){
cout<<"\n\n";
int month;
int year;
int week ; //week 代表星期幾 week = 1 即為星期一
cout << "Enter a year (1583-3000): " << endl ;
cin >> year ;
cout << endl ;
if(year <=3000 && year >= 1583){
cout << "Enter a month (1-12): " << endl ;
cin >> month ;
cout <<endl ;
//---------------↓列印月份---------------------------------------
if(month <=12 && month >= 1){
week = calendar(year,month);
cout << setw(4) <<"SU"<< setw(4) <<"MO"<< setw(4) <<"TU"<< setw(4) <<"WE";
cout << setw(4) <<"TH"<< setw(4) <<"FR"<< setw(4) <<"SA"<<endl;
int day ;
int monthday ;
for(int x=0; x < week;x++){
cout<<" ";
}
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)//大月
monthday = 31;
if(month==4||month==6||month==9||month==11)//小月
monthday = 30;
if(month==2){
if ( year % 4 == 0 )
if ( year % 100 == 0 && year % 400 != 0 )
monthday = 28;
else
monthday = 29;
else
monthday = 28;
}
for(day=1;day <= monthday;day++){
week++;
cout << setw(4) << day ;
if(week%7 == 0)
cout << endl ;
}
}
//---------------↑列印月份---------------------------------------
else
cout << "Wrong Month." ;
}
else
cout << "Wrong Year." ;
cout<<endl;}
return 0 ;
}
int calendar(int year,int month){
//西元元年1月1日是星期一
int week = 0 ;//week 代表星期幾 week = 1 即為星期一
//開始判斷你到你輸入的年份前一天有多少個日子
/*閏年規則
逢4的倍數閏, 例如:西元1992、1996年等,為4的倍數,故為閏年。
逢100的倍數不閏, 例如:西元1700、1800、1900年,為100的倍數,當年不閏年。
逢400的倍數閏, 例如:西元1600、2000、2400年,為400的倍數,有閏年
逢4000的倍數不閏, 例如:西元4000、8000年,不閏年。*/
int year29 = (year-1)/4 - (year-1)/100 + (year-1)/400 -(year-1)/4000 ; //閏年量(還不包括你要求的那年)
int year28 = ( year -1) - (year29) ; //平年量(還不包括你要求的那年)
//求出(year-1)年12月31日是星期幾
week = (366 * year29 + 365 * year28 ) % 7 ;
//判斷year是否為閏年
int month2_29 = 0 ; //0代表不為閏年
if ( year % 4 == 0 ){
if ( year % 100 == 0 && year % 400 != 0 )
month2_29 = 0 ;
else
month2_29 = 1 ;
}
else
month2_29 = 0 ;
int months28[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};//month[1]為1月有的天數 month[5]為5月有的天數
int months29[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};//month[1]為1月有的天數 month[5]為5月有的天數
int days = 0 ;
if (month2_29==1){
for (int m = 1 ; m <= month-1 ; m++ ){
days = days + months29[m] ;
}
week = (week + days +1 ) % 7 ;
}
if (month2_29==0){
for (int m = 1 ; m <= month-1 ; m++ ){
days = days + months28[m] ;
}
week = (week + days +1 ) % 7 ;
}
return week ;
}
引用網址:https://home.gamer.com.tw/TrackBack.php?sn=945772
All rights reserved. 版權所有,保留一切權利