summaryrefslogtreecommitdiffstats
path: root/7.00.Fehlerklassen.cc
blob: 645a8e62b9ac554b27ba7c12f226bae02f2efebf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * =====================================================================================
 *
 *       Filename:  7.00.Fehlerklassen.cc
 *
 *    Description:  Implements a datum class with exeptions
 *
 *        Version:  1.0
 *        Created:  23.06.2014 13:14:42
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Stefan Suhren (SSuhren), suhren.stefan@fh-swf.de
 *   Organization:  FH Südwestfalen, Iserlohn
 *
 * =====================================================================================
 */

#include	<cstdlib>
#include	<iostream>
#include	<iomanip>
#include	<fstream>
#include	<string>

using namespace std;

// #### TYPE DEFINITIONS - LOCAL TO THIS FILE ####

typedef unsigned int uint;

// =====================================================================================
//        Class:  FehlerDatum
//  Description:  
// =====================================================================================
class FehlerDatum
{
	public:
		FehlerDatum ( string msg = "FehlerDatum", uint val = 0 ) : message(msg), value(val) {} // constructor 
		string what ( ) { return message; }
		uint   when ( ) { return value; }

	private: 
		string message;
		uint value;
}; // -----  end of class FehlerDatum  ----- 

// =====================================================================================
//        Class:  FehlerTag
//  Description:  
// =====================================================================================
class FehlerTag : public FehlerDatum
{
	public:
		FehlerTag ( string msg = "FehlerTag", uint val = 0 ) : FehlerDatum(msg, val) {} // constructor 
}; // -----  end of class FehlerDatum  ----- 

// =====================================================================================
//        Class:  FehlerMonat
//  Description:  
// =====================================================================================
class FehlerMonat : public FehlerDatum
{
	public:
		FehlerMonat ( string msg = "FehlerMonat", uint val = 0 ) : FehlerDatum(msg, val) {} // constructor 
}; // -----  end of class FehlerDatum  ----- 

// =====================================================================================
//        Class:  Datum
//  Description:  Handhabung von Kalenderdaten (Minimalversion)
// =====================================================================================
class Datum
{
	public:
		// ====================  LIFECYCLE     ======================================= 
		Datum () {};                             // constructor 
		Datum ( uint tg, uint mnt, uint jhr );

		// ====================  ACCESSORS     ======================================= 
		uint getTag   () const { return tag;   }
		uint getMonat () const { return monat; }
		uint getJahr  () const { return jahr;  }

	private:
		// ====================  METHODS       ======================================= 
		bool monatOk   ();
		bool tagOk     ();
		bool schaltjahr();

		// ====================  DATA MEMBERS  ======================================= 
		uint tag;
		uint monat;
		uint jahr;

}; // -----  end of class Datum  ----- 

// ===  FUNCTION  ======================================================================
//         Name:  Datum::monatOk
//  Description:  
// =====================================================================================
bool Datum::monatOk (){
	return (monat >= 1 && monat <= 12) ? true : false;
}		// -----  end of function Datum::monatOk  ----- 

// ===  FUNCTION  ======================================================================
//         Name:  Datum::tagOk
//  Description:  
// =====================================================================================
bool Datum::tagOk (){
	if( !monatOk() ) throw FehlerMonat( "Falsche Monatsangabe", monat );
	if( tag < 1 ) return false;
	if( tag > 31 ) return false;

	switch( monat ){
		case 4:
		case 6:
		case 9:
		case 11:
			if( tag > 30 ) return false;
			break;

		case 2:
			if( tag > 29 ) return false;
			if( !schaltjahr() && tag == 29 ) return false;
			break;
	}
	
	return true;
}		// -----  end of function Datum::tagOk  ----- 

// ===  FUNCTION  ======================================================================
//         Name:  Datum::schaltjahr
//  Description:  
// =====================================================================================
bool Datum::schaltjahr (){
	if( jahr % 4 == 0 ){
		if( jahr % 100 == 0 ){
			if( jahr % 400 == 0 ){
				return true;
			}
			return false;
		}
		return true;
	}
	return false;
}		// -----  end of function Datum::schaltjahr  ----- 

// ===  FUNCTION  ======================================================================
//         Name:  operator <<
//  Description:  
// =====================================================================================
ostream & operator << ( ostream & os, const Datum & obj ){
	os << setfill('0')
	   << setw(4) << obj.getJahr()
	   << "-" << setw(2) << obj.getMonat()
	   << "-" << setw(2) << obj.getTag();
	os << setfill(' ');

	return os;
}		// -----  end of function operator <<  ----- 

// ===  FUNCTION  ======================================================================
//         Name:  Datum::Datum
//  Description:  
// =====================================================================================
Datum::Datum ( uint tg, uint mnt, uint jhr ){
	tag = tg;
	monat = mnt;
	jahr = jhr;
	if( !tagOk() ){
		throw FehlerTag( "Falsche Tagesangabe", tag );
	}
}		// -----  end of function Datum::Datum  ----- 

// ===  FUNCTION  ======================================================================
//         Name:  main
//  Description:  
// =====================================================================================
int main ( int argc, char *argv[] ){
	Datum dtm[100000];

	string    ifs_file_name = "datumsliste.dat";  // input  file name 
	ifstream  ifs;                                // create ifstream object 

	ifs.open ( ifs_file_name.c_str() );           // open ifstream 
	if (!ifs) {
		cerr << "\nERROR : failed to open input  file " << ifs_file_name << endl;
		exit (EXIT_FAILURE);
	}

	int tg = 0, mnt = 0, jhr = 0, i = 0;
	
	while(ifs >> tg >> mnt >> jhr){
		try{
			dtm[i] = Datum(tg, mnt, jhr);
		}
		catch( FehlerMonat &ExceptObj ){
			cout << "Satz Nr. " << setw(7) << i+1 << " : " << ExceptObj.what() << " : " << ExceptObj.when() << "\n";
		}
		catch( FehlerTag &ExceptObj ){
			cout << "Satz Nr. " << setw(7) << i+1 << " : " << ExceptObj.what() << "  : " << ExceptObj.when() << "\n";
		}
		i++;
	}

	ifs.close ();                                 // close ifstream 

	cout << i << " Datensätze ausgelesen.\n";

	return EXIT_SUCCESS;
}				// ----------  end of function main  ----------