소ê°� ¶
ì�Œ 만약ì—� 한글과 ê°™ì�€ 확장문ìž�ê°€ 담겼다면 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;
}










