Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C problem

Name: stallman 2011-11-06 9:12

Sup prog
i'm using C
i have a problem

i have a dvi file where each line is a command and its parameters
i have to output a file and write what the commands do

i have been able to parse the lines into strings and get the first string which is the command itself

my goal is to compare that first string (the command) with strings contained in a string array (which are all the commands that exist in a dvi file)

that way i can return the corresponding function which will do the calculation using the parameters from the line

problem is that there is a command named set_char_i where i can go from 0 to 127

my command string array looks like that:
char *commandes[40]={"pre", "xxx1", "set", "set_char_i", "set_rule", "put", "put_rule", "down", "fnt_num", "w0", "w", "x0", "x", "y0", "y", "z0", "z", "fnt_def", "post", "post_post", "eop", "bop", "push", "pop"};

how can i make it so that "set_char_i" has i between 0 and 127?

Name: Anonymous 2011-11-06 9:21

char (*commandes)[] = {"bop", "down", "eop", "fnt_def", "fnt_num", "pop", "post", "post_post", "pre", "push", "put", "put_rule", "set", "set_char_0", "set_char_1", "set_char_10", "set_char_100", "set_char_101", "set_char_102", "set_char_103", "set_char_104", "set_char_105", "set_char_106", "set_char_107", "set_char_108", "set_char_109", "set_char_11", "set_char_110", "set_char_111", "set_char_112", "set_char_113", "set_char_114", "set_char_115", "set_char_116", "set_char_117", "set_char_118", "set_char_119", "set_char_12", "set_char_120", "set_char_121", "set_char_122", "set_char_123", "set_char_124", "set_char_125", "set_char_126", "set_char_127", "set_char_13", "set_char_14", "set_char_15", "set_char_16", "set_char_17", "set_char_18", "set_char_19", "set_char_2", "set_char_20", "set_char_21", "set_char_22", "set_char_23", "set_char_24", "set_char_25", "set_char_26", "set_char_27", "set_char_28", "set_char_29", "set_char_3", "set_char_30", "set_char_31", "set_char_32", "set_char_33", "set_char_34", "set_char_35", "set_char_36", "set_char_37", "set_char_38", "set_char_39", "set_char_4", "set_char_40", "set_char_41", "set_char_42", "set_char_43", "set_char_44", "set_char_45", "set_char_46", "set_char_47", "set_char_48", "set_char_49", "set_char_5", "set_char_50", "set_char_51", "set_char_52", "set_char_53", "set_char_54", "set_char_55", "set_char_56", "set_char_57", "set_char_58", "set_char_59", "set_char_6", "set_char_60", "set_char_61", "set_char_62", "set_char_63", "set_char_64", "set_char_65", "set_char_66", "set_char_67", "set_char_68", "set_char_69", "set_char_7", "set_char_70", "set_char_71", "set_char_72", "set_char_73", "set_char_74", "set_char_75", "set_char_76", "set_char_77", "set_char_78", "set_char_79", "set_char_8", "set_char_80", "set_char_81", "set_char_82", "set_char_83", "set_char_84", "set_char_85", "set_char_86", "set_char_87", "set_char_88", "set_char_89", "set_char_9", "set_char_90", "set_char_91", "set_char_92", "set_char_93", "set_char_94", "set_char_95", "set_char_96", "set_char_97", "set_char_98", "set_char_99", "set_rule", "w", "w0", "x", "x0", "xxx1", "y", "y0", "z", "z0"};

Name: Anonymous 2011-11-06 9:23

what the fuck
i'm not going to write a function for each set_character_i saying "character numner i will be printed here"

is it possible in C?

Name: Anonymous 2011-11-06 9:24

>>2
char (*commandes)[]
U MEANA char *commandes[]

Name: Anonymous 2011-11-06 9:32

Special case it, generate the commands programmatically, or use some kind of pattern matcher. What the fuck were you expecting?

>>4
Go away, retard.

Name: Anonymous 2011-11-06 9:33

yes i meant char *commandes[]

Name: Anonymous 2011-11-06 9:36

>>5
so i'll have a string array for all the commands that takes a i that is between numbers right?

Name: Anonymous 2011-11-06 9:38

>>2
thanks to 2 i made this string array

char *commset_char_i[128]=("set_char_1", "set_char_10", "set_char_100", "set_char_101", "set_char_102", "set_char_103", "set_char_104", "set_char_105", "set_char_106", "set_char_107", "set_char_108", "set_char_109", "set_char_11", "set_char_110", "set_char_111", "set_char_112", "set_char_113", "set_char_114", "set_char_115", "set_char_116", "set_char_117", "set_char_118", "set_char_119", "set_char_12", "set_char_120", "set_char_121", "set_char_122", "set_char_123", "set_char_124", "set_char_125", "set_char_126", "set_char_127", "set_char_13", "set_char_14", "set_char_15", "set_char_16", "set_char_17", "set_char_18", "set_char_19", "set_char_2", "set_char_20", "set_char_21", "set_char_22", "set_char_23", "set_char_24", "set_char_25", "set_char_26", "set_char_27", "set_char_28", "set_char_29", "set_char_3", "set_char_30", "set_char_31", "set_char_32", "set_char_33", "set_char_34", "set_char_35", "set_char_36", "set_char_37", "set_char_38", "set_char_39", "set_char_4", "set_char_40", "set_char_41", "set_char_42", "set_char_43", "set_char_44", "set_char_45", "set_char_46", "set_char_47", "set_char_48", "set_char_49", "set_char_5", "set_char_50", "set_char_51", "set_char_52", "set_char_53", "set_char_54", "set_char_55", "set_char_56", "set_char_57", "set_char_58", "set_char_59", "set_char_6", "set_char_60", "set_char_61", "set_char_62", "set_char_63", "set_char_64", "set_char_65", "set_char_66", "set_char_67", "set_char_68", "set_char_69", "set_char_7", "set_char_70", "set_char_71", "set_char_72", "set_char_73", "set_char_74", "set_char_75", "set_char_76", "set_char_77", "set_char_78", "set_char_79", "set_char_8", "set_char_80", "set_char_81", "set_char_82", "set_char_83", "set_char_84", "set_char_85", "set_char_86", "set_char_87", "set_char_88", "set_char_89", "set_char_9", "set_char_90", "set_char_91", "set_char_92", "set_char_93", "set_char_94", "set_char_95", "set_char_96", "set_char_97", "set_char_98", "set_char_99")

Name: Anonymous 2011-11-06 9:40

op here, does python handle these kind of things more easily?

Name: Anonymous 2011-11-06 9:52

>>9
Yes:
commset_char_i = ["set_char_"+str(i) for i in range(128)]

Name: Anonymous 2011-11-06 10:55

can't you do (in pseudocode, not actual C)


if (substr(command, 0, 9) == "set_char_") {
  do_something_with_set_char_number(substr(command, 9))
}

Name: Anonymous 2011-11-06 11:44

>>11
no idea

i don't know if its possible to do the python code as we can see in post 10, using C

Name: Anonymous 2011-11-06 12:22

>>12
No shit, faggot. Python uses a higher level of abstraction.

Name: Anonymous 2011-11-06 12:23

This thread made me cry

Name: Anonymous 2011-11-06 12:28

>>14
Faggots are very sensitive people.

Name: Anonymous 2011-11-06 13:04

>>13
w/e nerd

Name: Anonymous 2011-11-06 13:44

>>15
thanks to 2 i made this string array


>>16
have you considered to stop being a Nigger

Name: Anonymous 2011-11-06 13:59

make a hard coded dfa


  char *letter;
  int state = 0;
  for(;;) {
    switch(*letter) {
      case 's': switch(state) { case 0: state = 1; break;
                                default: state = 0; break;
                              } break;
      case 'e': switch(state) { case 1: state = 2; break;
                                default: state = 0; break;
                              } break;
      case 't': switch(state) { case 2: state = 3; break;
                                default: state = 0; break;
                              } break;
      case '_': switch(state) { case 3: state = 4; break;
                                case 8: state = 9; break;
                                default: state = 0; break;
                              } break;
      case 'c': switch(state) { case 4: state = 5; break;
                                default: state = 0; break;
                              } break;
      case 'h': switch(state) { case 5: state = 6; break;
                                default: state = 0; break;
                              } break;
      case 'a': switch(state) { case 6: state = 7; break;
                                default: state = 0; break;
                              } break;
      case 'r': switch(state) { case 7: state = 8; break;
                                default: state = 0; break;
                              } break;
      default: state = 0; break;
    }
    switch(state) {
      case 9: parse_number(letter); break;
      default: break;
    }
    letter++;
  }

Name: Anonymous 2011-11-06 14:52

>>18

case 's': ...
case 'e': ...
case 't': ...


Nigga are you serious

Never heard of strcmp, have you, jewboy

Name: Anonymous 2011-11-06 14:55

>>17
There is no such thing as a "string array" you fucking dumb jew. Psstt... a string in C is an array of characters that get terminated with '\0'.

Name: Anonymous 2011-11-06 15:29

>>19

but...but...but..this way I can strcmp the buffer with a finite set of strings, determined at compile time, with the efficiency of a single strcmp!

for example, if the strings were: hi me how are you

I could do:


#define NOTHING -1
#define HI 2
#define ME 4
#define HOW 7
#define ARE 11
#define YOU 16

char *start = buffer;
char *position;
int state = 0;
int token = NOTHING;

for(;;) {
  switch(*position) {
    case 'h': switch(state) { case 0: state = 1; break;
                              default: state = 0; break;
                            }
    case 'i': switch(state) { case 1: state = 2; break;
                              default: state = 0; break;
                            }

    case 'm': switch(state) { case 0: state = 3; break;
                              default: state = 0; break;
                            }
    case 'e': switch(state) { case 3: state = 4; break;
                              default: state = 0; break;
                            }

    case 'h': switch(state) { case 0: state = 5; break;
                              default: state = 0; break;
                            }
    case 'o': switch(state) { case 5: state = 6; break;
                              default: state = 0; break;
                            }
    case 'w': switch(state) { case 6: state = 7; break;
                              default: state = 0; break;
                            }

    case 'a': switch(state) { case 8: state = 9; break;
                              default: state = 0; break;
                            }
    case 'r': switch(state) { case 9: state = 10; break;
                              default: state = 0; break;
                            }
    case 'e': switch(state) { case 10: state = 11; break;
                              default: state = 0; break;
                            }

    case 'y': switch(state) { case 0: state = 12; break;
                              default: state = 0; break;
                            }
    case 'o': switch(state) { case 13: state = 14; break;
                              default: state = 0; break;
                            }
    case 'u': switch(state) { case 15: state = 16; break;
                              default: state = 0; break;
                            }

    case '\0': state = NOTHING; break;
  }
  switch(state) {
    case NOTHING: return NULL; break;
    case HI:
    case ME:
    case HOW:
    case ARE:
    case YOU:
      return state;
      break;
  }
}


And this then reduces to:


#define NOTHING -1
#define HI 2
#define ME 4
#define HOW 7
#define ARE 11
#define YOU 16

char *start = buffer;
char *position;
int state = 0;
int token = NOTHING;

for(;;) {
  switch(*position) {
    case 'h': switch(state) { case 0: state = 1; break;
                              default: state = 0; break;
                            }
    case 'i': switch(state) { case 1: state = 2; break;
                              default: state = 0; break;
                            }

    case 'm': switch(state) { case 0: state = 3; break;
                              default: state = 0; break;
                            }
    case 'e': switch(state) { case 3: state = 4; break;
                              case 10: state = 11; break;
                              default: state = 0; break;
                            }

    case 'o': switch(state) { case 1: state = 6; break;
                              case 13: state = 14; break;
                              default: state = 0; break;
                            }
    case 'w': switch(state) { case 6: state = 7; break;
                              default: state = 0; break;
                            }

    case 'a': switch(state) { case 8: state = 9; break;
                              default: state = 0; break;
                            }
    case 'r': switch(state) { case 9: state = 10; break;
                              default: state = 0; break;
                            }

    case 'y': switch(state) { case 0: state = 12; break;
                              default: state = 0; break;
                            }
    case 'u': switch(state) { case 15: state = 16; break;
                              default: state = 0; break;
                            }

    case '\0': state = NOTHING; break;
  }
  switch(state) {
    case NOTHING: return NULL; break;
    case HI:
    case ME:
    case HOW:
    case ARE:
    case YOU:
      return state;
      break;
  }
}

