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.0069 sec