Ir ao conteúdo
  • Cadastre-se

Nicole Yamashita

Membro Júnior
  • Posts

    1
  • Cadastrado em

  • Última visita

Reputação

0
  1. Poderiam por gentileza me falar o que cada int e o que cada void faz, o que cada linha faz? pois preciso entender esse código e não estou conseguindo, quem puder colocar em forma de comentário, agradeceria MUITO. Este programa pega 2 formulas lógicas dadas pelo usuário e mostra a tabela verdade. #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <math.h> #define MAX_STR 10000 #define TYPE_VAR 0 #define TYPE_OP 1 #define NO_VALUE -1 char binaryOperators[] = {'+', '*', '>', '='}; int operationBinary(char op, int a, int { if (op == '+') { if (a==0 && b==0) return 0; return 1; } else if (op == '*') { if (a==1 && b==1) return 1; return 0; } else if (op == '>') { //implicação if (a==0 && b==1) return 1; return 0; } else if (op == '=') { //bi-implicacao if (a== return 0; return 1; } return 0; } int operationUnary(char op, int a) { if (op == '\'') { if (a==0) return 1; return 0; } return 0; } typedef struct _node { // estrutura nó, pois precisamos de dois ponteiros o ponteiro left e right para armazenar as iformaçøes que os nós deve conter. int type; char symbol; char value; struct _node *left; struct _node *right; } node; node *head = NULL; int strIndex = 0; node *createNode() { node *n = (node *) malloc(sizeof(node)); n->left = NULL; n->right = NULL; n->value = NO_VALUE; return n; } node *createNode2(char c) { node *pont = createNode(); if (!isalpha©) pont->type = TYPE_OP; else pont->type = TYPE_VAR; pont->symbol = c; return pont; } void setNoValues (node *pont) { pont->value = NO_VALUE; if (pont->left != NULL) setNoValues(pont->left); if (pont->right != NULL) setNoValues(pont->right); } int isBinaryOperator(char p) { int i; for (i=0; i < (int)sizeof(binaryOperators)/sizeof(char); i++) if (p==binaryOperators) return 1; return 0; } int find(char c, char *str) { int len = strlen(str); int i; for(i=0; i<len; i++) { if (str == c) return i; } return -1; } node *insertBinaryOperator(node *pont, char p) { node *newP = createNode2(p); if (pont->type==TYPE_OP && isBinaryOperator(pont->symbol)) { if (pont->right == NULL) { //newP->left = pont; pont->right = newP; } } else { newP->left = pont->left; pont->left = newP; } return newP; } node *insertVariable(node *pont, char p) { node *newP = createNode2(p); while (pont->left!=NULL && !(isBinaryOperator(pont->symbol) && pont->right==NULL)) { pont = pont->left; } if(pont->left == NULL) { pont->left = newP; } else if(pont->right == NULL) { pont->right = newP; } return newP; } node *insertUnaryOperator(node *pont, char p) { node *newP = createNode2(p); if (pont->left == NULL) { pont->left = newP; } else { pont->right = newP; } return newP; } void recursiveParser (node *pont, char *p) { strIndex++; node *newP = NULL; if (p[strIndex] == ' ') { recursiveParser(pont, p); } else if (isalpha(p[strIndex])) { newP = insertVariable(pont, p[strIndex]); //ret = recursiveParser(pont, p); } else if (isBinaryOperator(p[strIndex])) { newP = insertBinaryOperator(pont, p[strIndex]); if (newP->right==NULL) recursiveParser(newP, p); } else if (p[strIndex] == '(') { int temp = strIndex; newP = insertUnaryOperator(pont, p[strIndex]); while (p[strIndex+1]!=')') { recursiveParser(newP, p); } strIndex++; } else if (p[strIndex] == ' ') { recursiveParser(pont, p); } else if (p[strIndex] == '\0' || p[strIndex] == '\n') { return; } else { newP = insertUnaryOperator(pont, p[strIndex]); recursiveParser(newP, p); } } void parser (char *str) { head = createNode2('('); strIndex = -1; int len = strlen(str)-2; while (strIndex < len) { recursiveParser(head, str); } } void processExpression(node *pont, char *variables, int *values) { if (pont->left!=NULL) { processExpression(pont->left, variables, values); } if (pont->type == TYPE_VAR) { int pos = find(pont->symbol, variables); if (pos == -1) printf ("ERRO 1!!! O programa não funcionará corretamente!\n"); else { pont->value = values[pos]; } } else if (pont->type == TYPE_OP) { if (pont->left->value == NO_VALUE) printf ("ERRO 2!!! O programa não funcionará corretamente!\n"); if (isBinaryOperator(pont->symbol)) { if (pont->right==NULL) printf ("ERRO 3!!! O programa não funcionará corretamente!\n"); else { processExpression(pont->right, variables, values); } pont->value = operationBinary (pont->symbol, pont->left->value, pont->right->value); } else { //Operadores unários if (pont->right!=NULL) printf ("ERRO 4!!! O programa não funcionará corretamente!\n"); if (pont->symbol == '(') { pont->value = pont->left->value; } else { pont->value = operationUnary (pont->symbol, pont->left->value); } } } } void listVariables (char *str, char *variables) { int len = strlen(str); int i; variables[0] = '\0'; for(i=0; i<len; i++) { if (!isalpha(str)) continue; if(find(str, variables) == -1) { sprintf(variables, "%s%c", variables, str); } } } int *listValues (char *variablesList) { int len = strlen(variablesList); int *values = (int *) malloc(len*sizeof(int)); int i; for(i=0; i<len; i++) { values = 0; } return values; } void incrementValues (int *values, int len) { int i; for(i=len-1; i>=0; i--) { if (values==1) { values = 0; } else { values = 1; break; } } } void printTable(char *str, char *variables) { int *values = listValues(variables); int len = strlen(variables); int lines = (int)pow(2, len); int i; printf("TABELA VERDADE DA EXPRESSÃO \"%s\"\n", str); for(i=0; i<len; i++) { printf("%c ", variables); } printf(" EXP\n"); for(i=0; i<lines; i++) { int j; for (j=0; j<len; j++) { printf("%d ", values[j]); } processExpression(head, variables, values); printf ("=>%d", head->value); printf("\n"); incrementValues(values, len); setNoValues(head); } free (values); } int main (void) { int i[10], x; char str[MAX_STR]; char variables[MAX_STR]; for (x = 0; x<2; x++){ //head = NULL; printf (" 0 = True\n 1 = False\n"); printf (" ==============================\n Use ' para negação\n Use + para E\n Use * para ou\n Use > para implicação\n Use = para Bi implicação\n ==============================\n"); printf ("Digite a expressão:\n"); fgets(str, MAX_STR, stdin); listVariables(str, variables); parser(str); printTable(str, variables); printf ("\n\n"); } }

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...