Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
32
Task/Middle-three-digits/C/middle-three-digits-1.c
Normal file
32
Task/Middle-three-digits/C/middle-three-digits-1.c
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// we return a static buffer; caller wants it, caller copies it
|
||||
char * mid3(int n)
|
||||
{
|
||||
static char buf[32];
|
||||
int l;
|
||||
sprintf(buf, "%d", n > 0 ? n : -n);
|
||||
l = strlen(buf);
|
||||
if (l < 3 || !(l & 1)) return 0;
|
||||
l = l / 2 - 1;
|
||||
buf[l + 3] = 0;
|
||||
return buf + l;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int x[] = {123, 12345, 1234567, 987654321, 10001, -10001,
|
||||
-123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0,
|
||||
1234567890};
|
||||
|
||||
int i;
|
||||
char *m;
|
||||
for (i = 0; i < sizeof(x)/sizeof(x[0]); i++) {
|
||||
if (!(m = mid3(x[i])))
|
||||
m = "error";
|
||||
printf("%d: %s\n", x[i], m);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
56
Task/Middle-three-digits/C/middle-three-digits-2.c
Normal file
56
Task/Middle-three-digits/C/middle-three-digits-2.c
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void midThree(char arg[])
|
||||
{
|
||||
char output[4];
|
||||
int midPoint;
|
||||
arg[0]=='-'?midPoint = ((strlen(arg) + 1) / 2):midPoint = ((strlen(arg) + 1) / 2) - 1;
|
||||
|
||||
if(strlen(arg) < 3)
|
||||
{
|
||||
printf("Error, %d is too short.\n",atoi(arg));
|
||||
return ;
|
||||
}
|
||||
else if(strlen(arg) == 4 || strlen(arg) == 3)
|
||||
{
|
||||
printf("Error, %d has %d digits, no 3 middle digits.\n",atoi(arg),arg[0]=='-'?strlen(arg)-1:strlen(arg));
|
||||
return ;
|
||||
}
|
||||
else{
|
||||
for(int i=0; i<3; i++)
|
||||
{
|
||||
output[i] = arg[(midPoint-1) + i];
|
||||
}
|
||||
output[3] = '\0';
|
||||
printf("The middle three digits of %s are %s.\n",arg,output);
|
||||
}}
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
char input[50];
|
||||
int x[] = {123, 12345, 1234567, 987654321, 10001, -10001,
|
||||
-123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0,
|
||||
1234567890},i;
|
||||
|
||||
if(argc < 2)
|
||||
{
|
||||
printf("Usage: %s <integer>\n",argv[0]);
|
||||
printf("Examples with preloaded data shown below :\n");
|
||||
|
||||
for(i=0;i<18;i++)
|
||||
{
|
||||
midThree(itoa(x[i],argv[0],10));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(input,"%d",atoi(argv[1]));
|
||||
midThree(argv[1]);
|
||||
return 0;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue