Task: Ternary logic

C: #include <stdio.h>
 
typedef enum {
  TRITTRUE,  
  TRITMAYBE, 
  TRITFALSE  
} trit;
 

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 } };



const char* tritString[3] = {"T", "?", "F"};

void demo_binary_op(trit operator[3][3], const char* name)
{
  trit operand1 = TRITTRUE; 
  trit operand2 = TRITTRUE; 

  
  printf("\n");

  
  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; 
  trit op2 = TRITTRUE; 
 
  
  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;
}


C++: #include <iostream>
#include <stdlib.h>

class trit {
public:
    static const trit False, Maybe, True;

    trit operator !() const {
        return static_cast<Value>(-value);
    }

    trit operator &&(const trit &b) const {
        return (value < b.value) ? value : b.value;
    }

    trit operator ||(const trit &b) const {
        return (value > b.value) ? value : b.value;
    }

    trit operator >>(const trit &b) const {
        return -value > b.value ? static_cast<Value>(-value) : b.value;
    }

    trit operator ==(const trit &b) const {
        return static_cast<Value>(value * b.value);
    }

    char chr() const {
        return "F?T"[value + 1];
    }

protected:
    typedef enum { FALSE=-1, MAYBE, TRUE } Value;

    Value value;

    trit(const Value value) : value(value) { }
};

std::ostream& operator<<(std::ostream &os, const trit &t)
{
    os << t.chr();
    return os;
}

const trit trit::False = trit(trit::FALSE);
const trit trit::Maybe = trit(trit::MAYBE);
const trit trit::True = trit(trit::TRUE);

int main(int, char**) {
    const trit trits[3] = { trit::True, trit::Maybe, trit::False };

#define for_each(name) \
    for (size_t name=0; name<3; ++name)

#define show_op(op) \
    std::cout << std::endl << #op << " "; \
    for_each(a) std::cout << ' ' << trits[a]; \
    std::cout << std::endl << "  -------"; \
    for_each(a) { \
        std::cout << std::endl << trits[a] << " |"; \
        for_each(b) std::cout << ' ' << (trits[a] op trits[b]); \
    } \
    std::cout << std::endl;

    std::cout << "! ----" << std::endl;
    for_each(a) std::cout << trits[a] << " | " << !trits[a] << std::endl;

    show_op(&&);
    show_op(||);
    show_op(>>);
    show_op(==);
    return EXIT_SUCCESS;
}