Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- #include<fstream>
- #include<sstream>
- #include<vector>
- #include<queue>
- using namespace std;
- struct Rule;
- class Word {
- private :
- int uno ; // 中文字的萬國碼編號
- int sno ; // 中文字筆劃
- public :
- Word(){}
- Word( int u , int s ): uno(u) , sno(s) {}
- int get_sno() const {
- return sno;
- }
- // ex: 一 [4e00]
- friend wostream& operator<< (wostream& out , const Word& w){
- out << static_cast<wchar_t>(w.uno) << L" [";
- out << hex << w.uno << L"] ";
- return out;
- }
- friend struct Rule;
- };
- struct Rule{
- bool operator() ( const Word& w1 , const Word& w2 ) const {
- if( w1.sno != w2.sno )
- return w1.sno > w2.sno; // sno 小的排前面
- return w1.uno > w2.uno; // uno 小的排前面
- }
- };
- int main(){
- string file_name = "strokes.dat";
- setlocale( LC_ALL , "zh_TW.UTF8" ); // 設定輸出的編碼
- wifstream infile(file_name.c_str());
- infile.imbue(locale("zh_TW.UTF8")); // 設定讀檔的編碼
- wstring line;
- wistringstream wistr;
- wstring uno_str; // 儲存 U+4e00 之類的字串
- int uno, sno;
- priority_queue< Word , vector<Word> , Rule > words;
- while( getline(infile,line) ){
- wistr.str(line);
- // 讀入
- // uno_str: U+4E00
- // sno: 1
- wistr >> uno_str >> dec >> sno;
- wistr.clear();
- // 截斷 U+ 的部分
- wistr.str(uno_str.substr(2));
- wistr >> hex >> uno;
- wistr.clear();
- words.push( Word( uno , sno ) );
- }
- int sno_cc = 0; // 紀錄當前筆劃數
- int cc = 0; // 紀錄當前筆劃數的第幾個字
- while( !words.empty() ){
- int word_sno = words.top().get_sno();
- if( sno_cc != word_sno ){ // 當筆劃數變化時
- sno_cc = word_sno; // 更新
- if( sno_cc != 0 ) // 如果不是第一行,多印一個換行
- wcout << endl;
- if( cc%10 != 0 ) // 如果不是印到第十個字,多印一個換行
- wcout << endl;
- wcout << dec << sno_cc << L" 劃: " << endl; // 設定輸出為 10 進制,因為 Word 的輸出有用到 hex 會造成以 16 進制輸出
- cc = 0; // 歸零計數器
- }
- wcout << words.top();
- words.pop();
- cc ++;
- if( cc%10 == 0 ) // 每十個字換行
- wcout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement