文档编辑器的设计与实现
1 系统的基本功能
该系统要求对一个文本文件中的内容进行各种常规操作,如:插入、删除、查找、替换等功能。通过此课题,熟练掌握文本文件的操作及用字符数组来实现字符串操作的功能。
2 要求及提示
2.1 基本要求
要能提供以下几个基本功能:
(1)首先文件标准化处理:如果句子有前导空格和后导空格,则删除这些空格,单词与单词之间只保留一个空格。
(2)统计功能:可方便地统计出文档中所有出现次数最多和最少的单词(即字符串)。
(3)查找与替换功能:能够查找任意一个字符串在文档中出现的次数,并可以选择全部或有选择地将其替换为另一个字符串。
(4)显示功能:编辑完成后可以显示编辑后的文档。
(5)抽取功能:根据要求将文件中的某一类字符抽取,并存入另外的文件,如抽取所有的数字、抽取所有的字母等
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void list(char* s);
void statistics();
void selectstring();
void replacestring();
void extracttxt();
void myscreen();
int main()
{
int i = 0;
do {
cout << "***************************************" << endl;
cout << "* 文档编辑器 *" << endl;
cout << "* 主菜单 *" << endl;
cout << "***************************************" << endl;
cout << " 请选择操作类型: " << endl;
cout << " 0、退出 " << endl;
cout << " 1、标准化显示 " << endl;
cout << " 2、统计 " << endl;
cout << " 3、查找 " << endl;
cout << " 4、替换 " << endl;
cout << " 5、抽取 " << endl;
cout << "----------------------------------------" << endl;
cin >> i; //选择操作
switch (i)
{
case 0:
cout << "closing......" << endl;
break;
case 1:
myscreen(); //显示文档到屏幕
cout << endl;
break;
case 2:
statistics(); //统计文档中的高频与低频字符串
cout << endl;
break;
case 3:
selectstring(); //查找字符串在文档中的次数
cout << endl;
break;
case 4:
replacestring(); //替换掉用户想要被替换的字符串
cout << endl;
break;
case 5:
extracttxt(); //抽取用户需要的信息
cout << endl;
break;
default:
cout << "输入错误!!" << endl; //提示输入错误,并退出
}
} while (i); //直到输入0才退出
cout << "---------------------------------------------------" << endl;
cout << "谢谢使用!" << endl;
return 0;
}
void list(char* s) //显示任意文件到屏幕
{
ifstream ifile(s); //打开任意文件
if (!ifile)
{
cout << "File cannot be openned!" << endl;
return;
}
char ch;
while (ifile.get(ch)) //输出文档信息到屏幕
{
cout << ch;
}
ifile.close();
}
void statistics() //统计高频与低频字符串
{
ifstream ifile("File.txt");
if (!ifile)
{
cout << "File cannot be openned!" << endl;
return;
}
char str[2000] = {
' ' }; //用于存放文档全部数据
char array[2000][50] = {
"0" }; //用于存放各个字符串
int num[2000]; //存放字符串次数
char* p; //通过指针来读取文档信息
char ch