⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wordscan.cpp

📁 单词扫描
💻 CPP
字号:
#include <iostream.h>
#Include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define QUEUELENGTH 25

class wordscan
{
public:
	wordscan();
	~wordscan();
	void scan(int fd);
	void scan(char *fname);
	void scan(FILE* fp);
private:
	char *wordqueue[QUEUELENGTH];
	char wordstack[1024];
	
	int queuehead;
	int queuetail;
	int counter;
};
	
wordscan::wordscan()
{
	int i;
	queuehead = 0;
	queuetail = 0;
	counter = 0;
	for(i = 0; i < QUEUELENGTH; i++)
		wordqueue[i] = NULL;
}

wordscan::~wordscan()
{
}

void wordscan::scan(int fd)
{
	char ch, *chptr;
	int size;
	int cnt, i, stkptr, queueptr, queuetmp;
	
	cnt = 0;
	stkptr = 0;
	
	do
	{
		size = read(fd, &ch, 1);
		if(size == -1)
		{
			cout<<"error: read error!\n";
			exit(0);
		}
		if(size > 0)
		{
			if(ch == ' ' || ch == ';' || ch == ',' || ch == '.' || ch == '	' || ch == '?' || ch == '!') // use other symbols if more separators are considered.
			{
				if(cnt > 0)
				{
					chptr = new char[cnt + 1];
					chptr[cnt] = '\0';
					while(cnt > 0)
					{
						chptr[cnt - 1] = wordstack[cnt - 1];
						cnt--;
					}
					queueptr = queuehead;
					do
					{
						if(wordqueue[queueptr] != NULL)
						{
							if(strcmp(chptr, wordqueue[queueptr]) == 0)
							{
								cout<<"word: "<<chptr<<" repeated!\n";
							}
						}
						queuetmp = queueptr;
						queueptr = (queueptr + 1) % QUEUELENGTH;
					}while(queueptr != queuetail);
					
					if(wordqueue[queuetmp] != NULL)
					{
						delete wordqueue[queuetmp];
					}
					wordqueue[queuetmp] = chptr;
					queuetail = (queuetail + 1) % QUEUELENGTH;
					
					counter++;
					if(counter > QUEUELENGTH)
					{
						queuehead = (queuehead + 1) % QUEUELENGTH;
						counter = QUEUELENGTH;
					}
					cnt = 0;
				}
			}
			else
			{
				wordstack[cnt] = ch;
				cnt++;
			}
		}
		
	}while(size > 0);
}

void wordscan::scan(char *fname)
{
}

void wordscan::scan(FILE* fp)
{
	
}

int main(int argc, char** argv)
{
	wordscan app;
	int fd;
	
	if(argc != 2)
		cout<<"usage: wordscan filename\n";
	else
	{
		fd = open(argv[1], O_RDONLY);
		if(fd == -1)
			cout<<"error: file open error!\n";
		else
			app.scan(fd);
	}
	
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -