Chinaunix首页 | 论坛 | 博客
  • 博客访问: 399796
  • 博文数量: 76
  • 博客积分: 3016
  • 博客等级: 中校
  • 技术积分: 782
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-27 12:05
文章分类

全部博文(76)

文章存档

2017年(1)

2015年(1)

2009年(2)

2008年(28)

2007年(44)

我的朋友

分类: C/C++

2007-12-29 15:08:53

有两个程序,第一个有用,第二个没用。我也不知道是为什么,希望知道的人告诉我一下。
还有个问题:如果单词之间有2个空格之类的话,程序输出时会在单词之间多出一个空行。

#include <stdio.h>
int main()
{
        int has_a_return = 0;
        int c;
        while((c = getchar()) != EOF){
                if(c == '\n' || c == ' ' || c == '\t')
                        has_a_return = 1;
                else
                        putchar(c);
                if(has_a_return == 1){
                        printf("\n");
                        has_a_return = 0;
                }
        }
        return 0;
}




#include <stdio.h>
int main()
{
        int has_a_return = 0;
        int c;
        while((c = getchar()) != EOF){
                if(c != '\n' || c != ' ' || c != '\t')
                        putchar(c);
                else
                        has_a_return = 1;
                if(has_a_return == 1){
                        printf("\n");
                        has_a_return = 0;
                }
        }
        return 0;
}

阅读(891) | 评论(3) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2008-03-02 19:36:31

谢谢,看懂了!!终于想通为什么了!哈哈!!!太谢谢了!!! 其实我只要在 has_a_retur=1 前加一个判断,看一下是否已经设过 1了,就跟你给的 1==pre_is_an_alfa 一样的意思。哈哈,你写的那个很有意思,我第一次看到这样的写法,有创意!很好很强大 ;-)

chinaunix网友2008-03-02 18:38:47

int main() { // int has_a_return = 1; int pre_is_an_alfa = 1; int c; while((c = getchar()) != EOF){ if(c == '\n' || c == ' ' || c == '\t') if( 1 == pre_is_an_alfa) { printf("\n"); pre_is_an_alfa = 0; } else ; else { putchar(c); pre_is_an_alfa = 1; } } return 0; }

chinaunix网友2008-03-02 18:14:24

第二个 if(c != '\n' || c != ' ' || c != '\t') ????????????????????????????? 换成:if(c != '\n' && c != ' ' && c != '\t')就跟第一个一样了.