简体   繁体   English

如何执行以下递归函数?

[英]How do I do the following recursive function?

Ok, so I have a regular Node list, with members info and next. 好的,所以我有一个常规的Node列表,其中包含成员信息和下一个。

I need to use a function, recursively, to calculate the average, and then compare if each node is bigger than the average or not. 我需要递归地使用一个函数来计算平均值,然后比较每个节点是否大于平均值。

int Acount(NodeType* Node, int sum, int& avg){

    if (Node == NULL){//last call
        avg = sum / avg;
        return 0;
    }
    else {
        return (Acount(Node->next, sum + Node->info, ++avg) + (Node->info > avg ? 1 : 0));
        }
}

Which is quite simple. 这很简单。 Problem is the value returned is always 0. The problem appears to be with 问题是返回的值始终为0。问题似乎出在

(Node->info > avg ? 1 : 0));

I've done the tests and when I do the following: 我已经完成测试,并且在执行以下操作时:

return (Acount(Node->next, sum + Node->info, ++avg) + Node->info;

or 要么

return (Acount(Node->next, sum + Node->info, ++avg) + avg;

Results meet expectations. 结果符合预期。 As in, I'm getting the sum of the Node->info in the first case, and I'm getting average*number of nodes in the second case. 就像在第一种情况下,我得到的是Node-> info的总和,在第二种情况下,我得到的是节点的平均数。

Point of this, I've proved that the function is working perfectly. 对此,我已经证明该功能可以正常工作。

Yet when it comes to 然而当涉及到

(Node->info > avg ? 1 : 0));

Appears to be problematic, which is quite peculiar. 似乎是有问题的,这很奇怪。 if I place for example: 如果我放置例如:

(Node->info == 5 ? 1 : 0));

And there is only one 5 in the nodes, then the function returns 1. So everything is working as intended, yet I keep getting a 0. 并且节点中只有1个5,然后该函数返回1。所以一切都按预期进行,但我一直得到0。

The following are the main functions and additional functions for the Node. 以下是节点的主要功能和其他功能。

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
struct NodeType{
    int info;
    NodeType *next;
};
//pre: first node passed is not NULL
int Acount(NodeType* Node, int sum, int& avg){

    if (Node == NULL){//last call
        avg = sum / avg;
        return 0;
    }
    else {
        return (Acount(Node->next, sum + Node->info, ++avg) + (Node->info > avg ? 1 : 0));
        }
}
void fill(NodeType*& Node){

    NodeType *temp;
    Node = new NodeType;
    Node->info = 0;
    Node->next = NULL;
    temp = Node;

    for (int i = 1; i < 10; i++){
        temp->next = new NodeType;
        temp = temp->next;
        temp->info = i;
        temp->next = NULL;
    }
}
void print(NodeType* Node){
    NodeType *temp = Node;
    while (temp != NULL){
        cout << temp->info << " ";
        temp = temp->next;
    }
    cout << endl;
}
void Delete(NodeType* Node){
    NodeType *temp;
    while (Node != NULL){
        temp = Node;
        Node = Node->next;
        delete temp;
    }
}
void main(){

    int sum = 0, avg = 0;
    NodeType *Node;
    fill(Node);
    print(Node);

    cout << Acount(Node, sum, avg) << endl;

    Delete(Node);


}

I am not sure if your code has defined behaviour. 我不确定您的代码是否定义了行为。 But, this line 但是,这条线

return (Acount(Node->next, sum + Node->info, ++avg) + (Node->info > avg ? 1 : 0));

depends on if the left summand or the right summand is calculated first. 取决于是先计算左加数还是右加数。

If it is the left one, then Acount goes down the recursion an incrementing avg until avg equals the number of elements in the list (here 10 when starting from zero called by the main routine). 如果它是左数,则Acount递归递减avg直到avg等于列表中的元素数(此处是main例程调用的从零开始的10个元素)。 Note, that avg is passed by reference. 请注意, avg是通过引用传递的。 Thus, when the recursion goes back up, this term in the right summand 因此,当递归返回时,该术语在正确的求和中

Node->info > avg

will never be true because Node->info is set in the fill routine to values smaller then the number of elements. 永远不会为真,因为在fill例程中将Node->info设置为小于元素数量的值。

In C++ there is no concept of left-to-right (or right-to-left) evaluation order of expressions. 在C ++中,没有表达式的从左到右(或从右到左)评估顺序的概念。 Operator priorities will control associativity, but in the case of f1() + f2() there is no guarantee that f1() is invoked before f2() (and viceversa). 操作符优先级将控制的关联性,但在的情况下, f1() + f2()没有保证f1()被调用之前f2() (反之亦然)。 It may depend on the compiler or other. 它可能取决于编译器或其他。

My suggestion is to split the expression into 2 distinct statements as follows: 我的建议是将表达式分成两个不同的语句,如下所示:

int tmp = Acount(Node->next, sum + Node->info, ++avg);
return tmp + (Node->info > avg ? 1 : 0);

I don't think your method will work. 我认为您的方法无效。

In this statement: 在此语句中:

return (Acount(Node->next, sum + Node->info, ++avg) + (Node->info > avg ? 1 : 0))

You don't know when the second term has be evaluated. 您不知道第二项的评估时间。 It's not defined in C++. 它没有在C ++中定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM