zoukankan      html  css  js  c++  java
  • C程序设计语言习题(112)

    统计行数、单词数,字符数的程序: 

     1 #include<stdio.h>
     2 
     3 #define IN    1   /*在单词内*/
     4 #define OUT   0   /*在单词外*/
     5 
     6 int main()
     7 {
     8     int c, nl, nw, nc, state;
     9 
    10     state = OUT;
    11     nl = nw = nc  = 0;    //nl:行数  nw:单词数  nc:字符数
    12     while((c = getchar()) != EOF) {
    13         ++nc;
    14         if(c == '\n')
    15             ++nl;
    16         if(c == ' ' || c == '\n' || c == '\t')
    17             state = OUT;
    18         else if (state == OUT) {
    19             state = IN;
    20             ++nw;
    21         }
    22     }
    23     printf("%d %d %d\n", nl, nw, nc);
    24     return 0;
    25 }  

    【练习1-12】 
    编写一个程序,以每行一个单词的形式打印其输入 

     1 #include<stdio.h>
     2 
     3 #define IN    1   /*在单词内*/
     4 #define OUT   0   /*在单词外*/
     5 
     6 int main()
     7 {
     8     int c, state;
     9     state = OUT;
    10     
    11     while((c =getchar()) != EOF) {
    12         if(c == ' ' || c == '\n' || c == '\t') {
    13             if(state == IN) {
    14                 putchar('\n');
    15                 state = OUT;
    16             }
    17         } else if(state == OUT) {
    18             state = IN;
    19             putchar(c);
    20         } else {
    21             putchar(c);
    22         }
    23     }
    24     return 0;
    25 }
  • 相关阅读:
    (周日赛) Average is not Fast Enough!
    过山车
    (寒假CF3)B
    (寒假CF3)坑坑坑
    (周六赛1)Sum it up
    畅通工程
    vue 动态添加form表单item 校验问题
    html2canvas转pdf分页隔断问题处理
    vue中html转pdf并下载功能
    一个简单的滑动溢出效果
  • 原文地址:https://www.cnblogs.com/cpoint/p/3367500.html
Copyright © 2011-2022 走看看