📄 boolcheck.cpp
字号:
#include <iostream>
using namespace std;
const int MaxLength = 100;
template<class T>
class Stack
{
public:
Stack(int MaxStackSize = 100);
~Stack() {delete [] stack;}
T Top() const;
Stack<T>& Add(const T& x);
Stack<T>& Delete(T& x);
private:
int top;
int MaxTop;
char *stack;
};
template<class T>
Stack<T>::Stack(int MaxStackSize)
{
MaxTop = MaxStackSize - 1;
stack = new char[MaxStackSize];
top = -1;
}
template<class T>
T Stack<T>::Top() const
{
return stack[top];
}
template<class T>
Stack<T>& Stack<T>::Add(const T& x)
{
stack[++top] = x;
return *this;
}
template<class T>
Stack<T>& Stack<T>::Delete(T& x)
{
x = stack[top--];
return *this;
}
int poland(char *expr)
{
Stack<int> s(MaxLength);
int length=strlen(expr);
int m,n;
for(int i=1;i<=length;i++)
{
if(expr[i-1]!='+'&&expr[i-1]!='-'&&expr[i-1]!='*')
{
s.Add(int(expr[i-1]-48));
}
else
{
s.Delete(n);
s.Delete(m);
switch(expr[i-1])
{
case '+':
{if(m==0&&n==0)
s.Add(0);
else
s.Add(1);}
break;
case '-':
{if(m==2&&n==2)
s.Add(3);
else if(n==1&&m==2)
s.Add(0);
else if(n==0&&m==2)
s.Add(1);
else if(n==0&&m==3)
s.Add(0);
else if(n==1&&m==3)
s.Add(1);
}
break;
case '*':
{if(m==1&&n==1)
s.Add(1);
else
s.Add(0);}
break;
default: break;
}
}
}
int result=s.Top();
return result;
}
int Precedence(char op)
{
switch(op)
{
case '+':
case '*':
return 1;
case '-':
return 2;
case '(':
case '#':
default:
return 0;
}
}
void Change(char* s1, char* s2)
{
Stack<char> R(MaxLength);
R.Add('#');
int i,j;
i=0;
j=0;
char ch=s1[i];
while(ch!='#')
{
if(ch=='(')
{
R.Add(ch);
ch=s1[++i];
}
else if(ch==')')
{
while(R.Top()!='(')
{
R.Delete(s2[j++]);
}
R.Delete(ch);
ch=s1[++i];
}
else if(ch=='+'||ch=='-'||ch=='*')
{
char w=R.Top();
while(Precedence(w)>=Precedence(ch))
{
s2[j++]=w;
R.Delete(w);
w=R.Top();
}
R.Add(ch);
ch=s1[++i];
}
else if(ch!='('&&ch!=')'&&ch!='-'&&ch!='+'&&ch!='*'&&ch!='#')
{
while(ch!='('&&ch!=')'&&ch!='-'&&ch!='+'&&ch!='*'&&ch!='#')
{
s2[j++]=ch;
ch=s1[++i];
}
}
}
R.Delete(ch);
while(ch!='#')
{
if(ch=='(')
{
cerr<<"expression error!"<<endl;
exit(1);
}
else
{
s2[j++]=ch;
R.Delete(ch);
}
}
s2[j++]='#';
}
void compute(char *original,char *result1)
{
char *boolexp=new char [99];
for(int k=0;k<99;k++)
{
boolexp[k]=0;
}
int i=0;
int m=0;
while(original[i]!='#')
{
if(original[i]=='(')
{
boolexp[m]='(';
i++;
m++;
}
else if(original[i]==')')
{
boolexp[m]=')';
i++;
m++;
}
else if(original[i]=='N')
{
boolexp[m]='2';
boolexp[++m]='-';
i=i+3;
m++;
}
else if(original[i]=='A')
{
boolexp[m]='*';
i=i+3;
m++;
}
else if(original[i]=='O')
{
boolexp[m]='+';
i=i+2;
m++;
}
else if(original[i]!='N'&&original[i]!='A'&&original[i]!='O'&&original[i]!='('&&original[i]!=')')
{
boolexp[m]=original[i];
i++;
m++;
}
}
int length=strlen(boolexp);
int varnum1 =0;
int totalnum1=1;
int bincount1=0;
int tocount;
boolexp[length]='#';
char binnum1[30]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
char bb[30] ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
char cc[30] ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
Change(boolexp,bb);
for(int q=0;q<strlen(bb);q++)
{
if(bb[q]!='-'&&bb[q]!='+'&&bb[q]!='*'&&bb[q]!='2')
varnum1++;
}
varnum1--;
for(int pownum=0;pownum<varnum1;pownum++)
{
totalnum1=totalnum1*2;
}
for(int num=0;num<totalnum1;num++)
{
tocount=num;
for(int ccnum=0;ccnum<strlen(bb)-1;ccnum++)
{
cc[ccnum]=bb[ccnum];
}
for(int num2=0;num2<varnum1;num2++)
{
binnum1[num2]=tocount%2+48;
tocount=tocount/2;
}
int convertcount=0;
for(int convertnum=0;convertnum<strlen(cc);convertnum++)
{
if(cc[convertnum]!='2'&&cc[convertnum]!='-'&&cc[convertnum]!='+'&&cc[convertnum]!='*'&&cc[convertnum]!='#')
{
cc[convertnum]=binnum1[convertcount];
convertcount++;
}
}
result1[num]=poland(cc)+48;
}
cout<<"the result is "<<result1<<endl;
}
int main()
{
char origin[99];
char origin2[99];
char resulter[30] ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
char resulter2[30]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
cout<<"Please input a bool expression with a # to be the end..."<<endl;
cin.getline(origin,99);
compute(origin,resulter);
cout<<"Please input another bool expression with a # to be the end..."<<endl;
cin.getline(origin2,99);
compute(origin2,resulter2);
for(int compare=0;compare<strlen(resulter);compare++)
{
if(resulter[compare]!=resulter2[compare])
{
cout<<"NO,THEY ARE NOT EQUAL!"<<endl;
return 0;
}
}
cout<<"YES,THEY ARE EQUAL!"<<endl;
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -