博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c程序设计语言_习题1-18_删除输入流中每一行末尾的空格和制表符,并删除完全是空格的行...
阅读量:7108 次
发布时间:2019-06-28

本文共 6279 字,大约阅读时间需要 20 分钟。

 

Write a program to remove all trailing blanks and tabs from each line of input, and to delete entirely blank lines.

其实做这道题目有两种思路:

  1.后向模式:利用getline()先将输入流中,每一行完全接收,然后从接收的line字符串中末尾,往前扫,直到发现第一个非空格和制表符字符;

  2.前向模式:每接收一个字符,都要进行输出、判断。

 

/* K&R2 1-18 p31: Write a program to remove trailing blanks and tabs   from each line of input, and to delete entirely blank lines.   The program specification is ambiguous: does "entirely blank lines"   mean lines that contain no characters other than newline, or does   it include lines composed of blanks and tabs followed by newline?   The latter interpretation is taken here.   This implementation does not use any features not introduced in the   first chapter of K&R2.  As a result, it can't use pointers to   dynamically allocate a buffer to store blanks that it has seen, so   it must limit the number of blanks that are allowed to occur   consecutively.  (This is the value of MAXQUEUE, minus one.)   It is intended that this implementation "degrades gracefully."   Even though a particular input might have 1000 or more blanks or   tabs in a row, causing a problem for a single pass, multiple passes   through the file will correct the problem.  The program signals the   need for such an additional pass by returning a failure code to the   operating system.  (EXIT_FAILURE isn't mentioned in the first   chapter of K&R, but I'm making an exception here.) */#include 
#include
#define MAXQUEUE 1001int 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; retval = nonspace = head = tail = 0; while ((c = getchar()) != EOF) { if (c == '\n') { head = tail = 0; if (nonspace) putchar('\n'); nonspace = 0; } else if (c == ' ' || c == '\t') {   //能执行这个if,只能说明输入行中字符溢出了。   if (advance(head) == tail) { putchar(blank[tail]); tail = advance(tail); nonspace = 1; retval = EXIT_FAILURE; }     //只要遇到空格和制表符,就先存起来 blank[head] = c; head = advance(head); } else {
    //一次性把前面积攒的空格和制表符输出来 while (head != tail) { putchar(blank[tail]); tail = advance(tail); } putchar(c); nonspace = 1; } } return retval;}

 

Chris Sidi writes:

Ben,I thought your solution to 1-18 was really neat (it didn't occur to mewhen I was doing the exercise), the way it degrades gracefully andmultiple passes can get rid of huge blocks of whitespace.However, if there is a huge block of non-trailing whitespace (eg "A",2000spaces, "B\n") your program returns an error when there's not a need forit.  And if someone were to use your program till it passes it will loopinfinitely:  $ perl -e 'print "A"," "x2000,"B\n";' > in  $ until ./a.out < in > out; do echo failed, running another pass; cp out     in; done  failed, running another pass  failed, running another pass  failed, running another pass  [snip]Below I have added a variable spaceJustPrinted to your program and checkto see if the spaces printed early are trailing.  I hope you like theminor improvement.  (Though I can understand if you don't give a [1] :))

[1] expletive deleted - RJH.

/* K&R2 1-18 p31: Write a program to remove trailing blanks and tabs   from each line of input, and to delete entirely blank lines.   The program specification is ambiguous: does "entirely blank lines"   mean lines that contain no characters other than newline, or does   it include lines composed of blanks and tabs followed by newline?   The latter interpretation is taken here.   This implementation does not use any features not introduced in the   first chapter of K&R2.  As a result, it can't use pointers to   dynamically allocate a buffer to store blanks that it has seen, so   it must limit the number of blanks that are allowed to occur   consecutively.  (This is the value of MAXQUEUE, minus one.)   It is intended that this implementation "degrades gracefully."   Even though a particular input might have 1000 or more trailing   blanks or tabs in a row, causing a problem for a single pass,   multiple passes through the file will correct the problem.  The   program signals the need for such an additional pass by returning a   failure code to the operating system.  (EXIT_FAILURE isn't mentioned   in the first chapter of K&R, but I'm making an exception here.) */#include 
#include
#define MAXQUEUE 1001int 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;}

 

转载地址:http://hjlhl.baihongyu.com/

你可能感兴趣的文章
JavaScript实现XML与JSON互转代码(转载)
查看>>
Windows7下的免费虚拟机(微软官方虚拟机)
查看>>
Linux下暴力破解工具Hydra详解
查看>>
Offer是否具有法律效力?
查看>>
Android SQLite系列
查看>>
怎么去掉li标签前面的点??
查看>>
Conjugate prior relationships
查看>>
深入Javascript中apply、call、bind
查看>>
.NET_Framework_version_history
查看>>
Android程序员必备精品资源
查看>>
Oracle SQL函数之转换函数To_char汇总
查看>>
将主机IDS OSSEC日志文件存入MYSQL的方法
查看>>
Linux线程属性总结
查看>>
【原创】Kakfa log包源代码分析(二)
查看>>
Javascript 笔记与总结(2-16)事件对象
查看>>
[裴礼文数学分析中的典型问题与方法习题参考解答]4.4.7
查看>>
JAVA存取对象属性时,如果开程多线程,记得对相关存取方法作原子化操作定义...
查看>>
深度学习 vs. 概率图模型 vs. 逻辑学
查看>>
Eclipse中使用javap运行配置详解
查看>>
android开发连接wifi addNetwork 返回-1
查看>>