主要是让自己及时总结,顺便做给同学看的
罗sir第一周数据结构作业:分别统计文本文件中每一个字符个数、单词总的个数;要求统计结果输出到另外一个文件中,源文件和结果文件名均由命令行参数指定。
上代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Node {
public:
char data;
int count;
Node* next;
Node(char d) : data(d), count(0), next(nullptr) {}
};
class LinkedList {
public:
Node* head;
LinkedList() : head(nullptr) {}
// 尾插法插入字符计数链表
void insert(char ch) {
Node* node = new Node(ch);
if (head == nullptr) {
head = node;
}
else {
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = node;
}
}
// 按元素进行顺序查找
Node* find(char ch) {
Node* current = head;
while (current != nullptr) {
if (current->data == ch) {
return current;
}
current = current->next;
}
return nullptr;
}
// 字符计数器
void increaseCount(char ch) {
Node* node = find(ch);
if (node != nullptr) {
node->count++;
}
}
};
// 统计文本文件中的字符和单词数并写入
void countCharactersAndWords(const string& inputFile, const string& outputFile) {
ifstream fin(inputFile); // 打开输入文件
LinkedList charList; // 字符统计链表
int charCount = 0; // 字符统计数
int wordCount = 0; // 单词统计数
char ch;
while (fin.get(ch)) {
// 统计字符个数
if (ch != ' ' && ch != '\n' && ch != '\t') {
if (charList.find(ch) == nullptr) { // 如果字符不存在
charList.insert(ch); // 插入字符
charList.increaseCount(ch); // 喜加一
charCount++; // 字符总数喜加一
}
charList.increaseCount(ch); // 存在则喜加一
charCount++; // 字符总数喜加一
}
// 统计单词个数
if (ch == ' ' || ch == '\n' || ch == '\t') { // trigger是出现空格、换行符或制表符
wordCount++; // 单词总数喜加一
}
}
fin.close(); // 关闭输入文件
// 写入
ofstream fout(outputFile); // 打开输出文件
Node* current = charList.head; // 当前结点等于头结点
while (current != nullptr) {
fout << "字符 '" << current->data << "' 的个数为 " << current->count << endl;
current = current->next;
}
fout << "字符的个数为 " << charCount << endl;
fout << "单词的个数为 " << wordCount << endl;
fout.close(); // 关闭输出文件
}
int main(int argc, char* argv[]) {
string inputFile = "C:/Users/KingR/Desktop/DataStructureIn.txt";
string outputFile = "C:/Users/KingR/Desktop/DataStructureOut.txt";
countCharactersAndWords(inputFile, outputFile);
return 0;
}
输入:
With the developing of China and the reform and opening-up policy beingcarried out, the recent years have seen a dramatic increase in the number ofChinese people traveling abroad. Thousands upon thousands of visitors arecrowding into other country , as a result, tourism is developing rapidly all over theworld. Thus, there is a hot discussion about that whether the booming touristtrade helps tourists develop greater understanding of other nations/places or not.For my part, I agree with the former opinion for the following reasons.
输出:
字符 'W' 的个数为 1
字符 'i' 的个数为 32
字符 't' 的个数为 38
字符 'h' 的个数为 24
字符 'e' 的个数为 51
字符 'd' 的个数为 17
字符 'v' 的个数为 7
字符 'l' 的个数为 15
字符 'o' 的个数为 43
字符 'p' 的个数为 14
字符 'n' 的个数为 33
字符 'g' 的个数为 11
字符 'f' 的个数为 8
字符 'C' 的个数为 2
字符 'a' 的个数为 31
字符 'r' 的个数为 38
字符 'm' 的个数为 7
字符 '-' 的个数为 1
字符 'u' 的个数为 15
字符 'c' 的个数为 9
字符 'y' 的个数为 5
字符 'b' 的个数为 5
字符 ',' 的个数为 5
字符 's' 的个数为 28
字符 '.' 的个数为 4
字符 'T' 的个数为 2
字符 'w' 的个数为 5
字符 '/' 的个数为 1
字符 'F' 的个数为 1
字符 'I' 的个数为 1
字符的个数为 454
单词的个数为 81
课上还没讲新知识,链表学的有点久了顺便写一遍复习一下,顺便复习下文本读写,其实完全没必要随便弄个静态数组或者map就能实现。
简述思路就是创建一个记录每个字符和出现次数的单链表(说白了就是字典),顺序遍历输入文件文本,遇到非空格、换行、制表符就对应字符计数+1,总字符数+1,遇到了就单词数+1。
其实可以严谨一些的,比如加入cerr语句避免开闭文件的逻辑错误之类的,但怕被老师以为我全是ai写的所以删了(没错确实用ai了我是真的懒)。