文件内单词处理(C++)

题目描述:实现对某个文件中单词的查找、替换、删除等处理。
文件内单词处理

main.cpp

#include <iostream>
#include "word.h"

using namespace std;

void menu(){
	cout << " -----------------------------------------------------------------------------\n"
	     << "\n"
	     << "                		1: 查找指定的某个单词\n"
	     << "                		2: 替换指定某个单词\n"
	     << "                		3: 删除指定某个单词\n"
	     << "                		0: 结束操作\n"
	     << "\n"
	     << " -----------------------------------------------------------------------------\n";
}

int main(int argc, char** argv) {
	int choice;
	Open_file start;	//创建对象,打开文件
	string f_word;		//需要查找的单词 
	string d_word;		//需要删除的单词 
	while(1){
		menu(); 
		cout << "请输入你想实现的项目的序号:\n"; 
		cin >> choice;
		switch(choice){
		
			//查找单词 
			case 1:{
				cout << "请输入你想要查找的单词:\n";
				cin >> f_word;
				Find find(f_word);     //创建对象,查找单词
				find.dispay();			//调用类的成员函数
				break;
			}
			
			//替换单词 
			case 2:{
				cout << "请输入你想要替换的单词:\n";
				cin >> f_word;
				Replace replace(f_word);	//创建对象,替换单词
				replace.replaceWord();
				break;
			}
			
			//删除单词		
			case 3:{
				cout << "请输入你想要删除的单词:\n";
				cin >> d_word;
				Del del(d_word);			//创建对象,删除单词
				del.delWord();
				break;
			}
			
			case 0:
				exit(0);
			
			default:
				cout << "请输入正确的操作序号!\n";
				break;
		}
		system("pause");		//暂停
        system("cls");			//清屏 
	}
	return 0;
}

word.h

#include<iostream>
#include <cstring>
#include <cmath>

using namespace std;

//Open_file类,打开文件 
class Open_file{
	public:
		Open_file();					//构造函数
		
	protected:
		char word[500];					//储存文件内的单词
		int n;							//word的大小
};

//Find类,查找单词,继承Open_file类 
class Find:public Open_file{
	public:
		Find(string f_word);
		void findWord();			//查找单词的成员函数
		void dispay();				//输出查找结果
		
	protected:
		char fword[50];			//储存f_word
		char part_word[50];		//word里的一个单词
		int position[50];		//所查找的单词的位置
		int num[1];				//储存单词出现次数
		int pword_size;			//所提取的单词的大小
		int fword_length;		//所要查找的单词的大小
};

//Replace类,替换单词,继承Find类 
class Replace:public Find{
	public:
		Replace(string f_word):Find(f_word){
			cout << "你想替换成什么单词:\n";
			cin >> r_word;
			memset(rword,0,sizeof(rword));		//将数组置空
			strcpy(rword,r_word.c_str());		//将指定的单词转换为数组
			rword_length = strlen(rword);		//计算单词长度

			memset(new_word,0,sizeof(new_word));
			cha = fabs(rword_length-fword_length);
			//计算原单词与要替换的单词长度的差值,防止单词长度不一导致单词覆盖
		}
		void replaceWord();
		
	private:
		string r_word;			//定义所要替换为的单词
		char rword[50];			//存储所要替换为的单词
		char new_word[500];	 	//定义新数组,存储新的内容
		int rword_length;		//计算所要替换为的单词的长度
		int cha;				//计算字母的差值
};

//Del类,删除单词,继承Find类 
class Del:public Find{
	public:
		Del(string f_word):Find(f_word){
			memset(new_word,0,sizeof(new_word));
		}
		void delWord();
		
	private:
		char new_word[500];			//定义新数组,将删除后的文件内容输入到文件内
};

word.cpp

Open_file的构造函数

#include<iostream>
#include <fstream>
#include"word.h"
using namespace std;

//Open_file的构造函数 
Open_file::Open_file(){
	memset(word,0,sizeof(word));	//将数组置空
	ifstream file;
	file.open("target.txt");
	if(!file){						//判断文件是否打开
		cout << "打开target.txt失败!\n";
		exit(0);
	}
	else{
		ifstream fin("target.txt");
		fin >> noskipws;				//读入空格
		for(int i=0;i<500;i++){
			fin >> word[i];				//将文件内容读入数组中
		}
	}
	n = strlen(word);
	file.close();						//关闭文件
}

Find的构造函数

//Find的构造函数 
Find::Find(string f_word){
	memset(position,0,sizeof(position));
	memset(part_word,0,sizeof(part_word));
	memset(num,0,sizeof(num));
	memset(fword,0,sizeof(fword));
	
	strcpy(fword,f_word.c_str());//将字符串转化为字符数组
	fword_length = strlen(fword);
	findWord();
}

查找单词

//查找单词 
void Find::findWord(){
	int k = 0,number = 0;
	int i = 0,j = 0;
	int judge;//判断是否相等
	for(i;i < n;){
		if(word[i] < 65 || (word[i] > 90 && word[i] < 97) || word[i] > 122)//除去单词前不是单词部分
			i++;
		else{
			part_word[j] = word[i];
			j++;
			i++;
			if(word[i] < 65 || (word[i] > 90 && word[i] < 97) || word[i] > 122){//一个单词结束
				judge = strcmp(part_word,fword);//判断单词是否相等
				pword_size = strlen(part_word);//计算分离出来的单词大小
				if(judge == 0){
					if(k < n){
						position[k]=i-pword_size;
						k++;
					}
					number++;
					num[0] = number;
				}
				if(num[0] == 0)
					position[k] = -1;
				j = 0;
				memset(part_word,0,sizeof(part_word));//再次将part_word数组置空,放置前面长单词将后面单词覆盖
				continue;
			}
		}
	}
}

显示查找结果

//显示查找结果 
void Find::dispay(){
	if(num[0] == 0)
		cout << "文件内没有这个单词!\n"<<endl;
	else{
			cout << "所有出现的位置:";
			for(int i = 0;i < num[0];i++)
			 	cout << position[i]+1 << " ";
			cout << endl << fword << " 在此文件内出现 " << num[0] << " 次\n";
		}
}

替换单词

利用数组将长单词替换短单词时,这个短单词后面的单词也会出现长单词的一部分,出现查找不到后面单词的错误。将短单词替换成长单词时,长单词还会出现比短单词长的那部分字母。计算两个单词的长度之差,用后面的空格及单词将多余的字母覆盖。

cha=fabs(rword_length-fword_length);
if(rword_length<fword_length)
	i+=cha;
else
	i-=cha;
//替换单词 
void Replace::replaceWord(){
	int i = 0,j = 0,m = 0,k;
	int judge = 0;
	for(i;i < n;){
		if(i == position[m]){
			judge++;
			for(k = 0;k < rword_length;k++){
				new_word[j] = rword[k];
				i++;
				j++;
			}
			m++;//找到下一个相同单词的首位置
			if(rword_length < fword_length)
				i += cha;
			else
				i -= cha;
		}
		new_word[j] = word[i];
		i++;
		j++;
	}
	if(judge == 0)
		cout << "文件内没有你想替换的单词!\n";
	else{
			ofstream fout("target.txt");//将数组写入到文件内
			for(int j = 0;j < n;j++)
				fout << new_word[j];
			cout << endl << "替换了" << num[0] << "个单词\n";
		}
}

删除单词

//删除单词 
void Del::delWord(){
	int i = 0,j = 0,m = 0,k = 0;
	int judge = 0;
	for(i;i < n;){
		if(i == position[m]){
			judge++;
			m++;//找到下一个相同单词的首位置
			i += fword_length;
		}
		new_word[j] = word[i];
		i++;
		j++;
	}
	if(0 == judge)
		cout<<"文件内没有你想删除的单词!\n";
	else{
		ofstream fout("target.txt");//将数组写入到文件内
		for(int j = 0;j<n;j++)
			fout<<new_word[j];
		cout << endl << "删除了" << num[0] << "个单词\n";
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值