[Lula] Intermediate C class in the LA area?

Jesse W jessw at netwood.net
Sat Jul 28 16:55:19 EDT 2007


Sorry I didn't get back to you sooner...

Actually, I finally gave up trying to get meaningful numbers out of the 
C timing functions, and simply used zsh's "time" builtin and a shell 
for loop, which didn't get entangled with the file-handling code I was 
testing.

However, I am now throughly stuck regarding exercise 5.11, which asks 
for a tabs-to-spaces (and reverse) program that takes a list of tab 
stops as an argument.  What I'm stuck on is trying to figure out how to 
test it properly.  I *think* the code I have works, more or less, most 
of the time -- but I want it to be *right*, and I can't get my mind 
around the variable, list of tab stops nature of the problem.

I've attached the code I currently have to the bottom; it should be 
sufficiently commented...

Any help or suggestions greatly appreciated.

Jesse Weinstein

-----

/* Written by Jesse Weinstein <jessw at netwood.net>, based on
    code from K&R. Sat Jul 28 13:53:32 2007
    Released only under the implicit license granted by
    posting it on the publically-archived list lula at lula.org.
*/
#include <stdio.h>

#define DEBUG

#ifdef DEBUG
/*debug printing functions -- there's probably a better way to do 
this...*/
#define debugprint(a) fprintf(stderr, a)
#define debugprint1(a,b) fprintf(stderr, a, b)
#define debugprint2(a,b,c) fprintf(stderr, a, b, c)
#define debugprint3(a,b,c,d) fprintf(stderr, a, b, c, d)
#else
#define debugprint(a)
#define debugprint1(a,b)
#define debugprint2(a,b,c)
#define debugprint3(a,b,c,d)
#endif

/* this lets me use alternative, visible replacements for the actual
    tab or space characters */
#define TABC 'T'
#define SPAC 'S'

/* tabstops is an array of integers, terminated by -1, in ascending 
order. */

/* Entab -- replace spaces with tabs in a tab-stopped fashion. */
int entab(int tabstops[]) {
   debugprint("\nEntab\n");
   int c;
   int coln=0, spaces=0; /* coln is the column number;
			   spaces is a count of how many spaces
			   have been seen but not yet printed */
   int *first_tabstop=tabstops;
   while ((c=getchar()) != EOF) { /* get each char one after another */
     debugprint2("+%02i|%02x+",coln,c);
     if (c=='\n') { /* reset the coln and tabstop list on each newline */
       coln=0;
       tabstops=first_tabstop; /* put the pointer back to the first 
tabstop */
     } else if (coln==*tabstops) { /* if at the next tabstop... */
       tabstops++; /* bump the pointer to the next tabstop */
       if (spaces) { /* if also in a range of spaces... */
	spaces=0; /* replace the range of spaces with a tab. */
	putchar(TABC);
       }
     } else if (c==TABC) { /* if a tab is seen in the input... */
       coln=*tabstops; /* set the column number to the current tabstop */
       tabstops++; /* bump the pointer to the next tabstop */
     } else if (c==SPAC) { /* if a space is seen in the input... */
       spaces++;
     } else { /* otherwise, print any saved up spaces, and then the 
normal
		character */
       for(;spaces>0;spaces--)
	putchar(SPAC);
       putchar(c);
     }
     coln++; /* bump one more char along */
   }
   debugprint("\nEntab done.\n");
   return 0;
}


/* Detab -- replace tabs with spaces in a tab-stopped fashion. */
int detab(int tabstops[]) {
   debugprint("\nDetab\n");
   int c;
   int coln=0;
   int *first_tabstop=tabstops;
   while ((c=getchar()) != EOF) {
     debugprint2("+%02i|%c+",coln,c);
     if (c==TABC) { /* if a tab is seen... */
       debugprint1("!%02i!",*tabstops);
       if (coln==*tabstops) { /* and the tab is at a tabstop... */
	tabstops++;
	coln++;
	putchar(SPAC);
       }
       for (;coln!=*tabstops;coln++) /* always print spaces until the 
next
				       tabstop is reached */
	putchar(SPAC);
       tabstops++; /* and bump the tabstop list along */
     } else { /* otherwise, if anything else is seen... */
       if (coln==*tabstops) { /* if at a tabstop, bump the list */
	tabstops++;
       }
       if (c=='\n') { /* reset on newlines */
	coln=-1;
	tabstops=first_tabstop;
       }
       coln++;
       putchar(c);
     }
   }
   debugprint("\nDetab done.\n");
   return 0;
}

#include <string.h>
int main(int argc, char *argv[]) {
   int ts[]={8, 16, 18, 22, 49, -1}; /* fixed tabstop list for testing */
   debugprint1("argv[0]:%s\n",argv[0]);
   if (strstr(argv[0], "detab")) { /* if "detab" is contained in the name
				     this program is called under... */
     return detab(ts);
   } else {
     return entab(ts);
   }
}

------

On Jul 14, 2007, at 11:02 AM, Samuel Gasster wrote:
> I'm using Mac OS X and I have the Dev Tools installed, so if you can 
> post some same code I can try to help you figure out why it isn't 
> working.
>
> I'm assuming you installed the XTools - which version are you running?
>
> How are you compiling, from the command line using gcc?
>
> Jesse W <jessw at netwood.net> wrote:
>> > did anyone ever respond to your question?
>> > If not which exercise are you referring to? page number?
>> I looked at the answer for that exercise in Tondo & Gimpel, and they
>> didn't generate the float values either, so I just went on.
>>
>> But I do have a new spot of trouble ATM... Right now, I'm up to
>> exercise 5-7, p. 110, which asks about the relative speed of two
>> different approaches; and I've never been able to get any timing code
>> to work right on my OS X system. I know this isn't related to Linux,
>> but if you have any thoughts or pointers regarding this, I'd love to
>> hear them.
>>
>> BTW, I've come across a rather nice (if pretty inactive right now) 
>> wiki
>> devoted to C, with (among other things) pages providing solutions to
>> all the K&R exercises. If anyone is interested, it's at
>> http://clc-wiki.net ; and the page about the particular exercise I'm
>> working on is:
>> http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_5
>>
>> Jesse Weinstein
>>
>> > Jesse W wrote: On May 28, 2007, at 9:18 AM, Dan
>> > Kegel wrote:
>> >> On 5/27/07, Jesse W wrote:
>> >>> I'm trying to find a intermediate class in C programming
>> >
>> >> How about just going through Kernighan & Ritchie and doing the
>> >> exercizes?
>> >
>> > Sorry for my stupidly long time in responding -- I've finally 
>> decided
>> > your suggestion of K&R is a sensible idea, and have made it through
>> > chapter 1, and part way through chapter 2. Thanks for the 
>> suggestion!
>> >
>> > BTW, I didn't figure out how to calculate the max and min of 
>> floating
>> > point types, as suggested in an exercise in Chapter 2 -- if anyone
>> > feels like pointing me to some solutions for this, it'd be 
>> appreciated.




More information about the Lula mailing list