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
|
#include <iostream>
#include <map>
#include <interator>
#include <algorithm>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
string toLower(string s)
{
string data = s;
std::transform(data.begin(), data.end(), data.begin(), ::toLower);
return data;
}
string trim(string str)
{
stringstream trimmer;
trimmer << str;
str.clear();
trimmer >>str;
return str;
}
int main()
{
map<string, int> item_count;
string line; // variable used to read a line from file
int lines =0; // variable containing the numbers of transactions
ifstream myfile("C:\Users\marqu_000\Documents\marketdata.txt");
if(myfile.is_open()) // checking if the file was open
{
getline(myfile, line); // to ignore the first line which contains number of transactions
while(getline(myfile, line)) // read the file line by linr until end of file is reached
{
//now we are processing each line
stringstream ss(line);
int i;
string item;
// ignore Transection ID, #of Items
getline(ss, item, ',');
getline(ss, item, ',');
while (getline(ss, item, ','))
{
item = trim(toLower(item));
// Now the item variable is containing the item name
map<string, int> ::interator itr = item _count.find(item);
if (itr == item_count.end())
{
//means the element is not present
item_count.insert(pair<string, int>(item, 1));
}
else
{
// increment the count
itr->second = 1 + itr -> second;
}
}
}
// now traverse in the array and print entries which have count 1
cout << "unique item: " << endl;
for( map<string, int>::const_iterator it = item_count.begin(); it ! = item_count.end(); ++it)
{
cout << it -> first << endl;
}
}
cout << endl << "Items frequencies: " << endl;
for(map<string, int>::const_iterator it = item_count.begin(); it ! = item_count.end(); ++it)
{
cout << it->first << ":" << it->second << endl;
}
myfile.close(); //closing the file
}
else
{
cout << "Unable to open input file." << endl;
return 1;
}
return 0;
}
|