Mar 1, 2019 at 3:17pm UTC
thank you and very educationaly for me!
Mar 4, 2019 at 9:18pm UTC
on the right way?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
{
monthly_sales_map monthly_sales ;
for ( auto & elem : daily_sales )
monthly_sales[ elem.date/100 ] += elem.daily_sales_amount ;
return monthly_sales ;
}
int main()
{
std::vector<adb> sadb ( 5 );
sadb.at ( 0 ).date = 20181201;
sadb.at ( 0 ).daily_sales_amount = 321.00;
………..
monthly_sales_map erg_map;
erg_map = consolidate ( const std::vector<adb>& sadb )
}
Last edited on Mar 4, 2019 at 9:19pm UTC
Mar 6, 2019 at 8:06pm UTC
thank you for your help - I learned a lot!!!! Now I have changed struct to class as data source.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
#include <vector>
#include <map>
#include <iostream>
#include <sstream>
class adb
{
private :
long date {}; // Date invoice
double daily_sales_amount=0;
public :
void set_date ( std::string date );
void set_daily_sales_amount ( std::string daily_sales_amount );
long get_date () const ;
double get_daily_sales_amount () const ;
double f_stod ( std::string betrag );
};
void adb::set_date ( std::string date )
{
this ->date = std::stol ( date );
}
long adb::get_date() const
{
return date;
}
void adb::set_daily_sales_amount ( std::string daily_sales_amount )
{
this ->daily_sales_amount = adb::f_stod ( daily_sales_amount );
}
double adb::get_daily_sales_amount() const
{
return daily_sales_amount;
}
using monthly_sales_map = std::map<long ,double > ;
monthly_sales_map consolidate ( const std::vector<adb>& daily_sales )
{
monthly_sales_map monthly_sales ;
for ( const auto & elem : daily_sales )
monthly_sales[ elem.get_date()/100 ] += elem.get_daily_sales_amount() ;
return monthly_sales ;
}
int main()
{
// initialize vector
std::vector<adb> sadb ( 4 );
sadb[0].set_date ( "20181201" );
sadb[0].set_daily_sales_amount ( "400.00" );
sadb[1].set_date ( "20181225" );
sadb[1].set_daily_sales_amount ( "200.22" );
sadb[2].set_date ( "20190125" );
sadb[2].set_daily_sales_amount ( "100.11" );
sadb[3].set_date ( "20190131" );
sadb[3].set_daily_sales_amount ( "99.89" );
// call the function, store its result in erg_map
const monthly_sales_map erg_map = consolidate ( sadb ) ;
for ( const auto & kv_pair : erg_map )
std::cout << kv_pair.first << ' ' << kv_pair.second << '\n' ;
}
double adb::f_stod ( std::string amount ) //Change amount from string to double
{
std::vector<char > erg;
erg.reserve ( 10 );
int i {0};
do
{
if ( amount[i] == ',' )
{
erg.push_back ( '.' );
}
else
{
erg.push_back ( amount[i] );
}
i++;
}
while ( amount[i] != ' ' );
double derg = 0;
std::string serg ( erg.begin(), erg.end() );
std::istringstream stream;
stream.str ( serg );
stream >> derg;
//std::cout << "derg:" << derg << std::endl;
return derg;
}
Last edited on Mar 6, 2019 at 8:11pm UTC