home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / c / 12238 < prev    next >
Encoding:
Text File  |  1992-08-13  |  2.6 KB  |  98 lines

  1. Xref: sparky comp.lang.c:12238 comp.programming:2289
  2. Path: sparky!uunet!dtix!darwin.sura.net!zaphod.mps.ohio-state.edu!rpi!batcomputer!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
  3. From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
  4. Newsgroups: comp.lang.c,comp.programming
  5. Subject: Re: A LITTLE BRAINTEASER...
  6. Message-ID: <9222601.8298@mulga.cs.mu.OZ.AU>
  7. Date: 12 Aug 92 15:42:49 GMT
  8. References: <aet.713608023@munagin> <s1110238.713617229@giaeb>
  9. Sender: news@cs.mu.OZ.AU
  10. Organization: Computer Science, University of Melbourne, Australia
  11. Lines: 85
  12.  
  13. s1110238@giaeb.cc.monash.edu.au (Lee Hollingworth) writes:
  14.  
  15. >aet@mullian.ee.mu.OZ.AU (bert thompson) writes:
  16. >
  17. >>my question is this:
  18. >
  19. >>is it possible to write a program that reverse the order of input lines
  20. >>without using data structures.
  21. >
  22. >>the
  23. >>quick
  24. >>brown
  25. >
  26. >>becomes:
  27. >
  28. >>brown
  29. >>quick
  30. >>the
  31. >
  32. >>the rules of the game are you can use anything but data structures, or more
  33. >>specifically you cannot use the heap.
  34. >>i think it can be done, but i can't figure out how.
  35. >>(use of a few mutually recursive functions i think is the go.)
  36. >
  37. >>anyone got an idea??
  38. >
  39. >#include <stdio.h>
  40. >
  41. >void main(void)
  42. >{
  43. >    char str[81];
  44. >
  45. >    if (!*gets(str)) {
  46. >        return;
  47. >    }
  48. >    else {
  49. >        main();
  50. >        puts(str);
  51. >    }
  52. >}
  53. >
  54. >The above uses one local variable like your previous example..., but maybe
  55. >this is not what you meant.
  56. >
  57. >Lee Hollingworth
  58.  
  59. Yes, but it doesn't work for lines longer than 80 characters.
  60.  
  61. Also it does use an array, which is a data structure.
  62. Bert wanted to avoid arrays:
  63.  
  64. >>the interesting thing about the program is that it uses no data structures
  65. >>(structs, arrays, pointers, etc..), to do its thing.
  66.  
  67. I think that the problem is unsolveable without at least using pointers.
  68. Here's an example that works for arbitrary length lines and uses only
  69. chars and pointers. It's not strictly ANSI-compliant, but it is very portable.
  70.  
  71. #include <stdio.h>
  72. void rev(int *l,int **p) {
  73.     int c;   
  74.     int *n;  /* look, Ma, no structs or arrays, and no dynamic allocation! */
  75.  
  76.     *(l?p:&l)=&c; /* I'm practicing for the obfuscated C competition */
  77.     c = getchar();
  78.     if (c == EOF) return;
  79.     if (c == '\n') {
  80.     main();
  81.     n = 0;
  82.         while(l) {
  83.             putchar(*l);
  84.             l=*(int**)((char*)l+((char*)&n-(char*)&c)); /* PUKE! */
  85.         }
  86.     } else {
  87.         rev(l,&n);
  88.     }
  89. }
  90.  
  91. void main(void) { rev(0,0); }
  92.  
  93. -- 
  94. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  95. This .signature VIRUS is a self-referential statement that is true - but 
  96. you will only be able to consistently believe it if you copy it to your own
  97. .signature file!
  98.