i\'m doing something like excel, i have something like this:
1 2 3
A1 B1 C1
where it replaces the content for
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
#include
#include
#include
typedef struct pair {
char key[16];
int value;
} Pair;
int main(void){
Pair var_table[] = { { "A1", 1 }, {"B1", 2}, { "C1", 3 }};//sorted
size_t size = sizeof(var_table)/sizeof(*var_table);
char input[256] = "A1+B1+C1";
char outbuff[4096];
char *p;
int offset = 0;
printf("This -> %s\n\n", input);
for(p=input;*p;){
Pair *var;
char op, *opp;
opp=strpbrk(p, "+-*/");
if(opp){
op = *opp;
*opp = '\0';//cut string at op position
}
//search key(p)
var = (Pair*)bsearch(p, var_table, size, sizeof(*var), (int (*)(const void *, const void *))strcmp);
if(var)//find!
offset += sprintf(outbuff + offset, "%d", var->value);//or store array?
else
offset += sprintf(outbuff + offset, "%s", "#UNKNOWN_VAR_NAME#");
if(opp){
offset += sprintf(outbuff + offset, "%c", op);
p = opp + 1;
} else
break;
}
printf("to -> %s\n", outbuff);
return 0;
}