diff options
Diffstat (limited to '3.05.personenFunktion.cc')
| -rw-r--r-- | 3.05.personenFunktion.cc | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/3.05.personenFunktion.cc b/3.05.personenFunktion.cc new file mode 100644 index 0000000..a26797c --- /dev/null +++ b/3.05.personenFunktion.cc @@ -0,0 +1,168 @@ +// ===================================================================================== +// +// Filename: 3.05.personenFunktion.cc +// +// Description: Personen list with functions +// +// Version: 1.0 +// Created: 23.04.2014 22:53:33 +// 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 SOURCE FILE ############# +struct mitarbeiter{ + string identnummer; + string nachname; + string vorname; + string abteilung; + unsigned int durchwahl; + mitarbeiter *next; +}; // ---------- end of struct mitarbeiter ---------- + +// Header +int datenbank_lesen ( mitarbeiter **ma, string filename ); +void liste_loeschen ( mitarbeiter **ma ); +mitarbeiter* suche_durchwahl ( mitarbeiter **ma, unsigned int durchwahl ); +void mitarbeiter_aus ( mitarbeiter *ma ); +void alle_mitarbeiter_aus( mitarbeiter **ma ); + +// === FUNCTION ====================================================================== +// Name: main +// Description: Reading and printing the personen.dat file +// ===================================================================================== +int main ( int argc, char *argv[] ){ + mitarbeiter *ma = NULL; + + cout << "Gelesen: " << datenbank_lesen( &ma, "personen.dat" ) << "\n"; + + alle_mitarbeiter_aus( &ma ); + + cout << "Search for 4731" << "\n"; + + mitarbeiter_aus( suche_durchwahl( &ma, 4731 ) ); + + liste_loeschen( &ma ); + + return EXIT_SUCCESS; +} // ---------- end of function main ---------- + +// === FUNCTION ====================================================================== +// Name: datenbank_lesen +// Description: Reads database from filename and puts anchor into ma +// returns the count of read records +// ===================================================================================== +int datenbank_lesen ( mitarbeiter **ma, string filename ){ + ifstream ifs; // create ifstream object + + ifs.open ( filename.c_str() ); // open ifstream + if (!ifs) { + cerr << "\nERROR : failed to open input file " << filename << endl; + exit (EXIT_FAILURE); + } + + // Creating anchor and cursor + mitarbeiter *cursor = 0, *maNeu = 0; + int satz = 0; + + while ( ifs ) { + + // Create temporary object + maNeu = new mitarbeiter; + + if( ifs >> maNeu->identnummer >> maNeu->nachname >> maNeu->vorname >> maNeu->abteilung >> maNeu->durchwahl ){ + // Makes sure next is NULL + maNeu->next = NULL; + if( *ma == NULL ){ + *ma = maNeu; + cursor = *ma; + } + else{ + cursor->next = maNeu; + cursor = maNeu; + } + satz++; + } + else{ + delete maNeu; + } + } + + ifs.close (); // close ifstream + + return satz; +} + +// === FUNCTION ====================================================================== +// Name: liste_loeschen +// Description: Delete list at anchor ma +// ===================================================================================== +void liste_loeschen ( mitarbeiter **ma ){ + mitarbeiter *cursor = *ma, *delMa; + while( cursor != NULL ){ + delMa = cursor; + cursor = cursor->next; + delete delMa; + } + // Make list sane again + *ma = NULL; +} + +// === FUNCTION ====================================================================== +// Name: suche_durchwahl +// Description: Looks for durchwahl in list with anchor ma +// ===================================================================================== +mitarbeiter* suche_durchwahl ( mitarbeiter **ma, unsigned int durchwahl ){ + mitarbeiter *cursor = *ma; + while( cursor != NULL && cursor->durchwahl != durchwahl ){ + cursor = cursor->next; + } + if( cursor != NULL ){ + return cursor; + } + else{ + return NULL; + } +} + +// === FUNCTION ====================================================================== +// Name: mitarbeiter_aus +// Description: Pretty prints the employee ma +// ===================================================================================== +void mitarbeiter_aus ( mitarbeiter *ma ){ + cout << "Person:\n" + << "\t ID: " << ma->identnummer << "\n" + << "\t Nachname: " << ma->nachname << "\n" + << "\t Vorname: " << ma->vorname << "\n" + << "\t Abteilung: " << ma->abteilung << "\n" + << "\t Durchwahl: " << ma->durchwahl << "\n"; +} + +// === FUNCTION ====================================================================== +// Name: alle_mitarbeiter_aus +// Description: Pretty prints all employees from the anchor ma +// ===================================================================================== +void alle_mitarbeiter_aus( mitarbeiter **ma ){ + mitarbeiter *cursor = *ma; + while( cursor != NULL ){ + cout << "Person:\n" + << "\t ID: " << cursor->identnummer << "\n" + << "\t Nachname: " << cursor->nachname << "\n" + << "\t Vorname: " << cursor->vorname << "\n" + << "\t Abteilung: " << cursor->abteilung << "\n" + << "\t Durchwahl: " << cursor->durchwahl << "\n"; + cursor = cursor->next; + } +} |
