Talk:C: Difference between revisions

5,206 bytes added ,  1 year ago
latest pe.c
(done)
(latest pe.c)
 
(2 intermediate revisions by 2 users not shown)
Line 1:
 
===pe.c===
<nowiki>
 
 
#if 1
#else
#error NEXT= ??? 16! = c.20x10^40
#endif
 
/* TODO: PrecPup dev (gcc) */
/* https://www.onlinegdb.com/online_c_compiler */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
 
/**
#include <math.h>
**/
 
#include <assert.h>
static void (die)(const int line);
#define die() die(__LINE__);
 
#define H_(X) (#X)
#define H(X) H_(X)
 
/*\ \*/ /*\ \*/ /*\ \*/
 
typedef unsigned long lnat;
 
/*\ \*/
 
struct n_; typedef struct n_ n_; struct n_ {
lnat p, x;
n_ *n;
};
 
/*
struct l_; typedef struct l_ l_; struct l_ {
n_ *h;
}; /**/
 
typedef int rand_f(void);
/* static rand_f *rf= rand; /**/
 
static void dump(n_ *p);
 
/*\ \*/ /*\ \*/ /*\ \*/
 
static n_ **split(n_ **lo_, n_ **hi_, n_ **it, lnat y) {
n_ *r, **lo= lo_, **hi= hi_;
lnat t= 0;
 
if (it); else it= lo_;
 
for (r= *it; r; r= r->n) {
if (r->x < y) {
*lo= r; lo= &r->n;
t+= r->p + 1;
} else {
*hi= r; hi= &r->n;
r->p+= t;
/* t= 0; /**/
}
}
*hi= *lo= 0;
 
return hi_;
}
 
/*\ \*/ /*\ \*/ /*\ \*/
 
static void recurse_(lnat it, n_**hd) {
 
assert(*hd);
{ lnat x= (*hd)->x, p= (*hd)->p;
printf("it= %lu, r= (%lu, %lu, %lu)\t", it, x, p, x-p);
assert(x>=p);
}
dump(*hd);
fflush(0);
}
 
/*\ \*/
 
static void recurse(lnat lo, lnat hi, n_ **hd) {
lnat mi;
/**/
printf("\tlo= %lu hi= %lu\t", lo, hi);
dump(*hd);
fflush(0); /**/
 
assert(lo<=hi);
if (lo!=hi); else { recurse_(lo, hd); return; }
 
mi= 2+hi-lo; mi= mi/2+lo;
 
{ n_ *li[1];
/**/ split(hd, li, 0, mi); /**/
 
recurse(lo, mi-1, hd);
printf("lo= %lu mi= %lu hi= %lu\n", lo, mi, hi);
recurse(mi, hi, li);
}
}
 
/*\ \*/ /*\ \*/ /*\ \*/
 
#define M 1
#define M_ (1u<<M)
#define K (1u<<M_)
 
static n_ a__[1][K];
 
static void dump(n_ *p) {
lnat v= 0;
lnat d;
 
{ static lnat u= 0ul-1; assert((u>>= 1)); }
 
if (p); else { puts("NIL"); return; }
 
do {
assert(p>=*a__);
assert(p<a__[1]);
d= p-*a__;
printf(d>>4? "^": "%X(x%lu..=%lu)->", (unsigned)d&15u, p->p, p->x);
if (d>>5); else {
d= 1ul<<d;
v^= d;
if (v&d); else {
puts("etc"); return;
}
}
p= p->n; /* TODO: count, check LE K */
} while (p);
puts("#");
}
 
/*\ \*/ /*\ \*/ /*\ \*/
 
static void tests_0(n_ **hd, n_ (*a)[K]) {
n_ *q= a[1], *p= 0;
int i;
 
assert(hd); assert(a);
 
for (i= K+0; --i >= 0; ) { (--q)->n= p; p= q; p->p= 0; p->x= i; }
 
*hd= p;
}
 
#define rand() 0
 
