首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

小弟我写的一道习题,为什么实现不了功能呢~高手来帮帮小弟我啊(有分相赠)!

2012-02-26 
我写的一道习题,为什么实现不了功能呢~,高手来帮帮我啊(有分相赠)!!关于日期类的一道习题,实现将两个日期

我写的一道习题,为什么实现不了功能呢~,高手来帮帮我啊(有分相赠)!!
关于日期类的一道习题,实现将两个日期类相减求出相差天数,将一个日期类和天数相加求出所得的日期...我觉得我的思路没错啊,也没有语法错误,但是就是结果不正确.大家帮帮忙~
#include <iostream>
#include <iomanip>
#include <conio.h>
using   namespace   std;
class   Date{
int   year,month,day;
void   init();
public:
Date(const   string&   s);
Date(int   y=2000,int   m=1,int   d=1);
bool   isLeapYear()const;
int   absDate();                         //求绝对天数  
friend   ostream&   operator < <(ostream&   o,const   Date&   d);
friend   Date&   operator+(Date&   d,int   x);
friend   int   operator-(Date&   d,Date&   e);
};

int   Date::absDate()
{
  int   y,m,a,c;
  int   s1[12]={31,28,31,30,31,30,31,31,30,31,30,31};
  int   s2[12]={31,29,31,30,31,30,31,31,30,31,30,31};
  for(int   i=0,c=0;i <=year;++i)
  {if((i%4==0&&i%100)||i%400==0)
          ++c;}
  y=year*365+c;
  if(isLeapYear())
  {for(int   i=0;i <month;++i)
      m+=s2[i];}
    else   {for(int   i=0;i <month;++i)
                  m+=s1[i];}
  a=day;                
  return   (y+m+a);                      
}

void   Date::init(){
if(year> 5000||year <1||month <1||month> 12||day <1||day> 31)
exit(1);
}

Date::Date(const   string&   s){
year=atoi(s.substr(0,4).c_str());
month=atoi(s.substr(5,2).c_str());
day=atoi(s.substr(8,2).c_str());
init();
}

Date::Date(int   y,int   m,int   d){
year=y;month=m;day=d;
init();
}

bool   Date::isLeapYear()const{
return   (year%4==0&&year%100)||year%400==0;
}

ostream&   operator < <(ostream&   o,const   Date&   d){
o < <setfill( '0 ') < <setw(4) < <d.year < < '- ' < <setw(2) < <d.month < < '- ';
return   o < <setw(2) < <d.day < < '\n ' < <setfill( '   ');
}

Date&   operator+(Date&   d,int   x)
{
      int   y,m,a,c=0;
      int   s1[12]={31,28,31,30,31,30,31,31,30,31,30,31};
      int   s2[12]={31,29,31,30,31,30,31,31,30,31,30,31};
      int   s=d.absDate();
      s+=x;
      y=s/365;
      for(int   i=0;i <=s;++i)
            {if((i%4==0&&i%100)||i%400==0)
                      ++c;}
      for(int   i=0,m=0;i <12;++i)
            {if((y%4==0&&y%100)||y%400==0)
                  {if((s%365-c)> m)   m+=s2[i];
                      else   {a=(s%365-c)-(m-s2[i-1]);   m=(i-1+1);goto   l1;}                              


                  }
              else{if((s%365-c)> m)m+=s1[i];
                      else{a=(s%365-c)-(m-s2[i-1]);m=(i-1+1);goto   l1;}
                      }      
            }
l1:d.year=y;d.month=m;d.day=a;
      return   d;                      
}

int   operator-(Date&   d,Date&   e)
{int   c1,c2;
  c1=d.absDate();
  c2=e.absDate();
  return   (c1-c2);          
}
int   main()
{Date   c(2007,12,7);
Date   d(2007,12,6);
int   a=d.absDate();
cout < <a;
getchar();
}

[解决办法]
well,思路没错,语法没错,但语义有错.
在Date::absDate() Date& operator+(Date& d,int x)中都有两处错误:
1 变量m没初始化
2 注意for(int i=0,c=0;i <=year;++i)在循环内声明了两(!)个变量: i和c, 这个c把外面的同名变量c覆盖了,因此循环结束时外面变量c的值没有任何变化.

btw, 你在编译时有没有warning信息? 如果没有,你该换一个编译器,如果有而你没在意,就是你的错了,呵呵.

热点排行