Negative int counter
This commit is contained in:
parent
df047b3197
commit
413ca3faef
52
main.cpp
52
main.cpp
@ -5,8 +5,7 @@
|
||||
#include <iomanip>
|
||||
|
||||
using string = std::string;
|
||||
using dlist = std::list<double>;
|
||||
using wordstat = std::map<string,dlist*>;
|
||||
using ilist = std::list<int>;
|
||||
|
||||
//Place in header!
|
||||
template <typename T>
|
||||
@ -21,27 +20,24 @@ void printlm(T m){
|
||||
}
|
||||
|
||||
int prompti(string,bool*);
|
||||
double promptd(string,bool*);
|
||||
int prompti(string,bool*);
|
||||
string prompts(string,bool*);
|
||||
double avg(std::list<double>*);
|
||||
double med(std::list<double>*);
|
||||
bool isneg(int);
|
||||
|
||||
int main() {
|
||||
printlm("Get stats of inputs:");
|
||||
bool s1 {};
|
||||
bool s2 {};
|
||||
wordstat stat {};
|
||||
do {,
|
||||
auto v1 = prompts("Enter a word: ", &s1);
|
||||
auto v2 = promptd("Enter a double: ", &s2);
|
||||
if (s1 && s2) {
|
||||
if (stat[v1] == null) {
|
||||
stat[v1] = new dlist {};
|
||||
}
|
||||
stat[v1].push_back(v2);
|
||||
printlm("Get the number of negatives from::");
|
||||
bool s {};
|
||||
ilist lst {};
|
||||
do {
|
||||
auto v = prompti("Enter an integer: ", &s);
|
||||
if (s) {
|
||||
lst.push_back(v);
|
||||
}
|
||||
} while (s1 && s2);
|
||||
|
||||
} while (s);
|
||||
printm("Number of negatives (Defined): ");
|
||||
printlm(count_if(lst.cbegin(), lst.cend(), isneg));
|
||||
printm("Number of negatives (Lambda): ");
|
||||
printlm(count_if(lst.cbegin(), lst.cend(), [] (int x) { return x < 0;}));
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -84,20 +80,6 @@ string prompts(string message, bool *status) {
|
||||
return tr;
|
||||
}
|
||||
|
||||
double avg(std::list<double> *values) {
|
||||
if (values->size() < 1) return 0.0;
|
||||
double sum {};
|
||||
for (const auto c : *values) sum += c;
|
||||
return sum / values->size();
|
||||
}
|
||||
|
||||
double med(std::list<double> *values) {
|
||||
if (values->size() < 1) return 0.0;
|
||||
sort(values->begin(), values->end());
|
||||
auto hw {values->size()/2};
|
||||
if (values->size()%2 == 1) {
|
||||
return values->at(hw);
|
||||
} else {
|
||||
return (values->at(hw - 1) + values->at(hw)) / 2.0;
|
||||
}
|
||||
bool isneg(int x) {
|
||||
return x < 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user