Name: Anonymous 2011-11-06 15:34

>>19
Using strcmp() would make it into linear time you fucking idiot.

Name: Anonymous 2011-11-06 15:36

>>21

>>19 is a fucking stupid shit. Most, if not all C compilers can optimize a switch/case statement to a jump table. This in turn makes it constant time. However using strcmp() will result in linear time.

Name: Anonymous 2011-11-06 15:46

>>20
Nigger doesn't realize >>17-san was quoting >>8

also you just gotta love how jewboy number >>20-23 is desperately trying to make it look as if several people were hating on >>19-san



good god the autism in this thread is hilarious

Name: Anonymous 2011-11-06 15:49

>>24
I'm quoting the ANSI/ISO C standards you fucking uneducated bitch.

Name: Anonymous 2011-11-06 15:55

>>25
look at me i'm trying to cope with my autistic rage by using strong words! fear me!

nope, jewboy

Name: Anonymous 2011-11-06 15:55

>>24
Autism is a very serious disorder. It most certainly is not under any circumstances `hilarious'! Many of these people are most likely unable to live a normal life outside of /prog/ due to their tragic condition.

Name: Anonymous 2011-11-06 16:01

>>21
this switches by an unitialized *position in an infite loop,
 srsly is that what it was supposed to do?
my C99 is rusty btw
>>25
yup. which great job, these 2 clock cycles you gained day-um,not really worth all the typing, linear ain't that bad, OP doesn't even have moar than 10 elements anywheere

Name: Anonymous 2011-11-06 17:34

i fucking love \prog\

Name: Anonymous 2011-11-06 18:20

the /prog/ theorem
every normal iteration can be replaced by a cluster fuck of pointless switch case statements

Name: Anonymous 2011-11-06 18:59

>>26
Go scrub another toilet you fucking bitch. That is all will ever amount to you mental midget you.

Name: Anonymous 2011-11-06 19:05

>>28

yup. which great job, these 2 clock cycles you gained day-um,not really worth all the typing, linear ain't that bad, OP doesn't even have moar than 10 elements anywheere

It's more than that you idiot. If you take the disjoint union of the case statements over some f: X -> T... never mind. I don't think you have the mental capacity understand anything beyond googleable C.

Name: Anonymous 2011-11-06 19:42

>>32
ever considered why noone uses relativity theory on those basic Problems like:
...how much time will it take lolcat A to cross the road at velocity V...
None should ever be encouraged to use this rocket science math just to compare 2 strings lol,(and btw the compiler can prolly optimize it)

Name: Anonymous 2011-11-06 19:50

if(sscanf(cmdstr,"set_char_%d",&i)==1) {
 ...
}


Put that after all your other cases.

Check this out too:
http://www-users.math.umd.edu/~asnowden/comp-cont/dviread.c

Name: Anonymous 2011-11-06 19:59

>>33
Oh aren't you cool. You can lol on the internet. With that....

You aren't comparing strings you fucking jew. You're comparing each character in an array of characters. This is because a string in C is an array of characters that terminates with '\0'.

So if you compare something like "xle" in C, you would be doing a minimum of 4 comparisions. Now depending on how the if/else in structured, the number of comparisions could actually increase exponentially.

And just for the record you little fucking annoying stupid shit jew, the compiler can optimize it. However, you will still be stuck with 4 comparisons. Can you tell us why? I bet not.

Now go scrub another toilet you fucking mental midget.

Name: Anonymous 2011-11-06 21:48

>>28

the code is incomplete, and I forgot to increment le pointer. Outside of the loop, there would be some line buffered reading, or maybe just fixed sized buffering, and then the dfa would loop over the buffer, returning tokens that have sent it into an accept state. Normal people never hand code their dfas, and instead use tools like lex/flex to generate the code from a list of regular expressions to match. It isn't difficult to algorithmically generate such a dfa using the fewest amount of possible states, so there is no advantage to hand coding a dfa, but there is autistic enjoyment to be had, much like playing with legos.

If this is all new, check out http://en.wikipedia.org/wiki/Deterministic_finite_state_machine , although it is a bit brief and the language is abstract.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List