-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroceryTracker.cpp
More file actions
45 lines (39 loc) · 1.31 KB
/
GroceryTracker.cpp
File metadata and controls
45 lines (39 loc) · 1.31 KB
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
#include "GroceryTracker.h"
#include <iostream>
#include <fstream>
#include <algorithm> // for transform
#include <cctype> // for tolower
// Converts a string to lowercase.
std::string toLowerCase(const std::string& str) {
std::string lowerStr = str;
std::transform(lowerStr.begin(), lowerStr.end(), lowerStr.begin(),
[](unsigned char c) { return std::tolower(c); });
return lowerStr;
}
void GroceryTracker::addItem(const std::string& item) {
itemFrequency[toLowerCase(item)]++;
}
int GroceryTracker::getItemFrequency(const std::string& item) {
return itemFrequency[toLowerCase(item)];
}
void GroceryTracker::printAllFrequencies() const {
for (const auto& pair : itemFrequency) {
std::cout << pair.first << " " << pair.second << std::endl;
}
}
void GroceryTracker::printHistogram() const {
for (const auto& pair : itemFrequency) {
std::cout << pair.first << " ";
for (int i = 0; i < pair.second; ++i) {
std::cout << "*";
}
std::cout << std::endl;
}
}
void GroceryTracker::saveToFile(const std::string& filename) const {
std::ofstream outfile(filename);
for (const auto& pair : itemFrequency) {
outfile << pair.first << " " << pair.second << std::endl;
}
outfile.close();
}