Skip to content

Commit

Permalink
Allow decimal powers and improve rounding algorithm. (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZCG-coder committed May 11, 2024
2 parents e1ecbaf + 94cb882 commit b715de8
Show file tree
Hide file tree
Showing 24 changed files with 691 additions and 75 deletions.
13 changes: 9 additions & 4 deletions include/exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,31 @@
#pragma once

#include <exception>
#include <string>
#include <utility>

namespace steppable::exceptions
{
/**
* @class ZeroDenominatorException
* @brief Thrown when initializing a fraction with zero as denominator.
*/
class ZeroDenominatorException : public std::exception
class ZeroDenominatorException final : public std::exception
{
public:
const char* what() const throw() { return "The denominator is zero, which is not allowed."; }
[[nodiscard]] const char* what() const noexcept override
{
return "The denominator is zero, which is not allowed.";
}
};

/**
* @class MultiLengthLetterException
* @brief Thrown when initializing a Key object with two or more letters.
*/
class MultiLengthLetterException : public std::exception
class MultiLengthLetterException final : public std::exception
{
public:
const char* what() const throw() { return "The length of letter exceeds the 1 limit."; }
[[nodiscard]] const char* what() const noexcept override { return "The length of letter exceeds the 1 limit."; }
};
} // namespace steppable::exceptions
4 changes: 3 additions & 1 deletion include/fn/basicArithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,11 @@ namespace steppable::__internals::arithmetic
* @param _number The number to take root of.
* @param base The base of the root.
* @param decimals The decimals of the operation.
* @param steps The steps to show while taking the root.
*
* @return The result of the root operation.
*/
std::string root(const std::string& _number, const std::string& base, const size_t decimals = 8);
std::string root(const std::string& _number, const std::string& base, const size_t decimals = 8, const int steps = 2);

/**
* @brief Executes a given predicate function a specified number of times.
Expand Down
19 changes: 18 additions & 1 deletion include/fraction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "number.hpp"

#include <array>
#include <string>

namespace steppable
Expand All @@ -57,6 +58,13 @@ namespace steppable
*/
Fraction(const std::string& top = "1", const std::string& bottom = "1");

/**
* @brief Initialized a fraction from a number.
*
* @param number The number to convert to a fraction.
*/
Fraction(const Number& number);

/**
* @brief Initializes a fraction with no top component and bottom component specified.
* By default, this fraction equals to 1.
Expand All @@ -66,8 +74,17 @@ namespace steppable
/**
* @brief Returns the fraction as a string.
* The string is formatted as "top/bottom", and it will automatically simplify the fraction.
*
* @param inLine Whether to present the fraction in a single line.
* @return The fraction as a string.
*/
std::string present(const bool inLine = true);

/**
* @brief Returns the fraction as an array of its top and bottom components.
* @return The array of top and bottom components.
*/
std::string present();
std::array<std::string, 2> asArray() const;

/**
* @brief Adds two fractions together.
Expand Down
5 changes: 3 additions & 2 deletions include/rounding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ namespace steppable::__internals::numUtils
/**
* @brief Round off a number to the nearest integer.
*
* @param[in] number The number to round.
* @param[in] _number The number to round.
* @param[in] digits The number of decimal places to round to.
* @return The rounded number.
*/
std::string roundOff(const std::string& number);
std::string roundOff(const std::string& _number, const size_t digits = 0);

/**
* @brief Move the decimal places of a number.
Expand Down
184 changes: 182 additions & 2 deletions include/symbols.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,132 @@
#pragma once

#include <array>
#include <cstddef>
#include <string>
#include <vector>

#pragma once

#include <string>
#include <vector>

/**
* @namespace steppable::prettyPrint
* @brief The namespace containing utilities for pretty printing.
*/
namespace steppable::prettyPrint
{
/**
* @brief Represents a position in the console.
*/
struct Position
{
long long x = 0;
long long y = 0;
};

// // Not currently in use.
// enum PrintingAlignment
// {
// BASELINE = 0,
// MIDDLE = 1
// };

// struct PrintingObj
// {
// std::string str;
// PrintingAlignment alignment;
// };

/**
* @brief Represents a console output buffer.
*/
class ConsoleOutput
{
private:
/// @brief The current position.
Position curPos;

/// @brief The buffer object.
std::vector<std::vector<char>> buffer;

/// @brief The height of the buffer.
long long height = 10;

/// @brief The width of the buffer.
long long width = 10;

public:
/**
* @brief Creates a new console output buffer.
*
* @param height The height of the buffer.
* @param width The width of the buffer.
*/
ConsoleOutput(long long height, long long width);

/**
* @brief Writes a character to the buffer.
*
* @param c The character to write.
* @param dLine The change in line.
* @param dCol The change in column.
* @param updatePos Whether to update the current position.
*/
void write(const char c, const long long dLine, const long long dCol, bool updatePos = false);

/**
* @brief Writes a character to the buffer.
*
* @param c The character to write.
* @param pos The position to write to.
* @param updatePos Whether to update the current position.
*/
void write(const char c, const Position& pos, bool updatePos = false);

/**
* @brief Writes a string to the buffer.
*
* @param s The string to write.
* @param dLine The change in line.
* @param dCol The change in column.
* @param updatePos Whether to update the current position.
*/
void write(const std::string& s, const Position& pos, bool updatePos = false);

/**
* @brief Gets the buffer as a string.
* @return The buffer as a string.
*/
[[nodiscard]] std::string asString() const;
};

/**
* @brief Gets the minimal width needed to print a string.
*
* @param s The string.
* @return The width of the string.
*/
size_t getStringWidth(const std::string& s);

/**
* @brief Gets the minimal height needed to print a string.
*
* @param s The string.
* @return The height of the string.
*/
size_t getStringHeight(const std::string& s);
} // namespace steppable::prettyPrint

/**
* @namespace steppable::__internals::symbols
* @brief The namespace containing various unicode symbols.
*
* @deprecated This namespace is deprecated and will be removed in the future, as the unicode output is not flexible
* enough.
* @warning Usage of this namespace is strongly discouraged. Use the steppable::prettyPrint namespace for basic tools,
* and implement yours in steppable::prettyPrint::printers.
*/
namespace steppable::__internals::symbols
{
/// @brief The because symbol (3 dots in a triangle, Unicode U+2235)
Expand All @@ -47,8 +171,12 @@ namespace steppable::__internals::symbols
/// @brief The divide symbol (Unicode U+00F7)
#define DIVIDED_BY "\u00F7"

#define SURD "\u221A"
#define COMBINE_MACRON "\u0305"

/// @brief The large dot symbol (Unicode U+25C9)
#define LARGE_DOT "\u25C9"
#define ABOVE_DOT "\u02D9"

// Subscripts
/**
Expand All @@ -68,7 +196,7 @@ namespace steppable::__internals::symbols
#define SUB_MAGIC_NUMBER 8272

/// @brief A list of subscript characters.
extern const std::array<std::string_view, 10>& SUPERSCRIPTS;
extern const std::array<std::string, 10>& SUPERSCRIPTS;

/**
* @brief Create a subscript string from a normal string.
Expand Down Expand Up @@ -117,5 +245,57 @@ namespace steppable::__internals::symbols
* @param[in] normal The normal character.
* @return The superscript string.
*/
std::string_view makeSuperscript(char normal);
std::string makeSuperscript(char normal);

/**
* @brief Makes a surd expression from a radicand.
*
* @param radicand The radicand.
* @return The surd expression.
*/
std::string makeSurd(const std::string& radicand);
} // namespace steppable::__internals::symbols

/**
* @namespace steppable::prettyPrint::printers
* @brief The custom-implemented printer engines for outputting expressions.
*/
namespace steppable::prettyPrint::printers
{
/**
* @brief Pretty print a root expression.
*
* @param radicand The radicand.
* @param index The index.
* @return The pretty printed root expression.
*/
std::string ppRoot(const std::string& radicand, const std::string& index = "2");

/**
* @brief Pretty print a fraction.
*
* @param numerator The numerator.
* @param denominator The denominator.
* @param inLine Whether to print in a single line.
* @return The pretty printed fraction.
*/
std::string ppFraction(const std::string& numerator, const std::string& denominator, const bool inLine = false);

/**
* @brief Pretty print a base expression, (aka, subscript).
*
* @param base The base.
* @param subscript The subscript.
* @return The pretty printed base expression.
*/
std::string ppSubscript(const std::string& base, const std::string& subscript);

/**
* @brief Pretty print a power expression, (aka, superscript).
*
* @param base The base.
* @param exponent The exponent.
* @return The pretty printed power expression.
*/
std::string ppSuperscript(const std::string& base, const std::string& superscript);
} // namespace steppable::prettyPrint::printers
20 changes: 20 additions & 0 deletions include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
#include <array>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <locale.h>
#include <locale>
#include <sstream>
#include <string>
#include <vector>
Expand Down Expand Up @@ -72,6 +75,20 @@

namespace steppable::__internals::utils
{
#ifndef MS_STDLIB_BUGS
#if (_MSC_VER || __MINGW32__ || __MSVCRT__)
#define MS_STDLIB_BUGS 1
#else
#define MS_STDLIB_BUGS 0
#endif
#endif

#if MS_STDLIB_BUGS
#include <fcntl.h>
#include <io.h>
#endif

void initLocale();
#ifdef WINDOWS
#include <fcntl.h>
#include <windows.h>
Expand Down Expand Up @@ -134,6 +151,7 @@ namespace steppable::__internals::utils
*/
Utf8CodePage() : oldCodePage(GetConsoleOutputCP())
{
initLocale();
SetConsoleOutputCP(CP_UTF8);
dwModeOrig = enableVtMode();
}
Expand Down Expand Up @@ -164,6 +182,8 @@ namespace steppable::__internals::utils
*/
class Utf8CodePage
{
public:
Utf8CodePage() { initLocale(); }
};
#endif
} // namespace steppable::__internals::utils
Expand Down

0 comments on commit b715de8

Please sign in to comment.