标签:
1 #include "iostream" 2 #include "cstring" 3 #include "cstdlib" 4 #include "stack" 5 #include "cmath" 6 #define maxsize 100 7 using namespace std; 8 char str[100]; 9 int zhipai[20]={0}; 10 int length; 11 char expression[100]; 12 class symbol { 13 public: 14 void solve(); 15 private: 16 void change(); 17 int Calculate(); 18 void DoOperator(char op); 19 stack<int> s; 20 }; 21 22 void symbol ::DoOperator(char op){ 23 double left=0,right=0; 24 switch(op){ 25 case ‘|‘:s.push(left||right);break; 26 case ‘&‘:s.push(left&&right);break; 27 case ‘>‘:s.push(!left||right);break; 28 case ‘!‘:s.push((!right)&&left);break; 29 case ‘=‘:s.push(left==right);break; 30 } 31 32 } 33 void symbol ::change() 34 { 35 int k=0; 36 int flag=1; 37 int count=0; 38 for(int i=0;i<pow(2.0,count);i++) 39 { 40 k=1; 41 for(int m=0;m<length;m++) 42 { 43 if(isalpha(str[m])) 44 { 45 if(flag==1) 46 { 47 if(zhipai[k]==0) 48 expression[m]=‘0‘; 49 else 50 expression[m]=‘1‘; 51 k++; 52 } 53 else 54 expression[m]=‘0‘; 55 flag=1; 56 for(int t=m;t>=0;t--) 57 { 58 59 if((str[m+1]==str[t])&&isalpha(str[m+1])&&isalpha(str[t])) 60 flag=0; 61 } 62 } 63 else 64 expression[m]=str[m]; 65 } 66 for(int t=0;t<length;t++) 67 { 68 for(int j=t;j<length;j++) 69 { 70 if(str[t]==str[j]) 71 { 72 expression[j]=expression[t]; 73 } 74 } 75 } 76 } 77 } 78 void plus(int a[],int q) 79 { 80 a[q]=a[q]+1; 81 for(int i=q;a[i]==2;i--) 82 { 83 a[i]=0; 84 a[i-1]=a[i-1]+1; 85 } 86 } 87 int isp(char ch) //联结词的栈内优先级 88 { 89 switch(ch){ 90 case ‘#‘:return 0;break; 91 case ‘(‘:return 1;break; 92 case ‘!‘:return 10;break; 93 case ‘=‘:return 9;break; 94 case ‘&‘:return 7;break; 95 case ‘|‘:return 5;break; 96 case ‘>‘:return 3;break; 97 case ‘)‘:return 12;break; 98 default:return -1;break; 99 } 100 } 101 int icp(char ch) //联结词的栈外优先级 102 { 103 switch(ch){ 104 case ‘#‘:return 0;break; 105 case ‘(‘:return 12;break; 106 case ‘!‘:return 11;break; 107 case ‘=‘:return 8;break; 108 case ‘&‘:return 6;break; 109 case ‘|‘:return 4;break; 110 case ‘>‘:return 2;break; 111 case ‘)‘:return 1;break; 112 default:return -1;break; 113 } 114 } 115 int symbol ::Calculate() 116 { 117 stack<char> h; 118 char ch,y; 119 h.push(‘#‘); 120 for(int temp=0;temp<length-1;temp++){ 121 ch=expression[temp]; 122 if(isdigit(ch)) 123 { 124 if(ch==‘0‘) 125 s.push(0); 126 else 127 s.push(1); 128 } 129 else if(ch==‘)‘) 130 { 131 for(y=h.top(),h.pop();y!=‘(‘;y=h.top(),h.pop()){ 132 DoOperator(y);} 133 } 134 else{ 135 if(ch==‘!‘) 136 { 137 s.push(1); 138 } 139 for(y=h.top(),h.pop();isp(y)>icp(ch);y=h.top(),h.pop()) 140 DoOperator(y); 141 h.push(y); 142 h.push(ch); 143 } 144 } 145 while(h.size()!=1){ 146 y=h.top(); 147 h.pop(); 148 DoOperator(y); 149 } 150 cout <<s.top()<<endl; 151 return s.top(); 152 } 153 154 void symbol::solve() 155 { 156 cout<<"请输入命题逻辑表达式:"<<endl; 157 cout<<"(|:或运算,&:与运算,!非运算,>蕴涵运算,=等价运算,以#结尾)"<<endl; 158 cout<<""<<endl; 159 int flag=1; 160 int count=0; 161 cin >>str; 162 length=strlen(str); 163 if(str[length-1]!=‘#‘) 164 { 165 cout<<"未以#结尾,重新输入!"<<endl; 166 system("pause"); 167 exit(0); 168 } 169 char bianyuan[100]; 170 for(int i=0;i<length;i++) 171 { 172 if(isalpha(str[i])&&(flag==1)) 173 { 174 bianyuan[count]=str[i];count++; 175 } 176 flag=1; 177 for(int k=0;k<count;k++) 178 { 179 if(bianyuan[k]==str[i+1])flag=0; 180 } 181 } 182 if(count==0) 183 { 184 cout<<"无命题变元,重新输入!"<<endl; 185 solve(); 186 } 187 cout<<"真值表(用0代替F,用1代替T):" <<endl; 188 189 for(int w=0;w<count;w++) 190 cout<<bianyuan[w]<<‘ ‘; 191 cout<<"result"<<endl; 192 int* truth=new int[pow(2.0,count)]; 193 stack<char> xh1; 194 xh1.push(‘(‘); 195 stack<char> xh2; 196 stack<char> hx1; 197 hx1.push(‘(‘); 198 stack<char> hx2; 199 for(int r=0;r<pow(2.0,count);r++) 200 { 201 for(int j=1;j<=count;j++) 202 {cout<<zhipai[j]<<‘ ‘;} 203 change(); 204 truth[r]=Calculate(); 205 if(truth[r]==1) 206 { 207 for(int t=0;t<count;t++) 208 { 209 if(zhipai[t+1]==1) 210 { 211 xh1.push(bianyuan[t]); 212 xh1.push(‘&‘); 213 } 214 else 215 { 216 xh1.push(‘!‘); 217 xh1.push(bianyuan[t]); 218 xh1.push(‘&‘); 219 } 220 } 221 xh1.pop(); 222 xh1.push(‘)‘); 223 xh1.push(‘|‘); 224 xh1.push(‘(‘); 225 } 226 if(truth[r]==0) 227 { 228 for(int c=0;c<count;c++) 229 { 230 if(zhipai[c+1]==1) 231 { 232 hx1.push(bianyuan[c]); 233 hx1.push(‘|‘); 234 } 235 else 236 { 237 hx1.push(‘!‘); 238 hx1.push(bianyuan[c]); 239 hx1.push(‘|‘); 240 } 241 } 242 hx1.pop(); 243 hx1.push(‘)‘); 244 hx1.push(‘&‘); 245 hx1.push(‘(‘); 246 } 247 plus(zhipai,count); 248 } 249 cout<<"析合范式为:"; 250 if(xh1.size()==1) 251 cout<<"无析合范式,此表达式为矛盾式"; 252 else{ 253 xh1.pop(); 254 xh1.pop(); 255 while(xh1.size()!=0) 256 { 257 xh2.push(xh1.top()); 258 xh1.pop(); 259 } 260 while(xh2.size()!=0) 261 { 262 cout<<xh2.top(); 263 xh2.pop(); 264 } 265 } 266 cout<<endl; 267 cout<<"合析范式为:"; 268 if(hx1.size()==1) 269 cout<<"无合析范式,此表达式为重言式"; 270 else{ 271 hx1.pop(); 272 hx1.pop(); 273 while(hx1.size()!=0) 274 { 275 hx2.push(hx1.top()); 276 hx1.pop(); 277 } 278 while(hx2.size()!=0) 279 { 280 cout<<hx2.top(); 281 hx2.pop(); 282 } 283 } 284 cout<<endl; 285 } 286 void main() 287 { 288 289 symbol a; 290 a.solve(); 291 system("pause"); 292 }
标签:
原文地址:http://www.cnblogs.com/zlgxzswjy/p/4937273.html