From a660745f4efb1a8be426529a1f90d3a5a2010a83 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 20 Oct 2021 08:23:28 +0100 Subject: - Added solutions by Ulrich Rieke. --- challenge-135/ulrich-rieke/cpp/ch-2.cpp | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 challenge-135/ulrich-rieke/cpp/ch-2.cpp (limited to 'challenge-135/ulrich-rieke/cpp/ch-2.cpp') diff --git a/challenge-135/ulrich-rieke/cpp/ch-2.cpp b/challenge-135/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..03be87e9a2 --- /dev/null +++ b/challenge-135/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include + +int main( int argc , char * argv[ ] ) { + std::string sedol { argv[ 1 ] } ; + bool valid = false ; + if ( sedol.length( ) != 7 ) { + std::cout << 0 << std::endl ; + return 1 ; + } + std::transform( sedol.begin( ) , sedol.end( ) , sedol.begin( ) , + toupper ) ; + std::string checkReg ("(^\\w+$)" ) ; + std::regex checkRegex( checkReg ) ; + std::string anotherCheck ("([AEIOU])") ; + std::regex vowelCheck( anotherCheck ) ; + if ( (! ( std::regex_match( sedol , checkRegex ))) || + ( std::regex_match( sedol , vowelCheck ) ) ) { + std::cout << 0 << std::endl ; + return 2 ; + } + std::array weights {{ 1, 3 , 1 , 7 , 3 , 9 , 1 }} ; + int sum = 0 ; + for ( int i = 0 ; i < 6 ; i++ ) { + int value = static_cast( sedol[ i ] ) ; + if ( isupper( value )) { + sum += (value - 55) * weights[ i ] ; + } + else { + int value = std::stoi( sedol.substr( i , 1 ) ) ; + sum += value * weights[ i ] ; + } + } + std::string letterStr {sedol.substr( 6 , 1 )} ; + if ( std::stoi( letterStr ) == ( (10 - ( sum % 10 )) % 10 )) { + std::cout << 1 << std::endl ; + } + else + std::cout << 0 << std::endl ; + return 0 ; +} -- cgit