編程天數
直接算兩個日期距離公元元年1月1日的天數:
若干個完整的年,若干個完整的月,若干天,加起來之後兩個天數相減就行了.
日期的合法性,閏年之類的就不羅嗦了.
『貳』 c語言編寫程序,輸入某年某月,求該月的天數
#include<iostream.h>
voidmain()
{
intYear,Month,Day;
cout<<"Pleaseenterthecurrentdate(年月以空格分隔):";
cin>>Year>>Month;
while(Month<=0||Month>12)
{
cout<<"輸入時間有誤,請重新輸入:";
cin>>Year>>Month;
}
switch(Month)
{
case4:
case6:
case8:
case9:
case11:
Day=30;
break;
case2:
if(Year%400==0||Year%4==0&&nYear%100!=0)
Day=29;
else
Day=28;
break;
default:Day=31;
}
cout<<"該月天數為:"<<Day<<"天";
}
『叄』 給定年月日 怎樣用C語言編程計算2個日期之間的時間天數
1970 年以後的時間,可以用 time.h 里的函數計算。時間精度為秒。按題目要求,輸出時間單位用天。程序如下:
#include <stdio.h>
#include <time.h>
time_t YMD_hhmmss_2_s70(int Y, int M, int D, int hh, int mm, int ss){
struct tm *target_tm;
time_t tt;
time (&tt);
target_tm=localtime(&tt);
target_tm->tm_year = Y - 1900;
target_tm->tm_mon= M - 1;
target_tm->tm_mday = D;
target_tm->tm_hour = hh; // hour
target_tm->tm_min = mm;
target_tm->tm_sec = ss;
tt = mktime(target_tm); // from tm to time_t (s)
return tt;
}
int main()
{
int y1,m1,d1,y2,m2,d2;
time_t t1,t2;
int dt;
printf("input y1 m1 d1: ");
scanf("%d %d %d",&y1,&m1,&d1);
printf("\ninput y2 m2 d2: ");
scanf("%d %d %d",&y2,&m2,&d2);
t1 = YMD_hhmmss_2_s70(y1,m1,d1,0,0,0);
t2 = YMD_hhmmss_2_s70(y2,m2,d2,0,0,0);
dt = (t2-t1)/(24*3600);
printf("\ndt=%d\n",dt);
return 0;
}
這里未包含日期的合法性判斷。
1970 年以前 要另寫程序。某年的日子是當年的第幾天可用下面函數得出:
int YMD_2_JD(int Y, int M, int D){
const short MonthDay[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int JD,i;
JD=D;
for (i=0;i<M;i++) JD+=MonthDay[i];
if (((Y%4==0)&&(Y%100!=0)||(Y%400==0)) && (M>2)) JD++;
return JD;
}
整年的天數,涉及閏年的判斷:
某年是否閏年,用 (Y%4==0)&&(Y%100!=0)||(Y%400==0) 判斷。閏年366天,平年365天。 有了這些,寫程序不難。
未考慮公元前的年月日計算。
『肆』 輸入日期自動計算天數
是的,您可以使用編程語言編寫一個程序,讓用戶輸入日期,然後自動計算出這個日期是這一年中的第幾天。
下面是一個Python示例代碼:
from datetime import datetime
date_str = input("請輸入日期,格式為(YYYY-MM-DD):")
date = datetime.strptime(date_str, "%Y-%m-%d")
day_of_year = date.timetuple().tm_yday
print("這個枝簡日期是這一年中的第{}天。寬拍".format(day_of_year))
這個程序使用Python內置的datetime模塊,讓用戶輸入一個日期,然後計算出這個日期是這一年中的第幾天。其中,strptime方法將字元串格式的日期轉換為datetime對象,timetuple方法獲取時間元組,tm_yday屬性獲取這個日期是這一年中的第幾天。最後,程序輸出結猛巧褲果,告訴用戶這個日期是這一年中的第幾天。
『伍』 c語言用switch編程一個月有多少天,為什麼我輸出的結果沒有天數,輸出的只有年和月
用switch選擇的:
#include<stdio.h>
int main()
{
int y,m;
printf("請依次輸入年,月:\n");
scanf("%d,%d",&y,&m); //上機時注意打逗號,也可修改為其他格式
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:printf("這個月有31天\n");break;
case 2 : if(y%4==0&&y%100!=0||y%400==0) //最基本的判斷閏年的條件
printf("這個月有29天\n");
else printf("這個月有28天\n");break;
case 4:
case 6:
case 9 :
case 11:printf("這個月有30天\n");break;
}
return 0;
}
用指針型列舉的:
#include<stdio.h>
int a[]={31,28,31,30,31,30,31,31,30,31,30,31},*p1,*p2,*p3,i=1,k=0;
int main()
{
int p1,p2;
printf("請依次輸入年·月:\n");
scanf("%d%d",&p1,&p2);
if(p1%4==0&&p1%100!=0||p1%400==0)
a[1]=29;
if(p2>12)
printf("您的輸入有誤!\n");
printf("這個月的天數為:%d\n",a[p2-1]);
return 0;
}#include <stdio.h>
main()
{int a,b;
printf("請輸入年份和月份:\n");
scanf("%d%d",&a,&b);
if (a%4==0&&a%100!=0)
if(b==2)
printf("這個月有29天\n");
else if (b==1||b==3||b==5||b==7||b==8||b==10||b==12)
printf("這個月有31天\n");
else
printf("這個月有30天\n");
else if(b==2)
printf("這個月有28天\n");
else if (b==1||b==3||b==5||b==7||b==8||b==10||b==12)
printf("這個月有31天\n");
else
printf("這個月有30天\n");}
普通
#include <stdio.h>
main(){
int a,b;
printf("請輸入年份和月份:\n");
scanf("%d%d"茄州讓,&a,&b);
if (a%4==0&&a%100!=0)
if(b==2)
printf("這個月有29天\n");
else if (b==1||b==3||b==5||b==7||b==8||b==10||b==12)
printf("這個月有31天\n");
else
printf("這個月有30天\n");
else if(b==2)
printf("這個顫局月有28天\n");
else if (b==1||b==3||b==5||b==7||b==8||b==10||b==12)
printf("這個月有31天\n");
else
printf("這個月有30天\n");}
(5)編程天數擴展閱讀
switch 語句可以處理多分支選擇問題,根據其中break 語句的使用方法,一般分三種情況。
在switch 語句的每個語句跡螞段中都使用break 語句,這是switch 語句的主要使用方法,一般形式為:
switch (表達式)
case 常量表達式1: 語句段1; break;
case 常量表達式2: 語句段2; break;
case常量表達式n: 語句段n; break;
case常量表達式n+1:語句段n+ 1; break;
default :
『陸』 給定某年某月的數據,輸出此月的天數的C語言編程怎麼寫
如下:
#include<iostream.h>
voidmain()
{
intYear,Month,Day;
cout<<"Pleaseenterthecurrentdate(年月以空格分隔):";
cin>>Year>>Month;
while(Month<=0||Month>12)
{
cout<<"輸入時間有誤,請重新輸入:";
cin>>Year>>Month;
}
switch(Month)
{
case4:
case6:
case8:
case9:
case11:
Day=30;
break;
case2:
if(Year%400==0||Year%4==0&&nYear%100!=0)
Day=29;
else
Day=28;
break;
default:Day=31;
}
cout<<"該月天數為:"<<Day<<"天";
}
介紹
C語言是一種結構化語言。它層次清晰,便於按模塊化方式組織程序,易於調試和維護。C語言的表現能力和處理能力極強。它不僅具有豐富的運算符和數據類型,便於實現各類復雜的數據結構。
它跡弊還可以直接訪問內存的物理地址,進行位(bit)一級的世州數操作。由於C語言實現了對硬體的編程操作,因此C語言集高級語言和低級語言的功能於一體。
既可用於系統軟體的搜首開發,也適合於應用軟體的開發。此外,C語言還具有效率高,可移植性強等特點。因此廣泛地移植到了各類各型計算機上,從而形成了多種版本的C語言。
『柒』 編寫一個程序,輸入年,月,,列印出該年的天數
輸入年,月,列印出該年的天數:
#include<stdio.h>
main()
{
int y,m,d;
printf("year month=");scanf("%d%d",&y,&m);
switch(m){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:d=31;break;
case 4:
case 6:
case 9:
case 11:d=30;break;
case 2:if (y%4==0 && y%100!=0 || y%400==0) d=29; else d=28;
}
printf("days=%d
",d);
}
(7)編程天數擴展閱讀:
輸入某年某月(散基包括閏年),編程輸出該年的該月擁有的天數:
#include<stdio.h>
#define MONTHS 12
int main(){
int days[2][MONTHS]={{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}};
int year,month;
do{
printf("Input year,month:");
scanf("%d,%d",&year,&month);
}while(month<1||month>12);
if(((year%4==0)&&(year%100 !=0))||(year%400==0))
printf("The number of days is %d ",days[1][month-1]);
else
printf("The number of days is %d ",days[0][month-1]);
return 0;
}
『捌』 從鍵盤輸入一個年月日,編程求這一天到年底的天數
假設輸入日期格式為:yyyy-mm-dd,如2022-1-1,C語言代碼如下:
#include <stdio.h>
int main() {
int y, m, d, i;
scanf("%d-%d-%d", &y, &m, &d); // 要求輸入日期格式為yyyy-mm-dd
姿漏橋 int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)) // 閏年
month[2] = 29; // 閏年2月為29天
if (m <= 0 || m > 12) {
printf("月份%d不屬於[1,12],程序退出 ", m);
return -1;
}
int days = month[m] - d; // 距當月月底的天數
if (days < 0) {
printf("%d年%d月最多%d天,程序退出 ", y, m, month[m]);
return -1;
}
for (i = m + 1; i <= 12; i++)
days += month[i]; // 下月開始距年底的天數
printf("%d ", days);
return 0;
}
運行結果如下:
輸入2022-1-1,2022年為平年搜頌,一年共365天跡猛,所以2022年第一天距年底還有364天
輸出為364,符合題意,望採納~