(Помощь в домашнем задании) Странное избыточное добавление функциейC++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 (Помощь в домашнем задании) Странное избыточное добавление функцией

Сообщение Anonymous »

*Это проблема, с которой я столкнулся, когда делал домашнее задание.
В этом разделе домашнего задания нас просят написать функцию ctx_append (я думаю, это сокращение от context_append), которая должно работать следующим образом:

Код: Выделить всё

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 
#include 
#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

Подробнее здесь: [url]https://stackoverflow.com/questions/78308587/homework-help-a-strange-over-append-by-the-function[/url]
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C++»