文档介绍:人工智能第1次作业
通常合一算法(C++实现)
姓名:佟 学号:24 班级:12计本非师 学院:信息学院
简单程序以下
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int iC=0; //标识公式集中括号匹配数
class Syncretism{
private:
struct Transform{ // 一个代换(差异集)
string t_f1;
string t_f2;
};
public:
bool Issyn(string f1,string f2, vector<Transform> &t ); //是否能合一
Transform different(const string f1,const string f2) //求差异集
{
int i=0;
Transform t;
while((i)==(i))
i++;
int j1=i;
while(j1<()-1&&(j1)!=',')
j1++;
if(j1-i==0) return t;
=(i,j1-i);
int j2=i;
while(j2<()-1&&(j2)!=',')
j2++;
if(j2-i==0) return t;
=(i,j2-i);
while([j1-i-1]==[j2-i-1])
{
(j1-1-i);
(j2-i-1);
j1--;
j2--;
}
return t;
}
bool same(const string f1,const string f2) ; // 判定两个公式是否相同
string change(string f,Transform t); //用代换q 对公式f 进行合一代换
bool legal(Transform &t); //
int var(const string s); //s 中每个()内子串是变量还是常量
void show();//最终演示算法
};
bool Syncretism::Issyn(string f1,string f2,vector<Transform> &lan)
{
while(!same(f1,f2))
{
Transform t=different(f1,f2);
bool flag=legal(t);
if(!flag)
return false;
else
{
(t); //将t 加入vector lan 中
if(flag)
{
f1=change(f1,()); //用lan 最终一个元素代换
f2=change(f2,());
cout<<"变换后:"<<endl;
cout<<"f1:"<<f1<<endl;
cout<<"f2:"<<f2<<en