当前位置:
文档之家› 利用真值表法求主析取范式及主合取范式的实现
利用真值表法求主析取范式及主合取范式的实现
{
if(OriginalForm[i] == 'P' || OriginalForm[i] == 'Q'|| OriginalForm[i] == 'R')
{
v = OriginalForm[i] == 'P' ? p : OriginalForm[i]== 'Q' ? q : r;
stack.Push(v);
{
string tmp = "";
stack.Push('#');
for(int i = 0; (unsigned)i <OriginalForm.length(); i++)
{
if(OriginalForm[i] == 'P' || OriginalForm[i] == 'Q' ||OriginalForm[i] == 'R' || OriginalForm[i] == 'S' || OriginalForm[i] == 'T' || OriginalForm[i] == 'U')
for(p = 1; p >= 0; p--)
{
for(q = 1; q >= 0; q--)
{
for(r = 1; r >= 0; r--)
{
Calculate();
if(result == 1)
Xiqu =Xiqu +"(" + (p == 1 ? "P" : "!P") + "&" +(q == 1 ? "Q" : "!Q") + "&"
(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY
|FOREGROUND_GREEN|FOREGROUND_BLUE);
//设置绿色和蓝色相加(即青色)
system("cls");//清屏
cout<<"-----------------"<<endl;
cout <<"欢迎使用!"<<endl<<endl;
};
SeqStack::SeqStack(int mSize)
{
maxtop = mSize - 1;
top = -1;
st = new char[mSize];
}
SeqStack::~SeqStack()
{
delete[]st;
}
char SeqStack::Top()
{
return st[top];
二、实验环境(实验设备)
硬件:PC机。
软件:Code::Blocks(C++)
三、实验原理及内容
内容:编程实现用真值表法求任意含三个以内变量的合式公式的主析取范式和主合取范式。
原理:首先读入变元个数,然后读入合式公式,用堆栈的知识将中缀表达式转化为后缀表达式,调用否定、析取、合取、条件、双条件的函数计算P、Q、R取不同真值时合式公式的真值,然后输出真值表,调用计算主析取范式和主合取范式的函数并输出。
cout << " !表示否定" << endl<<endl;
cout << " |表示析取" << endl<<endl;
cout << " &表示合取" << endl<<endl;
cout << " >表示条件" << endl<<endl;
cout << " -表示双条件" << endl;
cout << "主析取范式:"<<Xiqu << endl << endl;
cout << "主合取范式:" << Hequ << endl << endl;
}
int main()
{
int flag=1;
while(flag==1)
{
SetConsoleTextAttribute(GetStdHandle
case '&':i = 9; break;
case '!':i = 11; break;
case ')':i = 12; break;
}
switch(out)
{
case '#':o = 0; break;
case '(':o = 12; break;
case '-':o = 2; break;
case '>':o = 4; break;
stack.Pop();
}
stack.Pop();
}
else
{
do
{
tmp = tmp + stack.Top();
stack.Pop();
} while(!CanIn(OriginalForm[i]));
stack.Push(OriginalForm[i]);
}
}
while(stack.Top() != '#')
}
bool SeqStack::Push(char x)
{
if(top == maxtop)
return false;
st[++top] = x;
return true;
}
bool SeqStack::Pop()
{
if(top == -1)
return false;
top--;
return true;
}
void If() //条件,b->a
{
result = (b == 1 && a == 0) ? 0 : 1;
stack.Push(result);
}
void Doubleif() //双条件
{
result = (b == a) ? 1 : 0;
stack.Push(result);
}
bool CanIn(char out)//优先级的判断
}
int p, q, r, s, t, u;
int a, b, result;
int v =0;
int number;//用number表示变元的个数
SeqStack stack(200);
void Not() //否定
{
a = stack.Top();
stack.Pop();
result = a == 1 ? 0 : 1;
{
Calculate();
if(result == 1)
Xiqu =Xiqu + "("+ (p == 1 ? "P" : "!P") + "&" + (q == 1 ?"Q" : "!Q") + ")" + " | ";
else
Hequ = Hequ + "(" + (p ==0 ? "P" : "!P") + "|" + (q == 0 ? "Q" :"!Q") + ")" + " & ";
case '|':Or(); break;
case '&':And(); break;
case '!':Not(); break;
}
}
}
if(number == 2)
{
for(int i = 0; (unsigned)i <OriginalForm.length(); i++)
{
if(OriginalForm[i] == 'P' || OriginalForm[i] == 'Q')
{
tmp=tmp+OriginalForm[i];
continue;
}
if(CanIn(OriginalForm[i]))
stack.Push(OriginalForm[i]);
else if(OriginalForm[i] == ')')
{
while(stack.Top() != '(')
{
tmp = tmp + stack.Top();
class SeqStack//建立一个堆栈,利用将中缀表达式转为后缀表达式
{
public:
SeqStack(int mSize);
~SeqStack();
char Top();
bool Push(char x);
bool Pop();
private:
char *st;
int top;