对任意合适公示求值 求思路
对任意合适公示求真值表
我仅仅实现了 对四个变元的 合适公示求值
对任意的长度 、变元的合式公式的求值改怎么弄?
求思路~
下面给出我的代码
请输入你的合适公示“|”表示或 ;”&“表示与;
例如”((P|(Q&S))|~R)= “以#号结束
((P|(Q&S))|~R)=
P Q R S result
0 0 0 0 1
0 0 0 1 1
0 0 1 0 0
0 0 1 1 0
0 1 0 0 1
0 1 0 1 1
0 1 1 0 0
0 1 1 1 1
1 0 0 0 1
1 0 0 1 1
1 0 1 0 1
1 0 1 1 1
1 1 0 0 1
1 1 0 1 1
1 1 1 0 1
1 1 1 1 1
请按任意键继续. . .
解释下我的代码
就是利用堆栈 解析表达式~~ 执行运算
我现在其实就是想知道,怎么判断出给出的任意公示中 的变元数量 和分别是那些~~
#include <iostream>#include <string>using namespace std;template<class Elem>class Link { public: Elem element; Link *next; Link (const Elem &elemval,Link *nextval = NULL) { element = elemval; next = nextval; } Link (Link *nextval = NULL) { next = nextval; } }; template <class Elem>class LStack{ private: Link<Elem> *top; int size; public: LStack() { top = NULL; size = 0; } ~LStack(){clear ();} void clear () { while (top!=NULL){ Link<Elem> *item = top; top=top->next; size=0; delete item; } } bool push(const Elem &item) { top = new Link<Elem>(item,top); size++; return true; } bool pop(Elem &it) { if(size==0) return false; it = top->element; Link<Elem> *Itemp=top->next; delete top; top=Itemp; size--; return true; } bool topValue(Elem &it)const { if(size==0) return false; it=top->element; return true; //以上堆栈的部分我保证正确 }};class HeShi{ private: char *M; int *A;//产生不同的变量组 int P,Q,R,S;//保存每组变量的真值 int NUM;//N中真值输出 int Result;//运算结果 LStack <int> Oprand;// 变元栈 LStack <char> Opnd;//逻辑运算符栈 public: HeShi(){ NUM=0; M = new char[50]; A = new int [4]; } int Docal(int l,char optr,int r) { int result; if(optr=='|') result=l||r; if(optr=='&') result=l&&r; return result; } bool ISopnd(char ch) { if(ch=='|'||ch=='&'||ch=='('||ch==')'||ch=='~') return true; return false; } int Cal(int p,int q,int r,int s,char *m) { int result;//遍历保存每个值 int i=0; while(m[i]!='='){ if(!ISopnd(m[i])){ int a;// 将表达式中的变元变成具体的值 if(m[i]=='P') a=p; else if(m[i]=='Q') a=q; else if(m[i]=='R') a=r; else if(m[i]=='S') a=s; else cout <<"error" << endl; Oprand.push(a); i++; } if(ISopnd(m[i])){ if(m[i]=='(') i++; else if(m[i]=='~'){//对合适公式的”~“的提前处理 i=i+1; int a;// 将表达式中的变元变成具体的值 if(m[i]=='P') a=p; else if(m[i]=='Q') a=q; else if(m[i]=='R') a=r; else if(m[i]=='S') a=s; else cout <<"error" << endl; if(a==1)a=0; else a=1; Oprand.push(a); i++; } else if(m[i]==')'){ char optr; int result;//运算中间结果 int left,right;//操作数 Oprand.pop(right); Oprand.pop(left); Opnd.pop(optr); result=Docal(left,optr,right);//执行运算 Oprand.push(result); i++; } else { Opnd.push(m[i]); i++; } } } Oprand.pop(result); return result; } void Run(){ cout<<" 请输入你的合适公示“|”表示或 ;”&“表示与;"<<endl; cout <<"例如”((P|(Q&S))|~R)= “以#号结束"<< endl; char ch[30]="((P|(Q&S))|~R)="; M=ch; puts(M); /*char ch[30]; gets(ch); for(int i=0;ch[i]!='#';i++){ M[i]=ch[i]; } */ cout<< "P"<< " " << "Q"<< " " << "R"<< " " << "S"<< " " <<" " <<" "<< "result" << endl; while(NUM!=2*2*2*2){//自动产生真值 int i=0; int num=NUM; while(i!=4){ A[i]= num%2; num=num/2; i++; } P=A[3];Q=A[2];R=A[1];S=A[0]; Result=Cal(P,Q,R,S,M); cout << P << " "<< Q << " " << R << " " << S <<" " <<" " <<" " <<" " <<" "; cout << Result << endl; NUM++; } } }; int main(){ HeShi heshi; heshi.Run(); system("pause"); return 0; }