|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi,
Lets say I have some 10 functions named - display1() display2() display3() display4() ...... ...... display10() Assume that all these functions have been defined. Now we have to modify the program so that whatever function name is entered by keyboard will be invoked. For example if we are accepting string display5 from the keyboard then display5() method should be invoked. The flow should be generic so that the code need not be modified even though some more functions are added. If any one has any idea do let me know. Regards Shiv |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Shiv Ranjan said:
> Hi, > Lets say I have some 10 functions named - > > display1() > display2() > display3() > display4() > ..... > ..... > display10() > > Assume that all these functions have been defined. Now we have to > modify the program so that whatever function name is entered by > keyboard will be invoked. Construct a lookup table (e.g. hash table, binary search tree, or something like that) whose entries are structures containing a key (the name of the function, expressed as a string) and a payload (a pointer to the function associated with that string). Yes, you will have to recompile whenever you add a new entry to the table. -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Richard Heathfield wrote:
> Shiv Ranjan said: > >> Hi, >> Lets say I have some 10 functions named - >> >> display1() >> display2() >> display3() >> display4() >> ..... >> ..... >> display10() >> >> Assume that all these functions have been defined. Now we have to >> modify the program so that whatever function name is entered by >> keyboard will be invoked. > > Construct a lookup table (e.g. hash table, binary search tree, or > something like that) Any of these will be overkill for this task I think, and likely to give more trouble than the main assignment. A simple linear search should be fine. Especially as it seems there will be one search per keyboard entry. -- Bartc |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Shiv Ranjan wrote:
> Hi, > Lets say I have some 10 functions named - > > display1() > display2() > display3() > display4() > ..... > ..... > display10() > > Assume that all these functions have been defined. Now we have to > modify the program so that whatever function name is entered by > keyboard will be invoked. For example if we are accepting string > display5 from the keyboard then display5() method should be invoked. > The flow should be generic so that the code need not be modified even > though some more functions are added. > > If any one has any idea do let me know. /* BEGIN new.c */ #include <stdio.h> #include <string.h> void display1(void); void display2(void); void display3(void); void display4(void); void display5(void); int main(int argc, char **argv) { struct { char *string; void (*func)(void); } table[] = { {"display1", display1}, {"display2", display2}, {"display3", display3}, {"display4", display4}, {"display5", display5}, { NULL, NULL} }, *p = table; putchar('\n'); if (argc > 1) { while (p -> string != NULL) { if (strcmp(argv[1], p -> string) == 0) { p -> func(); break; } ++p; } if (p -> string == NULL) { puts("Function not found."); } } else { puts("Not enough argc."); } return 0; } void display1(void) { puts("A"); } void display2(void) { puts("B"); } void display3(void) { puts("C"); } void display4(void) { puts("D"); } void display5(void) { puts("E"); } /* END new.c */ -- pete |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
"Szabolcs Borsanyi" <s.borsanyi@sussex.ac.uk> wrote in message news:20080528131219.GB13592@kroto.pact.cpes.susx.a c.uk... > On Wed, May 28, 2008 at 07:54:53AM -0500, pete wrote: >> Shiv Ranjan wrote: >>> Hi, >>> Lets say I have some 10 functions named - >>> >>> display1() > [snip] >>> display10() >>> >> struct { >> char *string; >> void (*func)(void); >> } table[] = { >> {"display1", display1}, >> {"display2", display2}, >> {"display3", display3}, >> {"display4", display4}, >> {"display5", display5}, >> { NULL, NULL} >> }, *p = table; > Surely, you will want to define macros: > #define F(x) {#x,x} > #define G(x) F(display ##x) > {G(1),G(2),G(3),{NULL,NULL}} > rather ther writing everything twice. I think these are just example function names and bodies. The real names could be all different. Anyway as this is homework you don't want to appear too clever. -- Bartc |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Bartc said:
> Richard Heathfield wrote: <snip> >> Construct a lookup table (e.g. hash table, binary search tree, or >> something like that) > > Any of these will be overkill for this task I think, and likely to give > more trouble than the main assignment. > > A simple linear search should be fine. A linear search through *what*? Well, gee, a lookup table. -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
"Richard Heathfield" <rjh@see.sig.invalid> wrote in message news:Q4mdnSqGmuFRKqDVnZ2dnUVZ8uWdnZ2d@bt.com... > Bartc said: > >> Richard Heathfield wrote: > > <snip> > >>> Construct a lookup table (e.g. hash table, binary search tree, or >>> something like that) >> >> Any of these will be overkill for this task I think, and likely to give >> more trouble than the main assignment. >> >> A simple linear search should be fine. > > A linear search through *what*? Well, gee, a lookup table. I was referring to your suggestions of using hash tables and binary trees. But, did I really have to clarify that? -- bartc |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Bartc said:
> > "Richard Heathfield" <rjh@see.sig.invalid> wrote in message > news:Q4mdnSqGmuFRKqDVnZ2dnUVZ8uWdnZ2d@bt.com... >> Bartc said: >> >>> Richard Heathfield wrote: >> >> <snip> >> >>>> Construct a lookup table (e.g. hash table, binary search tree, or >>>> something like that) >>> >>> Any of these will be overkill for this task I think, and likely to give >>> more trouble than the main assignment. >>> >>> A simple linear search should be fine. >> >> A linear search through *what*? Well, gee, a lookup table. > > I was referring to your suggestions of using hash tables and binary > trees. Each of those suggestions is perfectly reasonable, and they offer solutions that are scalable, whereas a linear search is very much an inferior stopgap solution - adequate (barely) if all you want is a pass mark for a college assignment, but not a tool you'd want to use very often for this problem in the Real World. You see, lookup tables have a habit of growing. (See "The Practice of Programming" for a fine example of how adopting a stopgap solution can be costly in the long term.) > But, did I really have to clarify that? I am at a loss to understand what you thought you were clarifying. -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999 |
|
![]() |
| Outils de la discussion | |
|
|