c 1-13

 编写一个读入文件的字符数并打印直方图

Code:
  1. /*......*/  
  2. #include<stdio.h>   
  3. #include<malloc.h>   
  4. #include<stdlib.h>   
  5. #define LEN sizeof(struct wordcount)   
  6. #define IN 1   
  7. #define OUT 0   
  8. int word_Allcharcount(int n,char filename[]);   
  9. int word_MaxCharacter(char filename[]);   
  10. char filename[20];   
  11. struct wordcount   
  12. {   
  13.     int wordlen;   
  14.     int wordnt;   
  15.     struct wordcount *next;   
  16. }   
  17.   
  18.   
  19.   
  20.   
  21.   
  22.   
  23.   
  24. /*-----------------------------------------------*/  
  25. /*********************主函数**********************/  
  26. main()   
  27. {   
  28.     struct wordcount *head;   
  29.     struct wordcount *Llist(void);   
  30.     void input(struct wordcount *head);   
  31.     scanf("%s",filename);   
  32.     head=Llist();   
  33.     input(head);   
  34. }   
  35.   
  36.   
  37. /*-----------------------------------------------*/  
  38. /*********************charcount函数***************/  
  39. /*读出指定字符数的单词出现几次*/  
  40. int word_Allcharcount(int n,char filename[])   
  41. {   
  42.     int word_CharacterCount;   
  43.     int word_Count;   
  44.     int state;   
  45.     int c;   
  46.     FILE *fp;   
  47.     word_Count=0;   
  48.     state=OUT;   
  49.     word_CharacterCount=0;   
  50.     if ((fp=fopen(filename,"r"))==NULL)   
  51.     {   
  52.         printf("cannot open flie/n");   
  53.         exit(0);   
  54.     }   
  55.     else  
  56.         do  
  57.             {   
  58.                 c=fgetc(fp);   
  59.                 if (c==' '||c=='/n'||c=='/t'||c==EOF)   
  60.                 {   
  61.                     state=OUT;   
  62.                     if (word_CharacterCount==n)   
  63.                         ++word_Count;   
  64.                     word_CharacterCount=0;   
  65.                 }   
  66.                 else if (state=OUT)   
  67.                 {   
  68.                     state=IN;   
  69.                     ++word_CharacterCount;   
  70.                 }   
  71.                 else if (c=='.')   
  72.                     ;   
  73.                 else if (c==',')   
  74.                     ;   
  75.                 else if (c=='!')   
  76.                     ;   
  77.                 else if (c=='?')   
  78.                     ;   
  79.                 else if (c==':')   
  80.                     ;   
  81.                 else if (c=='/'')   
  82.                     ;   
  83.                 else  
  84.                     ++word_CharacterCount;   
  85.   
  86.   
  87.             }while (c!=EOF);   
  88.     return(word_Count);   
  89. }   
  90. /*-----------------------------------------------*/  
  91. /******************max函数************************/  
  92. /*字符数最长的单词*/  
  93. int word_MaxCharacter(char filename[])   
  94. {   
  95.     int word_CharacterCount;   
  96.     int word_CharacterRemember;   
  97.     int c;   
  98.     int state;   
  99.     FILE *fp;   
  100.     state=OUT;   
  101.     word_CharacterRemember=0;   
  102.     word_CharacterCount=0;   
  103.        
  104.     if ((fp=fopen(filename,"r"))==NULL)   
  105.     {   
  106.         printf("cannot open flie/n");   
  107.         exit(0);   
  108.     }   
  109.     else  
  110.         while(!feof(fp))   
  111.         {   
  112.             c=fgetc(fp);   
  113.             if (word_CharacterCount>word_CharacterRemember)   
  114.                 word_CharacterRemember=word_CharacterCount;   
  115.             if(c==' '||c=='/n'||c=='/t')   
  116.             {   
  117.                 state=OUT;   
  118.                 word_CharacterCount=0;   
  119.             }   
  120.             else if (state=OUT)   
  121.             {   
  122.                 state=IN;   
  123.                 ++word_CharacterCount;   
  124.             }   
  125.             else  
  126.                 ++word_CharacterCount;   
  127.         }   
  128.     return(word_CharacterRemember);   
  129. }   
  130. /*-----------------------------------------------*/  
  131. /******************Llist函数**********************/  
  132. /*从1到max形成一个链表保存出现次数*/  
  133. struct wordcount *Llist(void)   
  134. {   
  135.     int word_Allcharcount(int n,char filename[]);   
  136.     int word_MaxCharacter(char filename[]);   
  137.     struct wordcount *head;   
  138.     struct wordcount *p1,*p2;   
  139.     int m,n;   
  140.     m=word_MaxCharacter(filename);   
  141.     p1=p2=(struct wordcount*)malloc(LEN);   
  142.     head=NULL;   
  143.     n=0;   
  144.     while(n<m)   
  145.     {   
  146.         ++n;   
  147.         p1->wordlen=n;   
  148.         p1->wordnt=word_Allcharcount(n,filename);   
  149.         if (n==1)   
  150.             head=p1;   
  151.         else if (n>1)   
  152.             p2->next=p1;   
  153.         p2=p1;   
  154.         p1=(struct wordcount*)malloc(LEN);   
  155.     }   
  156.     p2->next=NULL;   
  157.     free(p1);   
  158.     return(head);   
  159. }   
  160. /*-----------------------------------------------*/  
  161. /*****************input函数***********************/  
  162. /*输出函数*/  
  163. void input(struct wordcount *head)   
  164. {   
  165.     int m,n;   
  166.     int word_MaxCharacter(char filename[]);   
  167.     struct wordcount *p;   
  168.     m=word_MaxCharacter(filename);   
  169.     printf("this is chart/n");   
  170.     for(n=m;n>0;--n)   
  171.     {   
  172.         p=head;   
  173.         printf("%3d |",n);   
  174.         while(p->next!=NULL)   
  175.         {   
  176.             if ((n-(p->wordnt))<=0)   
  177.                 printf("*");   
  178.             else  
  179.                 printf(" ");   
  180.             p=p->next;   
  181.         }   
  182.         printf("/n");   
  183.     }   
  184.     printf("    ");   
  185.     printf("------------------");   
  186.     printf("/n");   
  187.     printf("    ");   
  188.     printf("0123456789");   
  189.     printf("/n");   
  190.   
  191.        
  192. }   
  193.   
  194.   
  195.   
  196.   

错误:

       形参和实参类型不一样。

      FILE定义要写在变量上面。

 

 

答案:

/* This program was the subject of a thread in comp.lang.c, because of the way it handled EOF.
  
  
 * The complaint was that, in the event of a text file's last line not ending with a newline,
  
  
 * this program would not count the last word. I objected somewhat to this complaint, on the
  
  
 * grounds that "if it hasn't got a newline at the end of each line, it isn't a text file".
  
  
 *
  
  
 * These grounds turned out to be incorrect. Whether such a file is a text file turns out to
  
  
 * be implementation-defined. I'd had a go at checking my facts, and had - as it turns out -
  
  
 * checked the wrong facts! (sigh)
  
  
 *
  
  
 * It cost me an extra variable. It turned out that the least disturbing way to modify the
  
  
 * program (I always look for the least disturbing way) was to replace the traditional
  
  
 * while((c = getchar()) != EOF) with an EOF test actually inside the loop body. This meant
  
  
 * adding an extra variable, but is undoubtedly worth the cost, because it means the program
  
  
 * can now handle other people's text files as well as my own. As Ben Pfaff said at the
  
  
 * time, "Be liberal in what you accept, strict in what you produce". Sound advice.
  
  
 *
  
  
 * The new version has, of course, been tested, and does now accept text files not ending in
  
  
 * newlines.
  
  
 *
  
  
 * I have, of course, regenerated the sample output from this program. Actually, there's no
  
  
 * "of course" about it - I nearly forgot.
  
  
 */

  
  
   
    
  
  
#include <stdio.h>

  
  
   
    
  
  
#define MAXWORDLEN 10

  
  
   
    
  
  
int main(void)
{
  int c;
  int inspace = 0;
  long lengtharr[MAXWORDLEN + 1];
  int wordlen = 0;

  
  
   
    
  
  
  int firstletter = 1;
  long thisval = 0;
  long maxval = 0;
  int thisidx = 0;
  int done = 0;

  
  
   
    
  
  
  for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++)
  {
    lengtharr[thisidx] = 0;
  }

  
  
   
    
  
  
  while(done == 0)
  {
    c = getchar();

  
  
   
    
  
  
    if(c == ' ' || c == '/t' || c == '/n' || c == EOF)
    {
      if(inspace == 0)
      {
        firstletter = 0;
        inspace = 1;

  
  
   
    
  
  
        if(wordlen <= MAXWORDLEN)
        {
          if(wordlen > 0)
          {
            thisval = ++lengtharr[wordlen - 1];
            if(thisval > maxval)
            {
              maxval = thisval;
            }
          }
        }
        else
        {
          thisval = ++lengtharr[MAXWORDLEN];
          if(thisval > maxval)
          {
            maxval = thisval;
          }
        }
      }
      if(c == EOF)
      {
        done = 1;
      }
    }
    else
    {
      if(inspace == 1 || firstletter == 1)
      {
        wordlen = 0;
        firstletter = 0;
        inspace = 0;
      }
      ++wordlen;
    }
  }

  
  
   
    
  
  
  for(thisval = maxval; thisval > 0; thisval--)
  {
    printf("%4d  | ", thisval);
    for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++)
    {
      if(lengtharr[thisidx] >= thisval)
      {
        printf("*  ");
      }
      else
      {
        printf("   ");
      }
    }
    printf("/n");
  }
  printf("      +");
  for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++)
  {
    printf("---");
  }
  printf("/n       ");
  for(thisidx = 0; thisidx < MAXWORDLEN; thisidx++)
  {
    printf("%2d ", thisidx + 1);
  }
  printf(">%d/n", MAXWORDLEN);

  
  
   
    
  
  
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值