为了输入和处理的时候方便,做了以下规定:
否定:!
合取:&
析取:|
蕴含:>
等价: -
#include<map>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define ElemPyte char
#define maxn 100
#pragma warning(disable : 4996)
using namespace std;
class Stack
{
public:
Stack();
int M;
ElemPyte* top;
ElemPyte* best;
void push(ElemPyte e)
{
if (top - best >= M)
{
best = (ElemPyte*)realloc(best, sizeof(ElemPyte) * (M + maxn));
if(!best)
{
exit(0);
}
M += maxn;
}
*top = e;
top++;
}
ElemPyte pop()
{
if (top == best) return 0;
return *(--top);
}
bool empty()
{
return best == top;
}
};
Stack::Stack()
{
M = maxn;
best = (ElemPyte*)malloc(sizeof(ElemPyte) * maxn);
if (!best)
{
exit(0);
}
top = best;
}
Stack s1;
char a[10000];
char rpn[10000];
int rpn_t;
map<char, int>book;
map<char, int>book2;
int t =0;
int e[26];
char slove(char s[], int n)
{
int i = 0;
while ( i < n)
{
if (s[i]=='!')
{
if (s[i + 1] == '!')
{
for (int j = i; j < n - 2; j++)
{
s[j] = s[j + 2];
}
n -= 2;
continue;
}
else
{
s[i + 1] ^= 1;
for (int j = i; j < n-1; j++)
{
s[j] = s[j + 1];
}
n--;
i++;
}
}
else
{
i++;
}
}
int sum;
do
{
sum = 0;
for (i = 0; i < n-1; i++)
{
if (s[i] == '&')
{
sum++;
s[i - 1] = ((s[i - 1] - '0') && (s[i + 1] - '0')) + '0';
for (int j = i; j < n - 2; j++)
{
s[j] = s[j + 2];
}
n -= 2;
}
}
} while (sum);
do
{
sum = 0;
for (i = 0; i < n - 1; i++)
{
if (s[i] == '|')
{
sum++;
s[i - 1] = ((s[i - 1] - '0') || (s[i + 1] - '0')) + '0';
for (int j = i; j < n - 2; j++)
{
s[j] = s[j + 2];
}
n -= 2;
}
}
} while (sum);
do
{
sum = 0;
for (i = 0; i < n - 1; i++)
{
if (s[i] == '>')
{
sum++;
if (s[i - 1] == '0')
{
s[i - 1] = '1';
}
else
{
if (s[i + 1] == '1')
{
s[i - 1] = '1';
}
else
{
s[i - 1] = '0';
}
}
for (int j = i; j < n - 2; j++)
{
s[j] = s[j + 2];
}
n -= 2;
}
}
} while (sum);
do
{
sum = 0;
for (i = 0; i < n - 1; i++)
{
if (s[i] == '-')
{
sum++;
if (s[i - 1] == s[i + 1])
{
s[i - 1] = '1';
}
else
{
s[i - 1] = '0';
}
for (int j = i; j < n - 2; j++)
{
s[j] = s[j + 2];
}
n -= 2;
}
}
} while (sum);
s[n] = '\0';
return s[0];
}
int main()
{
printf("请输入命题公式\n");
printf("否定:!\n合取:&\n析取:|\n蕴含:>\n等价:-\n");
scanf("%s", a);
int n = strlen(a);
for (int i = 0; i < n; i++) if (a[i] != '!' && a[i] != '&' && a[i] != '|' && a[i] != '>' && a[i] != '-' && a[i] != '(' && a[i] != ')' && !book2[a[i]]) book2[a[i]]=1,e[t++]=a[i]-'a';
sort(e, e + t);
for (int i = 0; i < t; i++) book[e[i] + 'a'] = t - i;
for (int pos = 0; pos < 1 << t; pos++)
{
char copy[1000];
for (int i = 0; i < n; i++)
{
copy[i] = a[i];
if (a[i] != '!' && a[i] != '&' && a[i] != '|' && a[i] != '>' && a[i] != '-' && a[i] != '(' && a[i] != ')')
{
copy[i] = '0' + (1 & pos >> book[a[i]]-1);
}
}
char str[1000];
int sum = 0,str_n=0;
do
{
sum = 0;
int i = n - 1;
int r = n;
while ( i >= 0 )
{
str_n = 0;
if (copy[i] == '(')
{
sum++;
int j;
for (j = i + 1; j < n; j++)
{
if (copy[j] != ')')
{
str[str_n++] = copy[j];
}
else
{
break;
}
}
str[str_n] = '\0';
copy[i] = slove(str, str_n);
int e = r;
r = i+1;
for (int k = j + 1; k < e; k++) copy[r++] = copy[k];
copy[r++] = '\0';
break;
}
i = min(r-1, i - 1);
}
} while (sum);
char ans = slove(copy, n);
printf("%c\n",ans);
}
return 0;
}