计算已经生活了多少天的小题目
生活随笔
收集整理的這篇文章主要介紹了
计算已经生活了多少天的小题目
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
讀書時(shí)的小測(cè)試題.重新寫一遍 .
可以將出生和當(dāng)前日期定為結(jié)構(gòu)體 這樣傳值就很方便.
#include <stdio.h> #include <math.h> #include <stdbool.h> int leap_year_arr[12] = {31,29,31,30,31,30,31,31,30,31,30,31}; //閏年 int Ordinary_year_arr[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; //平年bool check_is_leap(int year) {if ( year%400==0 ){return true;}else if(year%100!=0 && year%4 == 0){return true;}else{return false;} } int middle_sum(int born,int now) //中間年份日子的計(jì)算 {int middlesum = 0;while (born+1 < now){if(check_is_leap(born+1)){middlesum = middlesum + 366;}else{middlesum = middlesum + 365;}born++;} return middlesum; } int now_past_day(int pyear,int pmount,int pday) //今年已經(jīng)過去多久天 {int sumpastday = 0; //過去的總天數(shù)int mount = 0; //用于累加 去計(jì)算過去幾個(gè)月了;if (check_is_leap(pyear)){while( mount < pmount){sumpastday = sumpastday + leap_year_arr[mount]; //閏年數(shù)組mount ++;}}else{while( mount < pmount){sumpastday = sumpastday + Ordinary_year_arr[mount]; //平年數(shù)組mount ++;}}sumpastday = sumpastday + pday; //還要算上這個(gè)月過去的 return sumpastday; } int have_day(int pyear,int pmount,int pday) //今年還有多少天 {int haveday = 0;if( check_is_leap(pyear)){haveday = 366 - now_past_day(pyear,pmount,pday);}else{haveday = 365 - now_past_day(pyear,pmount,pday);} return haveday; } bool check_entry(int bornyear,int nowyear,int bornmonth,int nowmonth,int bornday, int nowday) {if( bornyear > nowyear || nowmonth > 12 || bornmonth > 12 || bornday > 31 || nowday > 31){printf("您的輸入有誤 請(qǐng)輸入正確值\n");return false;}else if( !check_is_leap(bornyear) && bornday > 28 && bornmonth == 2){printf("您出生年不是閏年 2月份 只有28 天\n");return false;}else if( !check_is_leap(nowyear) && nowday > 28 && nowmonth == 2){printf("現(xiàn)在不是閏年 2月份 只有28 天\n");return false;}else{return true;} } int main() {int bornyear = 0, bornmonth = 0, bornday = 0;int nowyear = 0, nowmonth = 0, nowday = 0;int sumday = 0;printf("請(qǐng)輸入當(dāng)前日期 年--月--日\(chéng)n");scanf("%d%d%d",&nowyear,&nowmonth,&nowday);printf("請(qǐng)輸入出生年份日期 年--月--日\(chéng)n");scanf("%d%d%d",&bornyear,&bornmonth,&bornday);if (check_entry(bornyear,nowyear,bornmonth,nowmonth,bornday,nowday) == false){return 0;}// 總天數(shù)等于出生年還剩多少天 + 現(xiàn)在年過去多少天 + 中間年if( bornyear == nowyear){sumday = have_day(bornyear,bornmonth,bornday) - have_day(nowyear,nowmonth,nowday);}else{sumday = have_day(bornyear,bornmonth,bornday) + now_past_day(nowyear,nowmonth,nowday) + middle_sum(bornyear,nowyear); }printf("你已經(jīng)來到地球%d天\n",sumday + 1); return 0; }也可以使用時(shí)間戳,這樣就沒上面有意思
#include<stdio.h> #include<time.h> #include<stdlib.h>int main() {char c_born[8] ={0};struct tm* tmp_time = (struct tm*)malloc(sizeof(struct tm));printf("請(qǐng)輸入您的出生年--月--日\(chéng)n");scanf("%s",c_born);strptime(c_born,"%Y%m%d",tmp_time);time_t born_t = mktime(tmp_time); //轉(zhuǎn)化時(shí)間戳//獲取系統(tǒng)的時(shí)間戳time_t nowtime = time(NULL);double day = difftime(nowtime,born_t);//printf("%f\n",difftime(nowtime,t));printf("您來到地球已經(jīng) = %f天\n",day/(60*60*24)); return 0; }?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的计算已经生活了多少天的小题目的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: switch怎么导出截图
- 下一篇: CSS怎么把一幅图片的边框定义为outs