qiangchen81@yahoo.com.cn wrote:
[color=blue]
>
> Eric 写道:
>[color=green]
>> Hello,
>>
>> I have been working through the K&R book (only chapter one so far)
>> examples
>> and exercises. After much sweat and hair pulling, I think I have a
>> solution for ex 1-18 on page 31.
>>
>> It seems to work but may be missing some error checking. Can you please
>> take a look and see if my logic is correct and any other improvements.
>>
>> This is not a homework but it could just as well be. I am trying to get
>> a handle on C programming and would eventually like to help out with some
>> of the GNU projects as I get closer to retirement.
>>[/color][/color]
....program cut off to save space ....[color=blue][color=green]
>>
>> Thank you,
>>
>> Eric[/color][/color]
[color=blue]
> #include <stdio.h>
> #include <stdlib.h>
>
> #define MAXQUEUE 1001
>
> int advance(int pointer)
> {
> if (pointer < MAXQUEUE - 1)
> return pointer + 1;
> else
> return 0;
> }
>
> int main(void)
> {
> char blank[MAXQUEUE];
> int head, tail;
> int nonspace;
> int retval;
> int c;
> int spaceJustPrinted; /*boolean: was the last character printed
> whitespace?*/
>
> retval = spaceJustPrinted = nonspace = head = tail = 0;
>
> while ((c = getchar()) != EOF) {
> if (c == '\n') {
> head = tail = 0;
> if (spaceJustPrinted == 1) /*if some trailing whitespace was
> printed...*/
> retval = EXIT_FAILURE;
>
> if (nonspace) {
> putchar('\n');
> spaceJustPrinted = 0; /* this instruction isn't really
> necessary since
> spaceJustPrinted is only used to
> determine the
> return value, but we'll keep this boolean
> truthful */
> nonspace = 0; /* moved inside conditional just to save a
> needless
> assignment */
> }
> }
> else if (c == ' ' || c == '\t') {
> if (advance(head) == tail) {
> putchar(blank[tail]); /* these whitespace chars being printed
> early
> are only a problem if they are trailing,
> which we'll check when we hit a \n or EOF
> */
> spaceJustPrinted = 1;
> tail = advance(tail);
> nonspace = 1;
> }
>
> blank[head] = c;
> head = advance(head);
> }
> else {
> while (head != tail) {
> putchar(blank[tail]);
> tail = advance(tail);
> }
> putchar(c);
> spaceJustPrinted = 0;
> nonspace = 1;
> }
> }
>
> /* if the last line wasn't ended with a newline before the EOF,
> we'll need to figure out if trailing space was printed here */
> if (spaceJustPrinted == 1) /*if some trailing whitespace was
> printed...*/
> retval = EXIT_FAILURE;
>
> return retval;
> }[/color]
Thank you for your reply. It will take me a while to fully digest this and
understand the logic behind your program. Your variables are much more
descriptive which I need to remember to do. I compiled and tested it with
a test file I made with different line lengths with trailing tabs and
blanks. It gave me the same results that I got with my version. I am
struggling through the boolean logic statements (==, !=, ||) which
surprised me as I use to be pretty good with truth tables and boolean
algebra back in the day.
Thanks,
Eric
Remove "all the spam" to reply.