word字符统计,并升序排列C++版

说明

本文创作的原因来源于CSDN问答模块网友的问题,再次备份记录一下。

功能要求

从txt文件中读取文件内容,并将单词升序排列,单词存放在链表中。单词的操作放在类中,链表的操作通过类的成员函数实现。显示所有字符及数量。

代码

#include <iostream>
#include <string>
using namespace std;
struct WordNode
{
	string word;
	int nmb;
	WordNode* next;
	WordNode(){next = 0;}
};

class WordCount
{
public:
	WordCount(){head = 0;}
	~WordCount()
	{
		WordNode* node = 0;
		while(head)
		{
			node = head->next;
			delete head;
			head = node;
		}
	}
	WordNode* findNode(string p);     //查找单词
	void insertNode(WordNode* node); //插入单词
	void addCount(WordNode* node);  //单词的数量加1
	void display();   //显示
private:
	WordNode* head;
};
//查找单词
WordNode* WordCount::findNode(string p)
{
	WordNode* node = head;
	while(node)
	{
		if (p.compare(node->word) == 0)
		{
			return node;
		}else
		{
			node = node->next;
		}
	}
	return 0;
}
//插入单词
void WordCount::insertNode(WordNode* node)
{
	if (head == 0)
	{
		head = node;
		return;
	}
	WordNode* P = head;
	WordNode* pre = head;
	while(P)
	{
		if (P->word.compare(node->word) > 0 )
		{
			if(P == head)
			{
				node->next = head;
				head = node; //插入头部
			}else
			{
				pre->next = node;
				node->next = P;
			}
			break;
		}else
		{
			pre = P;
			P = P->next;
			if(P==0)//插入到末尾
			{
			    pre->next = node;
			    break;
			}
		}
	}
}
//单词的数量加1
void WordCount::addCount(WordNode* node)
{
	node->nmb += 1;
}

//显示
void WordCount::display() 
{
	WordNode* node = head;
	while(node)
	{
		cout << node->word << "\t" << node->nmb << endl;
		node = node->next;
	}
}
bool isctrl(char ch)
{
	if (ch < 0x1F || ch == 0x7F)
	{
		return true;
	}
	return false;
}
string toLower(char* tt)
{
	int len = strlen(tt);
	for (int i = 0; i < len;i++)
	{
		tt[i] = tolower(tt[i]);
	}
	return tt;
}
//buf是存储文件的缓冲区,lSize是文件大小
char* textFileRead(char* filename,int *lSize)
{
	char* buf;
	FILE *pf = fopen(filename,"r");
	fseek(pf,0,SEEK_END);
	*lSize = ftell(pf);
	// 用完后需要将内存free掉
	rewind(pf); 
	buf = new char[*lSize+1];
	*lSize = fread(buf,sizeof(char),*lSize,pf);
	buf[*lSize] = '\0';
	return buf;
}

int main()
{
	int size,i=0,j= 0;
	char* buf = textFileRead("textfile.txt",&size);
	WordCount cc;
	if(size <= 0)
	{
		cout << "文件打开失败,或者为空"<< endl;
		return 0;
	}
	char text[50] = {0};
	while( i <= size)
	{
		if (i == size)
		{
			text[j] = '\0';
			string tt = toLower(text);
			if(tt.empty() || tt == " ")
			{
				j = 0;
				i++;
				continue;
			}
			WordNode* node = cc.findNode(tt);
			if (node)
			{
				cc.addCount(node);
			}else
			{
				node = new WordNode;
				node->word = tt;
				node->nmb = 1;
				node->next = 0;
				cc.insertNode(node);
			}
			break;
		}else
		{
			//此处不考虑中间含有.的单词,入地名,缩写等等
			if (buf[i] == ' ' || buf[i] == '\r' || buf[i] == ',' || buf[i] == '.' || buf[i] == '\n'|| buf[i] == '!' || isctrl(buf[i]))
			{
				text[j] = '\0';
				string tt = toLower(text);
				if(tt.empty() || tt == " ")
				{
					j = 0;
					i++;
					continue;
				}
				WordNode* node = cc.findNode(tt);
				if (node)
				{
					cc.addCount(node);
				}else
				{
					node = new WordNode;
					node->word = tt;
					node->nmb = 1;
					node->next = 0;
					cc.insertNode(node);
				}
				j = 0;
			}else
			{
				text[j++] = buf[i];
			}
			i++;
		}
	}

	cc.display();
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qfl_sdu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值