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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
// ifstream input( "data.txt" );
istringstream input(
"990112678909876001235486;register order;2014-03-26 10:32:56.000\n"
"990112678909876001235486;register order;2014-03-26 12:26:16.000\n"
"990112678909875001235486;register order;2014-03-26 10:25:56.000\n"
"990112678909876300123546;register order;2014-03-22 10:31:56.000\n"
"990112678909873001235436;register order;2014-01-26 11:42:56.000\n"
"990112678909872001235426;register order;2014-02-26 10:32:46.000\n"
"990112678909872002235216;check stock;2014-03-26 10:32:46.000\n"
"990112678909872002235216;ship order;2014-01-26 11:22:16.000\n"
"990112678909872002235216;handle payment;2014-03-26 10:32:46.000\n"
"990112678909871002435208;handle payment;2014-03-26 10:32:46.000\n"
"990112678909872002235216;ship order;2014-01-26 10:00:16.000\n"
"990112678909872002235216;ship order;2014-01-26 10:15:26.000\n"
"990112678909872002235216;ship order;2014-01-26 11:22:46.000\n" );
//======================================================================
string trim( const string &s, string junk = " " )
{
int i = s.find_first_not_of( junk );
if ( i == string::npos ) return "";
int j = s.find_last_not_of( junk );
return s.substr( i, j - i + 1 );
}
//======================================================================
struct Order
{
string order_number;
string activity;
string date;
friend istream & operator >> ( istream &in, Order & order )
{
if ( getline( in, order.order_number, ';' ) )
{
getline( in, order.activity, ';' );
getline( in, order.date , '\n' );
trim( order.order_number );
trim( order.activity );
trim( order.date );
}
return in;
}
};
//======================================================================
struct Event
{
int year, month, day, hour, minute;
double second;
Event( const string &A )
{
stringstream ss( A );
char c;
ss >> year >> c >> month >> c >> day >> hour >> c >> minute >> c >> second;
}
};
//-------------------------------------
bool operator < ( const Event &a, const Event &b )
{
if ( a.year != b.year ) return a.year < b.year ;
if ( a.month != b.month ) return a.month < b.month ;
if ( a.day != b.day ) return a.day < b.day ;
if ( a.hour != b.hour ) return a.hour < b.hour ;
if ( a.minute != b.minute ) return a.minute < b.minute;
return a.second < b.second;
}
//======================================================================
bool sortOrders( const Order &a, const Order &b )
{
if ( a.order_number != b.order_number ) return a.order_number < b.order_number;
return Event( a.date ) < Event( b.date );
}
//======================================================================
int main()
{
vector<Order> orders;
set< pair<string,string> > codes; // A set to keep track of duplicate order-number/activity pairs
for ( Order item; input >> item; ) // Read each line
{
if ( !codes.insert( { item.order_number, item.activity } ).second ) // If this pair exists ...
{
int pos = find_if( orders.begin(), orders.end(), // ... find its position ...
[&item]( const Order &e ){ return e.order_number == item.order_number
&& e.activity == item.activity ; } ) - orders.begin();
if ( Event( orders[pos].date ) < Event( item.date ) ) orders[pos] = item; // ... if newer, then replace
}
else
{
orders.push_back( item ); // otherwise, just add
}
}
sort( orders.begin(), orders.end(), sortOrders ); // Sort (order number, then date)
string previous;
for ( Order e : orders )
{
if ( previous != "" && e.order_number != previous ) cout << '\n';
cout << setw( 30 ) << e.order_number << ' '
<< setw( 20 ) << e.activity << ' '
<< setw( 30 ) << e.date << '\n';
previous = e.order_number;
}
}
|