From b2f7e26d27f2bc58a20cbba98d81b9243c96467d Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Thu, 8 May 2014 12:11:20 +0200 Subject: Assignment No. 3 These are the linked listes and personen structs. --- 3.01.singlelinkedList.cc | 78 ++++++++++++++++++++++ 3.02.rohdatenLesen.cc | 59 +++++++++++++++++ 3.03.personenStruktur.cc | 68 +++++++++++++++++++ 3.04.personenListe.cc | 113 +++++++++++++++++++++++++++++++ 3.05.personenFunktion.cc | 168 +++++++++++++++++++++++++++++++++++++++++++++++ personen.dat | 12 ++++ 6 files changed, 498 insertions(+) create mode 100644 3.01.singlelinkedList.cc create mode 100644 3.02.rohdatenLesen.cc create mode 100644 3.03.personenStruktur.cc create mode 100644 3.04.personenListe.cc create mode 100644 3.05.personenFunktion.cc create mode 100644 personen.dat diff --git a/3.01.singlelinkedList.cc b/3.01.singlelinkedList.cc new file mode 100644 index 0000000..73f765b --- /dev/null +++ b/3.01.singlelinkedList.cc @@ -0,0 +1,78 @@ +/* + * ===================================================================================== + * + * Filename: 3.01.singlelinkedList.cc + * + * Description: Single linked list + * + * Version: 1.0 + * Created: 23.04.2014 12:13:13 + * Revision: none + * Compiler: gcc + * + * Author: Stefan Suhren (SSuhren), suhren.stefan@fh-swf.de + * Organization: FH Südwestfalen, Iserlohn + * + * ===================================================================================== + */ + +#include +#include +#include + +using namespace std; + +struct Element{ + long key; // Schluessel des Listenelements + long info; // die im Listenelement zu verwaltende Information + struct Element *next; // Zeiger auf nächste Element +}; + +// === FUNCTION ====================================================================== +// Name: main +// Description: Simple program no functions needed +// ===================================================================================== +int main ( int argc, char *argv[] ){ + // Create pointer for start and search + struct Element* L = NULL, *cursor = NULL; + + // Create actual elements + struct Element x1, x2, x3, x4; + + // Initialize structs + x1.key = 1; + x1.info = 11; + x2.key = 2; + x2.info = 22; + x3.key = 3; + x3.info = 33; + x4.key = 4; + x4.info = 44; + + // Now create the linked list + L = &x1; + x1.next = &x2; + x2.next = &x3; + x3.next = &x4; + x4.next = NULL; + + // Set cursor to starting point + cursor = L; + + // Go over the List until the list ends or the key is found + while(cursor != NULL && cursor->key != 3){ + cursor = cursor->next; + } + + // Check if value was found + if(cursor != NULL){ + cout << "key = " << setw(15) << cursor->key << endl + << "info = " << setw(15) << cursor->info << endl + << "next = " << setw(15) << cursor->next << endl; + } + else{ + cout << "Value not found!!!"; + } + + return EXIT_SUCCESS; +} // ---------- end of function main ---------- diff --git a/3.02.rohdatenLesen.cc b/3.02.rohdatenLesen.cc new file mode 100644 index 0000000..bab932a --- /dev/null +++ b/3.02.rohdatenLesen.cc @@ -0,0 +1,59 @@ +// ===================================================================================== +// +// Filename: 3.02.rohdatenLesen.cc +// +// Description: Reads data from a file +// +// Version: 1.0 +// Created: 23.04.2014 12:46:15 +// Revision: none +// Compiler: gcc +// +// Author: Stefan Suhren (SSuhren), suhren.stefan@fh-swf.de +// Organization: FH Südwestfalen, Iserlohn +// +// ===================================================================================== + +#include +#include +#include +#include +#include + +using namespace std; +// === FUNCTION ====================================================================== +// Name: main +// Description: Reading and printing the personen.dat file +// ===================================================================================== +int main ( int argc, char *argv[] ){ + + string ifs_file_name = "personen.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); + } + + string identnummer; // Idenditätsnummer + string nachname; // Nachname + string vorname; // Vorname + string abteilung; // Abteilungsbezeichnung + unsigned int durchwahl; // Telefondurchwahl + + int satz = 0; + + while ( ifs >> identnummer >> nachname >> vorname >> abteilung >> durchwahl ) { + satz++; + cout << "Person Nr." << satz << "\n" + << "\t ID: " << identnummer << "\n" + << "\t Nachname: " << nachname << "\n" + << "\t Vorname: " << vorname << "\n" + << "\t Abteilung: " << abteilung << "\n" + << "\t Durchwahl: " << durchwahl << "\n"; + } + + ifs.close (); // close ifstream + return EXIT_SUCCESS; +} // ---------- end of function main ---------- diff --git a/3.03.personenStruktur.cc b/3.03.personenStruktur.cc new file mode 100644 index 0000000..2bef438 --- /dev/null +++ b/3.03.personenStruktur.cc @@ -0,0 +1,68 @@ +/* + * ===================================================================================== + * + * Filename: 3.03.personenStruktur.cc + * + * Description: Personen as globel array + * + * Version: 1.0 + * Created: 23.04.2014 21:38:18 + * Revision: none + * Compiler: gcc + * + * Author: Stefan Suhren (SSuhren), suhren.stefan@fh-swf.de + * Organization: FH Südwestfalen, Iserlohn + * + * ===================================================================================== + */ + +#include +#include +#include +#include +#include + +using namespace std; + +// ##### TYPE DEFINITIONS - LOCAL TO THIS SOURCE FILE ############# +struct mitarbeiter{ + string identnummer; + string nachname; + string vorname; + string abteilung; + unsigned int durchwahl; +}; // ---------- end of struct mitarbeiter ---------- + +// ##### VARIABLES - LOCAL TO THIS SOURCE FILE #################### +const unsigned int MitarbeiterMax = 1000; // max. Anzahl Mitarbeiter +mitarbeiter ma[MitarbeiterMax]; // Feld mit Mitarbeiter-Beschr. +// === FUNCTION ====================================================================== +// Name: main +// Description: Reading and printing the personen.dat file +// ===================================================================================== +int main ( int argc, char *argv[] ){ + + string ifs_file_name = "personen.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 satz = 0; + + while ( ifs >> ma[satz].identnummer >> ma[satz].nachname >> ma[satz].vorname >> ma[satz].abteilung >> ma[satz].durchwahl ) { + cout << "Person Nr." << satz+1 << "\n" + << "\t ID: " << ma[satz].identnummer << "\n" + << "\t Nachname: " << ma[satz].nachname << "\n" + << "\t Vorname: " << ma[satz].vorname << "\n" + << "\t Abteilung: " << ma[satz].abteilung << "\n" + << "\t Durchwahl: " << ma[satz].durchwahl << "\n"; + satz++; + } + + ifs.close (); // close ifstream + return EXIT_SUCCESS; +} // ---------- end of function main ---------- diff --git a/3.04.personenListe.cc b/3.04.personenListe.cc new file mode 100644 index 0000000..ac48021 --- /dev/null +++ b/3.04.personenListe.cc @@ -0,0 +1,113 @@ +// ===================================================================================== +// +// Filename: 3.04.personenListe.cc +// +// Description: Personen as list with Structs +// +// Version: 1.0 +// Created: 23.04.2014 21:50:42 +// Revision: none +// Compiler: gcc +// +// Author: Stefan Suhren (SSuhren), suhren.stefan@fh-swf.de +// Organization: FH Südwestfalen, Iserlohn +// +// ===================================================================================== + +#include +#include +#include +#include +#include + +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 ---------- + +// === FUNCTION ====================================================================== +// Name: main +// Description: Reading and printing the personen.dat file +// ===================================================================================== +int main ( int argc, char *argv[] ){ + + string ifs_file_name = "personen.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); + } + + // Creating anchor and cursor + mitarbeiter *ma = 0, *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; + } +// cout << "Person Nr." << satz+1 << "\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"; + + satz++; + } + else{ + delete maNeu; + } + } + + ifs.close (); // close ifstream + + // Reset cursor + cursor = ma; + // Walk over list to find the record + while( cursor != NULL && cursor->durchwahl != 4731 ){ + cursor = cursor->next; + } + + // Check if something was found + if( cursor != NULL ){ + cout << "Record found.\nName: " << cursor->vorname << " " << cursor->nachname << "\n"; + } + else{ + cout << "Record not found!!!" << "\n"; + } + + // Now delete the list + // Reset cursor + // Temp var for deleting + while( ma != NULL ){ + cursor = ma; + ma = cursor->next; + delete cursor; + } + // Make list sane again + // ma = NULL; + + return EXIT_SUCCESS; +} // ---------- end of function main ---------- 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 +#include +#include +#include +#include + +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; + } +} diff --git a/personen.dat b/personen.dat new file mode 100644 index 0000000..e21ad98 --- /dev/null +++ b/personen.dat @@ -0,0 +1,12 @@ +10001223 DOLBEL DAVID QC18 4700 +10002220 JAYALATH THILANAKA QC18 4701 +10002222 GRANT MARK QC18 4714 +10002222 ILLICH ANDREW QC18 4726 +10002223 ATTWELL SIMON QC18 4702 +10003222 GAMMON JAMIE QC15 4718 +10022049 BAILEY JASON QC18 4703 +10022194 ALDOUS MATTHEW QC15 4719 +10022252 CHATRANGSAN WERACHAT QC15 4222 +10022266 FU NING QC15 4224 +10022284 GILLETT NATHAN QC05 4731 +10022865 JACKSON SUZANNE QC18 4752 -- cgit v1.2.3-70-g09d2