remove duplicates of input string words
This commit is contained in:
parent
22e18284ff
commit
ccee442f97
49
main.cpp
49
main.cpp
@ -1,6 +1,7 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <iomanip>
|
||||
|
||||
using string = std::string;
|
||||
@ -19,21 +20,22 @@ void printlm(T m){
|
||||
|
||||
int prompti(string,bool*);
|
||||
double promptd(string,bool*);
|
||||
double avgitr(std::list<double>*);
|
||||
double avgfor(std::list<double>*);
|
||||
void delzrfrst(std::list<double>&);
|
||||
|
||||
string prompts(string,bool*);
|
||||
double avg(std::list<double>*);
|
||||
void remdups(std::list<string>&);
|
||||
|
||||
int main() {
|
||||
printlm("Remove first zero from:");
|
||||
printlm("Remove duplicates for input:");
|
||||
bool s {};
|
||||
std::list<double> lst {};
|
||||
std::list<string> lst {};
|
||||
do {
|
||||
auto v = promptd("Enter a double value: ", &s);
|
||||
auto v = prompts("", &s);
|
||||
if (s) lst.push_back(v);
|
||||
} while (s);
|
||||
printlm("First zero removed:");
|
||||
delzrfrst(lst);
|
||||
printlm("Current data:");
|
||||
for (const auto c : lst) printlm(c);
|
||||
printlm("With Duplicates removed:");
|
||||
remdups(lst);
|
||||
for (const auto c : lst) printlm(c);
|
||||
return 0;
|
||||
}
|
||||
@ -64,23 +66,34 @@ double promptd(string message, bool *status) {
|
||||
return tr;
|
||||
}
|
||||
|
||||
double avgitr(std::list<double> *values) {
|
||||
double sum {};
|
||||
for (auto itr = values->cbegin(); itr != values->cend(); ++itr) sum += *itr;
|
||||
return sum / values->size();
|
||||
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 avgfor(std::list<double> *values) {
|
||||
double avg(std::list<double> *values) {
|
||||
double sum {};
|
||||
for (const auto c : *values) sum += c;
|
||||
return sum / values->size();
|
||||
}
|
||||
|
||||
void delzrfrst(std::list<double> &values) {
|
||||
for (auto itr {values.cbegin()}; itr != values.cend(); ++itr) {
|
||||
if (*itr == 0) {
|
||||
void remdups(std::list<string> &values) {
|
||||
std::map<string,bool> has {};
|
||||
auto itr {values.cbegin()};
|
||||
while (itr != values.cend()) {
|
||||
if (has[*itr]) {
|
||||
itr = values.erase(itr);
|
||||
break;
|
||||
} else {
|
||||
has[*itr] = true;
|
||||
++itr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user