diff options
| author | 冯昶 <seaker@qq.com> | 2021-03-15 18:13:51 +0800 |
|---|---|---|
| committer | 冯昶 <seaker@qq.com> | 2021-03-15 18:13:51 +0800 |
| commit | 8b6be37fe4dac8b4c6489a95e55514b76b298d15 (patch) | |
| tree | ae36c8ec2c71f606c0e36adaa19dba366a68a0b4 /challenge-002/paulo-custodio/cpp | |
| parent | 865acfd056fb6f409ec6b1a81d60b931cbcb69fe (diff) | |
| parent | c9aec2da6bcb04b488183f09ca94bee488557aff (diff) | |
| download | perlweeklychallenge-club-8b6be37fe4dac8b4c6489a95e55514b76b298d15.tar.gz perlweeklychallenge-club-8b6be37fe4dac8b4c6489a95e55514b76b298d15.tar.bz2 perlweeklychallenge-club-8b6be37fe4dac8b4c6489a95e55514b76b298d15.zip | |
Merge branch 'master' of github.com:seaker/perlweeklychallenge-club
Diffstat (limited to 'challenge-002/paulo-custodio/cpp')
| -rw-r--r-- | challenge-002/paulo-custodio/cpp/ch-1.cpp | 18 | ||||
| -rw-r--r-- | challenge-002/paulo-custodio/cpp/ch-2.cpp | 81 |
2 files changed, 99 insertions, 0 deletions
diff --git a/challenge-002/paulo-custodio/cpp/ch-1.cpp b/challenge-002/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..1399dfe279 --- /dev/null +++ b/challenge-002/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,18 @@ +/* +Challenge 002 + +Challenge #1 +Write a script or one-liner to remove leading zeros from positive numbers. +*/ + +#include <iostream> +#include <cctype> + +int main(int argc, char* argv[]) { + if (argc==2) { + char* p = argv[1]; + while (*p=='0' && isdigit(p[1])) + p++; + std::cout << p << std::endl; + } +} diff --git a/challenge-002/paulo-custodio/cpp/ch-2.cpp b/challenge-002/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..8a3510766b --- /dev/null +++ b/challenge-002/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,81 @@ +/* +Challenge 002 + +Challenge #2 +Write a script that can convert integers to and from a base35 +representation, using the characters 0-9 and A-Y. Dave Jacoby came up +with nice description about base35, in case you needed some background. +*/ + +#include <iostream> +#include <string> +#include <cctype> + +// format a digit +char format_digit(int n) { + if (n < 10) + return '0' + n; + else + return 'A' + n - 10; +} + +// format a number +std::string format_number(int n, int base) { + bool negative = false; + std::string str; + if (n < 0) { + negative = true; + n = -n; + } + do { + int d = n % base; + n = n / base; + str.insert(0, 1, format_digit(d)); + } while (n > 0); + if (negative) + str.insert(0, 1, '-'); + return str; +} + +// scan digit +int scan_digit(char c) { + if (isdigit(c)) + return c - '0'; + else if (isalpha(c)) + return toupper(c) - 'A' + 10; + else + return -1; +} + +// scan number +int scan_number(const std::string& str, int base) { + const char* p = str.c_str(); + bool negative = false; + int n = 0; + if (*p == '-') { + negative = true; + p++; + } + while (*p != '\0') { + int d = scan_digit(*p++); + if (d < 0 || d >= base) { + std::cerr << "invalid number" << std::endl; + exit(EXIT_FAILURE); + } + n = n * base + d; + } + if (negative) + n = -n; + return n; +} + +int main(int argc, char* argv[]) { + if (argc == 2) + std::cout << format_number(atoi(argv[1]), 35) << std::endl; + else if (argc == 3 && std::string(argv[1]) == "-r") + std::cout << scan_number(argv[2], 35) << std::endl; + else { + std::cerr << "Usage: ch-2 [-r] number" << std::endl; + return EXIT_FAILURE; + } +} |
