gpt4 book ai didi

c++ - 和 C++ 中的运算符参数评估

转载 作者:行者123 更新时间:2023-11-28 02:13:56 25 4
gpt4 key购买 nike

<分区>

and 运算符如何计算其参数。我有一个代码来检查图形是否循环。在这段代码中,if 语句中有一个 and 条件。而且我认为,据我所知,它在第一次遇到错误表达式时终止,根本没有计算第二个表达式。

这是代码

bool Graph::isCyclicUtil(int v, bool *visited, bool *recStack){
if (visited[v] == false){
// Mark the current node as visited
visited[v] = true;
recStack[v] = true;

// Recur for all the vertices adjacent to this vertex
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); i++){
-------->**This and cond**if (!visited[*i] && isCyclicUtil(*i, visited, recStack))
return true;
else if (recStack[*i])
return true;
}
}
recStack[v] = false; // remove the vertex from the recursion stack
return false;
}

void Graph::printRecStack(bool *recStack){
cout << "\n \n";
for (int i = 0; i < V; i++){
if (recStack[i])
cout <<i<< "\n";
}
return;
}


bool Graph::isCyclic(){
// Mark all the vertices as not visited and not part of recursion stack
bool *visited = new bool[V];
bool *recStack = new bool[V];
for (int i = 0; i<V; i++){
visited[i] = false;
recStack[i] = false;
}

// Call the recursive helper function to detect cycle in different
// DFS trees.
if (isCyclicUtil(0,visited, recStack)){
printRecStack(recStack);
return true;
}
/*for (int i = 0; i < V; i++){
if (isCyclicUtil(i, visited, recStack))
printRecStack(recStack);
return true;
}*/
return false;
}

请注意isCyclicUtil函数中if语句中的and条件。

如果你拿一个简单的图作为这样的测试用例:

0->1
1->2
2->0
2->3
3->3

并为 0 调用 isCyclicUtil,第一个 3 recStack 中的 值结果为真。如果第二个表达式也在 if 语句 中求值,情况就不会如此。因为对节点 2 的调用将到达其子节点 0。但是由于循环是从0开始的,0已经被访问过,所以recStack[0]应该初始化为false。但这并没有发生,所有这些都被证明是真的。就好像 and 条件在遇到 visited[0] 为真时就终止了,甚至没有调用再次 isCyclicUtil(0,visited,recStack)。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com