#include <iostream>
using namespace std;
bool isLeap(int x)
{
if(x % 4 == 0 && ! (x % 400 != 0 && x % 100 == 0))
{
return true;
}
else
return false;
}
int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main(){
int startYear, startMonth, startDay;
int endYear, endMonth, endDay;
cout<<"請輸入正確的開始年、月、日,中間用空格鍵隔開:";
cin >> startYear >> startMonth >> startDay;
cout<<"請輸入正確的結束年、月、日,中間用空格鍵隔開:";
cin >> endYear >> endMonth >> endDay;
int ans=0;
if(startYear == endYear) //如果是同一年
{
if(startMonth == endMonth)
{
ans = endDay - startDay;
}
else
{
for(int i = startMonth + 1; i < endMonth; i ++)
{
ans += month[i];
}
ans += month[startMonth] - startDay + endDay;
if(isLeap(startYear)) //如果是閏年
{
if(startMonth <= 2 && endMonth > 2) //判斷起止月是否包含2月
{
ans += 1;
}
}
}
}
else
{
//計算兩個年份之間年份的總天數
for(int i = startYear + 1; i < endYear; i ++)
{
ans += 365;
if(isLeap(i))
ans += 1;
}
//計算起始年份的總天數
for(int i = startMonth + 1; i <= 12; i ++)
{
ans += month[i];
}
ans += month[startMonth] - startDay;
//特殊判斷起始月份如果在2月之前(包括2月),在閏年的情況下,天數需要加1
if(isLeap(startYear))
{
if(startMonth <= 2)
{
ans += 1;
}
}
//計算終止年份的天數
for(int i = 1; i < endMonth; i ++)
{
ans += month[i];
}
ans += endDay;
//特殊判斷終止月份如果在2月之后(不包括2月),在閏年的情況下,天數需要加1
if(isLeap(endYear))
{
if(endMonth > 2)
{
ans += 1;
}
}
}
cout <<ans <<endl;
system("pause");
return 0;
}
uj5u.com熱心網友回復:
為了回圈從1開始for(int i = 1; i < endMonth; i ++)
uj5u.com熱心網友回復:
你也可以只定義12個數.uj5u.com熱心網友回復:
這樣的話,月份和下標就一致了,不容易混淆轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/61572.html
標籤:C++ 語言
下一篇:請教關于隨機存盤/訪問
