假设我们有以下程序:
human(socrates).
day(tomorrow).
die(X) :- human(X).
may_go_to_school(Y) :- day(Y),
not holiday(Y).
Run Code Online (Sandbox Code Playgroud)
如果我们运行clingo来获取程序的答案集,我们就会得到
Answer: 1
human(socrates) day(tomorrow) die(socrates) may_go_to_school(tomorrow)
Run Code Online (Sandbox Code Playgroud)
我们知道地滚球将首先将所有变量实例化为常数,因此接地后的程序将是:
human(socrates).
day(tomorrow).
die(socrates) :- human(socrates).
may_go_to_school(tomorrow) :- day(tomorrow),
not holiday(tomorrow).
Run Code Online (Sandbox Code Playgroud)
我在Gelfond的书中读到它给出了3个获得答案集的规则:
满足Π的规则.换句话说,如果你相信它的身体,相信一个规则的头.
不要相信矛盾.
坚持"理性原则",其中说:"相信没有你不会被迫相信."
在规则中:
may_go_to_school(tomorrow) :- day(tomorrow),
not holiday(tomorrow).
Run Code Online (Sandbox Code Playgroud)
我们因为失败而得到了否定 not holiday(tomorrow)
如本书所示:
符号
not是一种新的逻辑连接,称为默认否定,(或否定为失败); 不是l经常被解读为"不相信我是真的."请注意,这并不意味着l被认为是假的.理所当然,理性推理者既不相信也不相信p否定,这是可以想象的¬p.
然后根据规则1,我应该相信believe in the head of a rule if you believe in its body身体,not holiday(tomorrow).因为我既不相信holiday(tomorrow).也不相信¬holiday(tomorrow).?
根据答案,我应该相信 ¬holiday(tomorrow).
我用 C++ 实现了一个二叉搜索树,它支持动态创建和删除节点。为了可视化树,我首先尝试使用/和显示边缘\。/然而,这给出了非常糟糕的可视化效果,因为和的位置\需要精确计算。目前的数字如下:

于是我找到了一个叫做Graphviz的工具。Graphviz支持的原始语言是点语言,我不太熟悉。
我阅读了文档,发现点语言易于编写和阅读,但我仍然想使用我的 C++ 代码来生成树,因为它包含很多内容,例如根据用户的输入插入等。
是否有机会使用某些函数来生成点文件?
我的二叉树的代码:
//BTNode.h
#include <iostream>
using namespace std;
template<class T>
struct BTNode{
BTNode(){
lChild = rChild = NULL;
}
BTNode(const T& x){
element = x;
lChild = rChild = NULL;
}
BTNode(const T& x, BTNode<T>* l, BTNode<T>* r){
element = x;
lChild = l;
rChild = r;
}
T element;
int digit, row;
BTNode<T>* lChild, *rChild;
};
//BSTree.h
#include"ResultCode.h"
#include "BTNode.h"
#include "seqqueue.h"
#include …Run Code Online (Sandbox Code Playgroud) visualization graphviz graph-visualization binary-search-tree
这是一段以递归方式定义数字的 Prolog 代码:
numeral(0).
numeral(succ(X)) :- numeral(X).
Run Code Online (Sandbox Code Playgroud)
当给定查询numeral(X).Prolog 将返回:
X = 0 ;
X = succ(0) ;
X = succ(succ(0)) ;
X = succ(succ(succ(0))) ;
X = succ(succ(succ(succ(0)))) ;
X = succ(succ(succ(succ(succ(0))))) ;
X = succ(succ(succ(succ(succ(succ(0)))))) ;
X = succ(succ(succ(succ(succ(succ(succ(0))))))) ;
X = succ(succ(succ(succ(succ(succ(succ(succ(0))))))))
yes
Run Code Online (Sandbox Code Playgroud)
根据我所了解的,在进行查询时,prolog 会首先X生成一个类似 的变量(_G42),然后它会搜索事实和规则以找到匹配项。
在这种情况下,它将找到0(事实)作为正确匹配。然后它也会尝试匹配规则。那是考虑_G42不是0,_G42而是另一个数字的成功。因此,生成了另一个变量(如_G44),_G44将匹配0并且也将像 一样走得更远_G42。既然_G44匹配0,那么它就会倒退_G42,得到_G42 = …
这是我程序中的函数原型:
void FindRepStr(char str[], const char findStr[], const char replaceStr[]);
它发现findStr[]在str[]与更换replaceStr[].
这是我的代码:
void FindRepStr(char str[], const char findStr[], const char replaceStr[])
{
char *s = nullptr;
s = strstr(str,findStr); //s points to the first-time appear in str
char tmp[] ="";
//length equal
if(strlen(findStr)==strlen(replaceStr))
{
for(int i=0;i<strlen(findStr);i++)
{
if(replaceStr[i]=='\0' || s[i] =='\0')
break;
else
s[i] = replaceStr[i];
}
cout<<str<<endl;
}
else
{
//find shorter than replace
if(strlen(findStr)<strlen(replaceStr))
{
//!!!problem here!!!
strncpy(tmp,s,strlen(s)+1); // store the left …Run Code Online (Sandbox Code Playgroud)