diff --git a/source/auxiliary/to_string.cpp b/source/auxiliary/to_string.cpp new file mode 100644 index 0000000..e67a776 --- /dev/null +++ b/source/auxiliary/to_string.cpp @@ -0,0 +1,51 @@ + + +#include +#include +#include +#include +#include + +#include "../auxiliary/to_string.h" + +std::string ksi::to_string (const double x) +{ + std::stringstream ss; + ss << std::scientific << x; + return ss.str(); +} + +std::string ksi::to_string (const int n, const unsigned int width) +{ + std::stringstream ss; + ss << std::setw(width) << std::setfill('0') << n; + return ss.str(); +} + +std::string ksi::to_string (const std::vector & numbers, const char sep) +{ + std::stringstream ss; + auto size = numbers.size(); + for (std::size_t i = 0; i < size - 1; ++i) + { + ss << numbers[i] << sep; + } + if (size > 0) + ss << numbers.back(); + + return ss.str(); +} + +std::string ksi::to_string (const std::vector & numbers, const unsigned int width, const char sep) +{ + std::stringstream ss; + auto size = numbers.size(); + for (std::size_t i = 0; i < size - 1; ++i) + { + ss << std::setw(width) << std::setfill('0') << numbers[i] << sep; + } + if (size > 0) + ss << std::setw(width) << std::setfill('0') << numbers.back(); + + return ss.str(); +} diff --git a/source/auxiliary/to_string.h b/source/auxiliary/to_string.h new file mode 100644 index 0000000..3a893de --- /dev/null +++ b/source/auxiliary/to_string.h @@ -0,0 +1,41 @@ +/** @file */ + +#ifndef TO_STRING_H +#define TO_STRING_H + +#include +#include + +namespace ksi +{ + /* @return The function returns a string representation of a double value in the exponential form. + * @author Krzysztof Siminski + * @date 2023-10-09 */ + std::string to_string (const double x); + + /* @return The function returns a string representation of an integer value with leading zeroes. + * @author Krzysztof Siminski + * @param n number to make a string for + * @param width width of the window for an integer (filled with zeroes) + * @date 2024-03-27 */ + std::string to_string (const int n, const unsigned int width = 0); + + /* @return The function returns a string representation of a vector of ints with leading zeroes. + * @author Krzysztof Siminski + * @param numbers numbers to make a string for + * @param width width of the window for an integer (filled with zeroes) + * @param sep separator of numbers + * @date 2024-03-29 */ + std::string to_string (const std::vector & numbers, const unsigned int width = 0, const char sep = '-'); + + /* @return The function returns a string representation of a vector of ints with leading zeroes. + * @author Krzysztof Siminski + * @param numbers numbers to make a string for + * @param width width of the window for an integer (filled with zeroes) + * @param sep separator of numbers + * @date 2024-03-29 */ + std::string to_string (const std::vector & numbers, const char sep = '-'); +} + + +#endif