Demo entry 6737265

红楼梦

   

Submitted by GEARING on Apr 30, 2018 at 08:24
Language: C++. Code size: 1.3 kB.

// 红楼梦.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<stdio.h>
#include<iostream>
#include<Windows.h>
#include<stdlib.h>
#include<string.h>
#include<locale.h>
using namespace std;
#define MAX 2048
int main()
{
	setlocale(LC_ALL, "Chinese_China");//定位
	FILE*fp1 = _wfopen(L"name.txt", L"r");//打开名单文件
	if (fp1 == NULL)
	{
		return 0;
	}
	FILE*fp2 = _wfopen(L"b.txt", L"w");//打开新文件(记录结果)
    if (fp2 == NULL)
	{
		return 0;
	}
	WCHAR str[MAX], name[MAX], namefind[MAX], replace[MAX] = { '嘤' };//str用于读取红楼梦原文一行,name用于记录姓名
	WCHAR *find,*head,*find2;
	int len,namelen,numb=0,qnumb=0;//len原文每行长度;namelen姓名每行长度;numb是统计次数;
	while (fgetws(name, MAX, fp1) !=NULL)//读入一个姓名
	{
		wprintf(L"%s", name);
		namelen = wcslen(name);
		name[namelen - 1] = '\0';
		FILE*fp = _wfopen(L"p1.txt", L"r");//打开红楼梦文本文件
		if (fp == NULL)
		{
			return 0;
		}
		while (fgetws(str, MAX,fp)!=NULL)//读取一行原文
		{
	
			head = str;
			find = str;
			len = wcslen(str);
			str[len - 1] = '\0';
			while ((head=wcsstr(head,name))!=NULL)//对每行中是否存在姓名进行定位
			{
				head += (namelen - 1);
			    numb++;
			}
		}

		fwprintf(fp2,L"%s %d\n", name, numb);//向b.txt输出姓名和统计结果
		fclose(fp);
		numb = 0; 
	}
	fclose(fp1);
	fclose(fp2);
    return 0;
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).