1
0
LearningCPP/main.cpp

102 lines
1.8 KiB
C++

#include <iostream>
#include <list>
#include <vector>
#include <map>
#include <iomanip>
using string = std::string;
using wordcount = std::map<string,int>;
//Place in header!
template <typename T>
void printm(T m){
std::cout << m;
}
template <typename T>
void printlm(T m){
printm(m);
printm("\n");
}
int prompti(string,bool*);
double promptd(string,bool*);
string prompts(string,bool*);
double avg(std::list<double>*);
void countw(std::list<string>&, wordcount*);
void histogram(int);
int main() {
printlm("Get histogram of input:");
bool s {};
std::list<string> lst {};
do {
auto v = prompts("", &s);
if (s) lst.push_back(v);
} while (s);
printlm("Histogram:");
wordcount wc {};
countw(lst,&wc);
for (const auto c : wc) {
printm(c.first);
printm(" ");
histogram(c.second);
}
return 0;
}
int prompti(string message, bool *status) {
int tr {};
*status = false;
std::cout << message;
if (!(std::cin >> tr)){
std::cin.clear();
std::cin.ignore();
return 0;
}
*status = true;
return tr;
}
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;
}
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;
}
double avg(std::list<double> *values) {
double sum {};
for (const auto c : *values) sum += c;
return sum / values->size();
}
void countw(std::list<string> &values, wordcount *holder) {
for (const auto &c : values) {
++(*holder)[c];
}
}
void histogram(int num) {
for (auto i = 1; i <= num; ++i) if (i == num) printlm("*"); else printm("*");
}