gpt4 book ai didi

c - 函数未接收我作为 const char * 发送的内容

转载 作者:行者123 更新时间:2023-11-30 16:59:03 26 4
gpt4 key购买 nike

static char DEFAULT_DELIMITER = ',';
char *calculator_retrieveFirstToken(char *numbers, const char * delimiters) {
//if (delimiters[strlen(delimiters)-1] == DEFAULT_DELIMITER)
if (delimiters[1] == DEFAULT_DELIMITER) {
return strtok(numbers, delimiters);
}
else {
char linefeed = '\n';
strtok(numbers, &linefeed);
return strtok(NULL, delimiters);
}
}

char calculator_findDelimiter(const char *numbers) {
char delimiter = DEFAULT_DELIMITER;
if (strlen(numbers) >= 4
&& numbers[0] == '/'
&& numbers[1] == '/') {
delimiter = numbers[2];
}
return delimiter;
}

double calculator_add(char *numbers) {
char delimiters[] = "\n";
char *token = NULL;

char mainDelimiter = calculator_findDelimiter(numbers);
strcat(delimiters, &mainDelimiter);
token = calculator_retrieveFirstToken(numbers, delimiters);
return calculator_addEveryToken(token, delimiters);
}

当我调试计算器_retrieveFirstToken 函数时,发生了最奇怪的情况。

数字输入示例:“”

mainDelimiter 等于 ','

在 strcat 调用之后:分隔符等于:“\n,”

当我进入计算器_retrieveFirstToken 函数时,分隔符参数等于:“\n,\n,”

我不明白这是如何工作的......我的 strcat 调用出了问题?

这里是主要内容

#include "calculator_kata.h"
#include <assert.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>

static char numbers[50];

static bool equals(const double expected, const double received)
{
double epsilon = 1e-7;
return fabs(received - expected) < epsilon;
}

static void test_addEmptyString_shouldReturnZero()
{
strcpy(numbers, "");
const double sum = calculator_add(numbers);
assert(equals(0, sum));
}

static void test_addOneNumber_shouldReturnThisNumber()
{
strcpy(numbers, "42");
const double sum = calculator_add(numbers);
assert(equals(42, sum));
}

static void test_addTwoNumbers_shouldReturnTheSum()
{
const double TWENTY_ONE = 9+10;
strcpy(numbers, "9,10");

const double sum = calculator_add(numbers);

assert(equals(TWENTY_ONE, sum));
}

static void test_addAnyQtyOfNumbers_shouldReturnTheSum()
{
strcpy(numbers, "1,2,3,4,5");
const double sum = calculator_add(numbers);
assert(equals(15, sum));
}

static void test_inputWithNewLines_insteadOfCommas_shouldStillBeParsed()
{
strcpy(numbers, "1\n2,3");
const double sum = calculator_add(numbers);
assert(equals(6, sum));
}

static void test_inputWithMultipleCommasBetweenNumbers_shouldReturnTheSum()
{
strcpy(numbers, "1\n,2,,,,,,,3");
const double sum = calculator_add(numbers);
assert(equals(6, sum));
}

static void test_inputWithCustomDelimiter_shouldChangeDelimiter()
{
strcpy(numbers, "//;\n1;2");
const double sum = calculator_add(numbers);
assert(equals(3, sum));
}

int main()
{
test_addEmptyString_shouldReturnZero();
test_addOneNumber_shouldReturnThisNumber();
test_addTwoNumbers_shouldReturnTheSum();
test_addAnyQtyOfNumbers_shouldReturnTheSum();
test_inputWithNewLines_insteadOfCommas_shouldStillBeParsed();
test_inputWithMultipleCommasBetweenNumbers_shouldReturnTheSum();
test_inputWithCustomDelimiter_shouldChangeDelimiter();
return 0;
}

最佳答案

strtokstrcat 采用指向 char 数组(即字符串)的指针,而不是指向char 变量。也就是说,它们需要字符串终止符 ('\0')。

此外,在 calculator_add 中,您没有在 delimiters 中定义足够的空间,因此您的 strcat 会超出末尾,导致 undefined行为。

我已经用错误注释了您的源代码并提供了修复。在可能的情况下,我已经这样做了: //注意/BUG: ... #if 0/* 原始代码 */#else/* 固定代码 */#endif:

static char DEFAULT_DELIMITER = ',';
char *calculator_retrieveFirstToken(char *numbers, const char * delimiters) {
//if (delimiters[strlen(delimiters)-1] == DEFAULT_DELIMITER)
if (delimiters[1] == DEFAULT_DELIMITER) {
return strtok(numbers, delimiters);
}
else {
// NOTE/BUG: this is incorrect -- you must pass a string pointer _not_
// the address of a [single] character
#if 0
char linefeed = '\n';
strtok(numbers, &linefeed);
#else
char *linefeed = "\n";
strtok(numbers, linefeed);
#endif

return strtok(NULL, delimiters);
}
}

char calculator_findDelimiter(const char *numbers) {
char delimiter = DEFAULT_DELIMITER;
if (strlen(numbers) >= 4
&& numbers[0] == '/'
&& numbers[1] == '/') {
delimiter = numbers[2];
}
return delimiter;
}

double calculator_add(char *numbers) {
// NOTE/BUG: this defines only enough space for a single character and the
// '\0' so when it is concatenated to, it is undefined behavior [you're
// running past the end of the string]
#if 0
char delimiters[] = "\n";
#else
char delimiters[4] = "\n";
#endif
char *token = NULL;

// NOTE/BUG: this repeats the same mistake with the strtok above
#if 0
char mainDelimiter = calculator_findDelimiter(numbers);
strcat(delimiters, &mainDelimiter);
#endif

// NOTE: either of these will fix the strcat
#if 0
char mainDelimiter = calculator_findDelimiter(numbers);
char temp[2];
temp[0] = mainDelimiter;
temp[1] = 0;
strcat(delimiters, temp);
#else
delimiters[1] = calculator_findDelimiter(numbers);
delimiters[2] = 0;
#endif

token = calculator_retrieveFirstToken(numbers, delimiters);
return calculator_addEveryToken(token, delimiters);
}

关于c - 函数未接收我作为 const char * 发送的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38416829/

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