Advertisement
Guest User

Tautologico Final

a guest
Aug 29th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5.  
  6. #define TRUE    1
  7. #define FALSE   0
  8. // Macro para calcular o valor da implicação.
  9. #define IMPVAL(b1, b2) (b1 && !b2 ? FALSE : TRUE)
  10. // Macro para calcular o valor de uma bi-implicação
  11. #define BIMPVAL(b1, b2) (b1 == b2)
  12. #define VARS   3
  13. #define PINDEX   0
  14. #define QINDEX   1
  15. #define RINDEX   2
  16.  
  17. char name[VARS];     // Variables names.
  18. int I[VARS];         // Variables Interp.
  19.  
  20.  
  21.  
  22.  
  23. // tipo do no da arvore
  24.  
  25. typedef enum tagType {
  26.  
  27.     NEG,
  28.     AND,
  29.     OR,
  30.     IMP,
  31.     BIMP,
  32.     P,
  33.     Q,
  34.     R
  35.  
  36. } Type;
  37. // Formula Logic.
  38.  
  39. typedef struct tagForm{
  40.  
  41.   Type tipo;
  42.  
  43.   struct tagForm *right;
  44.  
  45.   struct tagForm *left;
  46.  
  47. } Formula;
  48.  
  49.  
  50. // Formula Representation
  51.  
  52.  
  53.  
  54.  
  55. void initialize_formula(){
  56.     name[PINDEX] = 'P';
  57.     name[QINDEX] = 'Q';
  58.     name[RINDEX] = 'R';
  59.     int i;
  60.     for (i=0; i < VARS; i++){
  61.         I[i] = FALSE;
  62.  
  63.     }// for loop
  64.  
  65. }// end of initialize_formula function.
  66.  
  67.  
  68.  
  69. Formula* create_formula(Type tipo, Formula *right, Formula *left){
  70.  
  71.   Formula *res = (Formula*) malloc(sizeof(Formula));
  72.  
  73.  
  74.  
  75.   if (res == NULL){
  76.     return NULL;
  77.   }
  78.  
  79.  
  80.   res->tipo = tipo;
  81.  
  82.   res->right = right;
  83.  
  84.   res->left = left;
  85.  
  86.  
  87.  
  88.   return res;
  89.  
  90. }
  91.  
  92.  
  93.  
  94. Formula* neg(Formula *e)
  95.  
  96. {
  97.  
  98.   return create_formula(NEG, e, NULL);
  99.  
  100. }
  101.  
  102.  
  103.  
  104. Formula* and(Formula *d, Formula *e)
  105.  
  106. {
  107.  
  108.   return create_formula(AND, d, e);
  109.  
  110. }
  111.  
  112.  
  113.  
  114. Formula* or(Formula *d, Formula *e)
  115.  
  116. {
  117.  
  118.   return create_formula(OR, d, e);
  119.  
  120. }
  121.  
  122.  
  123.  
  124. Formula* imp(Formula *d, Formula *e)
  125.  
  126. {
  127.  
  128.   return create_formula(IMP, d, e);
  129.  
  130. }
  131.  
  132.  
  133.  
  134. Formula* bimp(Formula *d, Formula *e)
  135.  
  136. {
  137.  
  138.   return create_formula(BIMP, d, e);
  139.  
  140. }
  141.  
  142.  
  143.  
  144. Formula* var_p(){
  145.  
  146.   return create_formula(P, NULL, NULL);
  147.  
  148. }
  149.  
  150.  
  151.  
  152. Formula* var_q(){
  153.  
  154.   return create_formula(Q, NULL, NULL);
  155.  
  156. }
  157.  
  158.  
  159.  
  160. Formula* var_r(){
  161.  
  162.   return create_formula(R, NULL, NULL);
  163.  
  164. }
  165. // Frees the memory space occupied by a formula.
  166.  
  167. void destroy_formula(Formula *f){
  168.  
  169.     if (f != NULL) {
  170.         switch(f->tipo) {
  171.     case NEG:
  172.       destroy_formula(f->right);
  173.       destroy_formula(f->right);
  174.       break;
  175.     default:
  176.         destroy_formula(f->right);
  177.         destroy_formula(f->left);
  178.       break;
  179.  
  180.     // Other cases are variables that have no children.
  181.  
  182.     }
  183.     free(f);
  184.  
  185.   }
  186.  
  187. }
  188.  
  189. int index_variable(Type tipo){
  190.  
  191.   switch(tipo){
  192.     case P:
  193.         return PINDEX;
  194.  
  195.     case Q:
  196.         return QINDEX;
  197.     case R:
  198.         return RINDEX;
  199.     default:   // Other types do not represent variables
  200.         return -1;
  201.   }// switch statement
  202.  
  203. }// end of index_variable function
  204.  
  205.  
  206.  
  207. int value_formula(Formula *f){
  208.  
  209.   switch(f->tipo) {
  210.     case P:
  211.     case Q:
  212.     case R:
  213.         return I[index_variable(f->tipo)];
  214.     case NEG:
  215.         return !value_formula(f->right);
  216.     case AND:
  217.         return value_formula(f->right) && value_formula(f->left);
  218.     case OR:
  219.         return value_formula(f->right) || value_formula(f->left);
  220.     case IMP:
  221.         return IMPVAL(value_formula(f->right), value_formula(f->left));
  222.     case BIMP:
  223.         return BIMPVAL(value_formula(f->right),value_formula(f->left));
  224.   }// switch statement
  225.  
  226. }// end of value formula
  227. // retorna TRUE se a interpretacao atual eh a ultima na tabela-verdade
  228.  
  229. int last_interpretation(){
  230.  
  231.   int res = 1;
  232.   int c;
  233.   for (c=0; c < VARS; c++) {
  234.     res = res && I[c];
  235.   }// for loop
  236.  
  237.   return res;
  238.  
  239. }// end of last_interpretation function
  240.  
  241. //change the current interpretation in the array I [] to the next in the order of truth table
  242.  
  243. void next_interpretation(){
  244.  
  245.   int c = VARS - 1;
  246.   while (c >= 0 && I[c] != 0) {
  247.     I[c--] = 0;
  248.   }// while statement
  249.   if (c >= 0){
  250.     I[c] = 1;
  251.   }// if statement
  252. }// end of next_interpretation function
  253.  
  254. void show_table(Formula *f){
  255.   int end = FALSE;
  256.   initialize_formula();
  257.   printf("Formula:\n");
  258.   printf("H = (P -> Q) /\\ (~Q \\/ R)\n\n");
  259.   int c = 0;
  260.   for (; c < VARS; c++) {
  261.     printf(" %c |", name[c]);
  262.  
  263.   }// for loop
  264.  
  265.   printf(" H\n");
  266.  int d = 0;
  267.   for (; d < 4 * VARS + 3; d++){
  268.     printf("-");
  269.   }// for loop
  270.   printf("\n");
  271.   while (!end){
  272.     // imprime valores atuais das variaveis
  273.     int c = 0;
  274.     for ( ; c < VARS; c++) {
  275.       if (I[c]){
  276.         printf(" T |");
  277.       }// if statement
  278.       else{
  279.         printf(" F |");
  280.       }// else statement
  281.  
  282.     }// for loop
  283.  
  284.     // calcula e imprime o valor da formula
  285.     if (value_formula(f)){
  286.       printf(" T\n");
  287.     }// if statement
  288.     else{
  289.       printf(" F\n");
  290.     }// else statement
  291.     // verifica se acabou a tabela ou passa para
  292.     // a proxima linha
  293.     if (last_interpretation()){
  294.       end = TRUE;
  295.     }// if statement
  296.     else{
  297.       next_interpretation();
  298.     }// else statement
  299.  
  300.   }//while loop
  301.  
  302. }// end of show_table
  303.  
  304.  
  305.  
  306. int main(int argc, char **argv){
  307.   // (P -> Q) /\ (~Q \/ R)
  308.   printf("Bem vindo a calculadora de Tabela Verdade!\n\n");
  309.   printf("Professor:\n\tAndrei Formiga\n");
  310.   printf("Alunos do Projeto:\n"
  311.          "\tThiago Filipe Soares da Rocha\n"
  312.          "\tJoao Vitor Oliveira dos Anjos\n"
  313.          "\tJoao Vinicius Gomes de Lima\n\n");
  314.  
  315.   printf("Data: 29/08/2016\n\n"
  316.          "Programa com proposito de calcular a tabela verdade de uma determinada formula.\n\n\n");
  317.   Formula *f = and(imp(var_p(), var_q()),or(neg(var_q()), var_r()));
  318.   show_table(f);
  319.   destroy_formula(f);
  320.   return 0;
  321.  
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement