16,472
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXL 255
#define MAX_PRI 255
#define NOT '~' //非
#define AND '&' //与
#define OR '|' //或
#define IMP - //蕴含
#define EQ = //等值
#define LB '('
#define RB ')'
#define NUL 0
typedef struct tagFormNode
{
int type;
struct tagFormNode *left;
struct tagFormNode *right;
} FormNode;
int priority(int ch)
{
switch (ch)
{
case '~': return 5;
case '&': return 4;
case '|': return 3;
case '-': return 2;
case '=': return 1;
default: return 0;
}
}
FormNode *new_node()
{
FormNode *p;
if ((p = (FormNode*)malloc(sizeof(FormNode))) == NULL)
{
printf("malloc heap memory failed.");
exit(-1);
}
p->left = p->right = 0;
return p;
}
int check_paren(const char *formular, int start, int end, int *wrapped)
{
int np = 0, i, flag = 0;
for (i = start; i <= end; ++i)
{
if (formular[i] == '(')
++np;
else if (formular[i] == ')')
--np;
if (np < 0)
return 0;
if (np == 0 && i < end)
flag = 1;
}
if (np != 0) return 0;
*wrapped = 0;
if (formular[start] == '(' && formular[end] == ')' && !flag)
*wrapped = 1;
return 1;
}
int findOp(const char *formular, int start, int end, int *op)
{
int i, ch, pri, tmp, pos, np = 0;
*op = NUL;
pri = MAX_PRI;
for (i = start; i <= end; ++i)
{
ch = formular[i];
if (ch == '(')
++np;
else if (ch == ')')
--np;
if (np == 0)
if ((tmp = priority(ch)) > 0 && tmp < pri)
{
pri = tmp;
*op = ch;
pos = i;
}
}
if (*op == NUL) return -1;
if (*op == NOT)
if (formular[start] == NOT)
pos = start;
else
{
*op = NUL;
return -1;
}
return pos;
}
int setup(const char *formular, int start, int end, FormNode *pNode)
{
int i, wrapped, op, pos;
if (start > end) return 0;
if (start == end)
{
if (isalpha(formular[start]))
{
pNode->type = formular[start];
return 1;
} else
return 0;
}
if (!check_paren(formular, start, end, &wrapped))
{
printf("check_paren failed\n");
return 0;
}
if (wrapped)
{
//printf("wrapped\n");
return setup(formular, start+1, end-1, pNode);
}
if ((pos = findOp(formular, start, end, &op)) >= 0)
{
pNode->type = op;
pNode->left = new_node();
if (op == NOT)
return setup(formular, start+1, end, pNode->left);
else
{
if (!setup(formular, start, pos-1, pNode->left))
return 0;
pNode->right = new_node();
return setup(formular, pos+1, end, pNode->right);
}
}
return 0;
}
void deWhite(char *str)
{
int i, j;
for (i = j = 0; str[j] != '\0'; ++j)
{
if (!isspace(str[j]))
str[i++] = str[j];
}
str[i] = '\0';
}
void destroy(FormNode *pNode)
{
if (pNode == NULL)
return;
destroy(pNode->left);
destroy(pNode->right);
free(pNode);
}
int main(int argc, char *argv[])
{
char formular[MAXL];
FormNode *root = new_node();
fgets(formular, MAXL, stdin);
deWhite(formular);
printf("%s\n", formular);
if (setup(formular, 0, strlen(formular)-1, root))
printf("是公式\n");
else
printf("不是公式\n");
destroy(root);
system("PAUSE");
return 0;
}
//(C)
//A&B
//~~~~(~~~A)
//(A| ( ~(B|(~(C|(D&E))))) )
//((~P)=((P|((~Q)&R))-(~R)))
//~((~(~P))=((P|((~Q) &R))-(~ (~ (~R)))) )
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXL 255
#define MAX_PRI 255
#define NOT '~' //非
#define AND '&' //与
#define OR '|' //或
#define IMP - //蕴含
#define EQ = //等值
#define LB '('
#define RB ')'
#define NUL 0
typedef struct tagFormNode
{
int type;
struct tagFormNode *left;
struct tagFormNode *right;
} FormNode;
int priority(int ch)
{
switch (ch)
{
case '~': return 5;
case '&': return 4;
case '|': return 3;
case '-': return 2;
case '=': return 1;
default: return 0;
}
}
FormNode *new_node()
{
FormNode *p;
if ((p = (FormNode*)malloc(sizeof(FormNode))) == NULL)
{
printf("malloc heap memory failed.");
exit(-1);
}
return p;
}
int check_paren(const char *formular, int start, int end, int *wrapped)
{
int np = 0, i, flag = 0;
for (i = start; i <= end; ++i)
{
if (formular[i] == '(')
++np;
else if (formular[i] == ')')
--np;
if (np < 0)
return 0;
if (np == 0 && i < end)
flag = 1;
}
if (np != 0) return 0;
*wrapped = 0;
if (formular[start] == '(' && formular[end] == ')' && !flag)
*wrapped = 1;
return 1;
}
int findOp(const char *formular, int start, int end, int *op)
{
int i, ch, pri, tmp, pos, np = 0;
*op = NUL;
pri = MAX_PRI;
for (i = start; i <= end; ++i)
{
ch = formular[i];
if (ch == '(')
++np;
else if (ch == ')')
--np;
if (np == 0)
if ((tmp = priority(ch)) > 0 && tmp < pri)
{
pri = tmp;
*op = ch;
pos = i;
}
}
if (*op == NUL) return -1;
if (*op == NOT)
if (formular[start] == NOT)
pos = start;
else
{
*op = NUL;
return -1;
}
return pos;
}
int setup(const char *formular, int start, int end, FormNode *pNode)
{
int i, wrapped, op, pos;
if (start > end) return 0;
if (start == end)
{
if (isalpha(formular[start]))
{
pNode->type = formular[start];
return 1;
} else
return 0;
}
if (!check_paren(formular, start, end, &wrapped))
{
printf("check_paren failed\n");
return 0;
}
if (wrapped)
{
//printf("wrapped\n");
return setup(formular, start+1, end-1, pNode);
}
if ((pos = findOp(formular, start, end, &op)) >= 0)
{
pNode->type = op;
pNode->left = new_node();
if (op == NOT)
return setup(formular, start+1, end, pNode->left);
else
{
if (!setup(formular, start, pos-1, pNode->left))
return 0;
pNode->right = new_node();
return setup(formular, pos+1, end, pNode->right);
}
}
return 0;
}
void deWhite(char *str)
{
int i, j;
for (i = j = 0; str[j] != '\0'; ++j)
{
if (!isspace(str[j]))
str[i++] = str[j];
}
str[i] = '\0';
}
int main(int argc, char *argv[])
{
char formular[MAXL];
FormNode *root = new_node();
fgets(formular, MAXL, stdin);
deWhite(formular);
printf("%s\n", formular);
if (setup(formular, 0, strlen(formular)-1, root))
printf("是公式\n");
else
printf("不是公式\n");
system("PAUSE");
return 0;
}
//(C)
//A&B
//~~~~(~~~A)
//(A| ( ~(B|(~(C|(D&E))))) )
//((~P)=((P|((~Q)&R))-(~R)))
//~((~(~P))=((P|((~Q) &R))-(~ (~ (~R)))) )
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXL 255
#define NOT '~'
#define AND '&'
#define OR '|'
#define IMP '-'
#define EQ '='
#define LP '('
#define RP ')'
//公式树节点
typedef struct tagFormNode
{
int type;
struct tagFormNode *left;
struct tagFormNode *right;
} FormNode;
char form[MAXL];
int idx = -1;
FormNode *formTree;
FormNode *new_node()
{
FormNode *p = (FormNode*)malloc(sizeof(FormNode));
if (p == NULL)
{
printf("malloc failed.\n");
exit(-1);
}
p->left = p->right = NULL;
return p;
}
//打印树对应的公式
void printForm(FormNode *pNode)
{
if (pNode == NULL)
return;
if (isalpha(pNode->type))
printf("%c", pNode->type);
else
{
printf("%c", LP);
if (pNode->type == NOT)
{
printf("%c", NOT);
printForm(pNode->left);
}
else
{
printForm(pNode->left);
printf("%c", pNode->type);
printForm(pNode->right);
}
printf("%c", RP);
}
}
//打印所有子公式
void printChild(FormNode *pNode)
{
if (pNode == NULL)
return;
printChild(pNode->left);
printChild(pNode->right);
printForm(pNode);
printf("\n");
}
//去除蕴含
void deImply(FormNode *pNode)
{
if (pNode == NULL)
return;
deImply(pNode->left);
deImply(pNode->right);
if (pNode->type == IMP)
{
FormNode *pNot = new_node();
pNot->type = NOT;
pNot->left = pNode->left;
pNode->left = pNot;
pNode->type = OR;
}
}
//释放
void destroy(FormNode *pNode)
{
if (pNode == NULL)
return;
free(pNode->left);
free(pNode->right);
free(pNode);
}
//去除多重否定
void deMultiNot(FormNode *pNode)
{
FormNode *p, *q, *s;
int i, n;
if (pNode->left != NULL)
{
p = pNode->left;
n = 0;
while (p->type == NOT)
{
++n;
q = p;
p = p->left;
}
if (n > 0)
{
if (n % 2 == 0)
pNode->left = p;
else
pNode->left = q;
}
deMultiNot(pNode->left);
}
if (pNode->right != NULL)
{
p = pNode->right;
n = 0;
while (p->type == NOT)
{
++n;
q = p;
p = p->left;
}
if (n > 0)
{
if (n % 2 == 0)
pNode->right = p;
else
pNode->right = q;
}
deMultiNot(pNode->right);
}
}
//去除多重否定
FormNode *deNot(FormNode *pTree)
{
if (pTree->type == NOT)
{
FormNode *pRoot = new_node();
pRoot->left = pTree;
deMultiNot(pRoot);
pTree = pRoot->left;
free(pRoot);
}
else
deMultiNot(pTree);
return pTree;
}
int nextch()
{
int ch;
while ((ch = form[++idx]) == ' ' || ch == '\t');
return ch;
}
int prenext()
{
int ch, k = idx;
while ((ch = form[++k]) == ' ' || ch == '\t');
return ch;
}
//判断是否是公式并建立公式树
int Formular(FormNode *pNode)
{
int ch = nextch();
if (ch == '\0')
return 0;
if (isalpha(ch))
{
pNode->type = ch;
return 1;
}
if (ch == LP)
{
pNode->left = new_node();
if (prenext() == NOT)
{
pNode->type = NOT;
nextch();
if (!Formular(pNode->left))
return 0;
}
else
{
if (!Formular(pNode->left))
return 0;
ch = nextch();
if (!(ch == OR || ch == AND || ch == IMP || ch == EQ))
return 0;
else
{
pNode->type = ch;
pNode->right = new_node();
}
if (!Formular(pNode->right))
return 0;
}
return (nextch() == RP);
}
else
return 0;
}
//初始化
void init()
{
fgets(form, MAXL, stdin);
form[strlen(form)-1] = '\0';
formTree = new_node();
}
int main(int argc, char *argv[])
{
init();
int isform;
isform = Formular(formTree);
if (nextch() != '\0')
isform = 0;
if (isform)
{
printf("是公式, 所有子公式:\n");
printChild(formTree);
printf("\n\n");
printf("去除双重否定:");
formTree = deNot(formTree);
printForm(formTree);
printf("\n\n");
printf("去除蕴含:");
deImply(formTree);
printForm(formTree);
printf("\n\n");
}
else
{
printf("不是公式\n\n");
}
destroy(formTree);
system("PAUSE");
return 0;
}
//(A| ( ~(B|(~(C|(D&E))))) )
//((~P)=((P|((~Q)&R))-(~R)))
//(~((~(~P))=((P|((~Q) &R))-(~ (~ (~R)))) ))