U E D R , A S I H C RSS

문�반대출력/문보창

소�

� 만약� 한글과 같� 확장문�가 담겼다면 process_wchar() 함수�서 약간� 꼼수를 부린다. 가�� 예(실제로 �렇게 �지는 않지만, �리는 같�)를 들어보�. "가나" �는 문�열� ver1과 같� 통�� 프로그램으로 뒤집으면 "나가"와 같� 프로그래머가 �했� 결과가 나오는 것� 아니고 "�ㄴ�ㄱ"가 나온다. 그렇다면 확장문�를 �단해서 문�열� 뒤집기 전� "가나"�는 문�열� "�ㄱ�ㄴ" �렇게 만들어 놓는다면 기존 ver1� 프로세스를 전혀 바꾸지 않고�, process_wchar()만� 추가하는 것으로 �했� 기능� 모� 수행하게 �다.

코드

ver1 (확장 문� 지� 안함)

~cpp 
#include <fstream>
#include <algorithm>
#include <string>
using namespace std;

string read_file();
void write_file(const string & str);

void main()
{
	string str = read_file();
	reverse(str.begin(), str.end()); // 문�열� 거꾸로 해주는 STL 함수
	 write_file(str);
}

// 파�로부터 문�열� �어들�다.
string read_file()
{
	string str;
	fstream fin("source.txt");
	char ch = fin.get();
	while (ch != EOF)
	{
		str += ch;
		ch = fin.get();
	}
	return str;
}

// 파�� 문�열� 쓴다.
void write_file(const string & str)
{
	fstream fout("result.txt");
	fout << str;
}

ver2 (확장문�까지 �단해서 반대로 한다)

~cpp 
#include <fstream>
#include <algorithm>
#include <string>
using namespace std;

string read_file();
void write_file(const string & str);
void process_wchar(string & str);

void main()
{
	string str = read_file();
	process_wchar(str);
	reverse(str.begin(), str.end());  // 문�열� 거꾸로 해주는 STL 함수
	 write_file(str);
}

// 확장문�를 위한 전처리 함수
void process_wchar(string & str)
{
	// str[i]는 char. 하지만 �것� 확장문�� �부�면 �수가 담기게 �다.
	for (int i = 0; i < str.length(); i++)
	{
		if (str[i] < 0 && str[i + 1] < 0)
		{
			swap(str[i], str[i+1]);
			i++;
		}
	}
}

// 파�로부터 문�열� �어들�다.
string read_file()
{
	string str;
	fstream fin("source.txt");
	char ch = fin.get();
	while (ch != EOF)
	{
		str += ch;
		ch = fin.get();
	}
	return str;
}

// 파�� 문�열� 쓴다.
void write_file(const string & str)
{
	fstream fout("result.txt");
	fout << str;
}

나한테 할�

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:29
Processing time 0.0036 sec