南邮的好小子嗷 别老惦记着抄 把你宿舍号留下我改天提几箱牛奶去看你啊!
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int Loopjudgment(char* b,int a[],int n1) {
for (int i = 0;i < n1-1;i++) {
switch (b[i]){
case '!':
a[i + 1] = !a[i + 1];
break;
case '&':
a[i + 1] = (a[i - 1] && a[i + 1]);
break;
case'|':
a[i + 1] = (a[i - 1] || a[i + 1]);
break;
case'>':
if (a[i - 1])
{
if (a[i + 1]) a[i + 1] = 1; else a[i + 1] = 0;
}
else { a[i + 1] = 1; }
break;
case'-':
if ((a[i - 1] ==1&& a[i + 1]==1)||(a[i-1]==0&&a[i+1]==0)) { a[i + 1] = 1; }
else { a[i + 1] = 0; }break;
}
}
return a[n1 - 1];
}
int getvoluenum(char* b) {
int n1 = 0;
for (int i = 0;i < 20;i++) {
if (b[i] == 'P') {
n1++;
break;
}
}
for (int i = 0;i < 20;i++) {
if (b[i] == 'Q') {
n1++;
break;
}
}
for (int i = 0;i < 20;i++) {
if (b[i] == 'R') {
n1++;
break;
}
}
return n1;
}
void value(char* c, int a[],char b,int num,int n2) {
for (int i = 0;i < n2;i++) {
if (c[i] == b) {
a[i] = num;
}
}
}
void print(char* c, int a[],int n1, int n2,int nnn[]) {
string hequ = "";
string xiqu = "";
if (n1 == 2) {
cout<< "P " << "Q " << c << endl;
for (int i = 0;i < 2;i++) {
value(c, a,'P', i,n2);
for (int j = 0;j < 2;j++) {
value(c,a, 'Q', j,n2);
cout <<i<<" " <<j<<" " << Loopjudgment(c,a,n2)<< endl;
if (!Loopjudgment(c, a,n2)) {
if (i) {
if (j) { hequ += "(!P|!Q)"; }
else { hequ += "(!P|Q)&"; }
}
else {
if (j) { hequ += "(P|!Q)&"; }
else { hequ += "(P|Q)&"; }
}
}
if (Loopjudgment(c,a, n2)) {
if (i) {
if (j) { xiqu += "(P&Q)"; }
else { xiqu += "(P&!Q)|"; }
}
else {
if (j) { xiqu += "(!P&Q)|"; }
else { xiqu += "(!P&!Q)|"; }
}
}
}
}
}
if (n1 == 3) {
cout << "P " << "Q " << "R " << c << endl;
for (int i = 0;i < 2;i++) {
value(c, a,'P', i,n2);
value(c, nnn, 'Q', i, n2);
for (int j = 0;j < 2;j++) {
value(c,a, 'Q', j,n2);
value(c, nnn, 'Q', j, n2);
for (int k = 0;k < 2;k++) {
int nu;
if(!k){
value(c, a, 'R', k, n2);
nu = Loopjudgment(c, a, n2);
}
else {
value(c, nnn, 'R', k, n2);
if (c[0] = '!') {
if (!i)
{
nnn[1] = 0;
}
}
nu = Loopjudgment(c, nnn, n2);
}
cout << i << " " << j << " " <<k<<" " << nu << endl;
if (!nu) {
if (i) {
if (j) {
if (k) { hequ += "(!P|!Q|!R)&"; }
else { hequ += "(!P|!Q|R)&"; }
}
else {
if (k) { hequ += "(!P|Q|!R)&"; }
else { hequ += "(!P|Q|R)&"; }
}
}
else {
if (j) {
if (k) { hequ += "(P|!Q|!R)&"; }
else { hequ += "(P|!Q|R)&"; }
}
else {
if (k) { hequ += "(P|Q|!R)&"; }
else { hequ += "(P|Q|R)&"; }
}
}
}
if (nu) {
if (i) {
if (j) {
if (k) { xiqu += "(P&Q&R)|"; }
else { xiqu += "(P&Q&!R)|"; }
}
else {
if (k) { xiqu += "(P&!Q&R)|"; }
else { xiqu += "(P&!Q&!R)|"; }
}
}
else {
if (j) {
if (k) { xiqu += "(!P&Q&R)|"; }
else { xiqu += "(!P&Q&!R)|"; }
}
else {
if (k) { xiqu += "(!P&!Q&R)|"; }
else { xiqu += "(!P&!Q&!R)|"; }
}
}
}
}
}
}
}
cout<<"主合取式是:"<< endl;
cout<< hequ << endl;
cout<<"主析取式是:"<< endl;
cout<< xiqu << endl;
}
int main(){
char c[20];
int a[20];
int n3[20] = { 0 };
cin >> c;
int n2 = strlen(c);
int n1 = getvoluenum(c);
print(c,a, n1, n2,n3);
return 0;
}//cv李宁给你踹开线!