INFO1-CE9265 December 5, 2012

Which is easier to read?

		return ost<<d.month<<"/"<<d.day<<d.year<<"\n";

		return ost << d.month << "/" << d.day << d.year << "\n";

Make an array

Homework 4.1.1b in Chapter 4, p. 366. Class date has three int data members: year, month, day. The && d.day>21 && d.day<32 in the Brumaire paragraph is always true and therefore does nothing.

ostream& operator<<(ostream& ost, const date& d)
{
	switch (ost.iword(date::subscript)){
	case 0:
		return ost<<d.month<<"/"<<d.day<<d.year<<"\n";

	case 1:
		if(d.month==9 && d.day>21 && d.day<31){
			return ost<<d.day-21<<" Vendemiaire de l'Annee "<<d.year+1-1792<<" de la Republic\n";}
		if(d.month==10 && d.day<22){
			return ost<<d.day+9<<" Vendemiaire de l'Annee "<<d.year+1-1792<<" de la Republic\n";}

		if(d.month==10 && d.day>21 && d.day<32){
			return ost<<d.day-21<<" Brumaire de l'Annee "<<d.year+1-1792<<" de la Republic\n";}
		if(d.month==11 && d.day<21){
			return ost<<d.day+10<<" Brumaire de l'Annee "<<d.year+1-1792<<" de la Republic\n";}

		if(d.month==11 && d.day>20 && d.day<31){
			return ost<<d.day-20<<" Frimaire de l'Annee "<<d.year+1-1792<<" de la Republic\n";}
		if(d.month==12 && d.day<21){
			return ost<<d.day+10<<" Frimaire de l'Annee "<<d.year+1-1792<<" de la Republic\n";}

		if(d.month==12 && d.day>20 && d.day<32){
			return ost<<d.day-20<<" Nivose de l'Annee "<<d.year+1-1792<<" de la Republic\n";}
		if(d.month==1 && d.day<20){
			return ost<<d.day+11<<" Nivose de l'Annee "<<d.year+1-1792<<" de la Republic\n";}

		if(d.month==1 && d.day>19 && d.day<32){
			return ost<<d.day-19<<" Pluviose de l'Annee "<<d.year+1-1792<<" de la Republic\n";}
		if(d.month==2 && d.day<19){
			return ost<<d.day+12<<" Pluviose de l'Annee "<<d.year+1-1792<<" de la Republic\n";}

		if(d.month==2 && d.day>18 && d.day<29){
			return ost<<d.day-18<<" Ventose de l'Annee "<<d.year+1-1792<<" de la Republic\n";}
		if(d.month==3 && d.day<21){
			return ost<<d.day+10<<" Ventose de l'Annee "<<d.year+1-1792<<" de la Republic\n";}

		if(d.month==3 && d.day>20 && d.day<29){
			return ost<<d.day-20<<" Germinal de l'Annee "<<d.year+1-1792<<" de la Republic\n";}
		if(d.month==4 && d.day<20){
			return ost<<d.day+11<<" Germinal de l'Annee "<<d.year+1-1792<<" de la Republic\n";}

		if(d.month==4 && d.day>19 && d.day<31){
			return ost<<d.day-19<<" Floreal de l'Annee "<<d.year+1-1792<<" de la Republic\n";}
		if(d.month==5 && d.day<20){
			return ost<<d.day+11<<" Floreal de l'Annee "<<d.year+1-1792<<" de la Republic\n";}

		if(d.month==5 && d.day>19 && d.day<32){
			return ost<<d.day-19<<" Prairal de l'Annee "<<d.year+1-1792<<" de la Republic\n";}
		if(d.month==6 && d.day<19){
			return ost<<d.day+12<<" Prairal de l'Annee "<<d.year+1-1792<<" de la Republic\n";}

		if(d.month==6 && d.day>18 && d.day<31){
			return ost<<d.day-18<<" Messidor de l'Annee "<<d.year+1-1792<<" de la Republic\n";}
		if(d.month==7 && d.day<19){
			return ost<<d.day+12<<" Messidor de l'Annee "<<d.year+1-1792<<" de la Republic\n";}

		if(d.month==7 && d.day>18 && d.day<32){
			return ost<<d.day-18<<" Thermidor de l'Annee "<<d.year+1-1792<<" de la Republic\n";}
		if(d.month==8 && d.day<18){
			return ost<<d.day+13<<" Thermidor de l'Annee "<<d.year+1-1792<<" de la Republic\n";}

		if(d.month==8 && d.day>17 && d.day<32){
			return ost<<d.day-17<<" Fructidor de l'Annee "<<d.year+1-1792<<" de la Republic\n";}
		if(d.month==9 && d.day<17){
			return ost<<d.day+14<<" Fructidor de l'Annee "<<d.year+1-1792<<" de la Republic\n";}

		if(d.month==9 && d.day==17){
			return ost<<" Fete de la vertu de l'Annee "<<d.year+1-1792<<" de la Republic\n";}
		if(d.month==9 && d.day==18){
			return ost<<" Fete du genie de l'Annee "<<d.year+1-1792<<" de la Republic\n";}
		if(d.month==9 && d.day==19){
			return ost<<" Fete du travail de l'Annee "<<d.year+1-1792<<" de la Republic\n";}
		if(d.month==9 && d.day==20){
			return ost<<" Fete de l'opinion de l'Annee "<<d.year+1-1792<<" de la Republic\n";}
		if(d.month==9 && d.day==21){
			return ost<<" Fete des recompenses de l'Annee "<<d.year+1-1792<<" de la Republic\n";}
		else{
			return ost<<"Date not available.";}

	case 2:
		return ost<<d.month<<"/"<<d.day<<d.year<<"\n";

	default:
		cerr<<ost.iword(date::subscript)<<" is neither revolutionary or nonrevolutionary.\n";
		exit(EXIT_FAILURE);
}
}

