Skip to content

Commit

Permalink
missing files added
Browse files Browse the repository at this point in the history
  • Loading branch information
ksiminski committed Apr 23, 2024
1 parent 3be3f52 commit 651de15
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
51 changes: 51 additions & 0 deletions source/auxiliary/to_string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@


#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

#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<double> & 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<int> & 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();
}
41 changes: 41 additions & 0 deletions source/auxiliary/to_string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/** @file */

#ifndef TO_STRING_H
#define TO_STRING_H

#include <string>
#include <vector>

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<int> & 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<double> & numbers, const char sep = '-');
}


#endif

0 comments on commit 651de15

Please sign in to comment.