用户输入日、月和年(月份可以是月份号、月份名或月份缩写),程序返回一年中到给定日子(包括这一天)的总天数

来源:互联网 发布:声音剪辑软件 编辑:程序博客网 时间:2024/05/21 16:49
#include<stdio.h>//用户输入日、月和年(月份可以是月份号、月份名或月份缩写),程序返回一年中到给定日子(包括这一天)的总天数int days(int day,int mon,int year);int leapyear(int year);struct month{    char name[10];    char abbrev[4];//简写的    int days;    int monumb;};struct month months[12] = {    {"january","jan",31,1},    {"february","feb",28,2},    {"march","mar",31,3},    {"april","apr",30,4},    {"may","may",31,5},    {"june","jun",30,6},    {"july","jul",31,7},    {"august","aug",31,8},    {"september","sep",30,9},    {"october","oct",31,10},    {"november","nov",30,11},    {"december","dec",31,12}};int main(void){    int day = 1,mon = 12,year = 1,daytotal;    printf("Enter the number of day,month,year");    while(scanf("%d%d%d",&day,&mon,&year) == 3)    {        daytotal = days(day,mon,year);        if(daytotal>0)            printf("There are %d days through day %d,month %d,year %d\n",daytotal,day,mon,year);        else            printf("day%d,month%d,year%d is not valid input\n",day,mon,year);    printf("Next month(input q to quit)");//输入其他都可以退出       }    puts("bye");    return 0;}int days(int day,int mon,int year){    int i,total;    if(leapyear(year))        months[1].days = 29;    else        months[1].days = 28;    if(mon<1 || mon>12 || day<1 || day>months[mon-1].days)        return -1; //error    else     {        for(i=0,total=0;i<mon-1;i++)            total += months[i].days;    return (total+day);         }}int leapyear(int year){    if(year%400 == 0)//闰年        return 1;    else if(year%100 != 0 && year%4 == 0)//闰年        return 1;    else return 0;}
阅读全文
0 0