gpt4 book ai didi

c - 更换字符串片段

转载 作者:行者123 更新时间:2023-12-02 21:59:02 25 4
gpt4 key购买 nike

我正在做类似Excel的事情,我有这样的事情:

1         2           3
A1 B1 C1

其中它替换指定内容的内容,其中A1替换1的内容。B1替换2的内容...等等...

我正在使用多维数组,并且我执行如下操作:

int offset = 0, readCharCount;
while(sscanf(matris[i][c] + offset, "%c%d%*c%n", &col, &linha, &readCharCount) == 2){
//printf("%c, %d\n", col, linha);
//strcpy(matris[i][c], matris[linha-1][col - 'A']);

offset += readCharCount;
//printf(" {%c, %d}", col, linha);
//printf("\n");
}

但是当我有A1+B1+C1和其他东西时,我无法替换全部内容,因为其他引用将被删除......

所以,在单元格 A1+B1+C1 处,我想将 B1 更改为指定的内容......我想要这样:

This -> A1+B1+C1

to -> 1+2+3

...

谢谢。

最佳答案

您可以重复使用此 c++ solution (通过硬编码 char* 来替换通用迭代器)。

我试了一下。但是,我想发出警告:看起来您正在尝试实现一个表达式解析器。我强烈建议你要么

  • handroll(递归下降)解析器
  • 使用 flex/bison(或 lex/yacc)

这样你就不会陷入在 C 语言中容易出错的文本处理的尴尬境地。

Edit: I rewrote your C program using C++; you can see it working live here.

Edit 2: Another fixup of your C program in pure C: http://ideone.com/ExnufJ updated to support iterative expansions now, too

答案只涉及纯 C 方法:

那么,让我们开始吧。我假设了一个示例“电子表格”(它可以包含数字而不是字符串):

const char* cells[][4] = {
/* A B C D */
{ "the" , "lazy" , "cow" , "jumped" }, /* 1 */
{ "over" , "the" , "quick", "brown" }, /* 2 */
{ "paper", "packages", "tied" , "up" }, /* 3 */
{ "with" , "silver" , "white", "winters" }, /* 4 */
{ "that" , "melt" , "fox" , "springs" }, /* 5 */
};

仅使用两个助手:

const char* get_cell_value(const char* coordinate_b, const char* coordinate_e);
char* expand_cell_references(const char* f, const char* const l, char* o); /*the magic engine*/

我们可以编写以下演示程序:

int main()
{
const char in[] = "The C2 D2 C5 D1 A2 B2 B1 dog!";

char out[1024] = {0};
expand_cell_references(in, in+strlen(in), out);
puts(out); /* "The quick brown fox jumped over the lazy dog!" */

return 0;
}

根据注释打印众所周知的测试短语。现在,get_cell_value 非常简单:

const char* get_cell_value(const char* coordinate_b, const char* coordinate_e)
{
size_t col = 0, row = 0;
const char* it;
for (it=coordinate_b; it != coordinate_e; ++it)
{
if (*it >= 'A' && *it <= 'Z')
col = 26*col + (*it - 'A');
if (*it >= '0' && *it <= '9')
row = 10*row + (*it - '0'); /* or use atoi and friends */
}
row--; /* 1-based row nums in Excel */

return cells[row][col]; /* 1-based indexes in Excel */
}

并且 expand_cell_references 稍微复杂一点,是一个简单的 DFA 解析器:

char* expand_cell_references(const char* f, const char* const l, char* o)
{
enum parser_state {
other,
in_coord_col,
in_coord_row
} state = other;

/*temporary storage for coordinates being parsed:*/
char accum[16] = {0};
char* accit = accum;
while (f!=l)
{
switch(state) /*dummy, the transitions flow in fallthrough order for now*/
{
case other:
*(accit = accum) = 0; /*reset the accumulator*/
while (f!=l && !(*f>='A' && *f<='Z'))
*o++ = *f++;
/*fallthrough*/
case in_coord_col:
while (f!=l && *f>='A' && *f<='Z')
*accit++ = *f++;
/*fallthrough*/
case in_coord_row:
{
const char* expanded = accum;
if (f!=l && *f>='0' && *f<='9')
{
while (f!=l && *f>='0' && *f<='9')
*accit++ = *f++;
expanded = get_cell_value(accum, accit);
}
else
{
*accit = 0;
}
while (*expanded)
*o++ = *expanded++;
continue; /*state = other;*/
}
}
}
return o;
}

我在那里采取了一些捷径,因为这个语法非常简单,但它应该让您正确了解从哪里开始。

See a live demo here http://ideone.com/kS7XqB so you can play with it yourself. Note that I added debugging (asserts) to the get_cell_value function so you don't accidentally reference out-of-bounds indexes.

关于c - 更换字符串片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17241897/

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