#include "stdio.h"#include "stdlib.h"#include "string.h"#include "math.h"#define N 50void pd(int b[N],int f);int H1 (char T1[N], char T2[N], int T3[N], int y);int H2 (char T1[N], char T2[N], int T3[N], int y);int main(){int i1,i2,d=1,T3[N],kh=0,jg,j=0,y;int w=0,hequ[N],h=0,x=0,xiqu[N];char T1[N],T2[N],T10[N],s;hequ[0]=-1;xiqu[0]=-1;printf("#########################################\n"); printf("## 用!表示否定 ##\n");printf("## 用&表示合取 ##\n");printf("## 用|表示析取 ##\n");printf("## 用^表示条件 ##\n");printf("## 用~表示双条件 ##\n");printf("#########################################\n\n"); printf("请输入一个合法的命题公式:\n");gets(T1);strcpy(T10,T1);for(i1=0;i1<strlen(T1);i1++){if(T1[i1]==')' || T1[i1]=='(')kh++;if(T1[i1]>='a' && T1[i1]<='z' || T1[i1]>='A' && T1[i1]<='Z') {for(i2=0;i2<j;i2++)if(T2[i2]==T1[i1])d=0;if(d==1){T2[j]=T1[i1];j++;}d=1;}}1printf("\n输出真值表如下:\n \n");for(i1=0;i1<y;i1++)printf(" %c ",T2[i1]);printf(" ");puts(T1);printf("\n");for(i1=0;i1<j;i1++)T3[i1]=0;for(i2=0;i2<j;i2++)printf(" %d ",T3[i2]);jg=H1(T1,T2,T3,y);if(jg==0)hequ[h++]=w;elsexiqu[x++]=w;printf(" %d\n",jg);strcpy(T1,T10);for(i1=0;i1<(int)pow(2,j)-1;i1++) {++w;pd(T3,j-1);jg=H1(T1,T2,T3,y);if(jg==0)hequ[h++]=w;elsexiqu[x++]=w;strcpy(T1,T10);for(i2=0;i2<j;i2++)printf(" %d ",T3[i2]);printf(" %d\n",jg);}if(hequ[0]==-1)printf("\n该命题公式不存在主合取范式。
\n"); else{printf("\n主合取范式:\n\t");for(i1=0;i1<h;i1++){if (i1>0)printf("/\\");printf("M(%d)",hequ[i1]);}}if(xiqu[0]==-1)2printf("\n该命题公式不存在主析取范式。
\n"); else{printf("\n\n主析取范式:\n\t");for(i1=0;i1<x;i1++){if (i1>0)printf("\\/");printf("m(%d)",xiqu[i1]);}}printf("\n");getch();}void pd(int b[N],int f) {int i;i=f;if(b[f]==0)b[f]=1;else{b[f]=0;pd(b,--i);}}int H1 (char T1[N],char T2[N],int T3[N],int y) {int i,j,h,s,kh=0,wz[N],a;char xs1[N],ckh[N];s=strlen(T1);for(i=0;i<s;i++)if(T1[i]=='(' || T1[i]==')'){wz[kh]=i;ckh[kh]=T1[i];kh++;}if(kh==0)return H2(T1,T2,T3,y);else{for(i=0;i<kh;i++)if(ckh[i]==')')3break;for(j=wz[i-1]+1,h=0;j<wz[i];j++,h++) xs1[h]=T1[j];xs1[h]='\0';a=H2(xs1,T2,T3,y);if(a==1)T1[wz[i-1]]=1;elseT1[wz[i-1]]=-2;for(j=wz[i-1]+1;j<s+wz[i-1]-wz[i];j++) T1[j]=T1[j+wz[i]-wz[i-1]];T1[j]='\0';return H1(T1,T2,T3,y);}}int H2(char T1[N],char T2[N],int T3[N],int y){int i,h=0,j=0,j1=0,j2=0,j3=0,j4=0,j5=0,i1,i2,p1=-1,p2=-1,s; char dt[N];s=strlen(T1);if(s==1)if(T1[0]==-2)return 0;elsereturn 1;else{for(i=0;i<s-j;i++)if(T1[i]=='!'){for(i1=0;i1<y;i1++)if(T1[i+1]==T2[i1])p1=T3[i1];if(T1[i+1]==-2)p1=0;if(p1==-1)p1=T1[i+1];dt[j+2]=!p1;T1[i]=j+2;j++;p1=0;for(i1=i+1;i1<s-j;i1++) T1[i1]=T1[i1+1];}4p1=-1;j1=j;for(i=0;i<s-j1-2*j2;i++) if(T1[i]=='&'){for(i1=0;i1<y;i1++){if(T1[i-1]==T2[i1])p1=T3[i1];if(T1[i+1]==T2[i1])p2=T3[i1];}for(i2=2;i2<j+2;i2++) {if(T1[i-1]==i2)p1=dt[i2];if(T1[i+1]==i2)p2=dt[i2];}if(T1[i-1]==-2)p1=0;if(T1[i+1]==-2)p2=0;if(p1==-1)p1=(int)(T1[i-1]);if(p2==-1)p2=(int)(T1[i+1]);dt[j+2]=p1 && p2;T1[i-1]=j+2;j++;j2++;p1=-1;p2=-1;for(i1=i;i1<s-j1-2*j2;i1++) T1[i1]=T1[i1+2];i=i-1;}for(i=0;i<s-j1-2*j2-2*j3;i++) if(T1[i]=='|'){for(i1=0;i1<y;i1++) {if(T1[i-1]==T2[i1]) if(T1[i+1]==T2[i1]) 5p2=T3[i1];}for(i2=2;i2<j+2;i2++) {if(T1[i-1]==i2)p1=dt[i2];if(T1[i+1]==i2)p2=dt[i2];}if(T1[i-1]==-2)p1=0;if(T1[i+1]==-2)p2=0;if(p1==-1)p1=T1[i-1];if(p2==-1)p2=T1[i+1];dt[j+2]=p1 || p2;T1[i-1]=j+2;j++;j3++;p1=-1;p2=-1;for(i1=i;i1<s-j1-2*j2-2*j3;i1++) T1[i1]=T1[i1+2];i--;}for(i=0;i<s-j1-2*j2-2*j3-2*j4;i++) if(T1[i]=='^'){for(i1=0;i1<y;i1++){if(T1[i-1]==T2[i1])p1=T3[i1];if(T1[i+1]==T2[i1])p2=T3[i1];}for(i2=2;i2<j+2;i2++){if(T1[i-1]==i2)p1=dt[i2];if(T1[i+1]==i2)p2=dt[i2];}6if(T1[i-1]==-2)p1=0;if(T1[i+1]==-2)p2=0;if(p1==-1)p1=T1[i-1];if(p2==-1)p2=T1[i+1];dt[j+2]=!p1 || p2;T1[i-1]=j+2;j++;j4++;p1=-1;p2=-1;for(i1=i;i1<s-j1-2*j2-2*j3-2*j4;i1++) T1[i1]=T1[i1+2];i--;}for(i=0;i<s-j1-2*j2-2*j3-2*j4-2*j5;i++) if(T1[i]=='~'){for(i1=0;i1<y;i1++){if(T1[i-1]==T2[i1])p1=T3[i1];if(T1[i+1]==T2[i1])p2=T3[i1];}for(i2=2;i2<j+2;i2++){if(T1[i-1]==i2)p1=dt[i2];if(T1[i+1]==i2)p2=dt[i2];}if(T1[i-1]==-2)p1=0;if(T1[i+1]==-2)p2=0;if(p1==-1)p1=T1[i-1];if(p2==-1)p2=T1[i+1];dt[j+2]=(!p1 || p2)&&(!p2 || p1);7T1[i-1]=j+2;j++;j5++;p1=-1;p2=-1;for(i1=i;i1<s-j1-2*j2-2*j3-2*j4-2*j5;i1++) T1[i1]=T1[i1+2];i--;}return dt[j+1];}}8。