#include #include #include #include using namespace std; static string trim(string const& s) { string ret = s; const bool negative = ret.front() == '-'; const auto pos = ret.find_first_not_of("-0") - size_t(negative); if (pos != std::string::npos) ret.erase(size_t(negative), pos); return ret; } static std::string subtract([[maybe_unused]] std::string_view str1, [[maybe_unused]] std::string_view str2) { return ""; } static std::string add(std::string_view str1, std::string_view str2) { // returns arithmetic addition of str1+str2 if (str1.size() == 0) return std::string{str2}; else if (str2.size() == 0) return std::string{str1}; const bool neg1 = str1.front() == '-'; const bool neg2 = str2.front() == '-'; if (neg1) str1.remove_prefix(1); if (neg2) str2.remove_prefix(1); if (neg1 && neg2) { return "-" + add(str1, str2); } else if(neg1) { return subtract(str2, str1); } else if(neg2) { return subtract(str1, str2); } else { size_t carry = 0; auto it1 = str1.crbegin(); auto it2 = str2.crbegin(); const auto end1 = str1.crend(); const auto end2 = str2.crend(); std::string sum; sum.reserve(std::max(str1.size(), str2.size())); for (; it1 < end1 || it2 < end2; ++it1, ++it2) { uint64_t val1 = it1 < end1 ? uint64_t(*it1) - 48 : 0; uint64_t val2 = it2 < end2 ? uint64_t(*it2) - 48 : 0; uint64_t track_sum = val1 + val2 + carry; carry = track_sum / 10; sum = std::to_string(track_sum % 10) + sum; } if (carry != 0) { sum = std::to_string(carry) + sum; } return trim(sum); } } /* ./build//a.out -.0 0.0 -.0 1 -100000000000.0 ./build//a.out -0 00 -0 100000000000.0 -00000000000.0 ./build//a.out 12 -1.20 11 1.20 12 -1.20 0 1 1 -100000000000.0 */ auto main() -> int { // std::cout << add("1", "1") << std::endl; // std::cout << add("-3141526", "-3141526") << std::endl; // std::cout << trim("-000000000001.20") << std::endl; // std::cout << trim("1000000000001.20") << std::endl; // std::cout << trim("-100000000000.0") << std::endl; // std::cout << trim("-") << std::endl; std::cout << "Hello, world!" << std::endl; }