diff options
Diffstat (limited to '7.00.Fehlerklassen.cc')
| -rw-r--r-- | 7.00.Fehlerklassen.cc | 210 |
1 files changed, 210 insertions, 0 deletions
diff --git a/7.00.Fehlerklassen.cc b/7.00.Fehlerklassen.cc new file mode 100644 index 0000000..645a8e6 --- /dev/null +++ b/7.00.Fehlerklassen.cc @@ -0,0 +1,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 ---------- |
