Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define TRUE 1
- #define FALSE 0
- // Macro para calcular o valor da implicação.
- #define IMPVAL(b1, b2) (b1 && !b2 ? FALSE : TRUE)
- // Macro para calcular o valor de uma bi-implicação
- #define BIMPVAL(b1, b2) (b1 == b2)
- #define VARS 3
- #define PINDEX 0
- #define QINDEX 1
- #define RINDEX 2
- char name[VARS]; // Variables names.
- int I[VARS]; // Variables Interp.
- // tipo do no da arvore
- typedef enum tagType {
- NEG,
- AND,
- OR,
- IMP,
- BIMP,
- P,
- Q,
- R
- } Type;
- // Formula Logic.
- typedef struct tagForm{
- Type tipo;
- struct tagForm *right;
- struct tagForm *left;
- } Formula;
- // Formula Representation
- void initialize_formula(){
- name[PINDEX] = 'P';
- name[QINDEX] = 'Q';
- name[RINDEX] = 'R';
- int i;
- for (i=0; i < VARS; i++){
- I[i] = FALSE;
- }// for loop
- }// end of initialize_formula function.
- Formula* create_formula(Type tipo, Formula *right, Formula *left){
- Formula *res = (Formula*) malloc(sizeof(Formula));
- if (res == NULL){
- return NULL;
- }
- res->tipo = tipo;
- res->right = right;
- res->left = left;
- return res;
- }
- Formula* neg(Formula *e)
- {
- return create_formula(NEG, e, NULL);
- }
- Formula* and(Formula *d, Formula *e)
- {
- return create_formula(AND, d, e);
- }
- Formula* or(Formula *d, Formula *e)
- {
- return create_formula(OR, d, e);
- }
- Formula* imp(Formula *d, Formula *e)
- {
- return create_formula(IMP, d, e);
- }
- Formula* bimp(Formula *d, Formula *e)
- {
- return create_formula(BIMP, d, e);
- }
- Formula* var_p(){
- return create_formula(P, NULL, NULL);
- }
- Formula* var_q(){
- return create_formula(Q, NULL, NULL);
- }
- Formula* var_r(){
- return create_formula(R, NULL, NULL);
- }
- // Frees the memory space occupied by a formula.
- void destroy_formula(Formula *f){
- if (f != NULL) {
- switch(f->tipo) {
- case NEG:
- destroy_formula(f->right);
- destroy_formula(f->right);
- break;
- default:
- destroy_formula(f->right);
- destroy_formula(f->left);
- break;
- // Other cases are variables that have no children.
- }
- free(f);
- }
- }
- int index_variable(Type tipo){
- switch(tipo){
- case P:
- return PINDEX;
- case Q:
- return QINDEX;
- case R:
- return RINDEX;
- default: // Other types do not represent variables
- return -1;
- }// switch statement
- }// end of index_variable function
- int value_formula(Formula *f){
- switch(f->tipo) {
- case P:
- case Q:
- case R:
- return I[index_variable(f->tipo)];
- case NEG:
- return !value_formula(f->right);
- case AND:
- return value_formula(f->right) && value_formula(f->left);
- case OR:
- return value_formula(f->right) || value_formula(f->left);
- case IMP:
- return IMPVAL(value_formula(f->right), value_formula(f->left));
- case BIMP:
- return BIMPVAL(value_formula(f->right),value_formula(f->left));
- }// switch statement
- }// end of value formula
- // retorna TRUE se a interpretacao atual eh a ultima na tabela-verdade
- int last_interpretation(){
- int res = 1;
- int c;
- for (c=0; c < VARS; c++) {
- res = res && I[c];
- }// for loop
- return res;
- }// end of last_interpretation function
- //change the current interpretation in the array I [] to the next in the order of truth table
- void next_interpretation(){
- int c = VARS - 1;
- while (c >= 0 && I[c] != 0) {
- I[c--] = 0;
- }// while statement
- if (c >= 0){
- I[c] = 1;
- }// if statement
- }// end of next_interpretation function
- void show_table(Formula *f){
- int end = FALSE;
- initialize_formula();
- printf("Formula:\n");
- printf("H = (P -> Q) /\\ (~Q \\/ R)\n\n");
- int c = 0;
- for (; c < VARS; c++) {
- printf(" %c |", name[c]);
- }// for loop
- printf(" H\n");
- int d = 0;
- for (; d < 4 * VARS + 3; d++){
- printf("-");
- }// for loop
- printf("\n");
- while (!end){
- // imprime valores atuais das variaveis
- int c = 0;
- for ( ; c < VARS; c++) {
- if (I[c]){
- printf(" T |");
- }// if statement
- else{
- printf(" F |");
- }// else statement
- }// for loop
- // calcula e imprime o valor da formula
- if (value_formula(f)){
- printf(" T\n");
- }// if statement
- else{
- printf(" F\n");
- }// else statement
- // verifica se acabou a tabela ou passa para
- // a proxima linha
- if (last_interpretation()){
- end = TRUE;
- }// if statement
- else{
- next_interpretation();
- }// else statement
- }//while loop
- }// end of show_table
- int main(int argc, char **argv){
- // (P -> Q) /\ (~Q \/ R)
- printf("Bem vindo a calculadora de Tabela Verdade!\n\n");
- printf("Professor:\n\tAndrei Formiga\n");
- printf("Alunos do Projeto:\n"
- "\tThiago Filipe Soares da Rocha\n"
- "\tJoao Vitor Oliveira dos Anjos\n"
- "\tJoao Vinicius Gomes de Lima\n\n");
- printf("Data: 29/08/2016\n\n"
- "Programa com proposito de calcular a tabela verdade de uma determinada formula.\n\n\n");
- Formula *f = and(imp(var_p(), var_q()),or(neg(var_q()), var_r()));
- show_table(f);
- destroy_formula(f);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement