一.问题描述

使用宽输入流从一个有中文字符的文本文件中读入所有字符,统计每个字符出现的次数,将统计结果用宽输出流输出到另一个文本文件中。

二.设计思路

三.流程图

四.伪代码 

1

五.代码实现 

#include <iostream>
#include <fstream>
#include <string>
#include <locale.h>   
#include <map>
using namespace std;
 
int main()
{
	//locale loc(".936");
	//wcout.imbue(loc);
	puts(setlocale(LC_CTYPE, ""));    //设置中文编码方式
	wifstream in("习题11-10输入.txt");
	wofstream out("习题11-10输出.txt"); 
	//in.imbue(loc);
	//out.imbue(loc);
	wstring line;                //用来存储一行内容 
	map<wchar_t,int>counter;
	
	
	while(getline(in,line))
	{
		for(int i=0;i<line.length();++i)
		{
			counter[line[i]]++; 
		}
	}
	map<wchar_t,int>::iterator itor;
	int i=1;
	for(itor=counter.begin();itor!=counter.end();++itor,++i)  //迭代器
	{
		out<<itor->first<<"\t"<<itor->second<<"\t";
		if(i%4==0){
			out<<endl;
		}
	}
	in.close();
	out.close(); 
	
	return 0;
}

 

posted on 2023-05-13 23:26  leapss  阅读(29)  评论(0)    收藏  举报