| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 393 人关注过本帖
标题:【求助】我这段代码怎么加入排序。。。。
只看楼主 加入收藏
bpl2011
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-12-4
结帖率:0
收藏
 问题点数:0 回复次数:0 
【求助】我这段代码怎么加入排序。。。。
用文件输入一篇英文文章,统计不同单词个数(不计大小写和连字符),并按字典序输出。

代码:
#include<stdio.h>
#include<string.h>
#include<malloc.h>

typedef struct _Link                                                            //定义该链表是用于存储不同的英文单词 ,这里采用了*ch是为了
{                                                                               //采用动态数组的方式,这样不会因为单词长度的不同而浪费空间
        char *ch;
        int num;
        _Link *next;
}Link;

int main()
{
    FILE *fp;                                                                   //用文件读入一篇文章
    char filename[30],resultname[30];                                           //filename是要读入的文章文件名,resultname是输出文件文件名
    char word[1024],c;                                                          //这里的word[1024]只是一个临时存放单词的数组
    int pos=0;
    Link *head,*pnow,*ptmp;
    head=pnow=ptmp=NULL;
    printf("Please input file name: \n");
    scanf("%s",filename);
    printf("Please input result name: \n");
    scanf("%s",resultname);
    fp=fopen(filename,"r");
   
    while(!feof(fp))                                                            //逐个读入字符,以eof结尾
    {
                    c=fgetc(fp);                                                //采用逐个字符读入的方式
                    if((c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c=='-'))            //题目要求,单词不分大小写,连字符
                    {
                         word[pos++]=c;
                    }
                    else if(pos>0)                                              //链表的遍历,比较当前单词与链表结点中的单词
                    {
                         word[pos]='\0';
                         ptmp=head;
                         while(ptmp)
                         {
                                    if(strcmp(word,ptmp->ch)==0)
                                    {
                                      ptmp->num++;
                                      break;
                                    }
                                    ptmp=ptmp->next;
                         }                                                      //如果链表中没有当前单词,就在链表的末尾插入新的结点,存入当前单词
                         if(ptmp==NULL)
                         {
                            ptmp=(Link *)malloc(sizeof(Link));                  //新开辟一个链表结点
                            ptmp->ch=(char *)malloc(sizeof(pos));               //新开辟一个char型空间,用于存放新读入的字母
                            strcpy(ptmp->ch,word);
                            ptmp->num=1;
                            ptmp->next=NULL;
                            if(pnow)                                            //插入当前结点为尾结点
                            {
                               pnow->next=ptmp;
                               pnow=ptmp;
                            }
                            else                                                //这是第一次出现单词的地方
                               head=pnow=ptmp;
                         }
                         pos=0;
                    }
    }
    fclose(fp);                                                                 //关闭文件
    ptmp=head;                                                                  //读取链表,输出其单词及个数,用文件输出
    FILE *fp1=fopen(resultname,"w");
    while(ptmp)                                                                 
    {
               fprintf(fp1,"%d\t%s\n",ptmp->num,ptmp->ch);
               ptmp=ptmp->next;
    }
    fclose(fp1);                                                                //关闭文件
   
    system("pause");
}
搜索更多相关主题的帖子: 单词 英文单词 英文文章 include 动态 
2012-03-23 23:24
快速回复:【求助】我这段代码怎么加入排序。。。。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024512 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved