实验原理及内容
说明:这部分内容主要包括:
1、形式化描述实验中所使用的数据结构和存储结构,给出函数之间的调用关系和数据传递方式;
2、给出核心算法的C++或Java等语言的源代码,并加上详细注释,分析算法的时间复杂度;
该实验先将中缀表达式转换成后缀表达式,
例如:stack 栈用来存放运算符,post 栈用来存放最后的后缀表达式。具体规则如下:
从左到右扫描中缀表达式,若是操作数,直接存入 post 栈;若是运算符:
(1)该运算符是左括号(, 则直接存入 stack 栈。
(2)该运算符是右括号),则将 stack 栈中( 前的所有运算符出栈,存入 post 栈。
(3)若该运算符为非括号,则将该运算符和 stack 栈顶运算符作比较:若高于栈顶运算符,则直接存入 stack 栈,否则将栈顶运算符出栈(从栈中弹出元素直到遇到发现更低优先级的元素(或者栈为空)为止),存入 post 栈。
(4)当扫描完后,stack 栈中还有运算符时,则将所有运算符出栈,存入 post 栈。
再将后缀表达式中每一个字母变量一一赋值,用递归枚举的方法枚举所有赋值情况,并且用map映射将每一个字母变量与当前被枚举的值一一映射,对每一种赋值情况调用后缀表达式计算函数计算后缀表达式的值,打印真假情况。如果是真,记录到名为tr的vector数组中,如果是假,记录到名为flase的vector数组中。最后根据tr和flase的数组来打印主析取范式和主合取范式。
#include <cstdio>
#include <iostream>
#include <vector>//vector是顺序容器
#include <string>
#include <cstdlib>
#include <queue>//queue 模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque 类型
#include <stack>
#include <map>
#include <sstream>
using namespace std;
string middle; //中缀表达式
char latter[1000]; //后缀表达式
string alpha; //存放所有字母变量
map<char,int> M; //映射,将字母变量与0或1一一对应
struct note
{
int a[100];
};
vector<note> tr; //不定长数组,存放主析取范式对应字母变量的01情况,也就是表达式真值为T
vector<note> flase; //不定长数组,存放主合取范式对应字母变量的01情况,也就是表达式真值是F
void ddd() //预处理,去除中缀表达式中条件->中的>,和双条件<=>中的= and > ,将这两个运算符当成一个字符处理,更方便
{
string::iterator i=middle.begin();
int flag=1;
while(flag)
{
flag=0;
for(i=middle.begin();i!=middle.end();++i)
{
if(*i=='>')
{
middle.erase(i);
flag=1;
break;
}
if(*i=='=')
{
middle.erase(i);
flag=1;
break;
}
}
}
}
int icp(char a)
{
if(a=='#') return 0;
if(a=='(') return 12;
if(a=='!') return 10;
if(a=='&') return 8;
if(a=='|') return 6;
if(a=='-') return 4;
if(a=='<') return 2;
if(a==')') return 1;
}
int isp(char a)
{
if(a=='#') return 0;
if(a=='(') return 1;
if(a=='!') return 11;
if(a=='&') return 9;
if(a=='|') return 7;
if(a=='-') return 5;
if(a=='<') return 3;
if(a==')') return 12;
}
void change() //中缀表达式转换后缀表达式
{
int j=0;
stack<char> s;
char ch,y;
s.push('#');
stringstream ss(middle);
while(ss>>ch,ch!='#')
{
if(isalpha(ch))
{
latter[j++]=ch;
if(alpha.find(ch)==-1)
{
alpha.push_back(ch);
}
}
else if(ch==')')
{
for(y=s.top(),s.pop();y!='(';y=s.top(),s.pop())
{
latter[j++]=y;
}
}
else
{
for(y=s.top(),s.pop();icp(ch)<=isp(y);y=s.top(),s.pop())
{
latter[j++]=y;
}
s.push(y);
s.push(ch);
}
}
while(!s.empty())
{
y=s.top();
s.pop();
if(y!='#')
{
latter[j++]=y;
}
}
latter[j]='#';
}
int cal() //对赋值后的后缀表达式进行计算
{
stack<int> s;
char ch;
int j=0;
int t1,t2;
while(1)
{
ch=latter[j];
if(ch=='#') break;
if(ch==0) break;
j++;
if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z'))
{
s.push(M[ch]);
}
else
{
if(ch=='!')
{
t1=s.top();
s.pop();
s.push(!t1);
}
else if(ch=='&')
{
t1=s.top();
s.pop();
t2=s.top();
s.pop();
if(t1==1&&t2==1)
{
s.push(1);
}
else
{
s.push(0);
}
}
else if(ch=='|')
{
t1=s.top();
s.pop();
t2=s.top();
s.pop();
if(t1==0&&t2==0)
{
s.push(0);
}
else
{
s.push(1);
}
}
else if(ch=='-')
{
t1=s.top();
s.pop();
t2=s.top();
s.pop();
if(t1==0&&t2==1)
{
s.push(0);
}
else
{
s.push(1);
}
}
else if(ch=='<')
{
t1=s.top();
s.pop();
t2=s.top();
s.pop();
if((t1==1&&t2==1)||(t1==0&&t2==0))
{
s.push(1);
}
else
{
s.push(0);
}
}
}
}
int ans=s.top();
return ans;
}
void dfs(int cur) //递归枚举每一种字符变量的取值情况
{
if(cur==alpha.size())
{
int ans=cal();
for(int i=0;i<alpha.size();i++)
{
if(M[alpha[i]])
{
cout<<"T\t";
}
else
{
cout<<"F\t";
}
}
if(ans==1) //真值为T 计入到tr数组,以待后面主析取范式使用
{
cout<<"T\n";
note t;
for(int i=0;i<alpha.size();i++)
{
t.a[i]=M[alpha[i]];
}
tr.push_back(t);
}
else //真值为F 计入到flase数组,以待后面主合取范式使用
{
cout<<"F\n";
note t;
for(int i=0;i<alpha.size();i++)
{
t.a[i]=M[alpha[i]];
}
flase.push_back(t);
}
return ;
}
M[alpha[cur]]=1;
dfs(cur+1);
M[alpha[cur]]=0;
dfs(cur+1);
}
int main()
{
while(true)
{
int i;
M.clear();
alpha.clear();
tr.clear();
flase.clear();
printf("或运算为 | , 与运算为 & ,单条件为 -> ,双条件我 <=> ,非运算为 !\n");
printf("请输入表达式,回车结束\n");
cin>>middle;
middle.append("#");
ddd();
change();
for(i=0;i<alpha.size();i++)
{
printf("%c\t",alpha[i]);
}
printf("表达式真值\n");
dfs(0);
printf("主析取范式为\n");
int lena=tr.size();
for(i=0;i<lena;i++)
{
if(i!=0) printf("∨");
int *p=tr[i].a;
printf("(");
for(int j=0;j<alpha.size();j++)
{
if(j!=0) printf("∧");
if(p[j]==1)
{
printf("%c",alpha[j]);
}
else
{
printf("¬%c",alpha[j]);
}
}
printf(")");
}
printf("\n");
printf("主合取范式为\n");
for(i=0;i<flase.size();i++)
{
if(i!=0) printf("∧");
int *p=flase[i].a;
printf("(");
for(int j=0;j<alpha.size();j++)
{
if(j!=0) printf("∨");
if(p[j]==0)
{
printf("%c",alpha[j]);
}
else
{
printf("¬%c",alpha[j]);
}
}
printf(")");
}
printf("\n\n");
}
return 0;
}