简易文本编辑系统的设计与实现基本要求:输入一页文字,可以统计出文字、数字、空格的个数。(1)利用外部.txt文件存储一页文章,每行最多不超过80个字符,共N行。(2)分别统计出其中英文字母和空格数及整篇文章总字数。(3)统计某一字符串在文章中出现的次数,并输出该次数。 (4)删除某一子串,并将后面的字符前移,对文章的修改,同步到.txt文件中。提高部分:采用顺序和链式两种存储结构实现。
c++实现,并且以顺序表形式构造
该回答引用ChatGPT-3.5,仅供参考,不保证完全正确
下面是一个使用C++顺序表实现的简易文本编辑系统,满足您描述的基本要求:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// 文本编辑系统类
class TextEditor {
private:
string filename; // 文本文件名
string content; // 文本内容
public:
TextEditor(const string& filename) : filename(filename) {
loadFile();
}
// 从文件加载文本内容
void loadFile() {
ifstream file(filename);
if (file.is_open()) {
string line;
while (getline(file, line)) {
content += line;
content += '\n';
}
file.close();
}
}
// 统计英文字母和空格的数量
void countLettersAndSpaces() {
int letterCount = 0;
int spaceCount = 0;
for (char c : content) {
if (isalpha(c))
letterCount++;
else if (c == ' ')
spaceCount++;
}
cout << "字母数量: " << letterCount << endl;
cout << "空格数量: " << spaceCount << endl;
}
// 统计总字数
void countTotalWords() {
int wordCount = 0;
bool inWord = false;
for (char c : content) {
if (isalpha(c) && !inWord) {
inWord = true;
wordCount++;
} else if (!isalpha(c) && inWord) {
inWord = false;
}
}
cout << "总字数: " << wordCount << endl;
}
// 统计某一字符串出现的次数
void countStringOccurrences(const string& str) {
size_t pos = content.find(str);
int count = 0;
while (pos != string::npos) {
count++;
pos = content.find(str, pos + 1);
}
cout << "字符串\"" << str << "\"出现次数: " << count << endl;
}
// 删除子串并同步到文件
void deleteSubstring(const string& substr) {
size_t pos = content.find(substr);
while (pos != string::npos) {
content.erase(pos, substr.length());
pos = content.find(substr);
}
ofstream file(filename);
if (file.is_open()) {
file << content;
file.close();
}
}
};
int main() {
string filename = "text.txt"; // 输入文件名
TextEditor editor(filename);
editor.countLettersAndSpaces();
editor.countTotalWords();
string searchString = "example"; // 要搜索的字符串
editor.countStringOccurrences(searchString);
string deleteString = "some"; // 要删除的子串
editor.deleteSubstring(deleteString);
return 0;
}
在该示例中,首先创建了一个TextEditor
类来处理文本编辑操作。构造函数接受一个文件名作为输入,并在初始化时加载文件内容到内存中。然后,提供了几个方法来执行所需的功能:
loadFile()
:从外部.txt文件加载文本内容。countLettersAndSpaces()
:统计文本中的英文字母和空格数量。countTotalWords()
:统计文本的总字数。countStringOccurrences(const string& str)
:统计某一字符串在文本中出现的次数。deleteSubstring(const string& substr)
:删除文本中的某一子串,并将修改后的内容同步到.txt文件中。在主函数中,创建一个TextEditor
对象,并调用相应的方法进行文本编辑操作。您可以根据需要进行修改和扩展。
请确保在编译和运行代码之前,将要处理的文本保存在名为"text.txt"的外部.txt文件中,且每行不超过80个字符。
不知道你这个问题是否已经解决, 如果还没有解决的话:#include<iostream>
#include<ctype.h>
using namespace std;
int main(){
int chNumbers=0;//字符个数
int numNumbers=0;//数字个数
int spaceNumbers=0;//空格个数
char ch;
while((ch = getchar())!= EOF){//输入一个字符
if(isdigit(ch))//判断是否数字
numNumbers++;
else if(isalpha(ch))//判断是否字母
chNumbers++;
else if(ch==' ')//判断是否空格
spaceNumbers++;
}
printf("chNumbers=%d,numNumbers=%d,spaceNumbers=%d",chNumbers,numNumbers,spaceNumbers);
return 0;
}
#include<iostream>
#include<fstream>
using namespace std;
int main(){
int Letterscount = 0;
int Spacecount = 0;
int sumCount = 0;
char fileName[] = "test.txt";//文件名
char buf[128];//暂存读取的数据
ifstream inFile;
inFile.open(fileName,ios::in);//打开文件
if(inFile.fail()){//判断文件是否打开失败
cout<<"open file error!"<<endl;
return 0;
}
while(!inFile.eof()){
inFile.getline(buf,128);
for(int i=0;i<strlen(buf);i++){
if(isalpha(buf[i])){//判断是否为字母
Letterscount++;
}
if(buf[i] == ' '){//判断是否为空格
Spacecount++;
}
}
sumCount += strlen(buf);
}
inFile.close();
cout<<"Letterscount="<<Letterscount<<endl;//输出字母个数
cout<<"Spacecount="<<Spacecount<<endl;//输出空格个数
cout<<"sumCount="<<sumCount<<endl;//输出总数
return 0;//完整代码请看下方
}
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
int main(){
char fileName[] = "test.txt"; // 文件名
char str[] = "hello"; // 想要查找的字符串
char buffer[256]; // 读取 buffer
int count = 0; // 计数器
ifstream inFile; // ifstream 读取文件
inFile.open(fileName, ios::in);//打开文件
if(inFile.fail()){
cout<<"File is error"<<endl;// 判断文件是否打开失败
exit(1);
}
while( !inFile.eof() ){
inFile.getline(buffer,256);//每次读取一行
char *p = buffer;
while( (p = strstr(p,str)) != NULL ) { // 从当前的行查找指定的字符串
count++;
p++;
}
}
cout<< "\"hello\" Number of appearances: "<<count<<endl;//输出该字符串数量
inFile.close();
return 0;//完整代码请看下方
}
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
// 在字符串中删除一个子串,size为起点之后的字符串移动的偏移量
char* deleteSub(char *str,int start,int size){
int nstart=start+size;
for(int i=nstart,j=start;i<strlen(str);i++,j++)
str[j]=str[i];
return str;
}
int main(){
char fileName[] = "test.txt";//文件名
char deleteStr[] = "hello";
char buffer[256];//暂存读取的数据
ifstream inFile;//ifstream 读取文件
ofstream outFile;//ofstream 将修改写入文件
inFile.open(fileName, ios::in);//打开文件
outFile.open("out.txt", ios::out|ios::trunc);//打开要写入的文件
if(inFile.fail()){//判断文件是否打开失败
cout<<"open file error!"<<endl;
return 0;
}
while( !inFile.eof() ){
inFile.getline(buffer,256);//每次读取一行
char *p = buffer;
while( (p = strstr(p,deleteStr)) != NULL ) {//判断是否存在需要删除的字符
strcpy(p,deleteSub(p,strlen(deleteStr),strlen(deleteStr)));
}
outFile<<buffer<<endl;//将每行修改后的字符写入到文件中
}
inFile.close();//关闭读取文件
outFile.close();//关闭写入文件
return 0;//完整代码请看下方
}
提高部分:
#include<iostream>
#include<string>
#define size 10//行数
using namespace std;
void setLine(char *str[],int maxLineSize){//读取每一行的字符
for(int i=0;i<size;i++){
char temp[maxLineSize];
cout<<"please input line "<<i+1<<" , max size is "<<maxLineSize<<" :"<<endl;
cin.getline(temp,maxLineSize);
str[i]=new char[strlen(temp)+1];
memset(str[i], 0, strlen(temp)+1);//初始化
strcpy(str[i],temp);
}
}
void showLine(char *str[],int len){//输出每一行的字符
for(int i=0;i<len;i++){
cout<<i+1<<" : "<<str[i]<<endl;
}
}
void insertLine(char *str[],int pos,char *line){//插入一行字符
if(pos < 0 || pos > size-1){
cout<<"Error: The location to insert does not exist."<<endl;
exit(1);
}
char *s = new char[strlen(line)+1];
// 使用 strncpy 来复制数组
strncpy(s,line,strlen(line)+1);
str[pos] = s;
cout<<"Insert completed."<<endl;
}
void deleteLine(char *str[],int len,int pos){//删除一行字符
if(pos < 0 || pos > len-1){
cout<<"Error: The location to delete does not exist."<<endl;
exit(1);
}
for(int i=pos;i<len;i++)
str[i] = str[i+1];
cout<<"Delete completed."<<endl;
}
void modifyLine(char *str[],int pos,char *line){//修改一行字符
if(pos < 0 || pos > size-1){
cout<<"Error: The location to modify does not exist."<<endl;
exit(1);
}
strncpy(str[pos],line,strlen(line)+1);
cout<<"Modify completed."<<endl;
}
int main(){
// 使用指向指针的方式来保存字符串数组
char *line[size];//保存每一行的字符
setLine(line,20);//读取每一行的字符
cout<<"\n\n\n****** The lines you create are as follows ******"<<endl;
showLine(line,size);//输出被创建的字符
cout<<"\n\n\n****** Add another line of characters ******"<<endl;
// 在末尾添加一行字符串,并输出
insertLine(line,size,"This is the last line.");
showLine(line,size);
cout<<"\n\n\n****** Enter the line number to delete. ******"<<endl;
// 删除一行字符,并输出
deleteLine(line,size,2);
showLine(line,size);
cout<<"\n\n\n****** Please enter the line number and new content to modify.(For example: 3 hello world.) ******"<<endl;
// 修改一行字符,并输出
char temp[256];
cin.getline(temp,256);
int modifyLineNum = stoi(temp); // C++ 11 特性,stoi 方法可以将一个string转换成对应的int
char newLine[256];
cin.getline(newLine,256);
modifyLine(line,modifyLineNum-1,newLine);
showLine(line,size);
return 0;//完整代码请看下方
}
```c++
using namespace std; // 定义链表结构 typedef struct node{ char line;//内容 int lineLength;// 当前行的字符数 struct node next; // 下一个节点 }LinkList;
Link