查找红楼梦中各人物每回出现的频率并输出到excel中

#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <algorithm>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int cou = 0;
string&  erase_all(string&  str, const string& old_value)
{
	while (true)
	{
		string::size_type  pos(0);
		if ((pos = str.find(old_value)) != string::npos)
		{
			str.erase(pos,old_value.size());
		}
		else { break; }
	}
	return  str;
}
int find_all(string s, string name) {
	int sum = 0;
	while (true)
	{
		string::size_type  pos(0);
		if ((pos = s.find(name)) != string::npos)
		{
			s.erase(pos, name.size());
			sum++;
		}
		else { break; }
	}
	return sum;
}
void output(string s, string name,string flag,int sum1,int sum2)
{
	//定义文件输出流   
	ofstream oFile;

	//打开要输出的文件   
	oFile.open("output.csv", ios::out | ios::trunc); 
	oFile << name << "," << "频率1" ;
	int i = 0;
	while (true)
	{
		string::size_type pos(0);
		string::size_type pos1(0);
		pos = s.find(flag);
		if ((pos = s.find(flag)) == string::npos)break;
		int count = 0;
		while (s.find(flag, pos) != string::npos && (pos1 = s.find(name)) != string::npos&& s.find(flag, pos + flag.size())>pos1)
		{
			s.erase(pos1, name.size());
			count++;
		}
		s.erase(pos, flag.size());
		i++;
		if(i<=80)
			oFile << "," << double(count) / double(sum1);
		else
			oFile << "," << double(count) / double(sum2) ;
		if (i == 80)
			oFile << '\n' << name << "," << "频率2+40";
	}
	oFile << endl;
}
int main()
{
	//定义文件输出流   
	ofstream oFile;

	//打开要输出的文件   
	oFile.open("output.csv", ios::out | ios::trunc);
	oFile << "名字" << "," << "频率" <<",";
	for (int i = 1; i <= 40; i++)
		oFile << i << ",";
	oFile << endl;//表头
	ifstream in("input.txt", ios::in);
	istreambuf_iterator<char> beg(in), end;
	string s(beg, end);
	in.close();//输入语料包
	string name;
	string flag = "回 ";
	string flag2 = "第八十一回";
	string flag3 = "第四十一回";
	erase_all(s, "\n");//去掉换行
	string subs2 = s.substr(s.find(flag3));
	string subs = s.substr(s.find(flag2));//截取后四十回
	freopen("name.txt", "r", stdin);
	while (cin>>name)
	{
		string s1 = s;//复制一个新串
		string subs1 = subs;
		int sum2 = find_all(subs, name);
		int sum1 = find_all(s, name) - sum2;
		int sum3 = find_all(subs2, name) - sum2;
		oFile << name << "," << "频率1";
		int i = 0;
		while (true)
		{
			string::size_type pos(0);
			string::size_type pos1(0);
			pos = s1.find(flag);
			if ((pos = s1.find(flag)) == string::npos)break;
			int count = 0;
			while (s1.find(flag, pos) != string::npos && (pos1 = s1.find(name)) != string::npos&& s1.find(flag, pos + flag.size())>pos1)
			{
				s1.erase(pos1, name.size());
				count++;
			}
			s1.erase(pos, flag.size());
			i++;
			if (i <=40)
				oFile << "," << double(count) / double(sum1);
			else if(i<=80)
				oFile << "," << double(count) / double(sum3);
			else
				oFile << "," << double(count) / double(sum2);
			if (i == 40)
				oFile << '\n' << name << "," << "频率2";
			if (i == 80)
				oFile << '\n' << name << "," << "频率3";
		}
		oFile << endl;
	}
	oFile.close();
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值