程式C語言

C++語言|計算天數~ 亂七八糟的寫出來…先求Make it work吧!

上到第二期基礎程式語言課,從C到C++,開始每堂課後都有回家作業,壓力真的不小啊!
自己想邏輯寫、看參考書寫、看Youtube教學寫…… 雖然都有盡力把功課寫出來,解一題往往要3~6小時,自己也知道內容很多贅詞,一定是有更聰明的寫法,一看就知道是菜鳥中的菜菜鳥,但我想都是成長的過程,希望我半年以後再看,能笑自己菜,同時寫出更漂亮的程式。

在看到這個影片【Pinkoi 後端工程師 Mosky 專訪】給寫程式新手三個階段的參考後,放下更多心理壓力,一步一步來吧!

Make it work.
Make it right.
Make it fast.

會動就好→與目標一致加速

所以,今天分享第二支小程式~計算天數,是大比例用我自己的邏輯寫出來的,其他的小程式很多靠參考來源才寫出來的就不另分享了~

計算天數小程式DEMO

{ 撰寫邏輯 }
(1)如果起始年=(等於)終止年,判斷平年或閏年,帶入各月天數加總。
(2)如果起始年!=(不等於)終止年,加總以下A+B+C三個天數。
A.完整年度(不含起始和終止年)的天數(要判斷閏年366天、平年365天)
B.起始年的指定月份天數(要判斷閏年2月29天、平年28天)
C.終止年的指定月份天數(要判斷閏年2月29天、平年28天)
(3)加入檢核條件:西元年必須為1900~2050,月份必須為1~12,終止年必須大於起始年。

// 分享我的菜菜鳥作品 ///

void 計算天數2()
{
cout.width(50);cout.fill(‘-‘);cout<<“\n";
cout<<“儷穎 Homework:計算天數2″<<endl;
cout.width(50);cout.fill(‘-‘);cout<<“\n";

int day1[]={0,31,29,31,30,31,30,31,31,30,31,30,31};  //leap year閏年366天
int day2[]={0,31,28,31,30,31,30,31,31,30,31,30,31};  //normal year平年365天
int *day=day2;   

int startyear=0;
int startmonth=0;
int finishyear=0;
int finishmonth=0;
int total=0;

int addstart=1;
int addend=12;

for (int i=1; i<=1 ; i++)
{
    cout<<"起始西元年 = ";
    cin>>startyear;
    if  (startyear<1900 || startyear>2050)   //增加檢核
    {
        cout<<"起始年超過計算範圍(1900~2050),請重新輸入"<<endl;
        i--;
        continue;
    }
}

for (int i=1; i<=1 ; i++)
{
    cout<<"起始月 = ";
    cin>>startmonth;
    if  (startmonth<1 || startmonth>12)   //增加檢核
    {
        cout<<"月份輸入錯誤,請輸入1~12間的數字"<<endl;
        i--;
        continue;
    }
}

for (int i=1; i<=1 ; i++)
{
    cout<<"終止西元年 = ";
    cin>>finishyear;
    if  (finishyear<1900 || finishyear>2050  )   //增加檢核
    {
        cout<<"終止年超過計算範圍(1900~2050),請重新輸入"<<endl;
        i--;
        continue;
    }
    if  ( finishyear < startyear )   //增加檢核
    {
        cout<<"終止年度早於起始年度,請重新輸入"<<endl;
        i--;
        continue;
    }

}

for (int i=1; i<=1 ; i++)
{
    cout<<"終止月 = ";
    cin>>finishmonth;
    if  (finishmonth<1 || finishmonth>12)   //增加檢核
    {
        cout<<"月份輸入錯誤,請輸入1~12間的數字"<<endl;
        i--;
        continue;
    }
}

if (startyear==finishyear)  /////////////判斷開始和終止是同一年
{
    if( startyear%4==0 && startyear%100!=0   ||  startyear%400==0   )  //是閏年
    {   
        for (int i=1 ; i <= 1 ; i++)
        {
            for (int j=startmonth ; j <= finishmonth ; j++)
            {
                total=total+day1[j];
            }
        }
        cout<<"★總天數= "<<total<<" 天"<<endl;

    }
    else  //是平年
    {
        for (int i=1 ; i <= 1 ; i++)
        {
            for (int j=startmonth ; j <= finishmonth ; j++)
            {
                total=total+*(day2+j);
            }
        }
        cout<<"★總天數= "<<total<<" 天"<<endl;
    }

}

else   //////////////////////////////判斷開始和終止是不同年
{
    //////////////輸入兩個年度,帶出中間的所有完整年度的天數(不含起始和終止年)//////////////
    int s=startyear+1;
    int f=finishyear-1;

    for (s ; s <= f ; s++)
    {
        if( s%4==0 && s%100!=0   ||  s%400==0   )
        {   
            for (int i=addstart ; i <= addend ; i++)
            {
                total=total+day1[i];
            }
        }
        else
        {
            for (int i=addstart ; i <= addend ; i++)
            {
                total=total+day2[i];
            }
        }
    }

    //判斷起始年的指定月份天數
    if (startyear%4==0 && startyear%100!=0   ||  startyear%400==0)  //閏年
    {
        for (int i=1 ; i <= 1 ; i++)
        {
            for (int j=startmonth ; j <= addend ; j++)
            {
                total=total+day1[j];
            }
        }
    }
    else    //平年
    {
        for (int i=1 ; i <= 1 ; i++)
        {
            for (int j=startmonth ; j <= addend ; j++)
            {
                total=total+*(day2+j);
            }
        }
    }

    //判斷終止年的指定月份天數
    if( finishyear%4==0 && finishyear%100!=0   ||  finishyear%400==0   )  //閏年
    {   
        for (int i=1 ; i <= 1 ; i++)
        {
            for (int j=addstart ; j <= finishmonth ; j++)
            {
                total=total+day1[j];
            }
        }
        cout<<endl;
        cout<<"★"<<startyear<<"年"<<startmonth<<"月~"<<finishyear<<"年"<<finishmonth<<"月,總天數= "<<total<<" 天。"<<endl;
        cout<<endl<<endl;

    }
    else //平年
    {
        for (int i=1 ; i <= 1 ; i++)
        {
            for (int j=addstart ; j <= finishmonth ; j++)
            {
                total=total+*(day2+j);
            }
        }
        cout<<endl;
        cout<<"★"<<startyear<<"年"<<startmonth<<"月~"<<finishyear<<"年"<<finishmonth<<"月,總天數= "<<total<<" 天。"<<endl;
        cout<<endl<<endl;
    }
}

}

發表留言