Advertisement
Guest User

Untitled

a guest
May 26th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<fstream>
  4. #include<sstream>
  5. #include<vector>
  6. #include<queue>
  7. using namespace std;
  8.  
  9. struct Rule;
  10. class Word {
  11. private :
  12. int uno ; // 中文字的萬國碼編號
  13. int sno ; // 中文字筆劃
  14. public :
  15. Word(){}
  16. Word( int u , int s ): uno(u) , sno(s) {}
  17.  
  18. int get_sno() const {
  19. return sno;
  20. }
  21.  
  22. // ex: 一 [4e00]
  23. friend wostream& operator<< (wostream& out , const Word& w){
  24. out << static_cast<wchar_t>(w.uno) << L" [";
  25. out << hex << w.uno << L"] ";
  26. return out;
  27. }
  28.  
  29. friend struct Rule;
  30. };
  31.  
  32. struct Rule{
  33. bool operator() ( const Word& w1 , const Word& w2 ) const {
  34. if( w1.sno != w2.sno )
  35. return w1.sno > w2.sno; // sno 小的排前面
  36. return w1.uno > w2.uno; // uno 小的排前面
  37. }
  38. };
  39.  
  40. int main(){
  41.  
  42. string file_name = "strokes.dat";
  43. setlocale( LC_ALL , "zh_TW.UTF8" ); // 設定輸出的編碼
  44. wifstream infile(file_name.c_str());
  45. infile.imbue(locale("zh_TW.UTF8")); // 設定讀檔的編碼
  46.  
  47. wstring line;
  48. wistringstream wistr;
  49.  
  50. wstring uno_str; // 儲存 U+4e00 之類的字串
  51. int uno, sno;
  52.  
  53. priority_queue< Word , vector<Word> , Rule > words;
  54.  
  55. while( getline(infile,line) ){
  56. wistr.str(line);
  57.  
  58. // 讀入
  59. // uno_str: U+4E00
  60. // sno: 1
  61. wistr >> uno_str >> dec >> sno;
  62. wistr.clear();
  63.  
  64. // 截斷 U+ 的部分
  65. wistr.str(uno_str.substr(2));
  66. wistr >> hex >> uno;
  67. wistr.clear();
  68.  
  69. words.push( Word( uno , sno ) );
  70. }
  71.  
  72. int sno_cc = 0; // 紀錄當前筆劃數
  73. int cc = 0; // 紀錄當前筆劃數的第幾個字
  74. while( !words.empty() ){
  75. int word_sno = words.top().get_sno();
  76.  
  77. if( sno_cc != word_sno ){ // 當筆劃數變化時
  78. sno_cc = word_sno; // 更新
  79.  
  80. if( sno_cc != 0 ) // 如果不是第一行,多印一個換行
  81. wcout << endl;
  82.  
  83.  
  84. if( cc%10 != 0 ) // 如果不是印到第十個字,多印一個換行
  85. wcout << endl;
  86.  
  87. wcout << dec << sno_cc << L" 劃: " << endl; // 設定輸出為 10 進制,因為 Word 的輸出有用到 hex 會造成以 16 進制輸出
  88. cc = 0; // 歸零計數器
  89. }
  90.  
  91. wcout << words.top();
  92. words.pop();
  93. cc ++;
  94.  
  95. if( cc%10 == 0 ) // 每十個字換行
  96. wcout << endl;
  97. }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement