#include <iostream>
#include <string>
#include <vector>
#include <sstream>
//#include "header.h"
using namespace std;
// 文章结点(每一行)定义的结构体
struct ArticalNode {
string line;
ArticalNode *next;
//本行内容、下一行指针
};
// 文章链表定义
class ArticalList {
private:
ArticalNode *head;
//文章链表头结点
int lineCount;
int totalCount;
int letterCount;
int digitCount;
int spaceCount;
//文章行数、文章字数、字母个数、数字个数、空格个数
public:
ArticalList() {
head = nullptr;
lineCount = 0;
totalCount = 0;
letterCount = 0;
digitCount = 0;
spaceCount = 0;
//无参构造,为成员赋初值
}
/*
const:表示函数在执行过程中不会修改参数的值。
当参数被声明为 const 类型时,函数内部不能修改该参数的值。
这是一种安全机制,用于确保函数不会无意间修改传入的数据。
&:表示参数通过引用传递而不是通过值传递。
使用引用作为函数参数可以避免复制大型对象的开销,
并允许函数修改传递的参数。
nullptr: 是 C++ 中表示空指针的关键字,
它提供了更好的类型安全性和可读性。
在新的代码中,建议使用 nullptr 来代替旧标准的 NULL 或 0。
*/
// 添加一行文字到链表,无返回值,参数为输入的一行内容
void addLine(const string &line) {
//创建新结点
ArticalNode *newNode = new ArticalNode;
newNode->line = line;
newNode->next = nullptr;
if (head == nullptr) {
//链表为空,则新结点成为头结点
head = newNode;
} else {
//尾插法,新结点放到末尾
ArticalNode *current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
}
lineCount++;
//文章行数加一
countCharacters(line);
//统计添加的这一行的信息
}
// 统计字符数量,无返回值,参数为文章的一行内容
void countCharacters(const string &line) {
for (char c : line) {
if (isalpha(c)) {
letterCount++;//字母个数
} else if (isdigit(c)) {
digitCount++;//数字个数
} else if (isspace(c)) {
spaceCount++;//空格个数
}
totalCount++;//总文字个数
}
}
// 统计某一字符串出现的次数,无返回值,参数为输入的要统计出现次数的字符串
int countStrOccurrences(const string &str) {
int count = 0; //出现次数
ArticalNode *current = head;
// 从链表头节点开始遍历到末尾
while (current != nullptr) {
size_t pos = current->line.find(str); // 在当前行中查找目标字符串的位置
while (pos != string::npos) { // 当目标字符串存在于当前行中时
count++; // 计数器增加
pos = current->line.find(str, pos + 1);
// 继续在当前行中查找目标字符串存在的下一个位置
}
current = current->next; // 遍历下一行
}
return count; // 返回字符串出现的总次数
/*
size_t是一种无符号整数类型,在 C++ 标准库中定义。它通常用于表示对象的大小或索引,
在此用于存储字符串的位置,它返回目标字符串在原始字符串中的位置。
如果目标字符串不存在,则返回 std::string::npos,
npos是 size_t 类型的特殊值,表示未找到。
*/
}
// 删除某一子串,并将后面的字符前移,无返回值,参数为文章的一行内容
void deleteSubstring(const string &str) {
ArticalNode *current = head;
// 从链表头节点开始遍历到末尾
while (current != nullptr) {
size_t pos = current->line.find(str);
// 在当前行中查找目标字符串的位置
while (pos != string::npos) { // 当目标字符串存在于当前行中时
current->line.erase(pos, str.length());
//删除找到的目标字符串
pos = current->line.find(str, pos + 1);
// 继续在当前行中查找目标字符串存在的下一个位置
}
current = current->next; // 遍历下一行
}
}
// 获取文章链表内容内容(存到容器中)。返回值为容器,无参数
vector<string> getLines() {
vector<string> lines;//容器,存储文章每一行
ArticalNode *current = head;
//从文章链表头结点开始遍历
while (current != nullptr) {
lines.push_back(current->line);
//添加结点中的一行内容到到容器中
current = current->next;
//遍历下一结点
}
return lines;
}
// 获取文章统计结果(存到字符流中),返回值为字符串,无参数
string getStatistics() {
stringstream ss;
//ss << "文章总行数: " << lineCount << "\n";
ss << "文章总字数: " << totalCount << "\n";
ss << "全部字母数: " << letterCount << "\n";
ss << "数字个数: " << digitCount << "\n";
ss << "空格个数: " << spaceCount << "\n";
return ss.str();
/*使用了 stringstream 类来构建一个字符串流,
并通过串联输出运算符 << 将各种统计数据拼接到字符串流中。
最后,调用 ss.str() 函数将字符串流的内容转换为 string 类型并返回。
*/
}
};
int main() {
cout << "************************************************" << endl;
cout << "*** ***" << endl;
cout << "*** 文章编辑系统 ***" << endl;
cout << "*** ***" << endl;
cout << "************************************************" << endl;
ArticalList articalList; //文章链表对象
//不使用 new 运算符(手动创建对象),使用栈上的自动对象,
//不需要手动释放内存。栈上的自动对象会在其作用域结束时自动销毁,释放所占用的内存。
cout << endl;
cout << "请按行输入文章,以空行结束:" << endl;
cout << endl;
string line; //输入的一行内容
while (getline(cin, line) && !line.empty()) {
articalList.addLine(line);//添加到文章链表中
}
//从标准输入流(std::cin)中逐行读取用户输入,
//并将每行内容添加到 ArticalList 对象的链表中。
cout << endl;
// 输出用户输入的各行字符
vector<string> outputLines = articalList.getLines(); //获取文章链表的内容统计信息到容器中
cout << "您输入的文章:" << endl;
cout << endl;
//遍历输出容器中的各行内容
for (const string &line : outputLines) {
cout << line << endl;
}
cout << endl;
// 输出统计的文章信息
string statistics = articalList.getStatistics(); //获取文章的统计信息作为字符串输出
cout << statistics;
// 操作菜单
while ( true ) {
cout << endl;
cout << "*******************************************************************" << endl;
cout << "*** ***" << endl;
cout << "*** 可选择操作: ***" << endl;
cout << "*** 1. 统计某一字符串出现的次数 ***" << endl;
cout << "*** 2. 删除某一子串 ***" << endl;
cout << "*** 3. 编辑另一篇文章 ***" << endl;
cout << "*** 0. 退出程序 ***" << endl;
cout << "*** ***" << endl;
cout << "*******************************************************************" << endl;
// cout << "可选择操作:" << endl;
// cout << "1. 统计某一字符串出现的次数" << endl;
// cout << "2. 删除某一子串" << endl;
// cout << "3. 编辑另一篇文章" << endl;
// cout << "0. 退出程序" << endl;
cout << endl;
cout << "请输入选项号:";
string choice; //用户输入的选项
getline(cin, choice);
//使用 getline(cin, choice) 而不是 cin >> choice
//是为了能够读取包含空格的完整输入行。
if (choice == "0") {
// cout << "程序已退出。" << endl;
cout << endl;
cout << "************************************************" << endl;
cout << "*** ***" << endl;
cout << "*** 程序已经结束 ***" << endl;
cout << "*** ***" << endl;
cout << "************************************************" << endl;
exit(0); //调用程序强制退出的函数
} else if (choice == "1") {
// 统计某一字符串出现的次数
cout << "统计某一字符串出现的次数,请输入目标字符串,以回车结束:" << endl;
string searchString; //要统计出现次数的字符串
getline(cin, searchString); //输入
int occurrences = articalList.countStrOccurrences(searchString); //获取出现次数
cout << "字符串 \"" << searchString << "\" 出现的次数: " << occurrences << endl;
} else if (choice == "2") {
cout << endl;
// 删除某一子串
cout << "删除某一子串,请输入目标字符串,以回车结束:" << endl;
string substring; //要删除的字符串
getline(cin, substring); //输入
int occurrences = articalList.countStrOccurrences(substring);
//调用查找函数,传入要删除的字符串,并且返回查找结果
if (occurrences == 0) { //没有找到要删除的字符串
cout << "文章中不存在你要删除的字符串 \"" << substring << "\"。" << endl;
continue; // 进入下次选择操作的循环
} else { //找到了
articalList.deleteSubstring(substring);
//调用删除函数,传入要删除的字符串
cout << endl;
// 输出删除子串后的文章
outputLines = articalList.getLines(); //获取文章内容存入容器
cout << "删除子串后的文章:" << endl;
//遍历输出容器内容
cout << endl;
for (const string &line : outputLines) {
cout << line << endl;
}
}
} else if (choice == "3") {
cout << "3. 编辑另一篇文章" << endl;
main(); //重新进入主页面
} else {
cout << "无效选项,请重新输入。" << endl; //重新进入选择操作的循环
}
}
return 0;
}
01-04
06-22
07-11
02-22
2352
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
03-02
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交