替换字符串 [英] replacing pieces of string
问题描述
我正在做一些像excel这样的事情,我有这样的东西:
i'm doing something like excel, i have something like this:
1 2 3
A1 B1 C1
其中替换指定内容的内容,其中A1替换内容1.
B1替换2 ...等等的内容...
where it replaces the content for specified content, where A1 replaces the content for 1. B1 replaces the content of 2...and etc...
我正在使用多维数组,我做这样的事情:
i'm using a multidimensional array, and i do the things like this:
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和另一件事情,我不能取代总内容,因为其他引用将被删除....
But when i have A1+B1+C1 and another things, i cant replace the total content, because other references will be removed....
所以,在单元格A1 + B1 + C1,我想改变B1指定的内容。 ...我想要这样:
So, at the cell, A1+B1+C1, i wanna change B1 for the content specified....i wanna have like this:
This -> A1+B1+C1
to -> 1+2+3
....
谢谢。
推荐答案
您可能只需重新使用c++ solution (通过硬编码替换通用迭代器 char *
代替)。
You might just reuse this c++ solution (replacing the generic iterators by hardcoding char*
instead).
我给了它一个旋转。但是,我想发出警告:看起来你正在尝试实现一个表达式解析器。我强烈建议您使用
I gave it a whirl. However, I wish to give a warning: it looks like you're trying to implement an expression parser. I'd strongly advise you to either
- handroll a(递归下降)解析器
- 使用flex / bison(或lex / yacc)
所以你不要把自己画在容易出错的文本处理的一个尴尬的角落C。
so you don't paint yourself in an awkward corner of error-prone text-handling in C.
编辑:我重写了你的C程序使用C ++;你可以看到它在这里工作。
编辑2:在纯C中另外修复C程序: http://ideone.com/ExnufJ 更新以支持现在的迭代扩展
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方法:
The answer just concerns itself with the pure C approach:
所以,让我们开始吧。我假设一个样本电子表格(它可以包含数字而不是字符串):
So, let's get started. I assumed a sample "spreadsheet" (it could contain numbers instead of strings):
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*/
我们可以编写以下演示程序:
we can write the following demo program:
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
非常简单:
which prints the well-known test phrase as per the comment. Now, get_cell_value
is really simple:
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解析器:
And expand_cell_references
is slightly more involved, being a simple DFA parser:
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;
}
我在那里采取了一些捷径,因为这种语法是如此简约,但应该给你一个正确的想法从哪里开始。
I took some shortcuts there, because this grammar is so minimalist, but it should give you a proper idea of where to start.
看到一个现场演示这里 http://ideone.com/kS7XqB ,所以你可以自己玩。请注意,我将调试(断言)添加到
get_cell_value
函数中,以便您不会意外引用超出范围的索引。
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.
这篇关于替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!