#include<iostream>
#include "Chrono.h"
using namespace std;
namespace Chrono {
Date::Date(int yy, Month mm, int dd)
:y(yy), m(mm), d(dd){
if(!is_date(yy,mm,dd))throw Invalid();
}
Date::Date(){}
void Date::add_day(int n){
//...
}
void Date::add_month(int n){ // add_month is a member function
// add_day is a member function
// season is a member function
//...
}
bool is_date(int y, Date::Month m, int d)
{
// assume that y is valid
if(d<=0) return false;
int days_in_month = 31;
switch(m) {
// the length of February varies
/*case Date::feb:
days_in_month = (leapyear(y))?29:28;
break;
case Date::apr: case Date::jun: case Date::sep: case Date::nov:
// the rest have 31*/
days_in_month = 30;
break;
if(days_in_month<d) return false;
return true;
}
}
ostream& operator<<(ostream & os, Date& d){
return os<<'('<<d.year()<<','<<d.month()<<','<<d.day()<<')';
}
istream& operator>>(istream & is, Date& dd){
int y, d;
//Date::Month m;
int m;
char ch1, ch2, ch3, ch4;
is >> ch1 >> y >> ch2 >> m >> ch3 >> d >> ch4;
if(!is)return is;
if(ch1!='(' || ch2!=',' || ch3!=',' || ch4!=')'){
is.clear(ios_base::failbit);
return is;
}
dd = Date(y,Date::Month(m),d);
return is;
}
//Chrono
string Date::season()
{
int S;
if(S==12||S==1||S==2)
return "winter";
if(S==3||S==4||S==5)
return "spring";
if(S==6||S==7||S==8)
return "winter";
if(S==9||S==10||S==11)
return "spring";
}}
Hw4#4.cpp: In function `bool Chrono::is_date(int, Chrono::Date::Month, int)':
Hw4#4.cpp:56: warning: unreachable code at beginning of switch statement
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crt1.o(.text+0x18): In
function `_start':
../sysdeps/i386/elf/start.S:77: undefined reference to `main'
collect2: ld returned 1 exit status
Robert Ryan <[EMAIL PROTECTED]> wrote:
I am still working on this code. I have had to make other files:
Chono.h, Chono.cpp and season.cpp #include "Hw4#4.cpp"
using namespace Chrono;
int main()
{
cout << "season()" ;
Date d1(2008, Date::mar, 20);
cout << d1.season() << endl;
cout << "\n";
}
Robert Ryan <[EMAIL PROTECTED]> wrote:
//Change the Date class in the textbook by adding a function season()
//which returns a string depending on the month; e.g.,
// Date d1(2008, Month::mar, 20);
// cout << d1.season() << endl;
//should print spring. Use the following specification:
// mar-may spring
// jun-aug summer
// sep-nov fall
// dec-feb winter
//and also write a main function which tests season() on 12 dates,
//one from each month. Name your program season.cpp.
#include<iostream>
#include "Chrono.h"
using namespace std;
namespace Chrono {
Date::Date(Year yy, Month mm, int dd)
:y(yy), m(mm), d(dd){
if(check(yy,mm,dd))throw Invalid();
}
Date::Date(){}
void Date::add_day(int n){
//...
}
void Date::add_month(int n){
//...
}
void Date::add_year(int n){
if(m==feb && d==29 && !leapyear(y+n)){
m=mar;
d=1;
}
y+=n;
}
bool is_date(int y, Date::Month m, int d){
if(d<=0)return false;
int days_in_month=30;
switch(m){
case Date::jan: case Date::mar: case Date::may:
case Date::jul: case Date::aug: case Date::oct:
case Date::dec:
{
days_in_month=31;
break;
}
case Date::feb:
{
days_in_month=(leapyear(y))?29:28;
break;
} default:
days_in_month=30;
}
if(days_in_month<d)return false;
//return true;
}
ostream& operator<<(ostream & os, Date& d){
return os<<'('<<d.year()<<','<<d.month()<<','<<d.day()<<')';
}
istream& operator>>(istream & is, Date& dd){
int y, d;
//Date::Month m;
int m;
char ch1, ch2, ch3, ch4;
is >> ch1 >> y >> ch2 >> m >> ch3 >> d >> ch4;
if(!is)return is;
if(ch1!='(' || ch2!=',' || ch3!=',' || ch4!=')'){
is.clear(ios_base::failbit);
return is;
}
dd = Date(y,Date::Month(m),d);
return is;
}
//enum Day{sunday, monday, tuesday, wednesday, thursday,
//friday, saturday};
} //Chrono
void season()
{
enum S_code { SPRING, SUMMER, FALL, WINTER};
{
switch (s){
case SPRING:
cout << "spring";
break;
case SUMMER:
cout << "summer";
break;
case FALL:
cout << "fall";
break;
case WINTER:
cout << "winter";
break;
default:
cout << "Not a season" << endl;
}
}
int main()
{
cout << "season()" ;
}
$ g++ Hw4#4.cpp
.cpp:24: parse error before `yy'
.cpp:67: confused by earlier errors, bailing out
---------------------------------
Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.
----------
// file Chromo.h
namespace Chrono {
class Date {
public:
enum Month {
jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
};
class Invalid { };
Date(int y, Month m, int d);
Date();
// the default copy operations are fine
// non-modifying operations
int day() const {return d; }
Month month() const {return m; }
int year() const {return y; }
// modifying operations
//void add_year(int n);
void add_day(int n);
void add_month(int n);
void add_year(int n);
private:
int y;
Month m;
int d;
};
bool is_date(int y, Date::Month m, int d);
// true if y is a leap year
bool leapyear(int y);
bool operator==(const Date& a, const Date& b);
bool operator!=(const Date& a, const Date& b);
//ostream& operator << (ostream& os, const Date& d);
//istream& operator >> (istream& is, Date& dd);
}
[Non-text portions of this message have been removed]
---------------------------------
Looking for last minute shopping deals? Find them fast with Yahoo! Search.
[Non-text portions of this message have been removed]
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
[Non-text portions of this message have been removed]