Skip to content

Instantly share code, notes, and snippets.

@roktas
Created October 25, 2010 01:37
Show Gist options
  • Save roktas/644260 to your computer and use it in GitHub Desktop.
Save roktas/644260 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void
quovadis(void)
{
fprintf(stderr, "Quo Vadis!\n");
}
int
main(void)
{
atexit(quovadis);
fprintf(stderr, "birşeyler yapacağım...\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void
dohelp(void)
{
fprintf(stderr, "imdaaaaaaaaat\n");
}
static void
doexit(void)
{
fprintf(stderr, "çıkıyorum\n");
exit(EXIT_SUCCESS);
}
static struct command_t {
const char *name;
void (*action)(void);
} commands[] = {
{ "help", dohelp, },
{ "exit", doexit, },
{ NULL, NULL, },
};
int
main(void)
{
(*commands[1].action)();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void
dohelp(void)
{
fprintf(stderr, "imdaaaaaaaaat\n");
}
static void
doexit(void)
{
fprintf(stderr, "peki\n");
exit(EXIT_SUCCESS);
}
static struct command_t {
const char *name;
void (*action)(void);
} commands[] = {
{ "help", dohelp, },
{ "exit", doexit, },
{ NULL, NULL, },
};
static struct command_t *
find_command(const char *name)
{
int i;
for (i = 0; i < sizeof(commands)/sizeof(commands[0]); i++)
if (strcmp(name, commands[i].name) == 0)
return &commands[i];
return NULL;
}
int
main(int argc, char *argv[])
{
struct command_t *pc;
if ((pc = find_command(argv[1])) != NULL) {
fprintf(stderr, "%s komutunu çalıştırıyorum\n", pc->name);
(*pc->action)();
} else {
fprintf(stderr, "böyle bir komut yok\n");
exit(EXIT_FAILURE);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char error[128];
static void
die(void)
{
fprintf(stderr, "%s nedeniyle öldüm.\n", error);
exit(EXIT_FAILURE);
}
static void
cry(void)
{
fprintf(stderr, "%s nedeniyle yaralandım, devam ediyorum.\n", error);
}
static void
dosth(void (*errhandler)(void))
{
fprintf(stderr, "birşeyler yapıyorum...\n");
strcpy(error, "aşırı ısınma");
(*errhandler)();
}
int
main(void)
{
fprintf(stderr, "birşeyler yapacağım...\n");
dosth(cry);
fprintf(stderr, "birşeyler yaptım...\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void
die(const char *error)
{
fprintf(stderr, "%s nedeniyle öldüm.\n", error);
exit(EXIT_FAILURE);
}
static void
cry(const char *error)
{
fprintf(stderr, "%s nedeniyle yaralandım, devam ediyorum.\n", error);
}
static void
dosth(void (*errhandler)(const char*))
{
fprintf(stderr, "birşeyler yapıyorum...\n");
(*errhandler)("aşırı ısınma");
}
int
main(void)
{
fprintf(stderr, "birşeyler yapacağım...\n");
dosth(cry);
fprintf(stderr, "birşeyler yaptım...\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void (*errhandler)(const char *);
static void
cry(const char *error)
{
fprintf(stderr, "%s\n", error);
}
static void
silent(const char *error)
{
; /* noop */
}
static void
dosth(void)
{
fprintf(stderr, "birşeyler yapıyorum...\n");
(*errhandler)("aşırı ısınma");
}
int
main(int argc, char *argv[])
{
errhandler = (argc > 1 && strcmp(argv[1], "-s") == 0) ? silent : cry;
fprintf(stderr, "birşeyler yapacağım...\n");
dosth();
fprintf(stderr, "birşeyler yaptım...\n");
return 0;
}
#include <stdio.h>
#define MAXELEM 1024
static int v[MAXELEM];
/* swap: interchange v[i] and v[j] */
static void
swap(int v[], int i, int j)
{
int temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
/* qsort: sort v[left]...v[right] into increasing order */
static void
quicksort(int v[], int left, int right)
{
int i, last;
if (left >= right) /* do nothing if array contains */
return; /* fewer than two elements */
swap(v, left, (left + right)/2); /* move partition elem */
last = left; /* to v[0] */
for (i = left + 1; i <= right; i++) /* partition */
if (v[i] < v[left])
swap(v, ++last, i);
swap(v, left, last); /* restore partition elem */
quicksort(v, left, last-1);
quicksort(v, last+1, right);
}
int
main(int argc, char *argv[])
{
int nelem, i;
for (i = 1; i < argc && i < MAXELEM; i++)
v[i] = atoi(argv[i]);
nelem = i;
quicksort(v, 0, nelem - 1);
for (i = 0; i < nelem; i++)
printf("%d ", v[i]);
printf("\n");
return 0;
}
#include <stdio.h>
#define MAXELEM 1024
static int v[MAXELEM];
/* swap: interchange v[i] and v[j] */
static void
_swap(int v[], int i, int j)
{
int temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
/* qsort: sort v[left]...v[right] into increasing order */
static void
_quicksort(int v[], int left, int right)
{
int i, last;
if (left >= right) /* do nothing if array contains */
return; /* fewer than two elements */
_swap(v, left, (left + right)/2); /* move partition elem */
last = left; /* to v[0] */
for (i = left + 1; i <= right; i++) /* partition */
if (v[i] < v[left])
_swap(v, ++last, i);
_swap(v, left, last); /* restore partition elem */
_quicksort(v, left, last-1);
_quicksort(v, last+1, right);
}
static int
fill(int v[], char *source[], int nelem)
{
int i;
for (i = 0; i < nelem && i < MAXELEM; i++)
v[i] = atoi(source[i]);
return i;
}
static void
quicksort(int v[], int nelem)
{
_quicksort(v, 0, nelem-1);
}
static void
print(int v[], int nelem)
{
int i;
for (i = 0; i < nelem; i++)
printf("%d ", v[i]);
printf("\n");
}
int
main(int argc, char *argv[])
{
int nelem;
nelem = fill(v, &argv[1], argc-1);
quicksort(v, nelem);
print(v, nelem);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXELEM 1024
#define MAXLEN 128
static char *v[MAXELEM];
/* swap: interchange v[i] and v[j] */
static void
_swap(char *v[], int i, int j)
{
char *temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
/* qsort: sort v[left]...v[right] into increasing order */
static void
_quicksort(char *v[], int left, int right)
{
int i, last;
if (left >= right) /* do nothing if array contains */
return; /* fewer than two elements */
_swap(v, left, (left + right)/2); /* move partition elem */
last = left; /* to v[0] */
for (i = left + 1; i <= right; i++) /* partition */
if (strcmp(v[i], v[left]) < 0)
_swap(v, ++last, i);
_swap(v, left, last); /* restore partition elem */
_quicksort(v, left, last-1);
_quicksort(v, last+1, right);
}
static int
fill(char *v[], char *source[], int nelem)
{
int i;
for (i = 0; i < nelem && i < MAXELEM; i++)
v[i] = strndup(source[i], MAXLEN);
return i;
}
static void
release(char *v[], int nelem)
{
int i;
for (i = 0; i < nelem && i < MAXELEM; i++)
free(v[i]);
}
static void
quicksort(char *v[], int nelem)
{
_quicksort(v, 0, nelem-1);
}
static void
print(char *v[], int nelem)
{
int i;
for (i = 0; i < nelem; i++)
printf("%s ", v[i]);
printf("\n");
}
int
main(int argc, char *argv[])
{
int nelem;
nelem = fill(v, &argv[1], argc-1);
quicksort(v, nelem);
print(v, nelem);
release(v, nelem);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXELEM 1024
#define MAXLEN 128
static char *v[MAXELEM];
static int
fill(char *v[], char *source[], int nelem)
{
int i;
for (i = 0; i < nelem && i < MAXELEM; i++)
v[i] = strndup(source[i], MAXLEN);
return i;
}
static void
release(char *v[], int nelem)
{
int i;
for (i = 0; i < nelem && i < MAXELEM; i++)
free(v[i]);
}
static int
cmp(const void *p, const void *q)
{
return strcmp(*(const char **)p, *(const char **) q);
}
static void
quicksort(char *v[], int nelem)
{
qsort(v, (size_t) nelem, sizeof(v[0]), cmp);
}
static void
print(char *v[], int nelem)
{
int i;
for (i = 0; i < nelem; i++)
printf("%s ", v[i]);
printf("\n");
}
int
main(int argc, char *argv[])
{
int nelem;
nelem = fill(v, &argv[1], argc-1);
quicksort(v, nelem);
print(v, nelem);
release(v, nelem);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment