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