Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

52
Task/Nth/C/nth-1.c Normal file
View file

@ -0,0 +1,52 @@
#include <stdio.h>
char* addSuffix(int num, char* buf, size_t len)
{
char *suffixes[4] = { "th", "st", "nd", "rd" };
int i;
switch (num % 10)
{
case 1 : i = (num % 100 == 11) ? 0 : 1;
break;
case 2 : i = (num % 100 == 12) ? 0 : 2;
break;
case 3 : i = (num % 100 == 13) ? 0 : 3;
break;
default: i = 0;
};
snprintf(buf, len, "%d%s", num, suffixes[i]);
return buf;
}
int main(void)
{
int i;
printf("Set [0,25]:\n");
for (i = 0; i < 26; i++)
{
char s[5];
printf("%s ", addSuffix(i, s, 5));
}
putchar('\n');
printf("Set [250,265]:\n");
for (i = 250; i < 266; i++)
{
char s[6];
printf("%s ", addSuffix(i, s, 6));
}
putchar('\n');
printf("Set [1000,1025]:\n");
for (i = 1000; i < 1026; i++)
{
char s[7];
printf("%s ", addSuffix(i, s, 7));
}
putchar('\n');
return 0;
}

33
Task/Nth/C/nth-2.c Normal file
View file

@ -0,0 +1,33 @@
#include <stdlib.h>
#include <stdio.h>
static int digits(const int x) {
if (x / 10 == 0) return 1;
return 1 + digits(x / 10);
}
static char * get_ordinal(const int i) {
const int string_size = digits(i) + 3;
char * o_number = malloc(string_size);
char * ordinal;
if(i % 100 >= 11 && i % 100 <= 13) ordinal = "th";
else ordinal = i/10==1?"th":i%10==1?"st":i%10==2?"nd":i%10==3?"rd":"th";
sprintf_s(o_number, string_size, "%d%s", i, ordinal);
return o_number;
}
static void print_range(const int begin, const int end) {
printf("Set [%d,%d]:\n", begin, end);
for (int i = begin; i <= end; i++) {
char * o_number = get_ordinal(i);
printf("%s ", o_number);
free(o_number);
}
printf("\n");
}
int main(void) {
print_range(0, 25);
print_range(250, 265);
print_range(1000, 1025);
}