Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;函数返回的值与函数中的值不同_C++ - Fatal编程技术网

C++ C++;函数返回的值与函数中的值不同

C++ C++;函数返回的值与函数中的值不同,c++,C++,因此,我正在尝试实现trie结构,到目前为止,除了以下内容外,一切都很好: 当使用count_words函数时,我在返回结果之前打印结果,结果是正确的,但当我在main中打印它时,它会变成一些未知的奇怪整数值 typedef struct trie { int words; int prefixes; struct trie *characters[26]; } node; int find_character_location(int ascii) { ret

因此,我正在尝试实现trie结构,到目前为止,除了以下内容外,一切都很好:

当使用count_words函数时,我在返回结果之前打印结果,结果是正确的,但当我在main中打印它时,它会变成一些未知的奇怪整数值

typedef struct trie {
    int words;
    int prefixes;
    struct trie *characters[26];
} node;

int find_character_location(int ascii) {
    return ascii-97;
}

node * initialize() {
    node * new_node = (node*)malloc(sizeof(node));
    new_node->words = 0;
    new_node->prefixes = 0;

    //all english alphabet characters
    for(int i=0; i<26; i++) {
        new_node->characters[i] = NULL;
    }

    return new_node;
}

//when adding string use only english lowercase letters
//a=97 b=98 ... z=122
void add_string(node * root, string str) {
    int str_size = str.size();

    if(str_size == 0) {
        root->words++;
        return;
    }
    else {
        int position = find_character_location(str[0]);
        root->prefixes++;    //character doesnt exists
        if(root->characters[position] == NULL) {
            root->characters[position] = initialize();
    }
    add_string(root->characters[position],
    str_size==1?"":   str.substr(1));
    }
}

int count_prefixes(node *root, string prefix) {
    int str_size = prefix.size();

    if(prefix == "") {
        cout<<"number of prefixes: "<<(root->prefixes)<<endl;
        return (*root).prefixes;
    }

    int position = find_character_location(prefix[0]);

//character exists
if(root->characters[position] != NULL) {
    count_prefixes(
    root->characters[position],str_size==1?"":prefix.substr(1));

    }
    else {
        cout<<"no prefixes, returning 0"<<endl;
        return 0;
    }
}


int count_words(node *root, string str) {
    int str_size = str.size();

    if(str == "") {
        cout<<"Number of words: "<<root->words<<endl;
        int ret = root->words;
        return (int)ret;
}

int position = find_character_location(str[0]);

//that character exists
if(root->characters[position] != NULL) {
    count_words(root->characters[position], 
    str_size==1 ? "" :  str.substr(1));

    }
    else {
        cout<<"no words, returning 0"<<endl;
        return 0;
    }
}


int main() {

    node * root;
    root = initialize();

    add_string(root, "tomislav");
    add_string(root, "tomislav");
    add_string(root, "tomislav");
    add_string(root, "todoric");
    add_string(root, "tomahawk");
    add_string(root, "tosad");
    add_string(root, "tomo");

    cout<<"Counting words"<<count_words(root, "tomislav");
    return 0;
}
typedef结构trie{
智力词;
int前缀;
结构trie*字符[26];
}节点;
int查找字符位置(int ascii){
返回ascii-97;
}
节点*初始化(){
node*new_node=(node*)malloc(sizeof(node));
新建节点->单词=0;
新建节点->前缀=0;
//所有英文字母字符
for(int i=0;icharacters[i]=NULL;
}
返回新的_节点;
}
//添加字符串时,仅使用英文小写字母
//a=97 b=98…z=122
void add_字符串(节点*根,字符串str){
int str_size=str.size();
如果(str_size==0){
root->words++;
返回;
}
否则{
int position=find_character_location(str[0]);
根->前缀+++;//字符不存在
如果(根->字符[位置]==NULL){
根->字符[位置]=初始化();
}
添加字符串(根->字符[位置],
str_size==1?“:str.substr(1));
}
}
int count_前缀(节点*根,字符串前缀){
int str_size=前缀.size();
如果(前缀==“”){
cout
int count\u单词(节点*根,字符串str){
int str_size=str.size();
如果(str==“”){
cout
int count\u单词(节点*根,字符串str){
int str_size=str.size();
如果(str==“”){

cout
count\u words
并不总是返回一个值,这意味着,有时,你会给调用方提供堆栈上的任何废话。由于缩进,代码有点难以阅读,但我怀疑你的意思是

if(root->characters[position] != NULL) {
    count_words(root->characters[position], 
    str_size==1 ? "" :  str.substr(1));

    }
将来


count\u words
并不总是返回一个值,这意味着,有时,你会给调用方堆栈上的任何废话。由于缩进,代码有点难读,但我怀疑你的意思是

if(root->characters[position] != NULL) {
    count_words(root->characters[position], 
    str_size==1 ? "" :  str.substr(1));

    }
将来


确保将编译器的警告和错误设置为最大值。在两个函数中(
count\u prefixes()
count\u words()
)您有终止而不返回值的控制路径。修复这些路径,您的程序将正常工作。

确保将编译器的警告和错误设置为最大值。在两个函数中(
count\u prefixes()
count\u words()
)您的控制路径终止时不返回值。修复这些路径,您的程序将正常工作。

您有多个调用
count_words
active(递归就是这样工作的),但只有最后一个调用实际返回值

假设我们正在搜索“to”。
main
将使用“to”和根节点调用
count\u words
。这将使用“o”和“t”节点调用
count\u words
。这将使用“”和“to”节点调用
count\u words

最后一个调用打印“字数:7”,然后向倒数第二个调用返回7。中间的调用忽略此返回值,不返回值,因此其返回值是垃圾。第一个调用忽略此垃圾返回值,不返回值,因此其返回值也是垃圾(可能不同的垃圾).Then
main
打印第一次调用的返回值,它是垃圾


您的中间调用需要返回一些内容。可能您只想返回下一个调用返回的内容,因此在
count_words(root->characters[position],str_size==1?”:prefix.substr(1));
前面添加
return
(对于
count_prefixes

您有多个调用
count_words
active(递归就是这样工作的),但实际上只有最后一个调用返回一个值

假设我们正在搜索“to”。
main
将使用“to”和根节点调用
count\u words
。这将使用“o”和“t”节点调用
count\u words
。这将使用“”和“to”节点调用
count\u words

最后一个调用打印“字数:7”,然后向倒数第二个调用返回7。中间的调用忽略此返回值,不返回值,因此其返回值是垃圾。第一个调用忽略此垃圾返回值,不返回值,因此其返回值也是垃圾(可能不同的垃圾).Then
main
打印第一次调用的返回值,它是垃圾


您的中间调用需要返回一些内容。可能您只想返回下一个调用返回的内容,因此在
count_words(root->characters[position],str_size==1?”:prefix.substr(1));
前面添加
return
(对于
count_prefixes

首先正确缩进此代码。不确定是否与您的问题有关,但如果
str
不是空的,并且列表中有单词,您实际上不会返回任何内容。
if(root->characters[position]!=NULL)的
then
子句
语句不包含
return
语句。首先正确缩进此代码。不确定是否与您的问题有关,但如果
str
不是空的,并且列表中有单词,您实际上不会返回任何内容。
if(root->characters[position]!=NULL)的
then
子句
语句不包含
return
语句。谢谢大家的回复,你们都指出了同样的事情,这是正确的,这实际上是一个愚蠢的错误,但你们要做的是什么。@immibis你们的回答是最具描述性的,所以这就是为什么我将其标记为公认的答案谢谢大家的回复,你们都明白了d指出同样的事情是正确的,这实际上是一个愚蠢的错误,但是你
if(root->characters[position] != NULL) {
    return count_words(root->characters[position], 
    str_size==1 ? "" :  str.substr(1));

    }