Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
71
Task/Ternary-logic/C/ternary-logic-1.c
Normal file
71
Task/Ternary-logic/C/ternary-logic-1.c
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
#include <stdio.h>
|
||||
|
||||
typedef enum {
|
||||
TRITTRUE, /* In this enum, equivalent to integer value 0 */
|
||||
TRITMAYBE, /* In this enum, equivalent to integer value 1 */
|
||||
TRITFALSE /* In this enum, equivalent to integer value 2 */
|
||||
} trit;
|
||||
|
||||
/* We can trivially find the result of the operation by passing
|
||||
the trinary values as indeces into the lookup tables' arrays. */
|
||||
trit tritNot[3] = {TRITFALSE , TRITMAYBE, TRITTRUE};
|
||||
trit tritAnd[3][3] = { {TRITTRUE, TRITMAYBE, TRITFALSE},
|
||||
{TRITMAYBE, TRITMAYBE, TRITFALSE},
|
||||
{TRITFALSE, TRITFALSE, TRITFALSE} };
|
||||
|
||||
trit tritOr[3][3] = { {TRITTRUE, TRITTRUE, TRITTRUE},
|
||||
{TRITTRUE, TRITMAYBE, TRITMAYBE},
|
||||
{TRITTRUE, TRITMAYBE, TRITFALSE} };
|
||||
|
||||
trit tritThen[3][3] = { { TRITTRUE, TRITMAYBE, TRITFALSE},
|
||||
{ TRITTRUE, TRITMAYBE, TRITMAYBE},
|
||||
{ TRITTRUE, TRITTRUE, TRITTRUE } };
|
||||
|
||||
trit tritEquiv[3][3] = { { TRITTRUE, TRITMAYBE, TRITFALSE},
|
||||
{ TRITMAYBE, TRITMAYBE, TRITMAYBE},
|
||||
{ TRITFALSE, TRITMAYBE, TRITTRUE } };
|
||||
|
||||
/* Everything beyond here is just demonstration */
|
||||
|
||||
const char* tritString[3] = {"T", "?", "F"};
|
||||
|
||||
void demo_binary_op(trit operator[3][3], const char* name)
|
||||
{
|
||||
trit operand1 = TRITTRUE; /* Declare. Initialize for CYA */
|
||||
trit operand2 = TRITTRUE; /* Declare. Initialize for CYA */
|
||||
|
||||
/* Blank line */
|
||||
printf("\n");
|
||||
|
||||
/* Demo this operator */
|
||||
for( operand1 = TRITTRUE; operand1 <= TRITFALSE; ++operand1 )
|
||||
{
|
||||
for( operand2 = TRITTRUE; operand2 <= TRITFALSE; ++operand2 )
|
||||
{
|
||||
printf("%s %s %s: %s\n", tritString[operand1],
|
||||
name,
|
||||
tritString[operand2],
|
||||
tritString[operator[operand1][operand2]]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
trit op1 = TRITTRUE; /* Declare. Initialize for CYA */
|
||||
trit op2 = TRITTRUE; /* Declare. Initialize for CYA */
|
||||
|
||||
/* Demo 'not' */
|
||||
for( op1 = TRITTRUE; op1 <= TRITFALSE; ++op1 )
|
||||
{
|
||||
printf("Not %s: %s\n", tritString[op1], tritString[tritNot[op1]]);
|
||||
}
|
||||
demo_binary_op(tritAnd, "And");
|
||||
demo_binary_op(tritOr, "Or");
|
||||
demo_binary_op(tritThen, "Then");
|
||||
demo_binary_op(tritEquiv, "Equiv");
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
36
Task/Ternary-logic/C/ternary-logic-2.c
Normal file
36
Task/Ternary-logic/C/ternary-logic-2.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#include <stdio.h>
|
||||
|
||||
typedef enum { t_F = -1, t_M, t_T } trit;
|
||||
|
||||
trit t_not (trit a) { return -a; }
|
||||
trit t_and (trit a, trit b) { return a < b ? a : b; }
|
||||
trit t_or (trit a, trit b) { return a > b ? a : b; }
|
||||
trit t_eq (trit a, trit b) { return a * b; }
|
||||
trit t_imply(trit a, trit b) { return -a > b ? -a : b; }
|
||||
char t_s(trit a) { return "F?T"[a + 1]; }
|
||||
|
||||
#define forall(a) for(a = t_F; a <= t_T; a++)
|
||||
void show_op(trit (*f)(trit, trit), const char *name) {
|
||||
trit a, b;
|
||||
printf("\n[%s]\n F ? T\n -------", name);
|
||||
forall(a) {
|
||||
printf("\n%c |", t_s(a));
|
||||
forall(b) printf(" %c", t_s(f(a, b)));
|
||||
}
|
||||
puts("");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
trit a;
|
||||
|
||||
puts("[Not]");
|
||||
forall(a) printf("%c | %c\n", t_s(a), t_s(t_not(a)));
|
||||
|
||||
show_op(t_and, "And");
|
||||
show_op(t_or, "Or");
|
||||
show_op(t_eq, "Equiv");
|
||||
show_op(t_imply, "Imply");
|
||||
|
||||
return 0;
|
||||
}
|
||||
55
Task/Ternary-logic/C/ternary-logic-3.c
Normal file
55
Task/Ternary-logic/C/ternary-logic-3.c
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef double half_truth, maybe;
|
||||
|
||||
inline maybe not3(maybe a) { return 1 - a; }
|
||||
|
||||
inline maybe
|
||||
and3(maybe a, maybe b) { return a * b; }
|
||||
|
||||
inline maybe
|
||||
or3(maybe a, maybe b) { return a + b - a * b; }
|
||||
|
||||
inline maybe
|
||||
eq3(maybe a, maybe b) { return 1 - a - b + 2 * a * b; }
|
||||
|
||||
inline maybe
|
||||
imply3(maybe a, maybe b) { return or3(not3(a), b); }
|
||||
|
||||
#define true3(x) ((x) * RAND_MAX > rand())
|
||||
#define if3(x) if (true3(x))
|
||||
|
||||
int main()
|
||||
{
|
||||
maybe roses_are_red = 0.25; /* they can be white or black, too */
|
||||
maybe violets_are_blue = 1; /* aren't they just */
|
||||
int i;
|
||||
|
||||
puts("Verifying flowery truth for 40 times:\n");
|
||||
|
||||
puts("Rose is NOT red:"); /* chance: .75 */
|
||||
for (i = 0; i < 40 || !puts("\n"); i++)
|
||||
printf( true3( not3(roses_are_red) ) ? "T" : "_");
|
||||
|
||||
/* pick a rose and a violet; */
|
||||
puts("Rose is red AND violet is blue:");
|
||||
/* chance of rose being red AND violet being blue is .25 */
|
||||
for (i = 0; i < 40 || !puts("\n"); i++)
|
||||
printf( true3( and3(roses_are_red, violets_are_blue) )
|
||||
? "T" : "_");
|
||||
|
||||
/* chance of rose being red OR violet being blue is 1 */
|
||||
puts("Rose is red OR violet is blue:");
|
||||
for (i = 0; i < 40 || !puts("\n"); i++)
|
||||
printf( true3( or3(roses_are_red, violets_are_blue) )
|
||||
? "T" : "_");
|
||||
|
||||
/* pick two roses; chance of em being both red or both not red is .625 */
|
||||
puts("This rose is as red as that rose:");
|
||||
for (i = 0; i < 40 || !puts("\n"); i++)
|
||||
if3(eq3(roses_are_red, roses_are_red)) putchar('T');
|
||||
else putchar('_');
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue