作者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