| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1112 人关注过本帖
标题:输入年,月,日,计算是本年的第几天。两种算法,用if和函数,只用if没问题 ...
只看楼主 加入收藏
wangtan1314
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2018-1-20
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
输入年,月,日,计算是本年的第几天。两种算法,用if和函数,只用if没问题,加上函数就会出问题,求指导
#include <stdio.h>
#include <stdlib.h>
void leap();
void number();

int main()
{
    //输入年月日,求是这一年的第几天;
    int year, month,day,n;
    int sumday;
    printf("请输入年月日:");
    scanf("%d%d%d",&year,&month,&day);
    if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
    {
        switch(month)
        {
            case 1:
                sumday = day;
                printf("今天是今年的第%d天",sumday);
                break;
            case 2:
                sumday = day + 31;
                printf("今天是今年的第%d天",sumday);
            break;
            case 3:
                sumday = day + 60;
                printf("今天是今年的第%d天",sumday);
            break;
            case 4:
                sumday = day + 91;
                printf("今天是今年的第%d天",sumday);
            break;
            case 5:
            sumday = day + 121;
                printf("今天是今年的第%d天",sumday);
            break;
            case 6:
            sumday = day + 152;
                printf("今天是今年的第%d天",sumday);
            break;
            case 7:
            sumday = day + 182;
                printf("今天是今年的第%d天",sumday);
            break;
            case 8:
            sumday = day + 213;
                printf("今天是今年的第%d天",sumday);
            break;
            case 9:
            sumday = day + 244;
                printf("今天是今年的第%d天",sumday);
            break;
            case 10:
            sumday = day + 274;
                printf("今天是今年的第%d天",sumday);
            break;
            case 11:
            sumday = day + 305;
                printf("今天是今年的第%d天",sumday);
            break;
            case 12:
            sumday = day + 335;
                printf("今天是今年的第%d天",sumday);
            break;
        }
    }
     else
    {
        switch(month)
        {
            case 1:
                sumday = day;
                printf("今天是今年的第%d天",sumday);
                break;
            case 2:
                sumday = day + 31;
                printf("今天是今年的第%d天",sumday);
            break;
            case 3:
                sumday = day + 59;
                printf("今天是今年的第%d天",sumday);
            break;
            case 4:
                sumday = day + 90;
                printf("今天是今年的第%d天",sumday);
            break;
            case 5:
            sumday = day + 120;
                printf("今天是今年的第%d天",sumday);
            break;
            case 6:
            sumday = day + 151;
                printf("今天是今年的第%d天",sumday);
            break;
            case 7:
            sumday = day + 181;
                printf("今天是今年的第%d天",sumday);
            break;
            case 8:
            sumday = day + 212;
                printf("今天是今年的第%d天",sumday);
            break;
            case 9:
            sumday = day + 243;
                printf("今天是今年的第%d天",sumday);
            break;
            case 10:
            sumday = day + 273;
                printf("今天是今年的第%d天",sumday);
            break;
            case 11:
            sumday = day + 304;
                printf("今天是今年的第%d天",sumday);
            break;
            case 12:
            sumday = day + 334;
                printf("今天是今年的第%d天",sumday);
            break;
        }
    }
    //利用函数计算
     n = number(year, month, day);
     printf("今天是今年的第%d天",sumday);
    return 0;
}
void leap(int a)
{
    if(a % 4 == 0 && a % 100 != 0 || a % 400 == 0)
        return 1;
    else
        return 0;
}
void number(int year,int month,int day)
{
    int sum = 0, i;
    int a[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
    int b[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
    if(leap(year) == 1)
       for(i = 0;i < month-1; i++)
        {
            sum += b[i];
        }
    else
        for(i = 0;i < month-1; i++)
        {
            sum += a[i];
        }
    sum = sum +day;
    return sum;
}



||=== 构建: Debug 在 example14 day 中 (编译器: GNU GCC Compiler) ===|
E:\codeblocks\c class 282\example14 day\main.c||In function 'main':|
E:\codeblocks\c class 282\example14 day\main.c|13|warning: suggest parentheses around '&&' within '||' [-Wparentheses]|
E:\codeblocks\c class 282\example14 day\main.c|122|error: void value not ignored as it ought to be|
E:\codeblocks\c class 282\example14 day\main.c|9|warning: variable 'n' set but not used [-Wunused-but-set-variable]|
E:\codeblocks\c class 282\example14 day\main.c||In function 'leap':|
E:\codeblocks\c class 282\example14 day\main.c|128|warning: suggest parentheses around '&&' within '||' [-Wparentheses]|
E:\codeblocks\c class 282\example14 day\main.c|129|warning: 'return' with a value, in function returning void|
E:\codeblocks\c class 282\example14 day\main.c|131|warning: 'return' with a value, in function returning void|
E:\codeblocks\c class 282\example14 day\main.c||In function 'number':|
E:\codeblocks\c class 282\example14 day\main.c|138|error: void value not ignored as it ought to be|
E:\codeblocks\c class 282\example14 day\main.c|149|warning: 'return' with a value, in function returning void|
||=== 构建 失败: 2 error(s), 6 warning(s) (0 分, 0 秒) ===|
搜索更多相关主题的帖子: main printf case 今天 break 
2018-01-27 16:17
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10609
专家分:43210
注 册:2014-5-20
收藏
得分:20 
编译方面:
//void leap();
int leap(int a);
//void number();
int number(int year,int month,int day);

//void leap(int a)
int leap(int a)

//void number(int year,int month,int day)
int number(int year,int month,int day)
2018-01-27 16:43
wangtan1314
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2018-1-20
收藏
得分:0 
谢谢,已经明白,void 声明函数没有返回值,需要用int声明
2018-01-28 15:33
wangtan1314
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2018-1-20
收藏
得分:0 
回复 2楼 吹水佬
谢谢大佬指教
2018-01-28 15:33
快速回复:输入年,月,日,计算是本年的第几天。两种算法,用if和函数,只用if没 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035471 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved