作者joefaq (小瑜)
看板C_and_CPP
标题Re: [问题] 阵列内中英文字串的反转(序)
时间Sun Mar 22 12:15:47 2009
#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>
using namespace std;
int main()
{
ifstream Input("input.txt");
ofstream Output("output.txt");
if(!Input)
{
exit(1);
}
string temp;
while(getline(Input, temp))
{
for(string::size_type ix = 0; ix < temp.size(); ix++)
if(temp[ix] < 0)
swap(temp[ix], temp[ix++]);
reverse(temp.begin(), temp.end());
Output << temp << '\n';
temp.clear();
}
Input.close();
Output.close();
return 0;
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.116.165.235
※ 编辑: joefaq 来自: 59.116.165.235 (03/22 12:17)
1F:→ grantchiue:不过我是试着使用阵列去互相传入,目前不太考虑用 03/22 12:17
2F:→ grantchiue:reverse来写,还是说不用reverse会写不出来 囧? 03/22 12:17
※ 编辑: joefaq 来自: 59.116.165.235 (03/22 12:21)
3F:推 grantchiue:我也先试试看这方法,感谢(跪 03/22 12:40
4F:推 grantchiue:这方法完全可行 囧!我继续研究(磕头 03/22 12:43
5F:推 grantchiue:基本上都看懂了,不过我有一个地方不太懂,就是判断是 03/22 18:58
6F:→ grantchiue:否为中文字的那个swap,为什麽是写if(temp[ix] < 0)呢 03/22 18:58
7F:推 Kawasumi:你用bitwise去想 中文字的特徵就是第一个byte的第一bit是 03/22 20:07
8F:→ Kawasumi:零,这对应到signed char型态的话,就是负数了 03/22 20:07
9F:→ Kawasumi:更正 第一个byte的第一个bit是1 03/22 20:08
10F:推 grantchiue:感谢O3Q 我懂了! 03/22 21:52
11F:→ LoCKeR941010:想请问一下 我这个方法在DevC可行 可是在VC就没办法 03/23 18:06
12F:→ LoCKeR941010:在VC 中文字串会变乱码 这是为什麽? 03/23 18:06