diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Bachelor/Prog2/map/map.cpp | |
| download | Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.gz Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.bz2 | |
Diffstat (limited to 'Bachelor/Prog2/map/map.cpp')
| -rw-r--r-- | Bachelor/Prog2/map/map.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
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 <iostream>
+using std::cout;
+using std::cerr;
+using std::endl;
+using std::ios;
+
+#include <string>
+using std::string;
+
+#include <fstream>
+using std::ifstream;
+using std::ofstream;
+
+#include <map>
+
+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;
+}
|
