2022-10-14 09:27:47 +01:00
|
|
|
#include <iostream>
|
2022-10-28 10:11:47 +01:00
|
|
|
#include <list>
|
2022-10-28 10:30:27 +01:00
|
|
|
#include <vector>
|
2022-10-28 12:18:30 +01:00
|
|
|
#include <map>
|
2022-10-19 09:33:42 +01:00
|
|
|
#include <iomanip>
|
2022-10-14 09:27:47 +01:00
|
|
|
|
|
|
|
using string = std::string;
|
2022-10-28 12:31:46 +01:00
|
|
|
using wordcount = std::map<string,int>;
|
2022-10-14 09:27:47 +01:00
|
|
|
|
2022-10-28 09:43:51 +01:00
|
|
|
//Place in header!
|
2022-10-19 09:33:42 +01:00
|
|
|
template <typename T>
|
|
|
|
void printm(T m){
|
2022-10-14 09:27:47 +01:00
|
|
|
std::cout << m;
|
|
|
|
}
|
|
|
|
|
2022-10-19 09:33:42 +01:00
|
|
|
template <typename T>
|
|
|
|
void printlm(T m){
|
2022-10-14 09:27:47 +01:00
|
|
|
printm(m);
|
|
|
|
printm("\n");
|
|
|
|
}
|
|
|
|
|
2022-10-28 09:43:51 +01:00
|
|
|
int prompti(string,bool*);
|
2022-10-28 10:11:47 +01:00
|
|
|
double promptd(string,bool*);
|
2022-10-28 12:18:30 +01:00
|
|
|
string prompts(string,bool*);
|
|
|
|
double avg(std::list<double>*);
|
2022-10-28 12:31:46 +01:00
|
|
|
void countw(std::list<string>&, wordcount*);
|
2022-10-28 12:41:38 +01:00
|
|
|
void histogram(wordcount&);
|
|
|
|
void spacep(int);
|
|
|
|
void histogramp(int);
|
2022-10-19 09:33:42 +01:00
|
|
|
|
|
|
|
int main() {
|
2022-10-28 12:31:46 +01:00
|
|
|
printlm("Get histogram of input:");
|
2022-10-19 09:33:42 +01:00
|
|
|
bool s {};
|
2022-10-28 12:18:30 +01:00
|
|
|
std::list<string> lst {};
|
2022-10-19 09:33:42 +01:00
|
|
|
do {
|
2022-10-28 12:18:30 +01:00
|
|
|
auto v = prompts("", &s);
|
2022-10-28 10:11:47 +01:00
|
|
|
if (s) lst.push_back(v);
|
|
|
|
} while (s);
|
2022-10-28 12:31:46 +01:00
|
|
|
printlm("Histogram:");
|
|
|
|
wordcount wc {};
|
|
|
|
countw(lst,&wc);
|
2022-10-28 12:41:38 +01:00
|
|
|
histogram(wc);
|
2022-10-19 09:33:42 +01:00
|
|
|
return 0;
|
2022-10-14 09:27:47 +01:00
|
|
|
}
|
|
|
|
|
2022-10-28 09:43:51 +01:00
|
|
|
int prompti(string message, bool *status) {
|
|
|
|
int tr {};
|
2022-10-19 09:33:42 +01:00
|
|
|
*status = false;
|
2022-10-14 09:27:47 +01:00
|
|
|
std::cout << message;
|
2022-10-19 09:33:42 +01:00
|
|
|
if (!(std::cin >> tr)){
|
2022-10-14 09:27:47 +01:00
|
|
|
std::cin.clear();
|
|
|
|
std::cin.ignore();
|
2022-10-19 09:33:42 +01:00
|
|
|
return 0;
|
2022-10-14 09:27:47 +01:00
|
|
|
}
|
2022-10-19 09:33:42 +01:00
|
|
|
*status = true;
|
2022-10-14 09:27:47 +01:00
|
|
|
return tr;
|
|
|
|
}
|
2022-10-19 09:33:42 +01:00
|
|
|
|
2022-10-28 10:11:47 +01:00
|
|
|
double promptd(string message, bool *status) {
|
|
|
|
double tr {};
|
|
|
|
*status = false;
|
|
|
|
std::cout << message;
|
|
|
|
if (!(std::cin >> tr)){
|
|
|
|
std::cin.clear();
|
|
|
|
std::cin.ignore();
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
*status = true;
|
|
|
|
return tr;
|
|
|
|
}
|
|
|
|
|
2022-10-28 12:18:30 +01:00
|
|
|
string prompts(string message, bool *status) {
|
|
|
|
string tr {""};
|
|
|
|
*status = false;
|
|
|
|
std::cout << message;
|
|
|
|
if (!(std::cin >> tr)){
|
|
|
|
std::cin.clear();
|
|
|
|
std::cin.ignore();
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
*status = true;
|
|
|
|
return tr;
|
2022-10-28 10:11:47 +01:00
|
|
|
}
|
|
|
|
|
2022-10-28 12:18:30 +01:00
|
|
|
double avg(std::list<double> *values) {
|
2022-10-28 10:11:47 +01:00
|
|
|
double sum {};
|
2022-10-28 10:20:04 +01:00
|
|
|
for (const auto c : *values) sum += c;
|
2022-10-28 10:11:47 +01:00
|
|
|
return sum / values->size();
|
|
|
|
}
|
2022-10-28 10:20:04 +01:00
|
|
|
|
2022-10-28 12:31:46 +01:00
|
|
|
void countw(std::list<string> &values, wordcount *holder) {
|
|
|
|
for (const auto &c : values) {
|
|
|
|
++(*holder)[c];
|
2022-10-28 10:37:23 +01:00
|
|
|
}
|
2022-10-28 10:20:04 +01:00
|
|
|
}
|
2022-10-28 12:31:46 +01:00
|
|
|
|
2022-10-28 12:41:38 +01:00
|
|
|
void histogram(wordcount &wc) {
|
|
|
|
int maxl {};
|
|
|
|
for (const auto c : wc) if (c.first.length() > maxl) maxl = c.first.length();
|
|
|
|
for (const auto c : wc) {
|
|
|
|
printm(c.first);
|
|
|
|
spacep(1 + maxl - c.first.length());
|
|
|
|
histogramp(c.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void spacep(int num) {
|
|
|
|
for (auto i = 0; i < num; ++i) printm(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
void histogramp(int num) {
|
2022-10-28 12:31:46 +01:00
|
|
|
for (auto i = 1; i <= num; ++i) if (i == num) printlm("*"); else printm("*");
|
|
|
|
}
|