static void tests_R_(n_ **hd, rand_f *rnd) {
 
n_ *le[1], **lt[1], *ri[1], **rt[1], ***pt, **pt_, *it, *it_;
 
assert(*hd);
 
it= (it_= *hd)->n;
if (it); else return;
 
dump(it_);
 
/* it_->n= 0; /**/
*(*rt= ri)= *(*lt= le)= 0;
 
for (; it; it= it->n, **pt= 0) {
pt= rnd()&1u? rt: lt;
assert(!**pt);
**pt= it; /* append */
*pt= &it->n; /* advance */
}
**rt= **lt= 0;
 
/* if r is empty will prepend to r, and vice versa */
pt_= *ri? *le && (rnd()&1u)? ri: le: ri;
it_->n= *pt_;
*pt_= it_;
 
tests_R_(le, rnd);
tests_R_(ri, rnd);
 
*hd= it= *(pt_= ri); /* TODO: rnd() */
assert(it);
while ((it= *(pt_= &it->n)) != 0);
*pt_= *le;
}
 
static int rnd0(void) { return 0; }
static int rnd1(void) { return 1; }
 
static void tests_R0(n_ **hd, n_ (*a)[K]) {
 
tests_0(hd, a__);
tests_R_(hd, rnd0);
}
 
static void tests_R1(n_ **hd, n_ (*a)[K]) {
 
tests_0(hd, a__);
tests_R_(hd, rnd1);
}
 
static void tests_R(n_ **hd, n_ (*a)[K]) {
 
tests_0(hd, a__);
tests_R_(hd, rand);
}
 
static n_ *tests(void) {
n_ *hd[1];
 
*hd= 0;
tests_0(hd, a__);
assert(*hd);
dump(*hd);
printf("[[ at %u]]\n", __LINE__);
 
*hd= 0;
tests_R0(hd, a__);
assert(*hd);
dump(*hd);
printf("[[ at %u]]\n", __LINE__);
 
*hd= 0;
tests_R1(hd, a__);
assert(*hd);
dump(*hd);
printf("[[ at %u]]\n", __LINE__);
 
*hd= 0;
tests_R(hd, a__);
assert(*hd);
dump(*hd);
printf("[[ at %u]]\n", __LINE__);
 
/**/ return *hd; /**/
 
*hd= 0;
tests_R0(hd, a__);
assert(*hd);
dump(*hd);
printf("[[ at %u]]\n", __LINE__);
 
/*
*hd= 0;
tests_R1(hd, a__);
assert(*hd);
dump(*hd);
printf("[[ at %u]]\n", __LINE__);
/**/
 
return *hd;
}
 
/*\ \*/ /*\ \*/ /*\ \*/
 
static int dtj(void) {
n_ *hd[1];
 
/* (die)(0); /**/
*hd= tests();
dump(0); /**/
dump(*hd); /**/
recurse(0, K-1, hd); /**/
 
return 0;
}
 
/*\ \*/
 
static int dothejob(void) {
int rv;
 
rv= dtj();
 
return !rv? EXIT_SUCCESS: EXIT_FAILURE;
}
 
/*\ \*/ /*\ \*/ /*\ \*/
 
#undef NDEBUG
/** Using assert() to report runtime errors is, admittedly, very fckg lazy. -EMD
**/
#include <assert.h>
 
static void (die)(int line) {
 
if (line); else {
fprintf(stderr, "\n\nThe C program '%s' has aborted.\n", __FILE__); fflush(0);
assert(!"I'm too stupid to work this out, sorry.");
}
 
fprintf(stderr, "\n\nYour crappy wee computer doesn't have enough memory to run"
" the C program '%s', which is why it gave up trying somewhere around line %d.\n",
__FILE__, line); fflush(0);
assert(!"I'm shit out of memory, sorry.");
}
 
/*\ \*/ /*\ \*/ /*\ \*/ /*\ \*/ /*\ \*/ /*\ \*/ /*\ \*/ /*\ \*/ /*\ \*/
 
extern int main(int ac, const char *const *av) {
 
(void)ac; (void)av;
 
ac= dothejob(); /**/
return ac;
}
 
#if 1
#else
#error NEXT FILE
 
#include <stddef.h>
Line 44 ⟶ 361:
 
for (ln= 0-1; ++ln<K62; ) { b[ln][k][-1]= 0; puts(*b[ln]); }
 
puts(" @@@@@@@");
 
for (ln= 0-1; ++ln<K62; ) puts(b[ln][k]);
 
return EXIT_SUCCESS;
}
 
#endif
 
 
</nowiki>
3

edits