~aleteoryx/muditaos

ref: 0e1880a726f97def0117b53a4008fdbcaa6a7a34 muditaos/module-apps/application-calculator/data/CalculatorUtility.hpp -rw-r--r-- 1.4 KiB
0e1880a7 — Jakub Pyszczak [EGD-5624] Added window update 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once
#include <string>

namespace CalculatorConstants
{
    inline constexpr auto veryLowPrecision = 4;
    inline constexpr auto lowPrecision     = 5;
    inline constexpr auto precision        = 6;
    inline constexpr auto highPrecision    = 8;
    inline constexpr auto expLength        = 1;
    inline constexpr auto minusExpLength   = 2;
    inline constexpr auto maxStringLength  = 7;
} // namespace CalculatorConstants

struct Result
{
    std::string equation;
    std::string value;
    bool isError;
};

class Calculator
{
    /// @param from: string which is going to be replaced
    /// @param to: string which is going to replace the old one
    /// @return: new string with replaced all occurrences of given string to the new one in whole input
    std::string replaceAllOccurrences(std::string input, const std::string &from, const std::string &to);
    std::string prepareEquationForParser(std::string input);
    std::string getValueThatFitsOnScreen(double result);
    long double getCoefficient(double result, uint32_t precision);
    std::string convertToNumberWithPositiveExponent(double result, uint32_t exponent);
    std::string convertToNumberWithNegativeExponent(double result, long long base);

  public:
    Result calculate(std::string source);
};