|
| 1 | +/************************************************************************************************** |
| 2 | + * Copyright (c) 2023-2024 NWSOFT * |
| 3 | + * * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy * |
| 5 | + * of this software and associated documentation files (the "Software"), to deal * |
| 6 | + * in the Software without restriction, including without limitation the rights * |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * |
| 8 | + * copies of the Software, and to permit persons to whom the Software is * |
| 9 | + * furnished to do so, subject to the following conditions: * |
| 10 | + * * |
| 11 | + * The above copyright notice and this permission notice shall be included in all * |
| 12 | + * copies or substantial portions of the Software. * |
| 13 | + * * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * |
| 20 | + * SOFTWARE. * |
| 21 | + **************************************************************************************************/ |
| 22 | + |
| 23 | +/** |
| 24 | + * @file add.cpp |
| 25 | + * @brief This file contains the implementation of the add function, which add two number strings together. |
| 26 | + * |
| 27 | + * @author Andy Zhang |
| 28 | + * @date 9th October 2023 |
| 29 | + */ |
| 30 | + |
| 31 | +#include "argParse.hpp" |
| 32 | +#include "factorialReport.hpp" |
| 33 | +#include "fn/basicArithm.hpp" |
| 34 | +#include "output.hpp" |
| 35 | +#include "util.hpp" |
| 36 | + |
| 37 | +#include <algorithm> |
| 38 | +#include <iostream> |
| 39 | +#include <string_view> |
| 40 | +#include <vector> |
| 41 | + |
| 42 | +using namespace steppable::__internals::numUtils; |
| 43 | +using namespace steppable::__internals::utils; |
| 44 | +using namespace steppable::__internals::arithmetic; |
| 45 | +using namespace steppable::__internals::symbols; |
| 46 | +using namespace steppable::output; |
| 47 | +using namespace std::literals; |
| 48 | + |
| 49 | +namespace steppable::__internals::arithmetic |
| 50 | +{ |
| 51 | + std::string factorial(const std::string& _number, const int steps) |
| 52 | + { |
| 53 | + auto number = standardizeNumber(_number); |
| 54 | + if (not isInteger(number)) |
| 55 | + { |
| 56 | + // We cannot evaluate integrals yet, so this has to be returned. |
| 57 | + // Correct implementation of the gamma function should be |
| 58 | + // / +inf |
| 59 | + // | z-1 -s |
| 60 | + // G(z) = | s e ds ,where z is a non-zero decimal ............ (1) |
| 61 | + // | |
| 62 | + // / 0 |
| 63 | + // and factorial is defined as |
| 64 | + // n! = n * G(n) ,where n is a non-zero decimal ............ (2) |
| 65 | + error("factorial"s, "%s is not an integer."s, number.c_str()); |
| 66 | + return "0"; |
| 67 | + } |
| 68 | + if (isZeroString(number)) |
| 69 | + { |
| 70 | + if (steps == 2) |
| 71 | + return "By definition, 0! = 1"; |
| 72 | + return "1"; // By definition, 0! = 1 |
| 73 | + } |
| 74 | + // Negative numbers do not have a factorial. |
| 75 | + if (number.front() == '-') |
| 76 | + { |
| 77 | + error("factorial"s, "%s is negative."s, number.c_str()); |
| 78 | + return "0"; |
| 79 | + } |
| 80 | + |
| 81 | + // Calculate the factorial of the number |
| 82 | + std::string result = "1"; |
| 83 | + loop(_number, [&](const std::string& i) { result = multiply(result, add(i, "1", 0), 0); }); |
| 84 | + |
| 85 | + return reportFactorial(_number, result, steps); |
| 86 | + } |
| 87 | +} // namespace steppable::__internals::arithmetic |
| 88 | + |
| 89 | +#ifndef NO_MAIN |
| 90 | +int main(const int _argc, const char* _argv[]) |
| 91 | +{ |
| 92 | + Utf8CodePage _; |
| 93 | + ProgramArgs program(_argc, _argv); |
| 94 | + program.addPosArg('a', "Number"); |
| 95 | + program.addKeywordArg("steps", 2, "Amount of steps while calculating the factorial. 0 = No steps, 2 = All steps."); |
| 96 | + program.addSwitch("profile", false, "profiling the program"); |
| 97 | + program.parseArgs(); |
| 98 | + |
| 99 | + const int steps = program.getKeywordArgument("steps"); |
| 100 | + const bool profile = program.getSwitch("profile"); |
| 101 | + const auto& number = static_cast<std::string>(program.getPosArg(0)); |
| 102 | + |
| 103 | + if (profile) |
| 104 | + { |
| 105 | + TIC(Factorial) |
| 106 | + std::cout << "Factorial :\n" << factorial(number, steps) << '\n'; |
| 107 | + TOC() |
| 108 | + } |
| 109 | + else |
| 110 | + std::cout << factorial(number, steps) << '\n'; |
| 111 | +} |
| 112 | +#endif |
0 commit comments