Added files
This commit is contained in:
31
gcd/Makefile
Normal file
31
gcd/Makefile
Normal file
@ -0,0 +1,31 @@
|
||||
CXX := g++
|
||||
CFLAGS := -std=c++11 -m64 -Wall -Werror -Wextra -O3
|
||||
|
||||
.PHONY: all
|
||||
all: bin/gcd
|
||||
|
||||
bin:
|
||||
@mkdir -v bin
|
||||
|
||||
bin/gcd: src/gcd.cpp | check-cxx-works bin
|
||||
@echo Compiling $<
|
||||
@${CXX} ${CFLAGS} $^ -o $@
|
||||
|
||||
.PHONY: install
|
||||
install: bin/gcd
|
||||
@cp -v $^ /usr/bin
|
||||
|
||||
.PHONY: uninstall
|
||||
uninstall: /usr/bin/gcd
|
||||
@rm -v $^
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
@rm -rfv bin
|
||||
|
||||
.PHONY: mrproper
|
||||
mrproper: clean
|
||||
|
||||
.PHONY: check-cxx-works
|
||||
check-cxx-works:
|
||||
@${CXX} --version >/dev/null 2>&1 || (echo 'Please install a C++ compiler.' && exit 1)
|
63
gcd/src/gcd.cpp
Normal file
63
gcd/src/gcd.cpp
Normal file
@ -0,0 +1,63 @@
|
||||
#include <cstdlib>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <cstring>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* @brief Calculate the greatest common divisor (GCD)
|
||||
*
|
||||
* @param a First integer
|
||||
* @param b Second integer
|
||||
* @return greatest common divisor between a and b
|
||||
*/
|
||||
constexpr int64_t gcd(const int64_t& a, const int64_t& b) noexcept { return a == 0 ? b : gcd(b % a, a); }
|
||||
|
||||
/**
|
||||
* @brief Convert a given string to a 64 bit integer
|
||||
*
|
||||
* @param str Input string to convert
|
||||
* @param n Integer output
|
||||
* @return EXIT_SUCCESS if successful otherwise EXIT_FAILURE
|
||||
*/
|
||||
int32_t sstrtoll(const char* const str, int64_t& n) noexcept {
|
||||
errno = 0;
|
||||
char* endptr = nullptr;
|
||||
const int64_t a = strtoll(str, &endptr, 10);
|
||||
|
||||
switch(errno){
|
||||
case 0:
|
||||
n = a;
|
||||
return EXIT_SUCCESS;
|
||||
case ERANGE:
|
||||
fprintf(stderr, "Error while converting to integer : numerical result out of range (");
|
||||
if(a == LLONG_MIN)
|
||||
fprintf(stderr, "underflow occurred");
|
||||
else if(a == LLONG_MAX)
|
||||
fprintf(stderr, "overflow occurred");
|
||||
else
|
||||
fprintf(stderr, "unspecified");
|
||||
fprintf(stderr, ")\n");
|
||||
return EXIT_FAILURE;
|
||||
default:
|
||||
fprintf(stderr, "Unspecified error occurred while converting to integer: %s\n", strerror(errno));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
n = a;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int32_t main(const int32_t argc, const char* const* argv) noexcept {
|
||||
if (argc != 3){
|
||||
fprintf(stderr, "Syntax : gcd number1 number2\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
int64_t a, b;
|
||||
if(sstrtoll(argv[1], a) == EXIT_FAILURE) return EXIT_FAILURE;
|
||||
if(sstrtoll(argv[2], b) == EXIT_FAILURE) return EXIT_FAILURE;
|
||||
|
||||
const int64_t hcf = gcd(std::abs(a), std::abs(b));
|
||||
printf("Common factor = %ld\nResult = %ld/%ld\n", hcf, a / hcf, b / hcf);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Reference in New Issue
Block a user