M
hatte ein wenig zeit und hab versucht das ganze ein bisschen mehr nach c++ aussehen zu lassen... der code oben ist eher c (außer ein paar dinge wie streams und das schlüsselwort class, welches man aber ohne probleme mit typedef struct auswechseln könnte, also hier)
also code ist ungefähr genauso wie der ausgangscode, hab mir da auch nicht zu viel überlegt. Aja.. weiß nicht ob das eine sehr tolle lösung ist, kann natürlich sein, dass ich sachen komisch mache die andre nicht so machen würden, aber die werden das ja hoffentlich eh anmerken...
// date.h
#ifndef _DATE_
#define _DATE_
#include <string>
#include <cstdlib>
namespace util
{
template<class T> const T& min(const T& a, const T& b)
{
return (a>b) ? a : b;
}
class date
{
friend std::ostream& operator<< (std::ostream& os, const date& obj);
friend std::istream& operator>> (std::istream& is, date& obj);
public:
date(void);
date(const unsigned int day, const unsigned int month, const unsigned year);
date(const std::string& txt);
inline bool is_leap_year(void) const
{ return date::leap_year(m_year);}
unsigned int days_till (const date& obj) const;
inline ~date(void){}
private:
bool check_date(void);
unsigned int m_day;
unsigned int m_month;
unsigned int m_year;
bool m_val;
static bool leap_year(const unsigned int year);
};
extern std::ostream& operator<< (std::ostream& os, const date& obj);
extern std::istream& operator>> (std::istream& is, date& obj);
}; // end namespace util
#endif
// date.cc
#include "date.h"
#include <sstream>
namespace util
{
date::date(void) : m_day(0), m_month(0), m_year(0), m_val(false)
{}
date::date(const unsigned int day, const unsigned int month,
const unsigned year)
: m_year(year), m_month(month), m_day(day), m_val(check_date())
{}
date::date(const std::string& txt) : m_val(false)
{
std::istringstream is(txt);
char c=0;
is>>m_day>>c>>m_month>>c>>m_year;
m_val=check_date();
}
bool date::check_date(void)
{
if(m_month>12 || m_month==0)
{
m_month=0;
m_day=0;
m_year=0;
return false;
}
else
{
unsigned char days_of_month[12]={31, 28, 31, 30, 31,
30, 31, 31, 30, 31, 30, 31};
if(is_leap_year())
days_of_month[1]=29;
if(m_day==0 || m_day>days_of_month[m_month+1])
{
m_day=0;
m_month=0;
m_year=0;
return false;
}
else
return true;
}
}
unsigned int date::days_till (const date& obj) const
{
if(!obj.m_val || !m_val)
return 0;
else if((obj.m_year< m_year)||
(obj.m_year==m_year && obj.m_month<m_month)||
(obj.m_year==m_year && obj.m_month==m_month && obj.m_day<m_day))
{
return obj.days_till(*this);
}
else
{
unsigned char days_of_month[12]={31, 28, 31, 30, 31,
30, 31, 31, 30, 31, 30, 31};
const unsigned int years=obj.m_year-m_year;
unsigned int days=0;
for(unsigned int i=0; i<years; ++i)
{
days+=365;
if(leap_year(m_year+i))
++days;
}
if(obj.is_leap_year())
days_of_month[1]=29;
for(unsigned int i=1; i<obj.m_month; ++i)
days+=days_of_month[i-1];
days+=obj.m_day;
if(is_leap_year())
days_of_month[1]=29;
else
days_of_month[1]=28;
days-=m_day;
for(unsigned int i=1; i<m_month; ++i)
days-=days_of_month[i-1];
return days;
}
}
bool date::leap_year(const unsigned int year)
{
if(year%400==0)
return true;
else if(year%100==0)
return false;
else if(year%4==0)
return true;
else
return false;
}
std::ostream& operator<< (std::ostream& os, const date& obj)
{
if(obj.m_val)
os<<obj.m_day<<"."<<obj.m_month<<"."<<obj.m_year;
else
os<<" datum ungültig...";
return os;
}
std::istream& operator>> (std::istream& is, date& obj)
{
char c;
is>>obj.m_day>>c>>obj.m_month>>c>>obj.m_year;
obj.m_val=obj.check_date();
return is;
}
}; // end namespace util
// main.cc
#include <cstdlib>
#include <iostream>
#include "date.h"
using namespace std;
int main(int argc, char *argv[])
{
util::date a(1, 2, 2007);
util::date b(1, 3, 2010);
util::date c;
cout<<a<<endl<<b<<endl<<endl<<a.days_till(b)<<endl;
cin>>c;
cout<<endl<<c.days_till(a)<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
na ich hoffe du kannst was damit anfangen... naja sonst machts ja auch nix.
mfg Manuel