从文件读取一段英文段落,由各种符号进行分割,统计每个单词出现次数并按其次数排序(c语言)

比如说一个test.txt文件中存储的一个段落为;
    Rather than remembering to download a season of a favorite TV show before
heading to the airport! for example, you could do it while in line to board a plane,
said Justin Denison, a Samsung senior vice president.
结果要求输出其中每个单词的数量并且按出现次序排序。
我们可以先定义一个结构体用来存放单词和其出现个数:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORD 20 //每个单词最长长度
#define MAX_LINE 1024//定义一个缓冲大小存暂存段落信息

typedef struct LNode {
	char word[MAX_WORD];
	int count;
	LNode *next;//指向下一个存储单词和其个数的结构体
}LNode, *WNode;//声明别名 

    下面我先放出main中的代码段,再详细解释下面每个调用的函数作用:

int main() {
	int count = 0;//单词计数
	char buf[MAX_LINE];//暂存整个段落的所有字符
	char* words[MAX_LINE]; //存储所有单词,其中每个元素对于每个单词的字符串首地址
	// 以下是的C语言文件操作,不细说
	FILE *fp;
	if((fp = fopen("test.txt", "r+")) == NULL) {
		printf("Can not open this file");
		exit(0);
	}
	fscanf(fp, "%[^\r]s", buf);//这里用fscanf读取文件所有信息,并且忽略掉\r,避免读取到空格就结束读取了
	getWords(buf, words, count);
	WNode head = dedup(words, count);
	WNode head_sorted = sort(head);
	print(head_sorted);
	return 0;
}

    getWord函数的作用是将buf中暂存的段落分割成一个一个的单词存入words中

void getWords(char *buf, char **words, int &count) {
	char *p = strtok(buf, " ,.!-\n");
	while(p) {
		words[count++] = p;
		//p为分割后的单个单词,将其单词字符串的首地址给words中的元素。
		p = strtok(NULL, " ,.!-\n");
		//第一次被分割后被替换为NULL,所以后面都应该从NULL开始往后面访问
	}
}

    对于strtok函数的用法:
调用方式 : char *strtok(char *str1, char *str2);
说明 : strtok()函数的原型在string.h中
功能说明:函数strtok()返回字符串str1中指向一个由str2所指定的字符或者字符串的分隔符的指针,当没有要返回的分隔符时,就返回一个空指针。

函数strtok()实际上修改了有str1指向的字符串。每次找到一个分隔符后,一个空(NULL)就被放到分隔符处,函数用这种方法来连续查找该字符串。

    最后两个方法就是去重和排序:

WNode dedup(char **words, int count) {//去重复 
	WNode head, p, tmp;
	if((head = (WNode)malloc(sizeof(LNode)))== NULL) {
		printf("Not any room");
		exit(0);
	}
	head->count = 0;
	strcpy(head->word, "\0");
	head->next = NULL;
	p = head;
	
	int i, flag;
	WNode q = NULL;
	for(i = 0; i < count; ++i) {
		q = head->next;
		flag = 1; //标识是否重复
		while(q) {
			if(strcmp(q->word, words[i]) == 0) {
				q->count++;
				flag = 0; //重复单词,将flag=0
				break;
			}
			q = q->next;
		}
		if(flag) {//flag=1时表明不是重复单词
			if((tmp = (WNode)malloc(sizeof(LNode)))== NULL) {
				printf("Not any room");
				exit(0);
			}
			p->next = tmp;
			tmp->count = 1;
			strcpy(tmp->word, words[i]);
			tmp->next = NULL;
			p = tmp;
		}
	}
	return head;
}
//稳定的冒泡排序,这里如果用快排则破坏了稳定性
WNode sort(WNode head) {
	int tmp_count;
	char tmp_word[MAX_WORD];
	WNode start = NULL, end = NULL;
	start = head->next;
	while(start != end) {
		while(start->next != end) {
			if(start->count > start->next->count) {
				tmp_count = start->count;
				start->count = start->next->count;
				start->next->count = tmp_count;
				strcpy(tmp_word,start->word);
				strcpy(start->word, start->next->word);
				strcpy(start->next->word, tmp_word);
			}
			start = start->next;
		}
		end = start; //尾指针迁移,即排好序的节点不需要再管了
		start = head->next; //比较指针从头开始
	}
	return head;
}

    运行结果:
在这里插入图片描述

  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值