高分求:设计一个程序,验证一个表达式是不是命题公式。

cbc 2009-09-27 09:21:48
离散数学中的一个编程题:

设计一个程序,验证一个表达式是不是命题公式。
...全文
351 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zl10281049 2011-04-28
  • 打赏
  • 举报
回复
上述源代码中还应加入头文件#include<ctype.h>
Paradin 2009-09-29
  • 打赏
  • 举报
回复

#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)))) )

cbc 2009-09-28
  • 打赏
  • 举报
回复
为了方便大家答题,给出命题公式定义。

合式的公式
(1)单个命题变元是合式的公式。
(2)如果A是合式的公式,那么¬ A是合式的公式。
(3)如果A和B均是合式的公式,那么A∧B ,A∨B ,
A→B,A«B 都是合式的公式。
(4)当且仅当有限次的应用(1)、(2)、(3)条规则由逻辑联结词、圆括号所组成的有意义的符号串是合式的公式。
cbc 2009-09-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 paradin 的回复:]
C/C++ code
#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 ')'//公式树节点typedefstr¡­
[/Quote]

这个程序还有些问题,比如“p&q"是命题公式,但在你的程序里面结论是”不是公式“
Paradin 2009-09-28
  • 打赏
  • 举报
回复

#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)))) ))

coohai 2009-09-27
  • 打赏
  • 举报
回复
兄弟,把这发C语言版块啊.
cbc 2009-09-27
  • 打赏
  • 举报
回复
要求用C语言实现

21,497

社区成员

发帖
与我相关
我的任务
社区描述
汇编语言(Assembly Language)是任何一种用于电子计算机、微处理器、微控制器或其他可编程器件的低级语言,亦称为符号语言。
社区管理员
  • 汇编语言
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