Middle three digits: Difference between revisions

m
(→‎{{header|C}}: replaced incorrect implementation ("&128" was not reasonable; inherent overflow problem))
m (→‎{{header|C}}: lang tag)
Line 106:
}</lang>
 
=={{header|C++}}==
<lang cppc>#include <stdio.h>
#include <iostreamstdlib.h>
#include <sstreamstring.h>
#include <string>
 
// we return a static buffer; caller wants it, caller copies it
// @author Martin Ettl
char * mid3(int n)
// @date 2013-02-04
 
/**
* Convert variables of type T to std::string
*
*
* @param d --> digit of type T
*
* @return <-- the corresponding string value
*/
template <typename T> const std::string toString(const T &d)
{
static char buf[32];
std::ostringstream result;
int l;
result << d;
sprintf(buf, "%d", n > 0 ? n : -n);
return result.str();
l = strlen(buf);
if (l < 3 || !(l & 1)) return 0;
l = l / 2 - 1;
buf[l + 3] = 0;
return buf + l;
}
 
int main(void)
/**
* Determine the middle n digits of the integer. If it is not possible to determine the
* the middle n digits, an empty string is provided.
*
* @param iDigit --> The digit to test
* @param n --> The number of digits inbetween
*
* @return <-- the middle three digits
*/
std::string strMiddleNDigits(int iDigit, const int &n)
{
int x[] = {123, 12345, 1234567, 987654321, 10001, -10001,
// is negative: --> convert to a positive number
-123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0,
if(iDigit<0)
1234567890};
{
iDigit*=-1;
}
// convert to string
std::string strNumber (toString(iDigit));
size_t len(strNumber.length());
if( (len < n) || (len % 2 == 0) )
{
return "";
}
size_t mid(len/2);
return strNumber.substr(mid-n/2, n);
}
 
int i;
/**
char *m;
* Determine the middle three digits of the integer. If it is not possible to determine the
for (i = 0; i < sizeof(x)/sizeof(x[0]); i++) {
* the middle three digits, an empty string is provided.
if (!(m = mid3(x[i])))
*
m = "error";
* @param iDigit --> The digit to test
printf("%d: %s\n", x[i], m);
*
}
* @return <-- the middle three digits
return 0;
*/
}</lang>
std::string strMiddleThreeDigits(int iDigit)
{
return strMiddleNDigits(iDigit,3);
}
 
int main()
{
const int iPassing[] = {123, 12345, 1234567, 987654321, 10001, -10001,
-123, -100, 100, -12345
};
for(unsigned int ui = 0; ui < 10; ++ui)
{
std::cout << "strMiddleThreeDigits("<< iPassing[ui] <<"): "
<< strMiddleThreeDigits(iPassing[ui])<< "\n";
}
 
const int iFailing[] = {1, 2, -1, -10, 2002, -2002, 0};
for(unsigned int ui = 0; ui < 7; ++ui)
{
std::string strResult = strMiddleThreeDigits(iFailing[ui]);
std::cout << "strMiddleThreeDigits("<< iFailing[ui] <<"): "
<< (strResult.empty()?"Need odd and >= 3 digits":strResult)
<< "\n";
}
 
return 0;
}
</lang>
{{out}}
<pre>strMiddleThreeDigits(123): 123
strMiddleThreeDigits(12345): 234
strMiddleThreeDigits(1234567): 345
strMiddleThreeDigits(987654321): 654
strMiddleThreeDigits(10001): 000
strMiddleThreeDigits(-10001): 000
strMiddleThreeDigits(-123): 123
strMiddleThreeDigits(-100): 100
strMiddleThreeDigits(100): 100
strMiddleThreeDigits(-12345): 234
strMiddleThreeDigits(1): Need odd and >= 3 digits
strMiddleThreeDigits(2): Need odd and >= 3 digits
strMiddleThreeDigits(-1): Need odd and >= 3 digits
strMiddleThreeDigits(-10): Need odd and >= 3 digits
strMiddleThreeDigits(2002): Need odd and >= 3 digits
strMiddleThreeDigits(-2002): Need odd and >= 3 digits
strMiddleThreeDigits(0): Need odd and >= 3 digits</pre>
 
=={{header|D}}==
Anonymous user