home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:12238 comp.programming:2289
- 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
- From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
- Newsgroups: comp.lang.c,comp.programming
- Subject: Re: A LITTLE BRAINTEASER...
- Message-ID: <9222601.8298@mulga.cs.mu.OZ.AU>
- Date: 12 Aug 92 15:42:49 GMT
- References: <aet.713608023@munagin> <s1110238.713617229@giaeb>
- Sender: news@cs.mu.OZ.AU
- Organization: Computer Science, University of Melbourne, Australia
- Lines: 85
-
- s1110238@giaeb.cc.monash.edu.au (Lee Hollingworth) writes:
-
- >aet@mullian.ee.mu.OZ.AU (bert thompson) writes:
- >
- >>my question is this:
- >
- >>is it possible to write a program that reverse the order of input lines
- >>without using data structures.
- >
- >>the
- >>quick
- >>brown
- >
- >>becomes:
- >
- >>brown
- >>quick
- >>the
- >
- >>the rules of the game are you can use anything but data structures, or more
- >>specifically you cannot use the heap.
- >>i think it can be done, but i can't figure out how.
- >>(use of a few mutually recursive functions i think is the go.)
- >
- >>anyone got an idea??
- >
- >#include <stdio.h>
- >
- >void main(void)
- >{
- > char str[81];
- >
- > if (!*gets(str)) {
- > return;
- > }
- > else {
- > main();
- > puts(str);
- > }
- >}
- >
- >The above uses one local variable like your previous example..., but maybe
- >this is not what you meant.
- >
- >Lee Hollingworth
-
- Yes, but it doesn't work for lines longer than 80 characters.
-
- Also it does use an array, which is a data structure.
- Bert wanted to avoid arrays:
-
- >>the interesting thing about the program is that it uses no data structures
- >>(structs, arrays, pointers, etc..), to do its thing.
-
- I think that the problem is unsolveable without at least using pointers.
- Here's an example that works for arbitrary length lines and uses only
- chars and pointers. It's not strictly ANSI-compliant, but it is very portable.
-
- #include <stdio.h>
- void rev(int *l,int **p) {
- int c;
- int *n; /* look, Ma, no structs or arrays, and no dynamic allocation! */
-
- *(l?p:&l)=&c; /* I'm practicing for the obfuscated C competition */
- c = getchar();
- if (c == EOF) return;
- if (c == '\n') {
- main();
- n = 0;
- while(l) {
- putchar(*l);
- l=*(int**)((char*)l+((char*)&n-(char*)&c)); /* PUKE! */
- }
- } else {
- rev(l,&n);
- }
- }
-
- void main(void) { rev(0,0); }
-
- --
- Fergus Henderson fjh@munta.cs.mu.OZ.AU
- This .signature VIRUS is a self-referential statement that is true - but
- you will only be able to consistently believe it if you copy it to your own
- .signature file!
-