44 lines
1.1 KiB
C++
44 lines
1.1 KiB
C++
#include <stdio.h>
|
|
#include <utility>
|
|
#include <numeric>
|
|
#include <string>
|
|
#include <iostream>
|
|
#include <vector>
|
|
|
|
int parse(const std::string& str) {
|
|
if (str.empty()) {
|
|
std::cout << "String is empty!";
|
|
return 0;
|
|
}
|
|
if (str.size() > 20) {
|
|
std::cout << "Value is to big to fit in integer!";
|
|
return 0;
|
|
}
|
|
|
|
return std::stoi(str);
|
|
}
|
|
|
|
std::string read(const std::vector<int>& vec) {
|
|
if (vec.empty()) {
|
|
std::cout << "Vector is empty, can't read";
|
|
return "";
|
|
}
|
|
if (vec.size() > 20) {
|
|
std::cout << "Vector is to big";
|
|
return "";
|
|
}
|
|
|
|
return std::accumulate(vec.begin(), vec.end(), std::string{}, [](const auto& str, int num){
|
|
if (str.empty()) {
|
|
return std::to_string(num);
|
|
}
|
|
return str + ", " + std::to_string(num);
|
|
});
|
|
}
|
|
|
|
int main() {
|
|
std::cout << "Parsed number: " << parse("-123") << '\n';
|
|
std::cout << "Read numbers: " << read({1,2,3-1,-2,-3}) << '\n';
|
|
std::cout << "Parsed number: " << parse("123456789012345678901234") << '\n';
|
|
std::cout << "Read numbers: " << read({}) << '\n';
|
|
}
|