Let’s strip out the code that does nothing.

ostream& operator<<(ostream& ost, const date& d)
{
	switch (ost.iword(date::subscript)) {
	case 0:
		return ost << d.month << "/" << d.day << d.year << "\n";

	case 1:
		if (d.month == 9 && d.day > 21) {
			return ost << d.day-21 << " Vendemiaire de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}
		if (d.month == 10 && d.day < 22) {
			return ost << d.day+9 <<" Vendemiaire de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}

		if (d.month == 10) {
			return ost << d.day-21 << " Brumaire de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}
		if (d.month == 11 && d.day < 21) {
			return ost << d.day+10 << " Brumaire de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}

		if (d.month == 11) {
			return ost << d.day-20 << " Frimaire de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}
		if (d.month == 12 && d.day < 21) {
			return ost << d.day+10 << " Frimaire de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}

		if (d.month == 12 && d.day > 20) {
			return ost << d.day-20 << " Nivose de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}
		if (d.month == 1 && d.day < 20) {
			return ost << d.day+11 << " Nivose de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}

		if (d.month == 1) {
			return ost << d.day-19 << " Pluviose de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}
		if (d.month == 2 && d.day < 19) {
			return ost << d.day+12 << " Pluviose de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}

		if (d.month == 2) {
			return ost << d.day-18 << " Ventose de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}
		if (d.month == 3 && d.day < 21) {
			return ost << d.day+10 << " Ventose de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}

		if (d.month == 3) {
			return ost << d.day-20 << " Germinal de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}
		if (d.month == 4 && d.day < 20) {
			return ost << d.day+11 << " Germinal de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}

		if (d.month == 4) {
			return ost << d.day-19 << " Floreal de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}
		if (d.month == 5 && d.day < 20) {
			return ost << d.day+11 << " Floreal de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}

		if (d.month == 5) {
			return ost << d.day-19 << " Prairal de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}
		if (d.month == 6 && d.day < 19) {
			return ost << d.day+12 << " Prairal de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}

		if (d.month == 6) {
			return ost << d.day-18 << " Messidor de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}
		if (d.month == 7 && d.day < 19) {
			return ost << d.day+12 << " Messidor de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}

		if (d.month == 7) {
			return ost << d.day-18 << " Thermidor de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}
		if (d.month == 8 && d.day < 18) {
			return ost << d.day+13 << " Thermidor de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}

		if (d.month == 8) {
			return ost << d.day-17 << " Fructidor de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}
		if (d.month == 9 && d.day < 17) {
			return ost << d.day+14 << " Fructidor de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}

		if (d.month == 9 && d.day == 17) {
			return ost << " Fete de la vertu de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}
		if (d.month == 9 && d.day == 18) {
			return ost << " Fete du genie de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}
		if (d.month == 9 && d.day == 19) {
			return ost << " Fete du travail de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}
		if (d.month == 9 && d.day == 20) {
			return ost << " Fete de l'opinion de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}
		if (d.month == 9 && d.day == 21) {
			return ost << " Fete des recompenses de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}
		else {
			return ost << "Date not available.";
		}

	case 2:
		return ost << d.month << "/" << d.day <<d.year << "\n";

	default:
		cerr << ost.iword(date::subscript) << " is neither revolutionary nor nonrevolutionary.\n";
		exit(EXIT_FAILURE);
	}
}

The above code compares d.month to 9 in 7 places.

#include <string>
using namespace std;

struct month {
	int cutoff;
	string name;
	int offset1;
	int offset2;
};

month a[] = {
	{0,	"",		 0, -0},
	{19,	"Novose",	11, -19},
	{18,	"Pluviose",	12, -18},
	{20,	"Ventose",	10, -20},
	{19,	"Germinal",	11, -19},
	{19,	"Floreal",	11, -19},
	{18,	"Prairal",	12, -18},
	{18,	"Messidor",	12, -18},
	{17,	"Thermidor",	13, -17},
	{17,	"Fructidor",	13, -17},
	{21,	"Vendemiaire",	 9, -21},
	{21,	"Brumaire",	 9, -21},
	{20,	"Frimaire",	10, -20}
};
const size_t n = sizeof a / sizeof a[0];

ostream& operator<<(ostream& ost, const date& d)
{
	switch (ost.iword(date::subscript)) {
	case 0:
	case 2:
		return ost << d.month << "/" << d.day << d.year <<"\n";

	case 1: {
		if (d.month != 9 || d.day < 17 || d.day > 21) {
			const int cutoff = a[d.month].cutoff;
			string name;
			if (d.day <= cutoff) {
				name = a[d.month].name;
			} else {
				name = a[d.month < 12 ? d.month + 1 : 1].name;
			}
			const int offset = d.day <= cutoff ? a[d.month].cutoff1 : a[d.month].cutoff2;
			return ost << d.day + offset << " " << name
				<< " de l'Annee " << d.year+1-1792
				<< " de la Republic\n";
		}

		if (d.day == 17) {
			return ost << " Fete de la vertu de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}

		if (d.day == 18) {
			return ost << " Fete du genie de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}

		if (d.day == 19) {
			return ost << " Fete du travail de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}

		if (d.day == 20) {
			return ost << " Fete de l'opinion de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}

		if (d.day == 21) {
			return ost << " Fete des recompenses de l'Annee " << d.year+1-1792 << " de la Republic\n";
		}
	}

	default:
		cerr << ost.iword(date::subscript) << " is neither revolutionary nor nonrevolutionary.\n";
		exit(EXIT_FAILURE);
	}
}
#include <string>
using namespace std;

struct month {
	int cutoff;
	string name;
	int offset1;
	int offset2;
};

month a[] = {
	{0,	"",		 0, -0},
	{19,	"Novose",	11, -19},
	{18,	"Pluviose",	12, -18},
	{20,	"Ventose",	10, -20},
	{19,	"Germinal",	11, -19},
	{19,	"Floreal",	11, -19},
	{18,	"Prairal",	12, -18},
	{18,	"Messidor",	12, -18},
	{17,	"Thermidor",	13, -17},
	{17,	"Fructidor",	13, -17},
	{21,	"Vendemiaire",	 9, -21},
	{21,	"Brumaire",	 9, -21},
	{20,	"Frimaire",	10, -20}
};
const size_t n = sizeof a / sizeof a[0];

const string holiday[] = {
	"de la vertu",
	"du genie",
	"du travail",
	"de l'opinion",
	"des recompenses"
};

ostream& operator<<(ostream& ost, const date& d)
{
	switch (ost.iword(date::subscript)) {
	case 0:
	case 2:
		return ost << d.month << "/" << d.day << d.year << "\n";

	case 1: {
		if (d.month != 9 || d.day < 17 || d.day > 21) {
			const int cutoff = a[d.month].cutoff;
			string name;
			if (d.day <= cutoff) {
				name = a[d.month].name;
			} else {
				name = a[d.month < 12 ? d.month + 1 : 1].name;
			}
			const int offset = d.day <= cutoff ? a[d.month].cutoff1 : a[d.month].cutoff2;

			return ost << d.day + offset << " " << name
				<< " de l'Annee " << d.year+1-1792
				<< " de la Republic\n";
		}

		return ost << " Fete " << holiday[d.day - 17]
			<< " de l'Annee " << d.year+1-1792 << " de la Republic\n";
	}

	default:
		cerr << ost.iword(date::subscript) << " is neither revolutionary nor nonrevolutionary.\n";
		exit(EXIT_FAILURE);
	}
}