I have the following codeblock I'm using for German verb drills:
if (strcmp(*option, "sein") == 0)
*option = linie.sein;
if (strcmp(*option, "haben") == 0)
*option = linie.haben;
if (strcmp(*option, "possessiv") == 0)
*option = linie.possessiv;
if (strcmp(*option, "reflexiv") == 0)
*option = linie.reflexiv;
if (strcmp(*option, "accusativ") == 0)
*option = linie.accusativ;
if (strcmp(*option, "dativ") == 0)
*option = linie.dativ;
However I would like to condense it to something like:
*option = linie.(*option);
Or perhaps:
*option = linie.(*option)();
Unfortunately neither of these work. Any ideas?
Edit @dasblinkenlight:
typedef struct
{
char subjekt[20];
char sein[20];
char haben[20];
char possessiv[20];
char reflexiv[20];
char accusativ[20];
char dativ[20];
} satz;
satz linie =
{
.subjekt = "",
.sein = "",
.haben = "",
.possessiv = "",
.reflexiv = "",
.accusativ = "",
.dativ = ""
};
char *option = argv[1];
Dynamic Programming is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of subproblems, so that we do not have to re-compute them when needed later.
Dynamic programming approach was developed by Richard Bellman in 1940s. It was an attempt to create the best solution for some class of optimization problems, in which we find a best solution from smaller sub problems.
When the allocation of memory performs at the compile time, then it is known as static memory. When the memory allocation is done at the execution or run time, then it is called dynamic memory allocation. 2. The memory is allocated at the compile time. The memory is allocated at the runtime.
Kirilenko's answer is a good one and works well for short structures like yours. However, for longer structures, maintaining all of the strcmp calls can be cumbersome. To solve this, you can define a relationship between the keyword you are trying to match and the offset of the corresponding element in your structure.
struct relation
{
char keyword[20];
int offset;
};
Then you can use the offsetof macro (in stddef.h) to link the keyword to its position in your structure.
#define REL_LEN (7)
struct relation rel[REL_LEN] = {
{"subjekt", offsetof(satz, subjekt) },
{"sein", offsetof(satz, sein) },
{"haben", offsetof(satz, haben) },
{"possessiv", offsetof(satz, possessiv) },
{"reflexiv", offsetof(satz, reflexiv) },
{"accusativ", offsetof(satz, accusativ) },
{"dativ", offsetof(satz, dativ) }
};
Finally, the function to retrieve your character string using the mapping above could look something like this.
char *lookup_keyword(const satz *linie, const char *option,
const struct relation *rel, size_t rel_size)
{
int i;
char *pchar = (char *)linie;
for (i=0; i<rel_size; i++)
{
if (strcmp(option, rel->keyword) == 0)
{
pchar += rel->offset;
return pchar;
}
rel++;
}
printf("Error: no mapping found matching %s!\n", option);
return "";
}
And you can invoke it like so
char *option = argv[1];
printf("Result for %s: %s\n", option,
lookup_keyword(&linie, option, rel, REL_LEN));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With