输入一串中文字符并打印最频繁出现的文字。

  1 #include<iostream>
  2 #include<string>
  3 #include<cstring>
  4 #include<list>
  5 #include<iomanip>
  6 using namespace std;
  7 struct cell //结构单体
  8 {
  9     wchar_t word; //中文
 10     int count; //计数
 11 };
 12 list<cell> aim;
 13 struct conpare //list 排序
 14 {
 15     bool operator()(cell& a, cell& b)const
 16     {
 17         return(a.count > b.count);
 18     }
 19 };
 20 bool check(wchar_t a) //检查是否存在
 21 {
 22     if (aim.empty())
 23     {
 24         return 0;
 25     }
 26     for each (auto var in aim)
 27     {
 28         if (a == var.word)
 29         {
 30             return 1; //存在
 31             break;
 32         }
 33     }
 34     return 0;// 不存在
 35 }
 36 cell temp;
 37 void COPPY(wstring A) //将宽字符串A 中的单体存储在list中,并要保证不重复
 38 {
 39     for each (auto var in A)
 40     {
 41         if (!check(var))
 42         {
 43             temp.word = var;
 44             temp.count = 0;
 45             aim.push_back(temp);
 46         }
 47 
 48     }
 49 }
 50 void CALCULATE(wstring& SRC)
 51 {
 52     auto sign = aim.begin();
 53 
 54     while (sign != aim.end())
 55     {
 56         for (int i = 0; i < SRC.length(); i++)
 57         {
 58             if (SRC[i] == (*sign).word)
 59             {
 60                 (*sign).count += 1;
 61 
 62             }
 63 
 64         }
 65         sign++;
 66 
 67     }
 68 }
 69 
 70 void printf_result()
 71 {
 72     auto sign = aim.begin();
 73     aim.sort(conpare());
 74     temp.count = 0;
 75     sign = aim.begin();
 76     cout << "出现最多的字:";
 77     while (sign != aim.end()) //防止显示标点 。 ,
 78     {
 79         if ((*sign).word != L'。' && (*sign).word != ',')
 80         {
 81             wcout << (*sign).word;
 82             cout << "   出现了:" << (*sign).count << "" << "       顺序如下";
 83             break;
 84         }
 85         else
 86         {
 87             sign++;
 88         }
 89     }
 90 
 91     for each (auto var in aim) //全排序
 92     {
 93 
 94         if (temp.count % 5 == 0)
 95         {
 96             cout << endl;
 97         }
 98         wcout << var.word << setw(5) << var.count << "    ";
 99         temp.count++;
100 
101 
102 
103     }
104 }
105 void max()
106 {
107     auto sign = aim.begin();
108     while (sign != aim.end())
109     {
110         if ((*sign).count >= temp.count)
111         {
112             temp.count = (*sign).count;
113             temp.count = (*sign).count;
114         }
115         sign++;
116     }
117 }
118 void OK(wstring& SRC)
119 {
120     COPPY(SRC);
121     CALCULATE(SRC);
122     max();
123     printf_result();
124 }
125 int main()
126 {
127     wcout.imbue(locale("chs"));
128     wcin.imbue(locale("chs"));
129     wstring SRC;
130     wcin >> SRC;;
131     OK(SRC);
132     system("pause");
133     return 0;
134 }

其实要加条件的,把所有的中文符号全筛选掉,否则万一是逗号最好尴尬了。

有个小经验(对我来说啥经验都是教训,都是掉坑摸索好几天得来的。蒙蔽)

字符串或者字前加L的意思就是转换。比如 L U 等 不转换是无法比较的。捂脸。。。。

 

这个是面试题,不能说是哪家公司的。。。。电子邮件面试,时间稍微有点紧呐。编译器也不给力,居然卡了。无语。

end

 哦对了。。。

1 wcout.imbue(locale("chs"));
2 wcin.imbue(locale("chs"));

这两个必须都要。。。。捂脸。。

而且如果

 1 #include<locale> 

没有这个头文件就会不能打印wstring类型。

再次 end

posted on 2017-03-24 21:50  王猛ALL  阅读(215)  评论(0)    收藏  举报

导航