以及距他下一次生日的天数。是C语言,不是C++
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了以及距他下一次生日的天数。是C语言,不是C++相关的知识,希望对你有一定的参考价值。
希望能给完整的代码,谢谢
从键盘输入一个人的出生年月日和当前日期,计算他的实际年龄,以及距他下一次生日的天数。C语言程序
代码如下,最下面有用例示范:
#include <stdio.h>
#include <math.h>
int bigMonth[7]=1,3,5,7,8,10,12;
int smallMonth[4]=4,6,9,11;
int sumOfDays(int year1,int year2,int month1,int month2,int date1,int date2 )
int totalDays=0;
int dif_year,dif_month,dif_date;//difference between strating ana ending year/month/day
int startingDayOfYear,endingDayOfYear;//起始日期在当年是第多少天
dif_year=year2-year1;
dif_month=month2-month1;
dif_date=date2-date1;
switch(month1)
case(1):
startingDayOfYear=date1;break;
case(2):
startingDayOfYear=31+date1;break;
case(3):
startingDayOfYear=60+date1;break;
case(4):
startingDayOfYear=91+date1;break;
case(5):
startingDayOfYear=121+date1;break;
case(6):
startingDayOfYear=152+date1;break;
case(7):
startingDayOfYear=182+date1;break;
case(8):
startingDayOfYear=213+date1;break;
case(9):
startingDayOfYear=244+date1;break;
case(10):
startingDayOfYear=274+date1;break;
case(11):
startingDayOfYear=305+date1;break;
case(12):
startingDayOfYear=335+date1;break;
default:break;
if(month1>2 && year1%4!=0)
startingDayOfYear--;
switch(month2)
case(1):
endingDayOfYear=date2;break;
case(2):
endingDayOfYear=31+date2;break;
case(3):
endingDayOfYear=60+date2;break;
case(4):
endingDayOfYear=91+date2;break;
case(5):
endingDayOfYear=121+date2;break;
case(6):
endingDayOfYear=152+date2;break;
case(7):
endingDayOfYear=182+date2;break;
case(8):
endingDayOfYear=213+date2;break;
case(9):
endingDayOfYear=244+date2;break;
case(10):
endingDayOfYear=274+date2;break;
case(11):
endingDayOfYear=305+date1;break;
case(12):
endingDayOfYear=335+date2;break;
default:break;
if(month2>2 && year2%4!=0)
endingDayOfYear--;
totalDays=endingDayOfYear-startingDayOfYear+365*(year2-year1);
for(int i=year1;i<year2;i++)
if(i%4==0)
totalDays++;
return totalDays;
void main()
int year1,year2,month1,month2,date1,date2;
printf("请输入出生日期:\\n");
scanf("%d %d %d",&year1,&month1,&date1);
printf("请输入当前日期:\\n");
scanf("%d %d %d",&year2,&month2,&date2);
if(month2>month1 || (month2==month1 && date2>=date1))
printf("实际年龄为: %d\\n",year2-year1);
printf("距离下一次生日还有 %d 天\\n",sumOfDays(year2,year2+1,month2,month1,date2,date1));
else
printf("实际年龄为: %d\\n",year2-year1-1);
printf("距离下一次生日还有 %d 天\\n",sumOfDays(year2,year2,month2,month1,date2,date1));
追问
非常感谢
参考技术A #include <stdio.h>#include <stdlib.h>
#include <memory.h>
#include <time.h>
int verify(const struct tm *ptm)
int leap;
int year;
int mday;
year = ptm->tm_year;
mday = ptm->tm_mday;
leap = (year%4 == 0 && year%100 != 0) || (year%400 == 0);
switch(ptm->tm_mon)
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return mday <= 31;
case 4:
case 6:
case 9:
case 11:
return mday <= 30;
case 2:
if(leap)
return mday <= 29;
else
return mday <= 28;
default:
return 0;
int main()
int age;
double secs;
time_t birth;
time_t today;
struct tm tm1;
struct tm tm2;
struct tm *ptm;
memset(&tm1, 0, sizeof(struct tm));
memset(&tm2, 0, sizeof(struct tm));
today = time(NULL);
ptm = localtime(&today);
tm2.tm_year = ptm->tm_year;
tm2.tm_mon = ptm->tm_mon;
tm2.tm_mday = ptm->tm_mday;
today = mktime(&tm2);
while(1)
printf("请输入生日(如:1990-3-15):\n");
scanf("%d-%d-%d", &tm1.tm_year, &tm1.tm_mon, &tm1.tm_mday);
if(!verify(&tm1))
printf("输入的日期不合法,请重新输入。\n");
continue;
tm1.tm_year -= 1900;
tm1.tm_mon -= 1;
birth = mktime(&tm1);
if(today > birth)
break;
printf("您输入的出生日期大于或等于当前日期。\n");
printf("出生日期:%d-%d-%d\n", tm1.tm_year+1900, tm1.tm_mon+1, tm1.tm_mday);
printf("当前日期:%d-%d-%d\n", tm2.tm_year+1900, tm2.tm_mon+1, tm2.tm_mday);
age = tm2.tm_year - tm1.tm_year;
tm1.tm_year = tm2.tm_year;
birth = mktime(&tm1);
if(today >= birth)
age++;
tm1.tm_year++;
birth = mktime(&tm1);
secs = difftime(birth, today);
printf("年龄(周岁加一):%d\n", age);
printf("距离下次生日%d-%d-%d还有%d天。\n",
tm1.tm_year+1900, tm1.tm_mon+1, tm1.tm_mday, (int)(secs/24/3600));
return 0;
本回答被提问者采纳 参考技术B 这个问题很简单么,其实就一个判断平年和瑞年就好了,别的都是纯数字计算的。你是刚学C还是学了很久了?追问
学了不久,懂的不多
参考技术C 舒服个当然分公司岁生日歌生日乖乖乖乖乖乖乖乖乖乖乖乖乖乖乖乖乖乖乖乖乖乖乖乖乖乖乖乖怎样用c语言编写以年月日的格式输入一个人的生日和当前日期,来计算这个人的年龄
参考技术A 我给你写的不仅能显示年龄而且能显示距现在的天数。当前日期无需输入,程序自动调取系统日期。。运行过了没有任何问题。有什么问题可以交流下。#include <stdio.h>
#include<time.h>
//计算给定的日期是本年的第几天
int count(int year,int month,int day)
int i,sum=0,flag=0;
int a[13]=0,31,28,31,30,31,30,31,31,30,31,30,31;
for(i=1;i<month;i++)
sum=a[i]+sum;
if(year%400==0||year%100!=0&&year%4==0)
flag=1;
if(flag==1&&month>2) sum++;
sum=sum+day;
return sum;
int main()
int year,month,day;
time_t t;
struct tm *local;
int sum=0,i;
t=time(NULL);
local=localtime(&t);
printf("今天的日期:%d年%d月%d日\n",local->tm_year+1900,local->tm_mon+1,local->tm_mday);
printf("输入生日:");
scanf("%d%d%d",&year,&month,&day);
for(i=year+1;i<local->tm_year+1900;i++)
if(i%400==0||i%100!=0&&i%4==0)
sum+=366;
else sum+=365;
if((year%400==0||year%100!=0&&year%4==0))
sum+=366-count(year,month,day);
else sum+=365-count(year,month,day);
sum+=count(local->tm_year+1900,local->tm_mon+1,local->tm_mday);
printf("%d年%d月%d日距今天有%d天\n",year,month,day,sum);
printf("\n你的年龄为%d\n",local->tm_year+1900-year);
c编程高手团队正在招新,有意者速速行动,一起学习,一起努力!
以上是关于以及距他下一次生日的天数。是C语言,不是C++的主要内容,如果未能解决你的问题,请参考以下文章
怎样用c语言编写以年月日的格式输入一个人的生日和当前日期,来计算这个人的年龄