1
0
LearningCPP/main.cpp

86 lines
1.5 KiB
C++
Raw Normal View History

2022-10-14 09:27:47 +01:00
#include <iostream>
2022-10-28 10:11:47 +01:00
#include <list>
2022-11-04 11:58:21 +00:00
#include <algorithm>
#include <map>
2022-10-19 09:33:42 +01:00
#include <iomanip>
2022-10-14 09:27:47 +01:00
using string = std::string;
2022-11-04 12:22:52 +00:00
using ilist = std::list<int>;
2022-10-14 09:27:47 +01:00
2022-10-28 09:43:51 +01:00
//Place in header!
2022-10-19 09:33:42 +01:00
template <typename T>
void printm(T m){
2022-10-14 09:27:47 +01:00
std::cout << m;
}
2022-10-19 09:33:42 +01:00
template <typename T>
void printlm(T m){
2022-10-14 09:27:47 +01:00
printm(m);
printm("\n");
}
2022-10-28 09:43:51 +01:00
int prompti(string,bool*);
2022-11-04 12:22:52 +00:00
int prompti(string,bool*);
string prompts(string,bool*);
2022-11-04 12:22:52 +00:00
bool isneg(int);
2022-10-19 09:33:42 +01:00
int main() {
2022-11-04 12:22:52 +00:00
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);
2022-11-04 11:58:21 +00:00
}
2022-11-04 12:22:52 +00:00
} 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;}));
2022-10-19 09:33:42 +01:00
return 0;
2022-10-14 09:27:47 +01:00
}
2022-10-28 09:43:51 +01:00
int prompti(string message, bool *status) {
int tr {};
2022-10-19 09:33:42 +01:00
*status = false;
2022-10-14 09:27:47 +01:00
std::cout << message;
2022-10-19 09:33:42 +01:00
if (!(std::cin >> tr)){
2022-10-14 09:27:47 +01:00
std::cin.clear();
std::cin.ignore();
2022-10-19 09:33:42 +01:00
return 0;
2022-10-14 09:27:47 +01:00
}
2022-10-19 09:33:42 +01:00
*status = true;
2022-10-14 09:27:47 +01:00
return tr;
}
2022-10-19 09:33:42 +01:00
2022-10-28 10:11:47 +01:00
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;
2022-10-28 10:11:47 +01:00
}
2022-11-04 12:22:52 +00:00
bool isneg(int x) {
return x < 0;
2022-10-28 10:20:04 +01:00
}