zoukankan      html  css  js  c++  java
  • 定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天?注意闰年问题。

    本题没有加入正确日期判断功能
    #include <stdio.h>

    struct date 
    {
        
    int year; 
        
    int month; 
        
    int day; 
    }; 

    int leap_year(int a);
    int cal_day(struct date a);

    /*判断闰年*/ 
    int leap_year(int a)
    {
        
    if(a%400==0||(a%4==0&&a%100!=0)) 
            
    return 1;
        
    else 
            
    return 0;

    //     if(a%400==0)
    //         return 1;
    //     else if(a%100==0)
    //         return 0;
    //     else if(a%4==0)
    //         return 1;
    //     else 
    //         return 0;
    }

    /*计算一年中的第几天*/ 
    int cal_day(struct date a) 
    {
        
    int sum=0,b[]={31,28,31,30,31,30,31,31,30,31,30,31} ;
        
    for(int i=0;i<a.month-1;i++
            sum
    +=b[i]; 
        
    if(a.month>2)
            sum
    =sum+a.day+leap_year(a.year); 
        
    else
            sum
    =sum+a.day;
        
    return sum; 


    void main()
    {
        
    struct date a;
        
    int n;
        printf(
    "\n请输入日期(年 月 日)\n"); 
        scanf(
    "%d%d%d",&a.year,&a.month,&a.day); 
        n
    =cal_day(a);
        printf(
    "该日在本年中是第%d天\n",n);
    }
  • 相关阅读:
    Linux文本检索命令grep笔记
    Python中字典的相关操作
    Go 语言函数闭包
    Go 语言多维数组
    Go 错误处理
    Go 语言接口
    Go 语言类型转换
    Go 语言递归函数
    Go 语言Map(集合)
    Go 语言范围(Range)
  • 原文地址:https://www.cnblogs.com/qixin622/p/1238271.html
Copyright © 2011-2022 走看看