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 <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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user