| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 529 人关注过本帖
标题:请大家帮我看看这个程序(能运行)哪里出问题了?
只看楼主 加入收藏
a874695162
Rank: 2
等 级:论坛游民
帖 子:37
专家分:15
注 册:2014-7-21
结帖率:100%
收藏
已结贴  问题点数:30 回复次数:4 
请大家帮我看看这个程序(能运行)哪里出问题了?
/*输入年月日,输出这是该年的第几天,尚未添加输入错误判断功能*/

#include "stdafx.h"
#define Y p->year
#define M p->month
#define D p->day
#include<iostream>
#include<cmath>
using namespace std;
bool judge(int);
int add(int);
struct date
{int year;
int month;
int day;
};
int main()
{date * p;
p=new date;
cout<<"输入年,月,日"<<endl;
cout<<"年:"<<endl;
cin>>Y;
cout<<"月:"<<endl;
cin>>M;
cout<<"日:"<<endl;
cin>>D;
int count=0,i;
bool T=judge(Y);//判断该年是否为闰年,若是则T为true
 for(i=1;i<=(M-1);i++)//此处循环的作用是当月份为2时,根据T的情况给count加相应的天数
 {if(i==2)
 {if(T)count=count+29;
 else count=count+28;}
   
 }
 count=count+add(M)+D;//天数=二月份的天数+其他完整的月份的天数+剩余的天数
 cout<<"您输入的日期是"<<Y<<"年的第"<<count<<"天"<<endl;
 delete p;
 return 0;
}
int add(int k)
{int count=0,i;
for(i=1;i<=k-1;i++)//一月至七月每隔一月是31天,八月至十二月每隔一月是31天
{if(i<=7)
  if((i%2==0)&&(i!=2))count=count+30;
  else count=count+31;
else
  if((i-7)%2==0)count=count+30;
  else count=count+31;
}
return count;
}
bool judge(int t)
{
    bool T=false;
    if((abs(2004-t)%4==0)T=true;//2004年是闰年,那么和它相差4年的年份就应该是闰年
    return T;
}
输入2008 3 1
输出:“您输入的日期是2008年的第92天”
显然错误,请问错在哪里?
搜索更多相关主题的帖子: include 年月日 count 
2014-08-15 14:48
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
if((abs(2004-t)%4==0)T=true;//2004年是闰年,那么和它相差4年的年份就应该是闰年

-------闰年显然不是如此判断的

DO IT YOURSELF !
2014-08-15 15:18
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
程序代码:
#include <stdio.h>

//获取公历年初至某整月的天数
int year_sumday(int year,int month)
{
    int sum=0;
    int rui[12]={31,29,31,30,31,30,31,31,30,31,30,31};
    int ping[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    int ruiflag=0;
    if((year%4==0 &&year%100!=0) || year%400==0) ruiflag=1;
    for(int index=0;index<month-1;index++)
    {
        if(ruiflag==1) sum+=rui[index];else sum+=ping[index];
    }
    return sum;
}

bool y_check(int year,int month,int day)
{
    if(year>2100 || year<1800) return false;
    if (month>12) return false;
    if (day>31) return false;
    return true;
}
int main()
{
    int syear,smonth,sday;
_input:
    printf("请输入阳历年月日例如2012,11,30-->");
    scanf("%d,%d,%d",&syear,&smonth,&sday);
    if(y_check(syear,smonth,sday)==false) 
    {
        printf("输入日期有错误,请重新输入\n");
        goto _input;
    }
    int days=year_sumday(syear,smonth)+sday;
    printf("%d年%d月%d日是该年的第%d天\n",syear,smonth,sday,days);
    return 0;

}

DO IT YOURSELF !
2014-08-15 15:23
a874695162
Rank: 2
等 级:论坛游民
帖 子:37
专家分:15
注 册:2014-7-21
收藏
得分:0 
回复 3 楼 wp231957
多谢!你的算法要比我的好很多。
把闰年判断方法改了之后还是有误,应该还有别的问题,我将努力找出错误。

加油,再加油!编程,再编程!
2014-08-15 16:28
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
你应该是多算了一个整月

DO IT YOURSELF !
2014-08-16 09:33
快速回复:请大家帮我看看这个程序(能运行)哪里出问题了?
数据加载中...
 
   



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

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