diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-11-02 07:05:36 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-11-02 07:05:36 +0000 |
| commit | 101f074b87a4356d716bf3cce087547cb0959b76 (patch) | |
| tree | e20f7f9cf3eb4f18a480b1aa221ee40a8e42ceda /challenge-032/ulrich-rieke/cpp | |
| parent | c6ed50dffe9799b9c8a48b270b744449dc083acd (diff) | |
| download | perlweeklychallenge-club-101f074b87a4356d716bf3cce087547cb0959b76.tar.gz perlweeklychallenge-club-101f074b87a4356d716bf3cce087547cb0959b76.tar.bz2 perlweeklychallenge-club-101f074b87a4356d716bf3cce087547cb0959b76.zip | |
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-032/ulrich-rieke/cpp')
| -rw-r--r-- | challenge-032/ulrich-rieke/cpp/ch-1.cpp | 72 | ||||
| -rw-r--r-- | challenge-032/ulrich-rieke/cpp/ch-2.cpp | 92 |
2 files changed, 164 insertions, 0 deletions
diff --git a/challenge-032/ulrich-rieke/cpp/ch-1.cpp b/challenge-032/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..39c61373e3 --- /dev/null +++ b/challenge-032/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,72 @@ +#include <iostream> +#include <vector> +#include <map> +#include <algorithm> +#include <utility> +#include <fstream> + +void printStrings( const std::vector<std::string> & strings ) { + std::map<std::string, int> frequencies ; + int maxlen = 0 ; + for ( std::string str : strings ) { + frequencies[ str ]++ ; + if ( str.length( ) > maxlen ) + maxlen = str.length( ) ; + } + std::vector<std::pair<std::string , int>> orderedFreq { frequencies.begin( ), + frequencies.end( ) } ; + std::sort ( orderedFreq.begin( ) , orderedFreq.end( ) , []( const + std::pair<std::string, int> a , const std::pair<std::string, int> b ) + { return a.second > b.second ; } ) ; + std::string answer ; + std::cout << "Output as csv : (y)es or (n)o ?\n" ; + std::cin >> answer ; + if ( answer.substr(0, 1 ) == "n" ) { + for ( auto & p : orderedFreq ) { + std::cout << p.first ; + std::cout.width( maxlen - p.first.length( ) + 1 ) ; + std::cout << p.second << std::endl ; + } + } + else { + for ( auto & p : orderedFreq ) { + std::cout << p.first << ',' << p.second << '\n' ; + } + } +} + +int main( int argc , char * argv[] ) { + std::vector<std::string> argstrings ; + std::vector<std::string> argfiles ; + for ( int i = 1 ; i < argc ; i++ ) { + std::string argument( argv[ i ] ) ; + std::ifstream infile ( argument , std::ios::in ) ; + if ( infile ) { + argfiles.push_back( argument ) ; + infile.close( ) ; + } + else { + argstrings.push_back( argument ) ; + } + } + if ( ! argstrings.empty( ) ) { + std::cout << "Strings in the argument list:\n" ; + printStrings( argstrings ) ; + } + if ( ! argfiles.empty( ) ) { + for ( auto & file : argfiles ) { + std::ifstream inputfile( file , std::ios::in ) ; + std::vector<std::string> filelines ; + std::string line ; + std::cout << "Content of file " << file << " by frequency:\n" ; + while ( inputfile ) { + std::getline( inputfile , line ) ; + filelines.push_back( line ) ; + } + inputfile.close( ) ; + printStrings( filelines ) ; + } + } + return 0 ; +} + diff --git a/challenge-032/ulrich-rieke/cpp/ch-2.cpp b/challenge-032/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..7d9043ca6c --- /dev/null +++ b/challenge-032/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,92 @@ +#include <iostream> +#include <string> +#include <vector> +#include <utility> +#include <algorithm> +#include <cmath> +#include <map> +#include <iterator> + +using e_type = std::pair<std::string, double> ; +using e_r_type = std::pair<std::string, int> ; + +std::map<std::string, double> enterData( ) { + std::map<std::string, double> theData ; + std::string item ; + std::cout << "Enter items( end to end) : " ; + std::cin >> item ; + while ( item != "end" ) { + double quantity = 0.0 ; + std::cout << "enter quantity : " ; + std::cin >> quantity ; + theData[ item ] = quantity ; + std::cout << "Enter items( end to end) : " ; + std::cin >> item ; + } + return theData ; +} + +std::vector<e_r_type> normalizeData( std::map<std::string, double> & dats ) { + std::map<std::string, double>::iterator result ; + result = std::max_element( dats.begin( ) , dats.end( ) , + []( const e_type & a, const e_type & b ) { return a.second < + b.second ; } ) ; + double maximum = result->second ; + //if the maximum item number is greater than 40, we want to scale the + //numbers in order to make rendering easier + double scalefactor = 0.0 ; + if ( maximum > 40.0 ) { + scalefactor = 40.0 / maximum ; + } + if ( scalefactor != 0.0 ) { + std::vector<e_r_type> allRounded ; + for ( auto & el : dats ) { + el.second *= scalefactor ; + } + } + std::vector<e_r_type> allRounded ; + for ( auto & el : dats ) { + allRounded.push_back( std::make_pair( el.first , + static_cast<int>( round ( el.second ) ) ) ) ; + } + return allRounded ; +} + +void generate_bar_graph( std::vector<e_r_type> & myData ) { + //in order to "pretty print" the items we want to find out the maximum + //length of an item + std::string answer ; + std::vector<e_r_type>::iterator result ; + result = std::max_element( myData.begin( ) , myData.end( ) , + []( const e_r_type & el1, const e_r_type & el2 ) { return + el1.first.length( ) < el2.first.length( ) ; } ) ; + int maxlen = (result->first).length( ) ; + std::cout << "maxlen is " << maxlen << '\n' ; + std::cout << "You can order the output by item or by quantity!\n" ; + std::cout << "Do you want to order by item ? (y)es or (n)o ?" ; + std::cin >> answer ; + if (answer.substr( 0, 1 ) == "n" ) {//we order by quantity and have to sort + std::sort( myData.begin( ) , myData.end( ) , []( e_r_type & el1 , + e_r_type & el2 ) { return el1.second > el1.second ; } ) ; + } + else {//we order by item + std::sort( myData.begin( ) , myData.end( ) ) ; + } + for ( const e_r_type & element : myData ) { + std::cout << element.first ; + std::cout.width( maxlen - element.first.length( ) + 1 ) ; + std::cout << "|" ; + for ( int i = 0 ; i < element.second ; i++ ) { + std::cout << '#' ; + } + std::cout << std::endl ; + } +} + +int main( ) { + std::map<std::string, double> theData {enterData( ) } ; + std::vector<e_r_type> rounded { normalizeData( theData ) } ; + generate_bar_graph( rounded ) ; + return 0 ; +} + |
