From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- Bachelor/Prog2/map/map.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Bachelor/Prog2/map/map.cpp (limited to 'Bachelor/Prog2/map/map.cpp') diff --git a/Bachelor/Prog2/map/map.cpp b/Bachelor/Prog2/map/map.cpp new file mode 100644 index 0000000..c140a88 --- /dev/null +++ b/Bachelor/Prog2/map/map.cpp @@ -0,0 +1,65 @@ +// Praktikum 4.1 using an associative container (std::map) +// Author: Hans-Peter Weber +// Date: 20.02.04 + +#pragma warning( disable: 4786 ) + +#include +using std::cout; +using std::cerr; +using std::endl; +using std::ios; + +#include +using std::string; + +#include +using std::ifstream; +using std::ofstream; + +#include + +int main() +{ + typedef std::map< string, int > stringMap; + stringMap words; + string s; + int numberOfWords = 0; + + ifstream in( "Max.txt" ); + if( !in ) { + cerr << "Datei konnte nicht geoeffnet werden." << endl; + exit( 1 ); + } + +// fill map 'words' with all words contained in file (implicitly sorted) and their frequencies + while( in >> s ) { + ++words[ s ]; + numberOfWords++; + } + + in.close(); + +// store words and frequencies in text file + ofstream out( "WordList.txt", ios::app ); + if( !out ) { + cerr << "Datei konnte nicht geoeffnet werden." << endl; + exit( 1 ); + } + string mapElement; + + for( stringMap::const_iterator wordIterator = words.begin(); + wordIterator != words.end(); ++wordIterator ) { + mapElement = wordIterator->first; + mapElement.resize( 20, '.' ); +// cout << mapElement << wordIterator->second << endl; + out << mapElement << wordIterator->second << endl; + } + + out.close(); + + cout << "Woerter in der Datei: " << numberOfWords << endl + << "unterschiedliche Woerter: " << words.size() << endl << endl; + + return 0; +} -- cgit v1.2.3