1
0

remove duplicates of input string words

This commit is contained in:
Captain ALM 2022-10-28 12:18:30 +01:00
parent 22e18284ff
commit ccee442f97
3 changed files with 31 additions and 18 deletions

View File

@ -1,6 +1,7 @@
#include <iostream> #include <iostream>
#include <list> #include <list>
#include <vector> #include <vector>
#include <map>
#include <iomanip> #include <iomanip>
using string = std::string; using string = std::string;
@ -19,21 +20,22 @@ void printlm(T m){
int prompti(string,bool*); int prompti(string,bool*);
double promptd(string,bool*); double promptd(string,bool*);
double avgitr(std::list<double>*); string prompts(string,bool*);
double avgfor(std::list<double>*); double avg(std::list<double>*);
void delzrfrst(std::list<double>&); void remdups(std::list<string>&);
int main() { int main() {
printlm("Remove first zero from:"); printlm("Remove duplicates for input:");
bool s {}; bool s {};
std::list<double> lst {}; std::list<string> lst {};
do { do {
auto v = promptd("Enter a double value: ", &s); auto v = prompts("", &s);
if (s) lst.push_back(v); if (s) lst.push_back(v);
} while (s); } while (s);
printlm("First zero removed:"); printlm("Current data:");
delzrfrst(lst); for (const auto c : lst) printlm(c);
printlm("With Duplicates removed:");
remdups(lst);
for (const auto c : lst) printlm(c); for (const auto c : lst) printlm(c);
return 0; return 0;
} }
@ -64,23 +66,34 @@ double promptd(string message, bool *status) {
return tr; return tr;
} }
double avgitr(std::list<double> *values) { string prompts(string message, bool *status) {
double sum {}; string tr {""};
for (auto itr = values->cbegin(); itr != values->cend(); ++itr) sum += *itr; *status = false;
return sum / values->size(); std::cout << message;
if (!(std::cin >> tr)){
std::cin.clear();
std::cin.ignore();
return "";
}
*status = true;
return tr;
} }
double avgfor(std::list<double> *values) { double avg(std::list<double> *values) {
double sum {}; double sum {};
for (const auto c : *values) sum += c; for (const auto c : *values) sum += c;
return sum / values->size(); return sum / values->size();
} }
void delzrfrst(std::list<double> &values) { void remdups(std::list<string> &values) {
for (auto itr {values.cbegin()}; itr != values.cend(); ++itr) { std::map<string,bool> has {};
if (*itr == 0) { auto itr {values.cbegin()};
while (itr != values.cend()) {
if (has[*itr]) {
itr = values.erase(itr); itr = values.erase(itr);
break; } else {
has[*itr] = true;
++itr;
} }
} }
} }

BIN
main.o

Binary file not shown.

BIN
t

Binary file not shown.