i'm doing something like excel, i have something like this:
1 2 3
A1 B1 C1
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");
}
But when i have A1+B1+C1 and another things, i cant replace the total content, because other references will be removed....
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
....
Thanks.
I think that as may be replaced with values by table lookup by cutting the name simply its corresponding value in the name of the string you want to replace.
E.g
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
so you don't paint yourself in an awkward corner of error-prone text-handling in 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):
Using just two helpers:
we can write the following demo program:
which prints the well-known test phrase as per the comment. Now,
get_cell_value
is really simple:And
expand_cell_references
is slightly more involved, being a simple DFA parser:I took some shortcuts there, because this grammar is so minimalist, but it should give you a proper idea of where to start.
Any time you run into some string manipulation problem in C, your first instinct should be to look at
string.h
and see what's available there. As outlined in several answers here, there is no function to do string replacement directly, but it should be possible to usestrstr
andstrncpy
to find occurrences of substrings and then copy the replacement (into a new buffer so as not to clobber the rest of the original, obviously).