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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
#include <iostream>
#include <string>
#include <map>
#include <cctype>
#include <iomanip>
#include <fstream>
using namespace std;
// merge information for common items
// return the logical size of the array after merging
// size_t: http://en.cppreference.com/w/cpp/types/size_t
size_t merge( string items[], unsigned int quantities[], size_t sz )
{
// define a map which maps unique items to merged quantities
// https://www.cprogramming.com/tutorial/stl/stlmap.html
// http://en.cppreference.com/w/cpp/container/map
map< string, unsigned int > iems_to_quantities_map ;
// for each pair item - quantity in the original arrays
for( size_t i = 0 ; i < sz ; ++i )
{
// add the quantity to the entry in the map if the item is present
// otherwise create a new entry with quantity as zero, and then add this quantity
iems_to_quantities_map[ items[i] ] += quantities[i] ;
}
// copy the merged entries back into the two arrays
size_t pos = 0 ; // starting at position zero
// range-based loop: http://www.stroustrup.com/C++11FAQ.html#for
for( const auto& pair : iems_to_quantities_map ) // for each item-quantiy pair in the map
{
items[pos] = pair.first ; // unique key: item
quantities[pos] = pair.second ; // mapped value: merged quantity
++pos;
}
return pos ; // size after merging
}
size_t merge2( string items[], unsigned int quantities[], size_t sz )
{
// define a map which maps unique items to merged quantities
// https://www.cprogramming.com/tutorial/stl/stlmap.html
// http://en.cppreference.com/w/cpp/container/map
map< string, unsigned int > iems_to_quantities_map ;
// for each pair item - quantity in the original arrays
for( size_t i = 0 ; i < sz ; ++i )
{
// add the quantity to the entry in the map if the item is present
// otherwise create a new entry with quantity as zero, and then add this quantity
iems_to_quantities_map[ items[i] ] += quantities[i] ;
}
// copy the merged entries back into the two arrays
size_t pos = 0 ; // starting at position zero
// range-based loop: http://www.stroustrup.com/C++11FAQ.html#for
for( const auto& pair : iems_to_quantities_map ) // for each item-quantiy pair in the map
{
items[pos] = pair.first ; // unique key: item
quantities[pos] = pair.second ; // mapped value: merged quantity
++pos;
}
return pos ; // size after merging
}
// get item, quantity from user input
// return false if stop was entered to signal end of input
bool get_item( string& item, unsigned int& qty, int slno )
{
cout << "(List 1) Enter item #" << slno << " : ";
getline( cin, item ) ;
if( item == "STOP" || item == "stop" ) return false ;
cout << "Enter Quantity: " ;
cin >> qty ;
cin.ignore( 1000, '\n' ) ;
return true ;
}
bool get_item2( string& item, unsigned int& qty, int slno2)
{
cout << endl << "(List 2) Enter item #" << (slno2+1) << " : ";
getline( cin, item ) ;
if( item == "STOP" || item == "stop" ) return false ;
cout << "Enter Quantity: " ;
cin >> qty ;
cin.ignore( 1000, '\n' ) ;
cout << endl;
return true ;
}
int main()
{
ofstream myfile;
myfile.open("shoppingList.txt");
unsigned int count = 0;
const size_t array_Size = 256 ;
unsigned int quantities[array_Size];
string items[array_Size];
// for each item, quantity read, increment the count of items
while( count < array_Size && get_item( items[count], quantities[count], count+1 ) ) ++count ;
cout << "before merge shopping List:\n------------\n" << endl;
for( size_t i = 0 ; i < count ; ++i ) cout << items[i] << " : " << quantities[i] << '\n';
myfile << "\n-------------";
cout << "\n-------------";
myfile << "\nafter merge Shopping List 1:\n-------------\n" << endl;
cout << "\nafter merge Shopping List 1:\n-------------\n" << endl;
// auto: http://www.stroustrup.com/C++11FAQ.html#auto
// *** count is the logical size of the array before merge
const auto new_sz = merge2( items, quantities, count ); // ****
// *** and new_size is the logical size of the array after merge
// start with position 0, upto position new_sz-1
for( size_t i = 0 ; i < new_sz; ++i ) {
myfile << items[i] << " : " << quantities[i] << '\n' ;
cout << items[i] << " : " << quantities[i] << '\n' ;
}
myfile << endl << "-------------";
cout << endl << "-------------";
//FOR SECOND LIST
// for each item, quantity read, increment the count of items
while( count < array_Size && get_item2( items[count], quantities[count], count+1 ) ) ++count ;
cout << endl <<"before merge shopping List 1 and 2:\n------------\n" << endl;
for( size_t k = 0 ; k < count ; ++k ) cout << items[k] << " : " << quantities[k] << '\n' ;
myfile << "\nafter merge Shopping List 1 and 2:\n------------\n" << endl;
cout << "\nafter merge Shopping List 1 and 2:\n------------\n" << endl;
// auto: http://www.stroustrup.com/C++11FAQ.html#auto
// *** count is the logical size of the array before merge
const auto new_sz2 = merge( items, quantities, count ); // ****
// *** and new_size is the logical size of the array after merge
// start with position 0, upto position new_sz-1
for( size_t k = 0 ; k < new_sz; ++k ) {
myfile << items[k] << " : " << quantities[k] << '\n' ;
cout << items[k] << " : " << quantities[k] << '\n' ;
}
// system("pause");
return 0;
}
|