函数奇怪的过度追加

问题描述 投票:0回答:1

在这部分作业中,我们被要求写一个函数

ctx_append
(我猜是contextappend的缩写),它的操作应该是这样的:

Before:     ctx_k: { "a", "b", "" }
            ctx_v: { 42,  43 }

Operation:  ctx_append("c", 44, ctx_k, ctx_v, 20);

After:      ctx_k: { "a", "b", "c", "" }
            ctx_v: { 42,  43,  44 }

以下表示空上下文:

ctx_k: { "" }
ctx_v: { }

程序应使用辅助函数

ctx_append_impl
,该函数必须以递归方法运行。

以下是我的作品:

#include <iostream>
#include <sstream>
#include "interpreter.h" //the only use of this header file in the following functions is 
                         //provide the definition of MAX_INDENT_LEN

#include "raise.h" //the only use of this header file is provide the definition of raise() and
                   //ctx_overflow

using namespace std;



int find_empty_string(char ctx_k[][MAX_IDENT_LEN], unsigned int idx){

    //since the ctx_k ends with "", we have to use this function to locate where shall we append the content in the char array ident[]
    //MAX_INDENT_LEN = 64

    if (ctx_k[idx][0] == '"' && ctx_k[idx][1] == '"'){ //find the empty string
        return idx;
    }
    else{
        find_empty_string(ctx_k, idx + 1);
    }
}

int find_zero_val(int ctx_v[], unsigned int idx){

    //since the ctx_v ends with 0, we have to use this function to locate where shall we append the value in the int array val[]
    //MAX_INDENT_LEN = 64

    if (ctx_v[idx] == 0){
        return idx;
    }
    else{
        return find_zero_val(ctx_v, idx + 1);
    }
}

void ctx_append_impl(const char ident[], int val, char ctx_k[][MAX_IDENT_LEN], int ctx_v[], unsigned int size, unsigned int idx){ 

    // the function for appending the content inside ident[] to ctx_k and val to ctx_v
    // size is the max number of strings allowed to contain
    // idx is the current index of the ident
    // MAX_INDENT_LEN = 64

    int empty_string_position = find_empty_string(ctx_k, 0);
    cout<<"empty_string_position: "<<empty_string_position<<endl;
    int zero_val_position = find_zero_val(ctx_v, 0);
    cout<<"zero_val_position: "<<zero_val_position<<endl;

    if (idx>size){
        raise(ctx_overflow);
    }
    else if(idx==size || ident[idx] == '\0'){
        return;
    }
    
    else {
        ctx_append_impl(ident, val, ctx_k, ctx_v, size, idx+1);
        ctx_k[empty_string_position][idx] = ident[idx];
    }
    
    ctx_k[empty_string_position+1][0] = '"'; //closing string, step 1
    ctx_k[empty_string_position+1][1] = '"'; //closing string, step 2
    ctx_v[zero_val_position] = val; //update the value
    ctx_v[zero_val_position+1] = 0; //close the ctx_v array
    
}

void ctx_append(const char ident[], int val, char ctx_k[][MAX_IDENT_LEN], int ctx_v[], unsigned int size){
    ctx_append_impl(ident, val, ctx_k, ctx_v, size, 0);
}

int main(){
    char s[100][MAX_IDENT_LEN]={{'a', 'b','\0'}, {'"','"','\0'}};
    cout<<"s: "<<s[0][1]<<s[1][1]<<s[2][2]<<endl;
    const char t[]={'c'};
    int v[100] = {1, 2, };
    int a=3;
    
    ctx_append(t, a, s, v, 100);
    cout<<"new s: "<<s[0][0]<<s[0][1]<<s[1][0]<<s[1][1]<<endl;
    cout<<"new v: "<<v[0]<<v[1]<<v[2]<<v[3]<<endl;
}

“new s”的预期输出应该只是

abc 
,但是当我编译并运行它时,我们的输出变成了
abca
,我不知道最后一个“a”出现在哪里。

最初我怀疑这个bug是由于const char t[]中缺少''造成的,所以我将t[]编辑为

const char t[]={'c', '\0'}

但是bug仍然出现,唯一的变化是现在输出是

abc"

c++ recursion
1个回答
0
投票

所以出现问题是因为最初 ctx_k[1][1] 中有一个 '"' 而我忘了处理它...在我插入以下代码 'ctx_k[empty_string_position][idx] = 后问题就解决了' ';'之间

    else if(idx==size || ident[idx] == '\0'){return}

感谢所有帮助我调试的人!

© www.soinside.com 2019 - 2024. All rights reserved.