[C_ST38-中] 字數統計
問題描述:
輸入一英文字串 S ,統計 S 中各個單字的出現次數 ( 大小寫視為相同,標點符號不計 ) 。
( 例: S 字串 : "A boy picked a ball, and the ball was red."
輸出 : "A" : 2
"boy" : 1
"picked" : 1
"ball" : 2
"and" : 1
"the" : 1
"was" : 1
"red" : 1 )
輸入說明:
1. 鍵盤輸入n行字串 S,0 < n < 5
2. 字串為 ASCII Code 組成,且不區分大小寫。輸出結果不包括標點符號
輸出說明:
[ 單字 1] : [ 出現次數 ]
[ 單字 2] : [ 出現次數 ]
.
.
.
[ 單字 n] : [ 出現次數 ]
範例:
輸入一英文字串 S ,統計 S 中各個單字的出現次數 ( 大小寫視為相同,標點符號不計 ) 。
( 例: S 字串 : "A boy picked a ball, and the ball was red."
輸出 : "A" : 2
"boy" : 1
"picked" : 1
"ball" : 2
"and" : 1
"the" : 1
"was" : 1
"red" : 1 )
輸入說明:
1. 鍵盤輸入n行字串 S,0 < n < 5
2. 字串為 ASCII Code 組成,且不區分大小寫。輸出結果不包括標點符號
輸出說明:
[ 單字 1] : [ 出現次數 ]
[ 單字 2] : [ 出現次數 ]
.
.
.
[ 單字 n] : [ 出現次數 ]
範例:
Sample Input | Sample Output |
A boy picked a ball, and the ball was red. | A : 2 boy : 1 picked : 1 ball : 2 and : 1 the : 1 was : 1 red : 1 |
Sprint and HTC launch the first 4G handset in America -- and its got Android onboard. | Sprint : 1 and : 2 HTC : 1 launch : 1 the : 1 first : 1 4G : 1 handset : 1 in : 1 America : 1 its : 1 got : 1 Android : 1 onboard : 1 |
SanDisk 32GB microSDHC vs. SanDisk 4GB microSDHC... fight! | SanDisk : 2 32GB : 1 microSDHC : 2 vs : 1 4GB : 1 fight : 1 |
說明:
1. 讓有換行的句子變成一個句子,下面有四行(讓我卡超久)
SanDisk 32GB
microSDHC vs.
SanDisk 4GB
microSDHC... fight!
-->
SanDisk 32GB microSDHC vs. SanDisk 4GB microSDHC... fight!
2. 讓句子留下英文大小寫、數字和空白(原句子)
SanDisk 32GB microSDHC vs. SanDisk 4GB microSDHC... fight!
-->
SanDisk 32GB microSDHC vs SanDisk 4GB microSDHC fight
3. 將句子運用字串切割,切成一個一個的單字(原句子)
SanDisk 32GB microSDHC vs SanDisk 4GB microSDHC fight
-->
SanDisk
32GB
microSDHC
vs
SanDisk
4GB
microSDHC
fight
4. 讓句子留下英文大小寫、數字和空白(轉成小寫句子)
SanDisk 32GB microSDHC vs. SanDisk 4GB microSDHC... fight!
-->
SanDisk 32GB microSDHC vs SanDisk 4GB microSDHC fight
-->
sandisk 32gb microsdhc vs sandisk 4gb microsdhc fight
5. 將句子運用字串切割,切成一個一個的單字(轉成小寫單字)
sandisk 32gb microsdhc vs sandisk 4gb microsdhc fight
-->
sandisk
32gb
microsdhc
vs
sandisk
4gb
microsdhc
fight
6. 開始比較計算(我陣列,沒用map因為,答案不需要排序,可是map會排序)
宣告一個陣列key和value,來模仿map
- #include <iostream>
- #include <vector>
- #include <map>
- #include <string>
- #include <sstream>
- #include <stdio.h>
- #include <ctype.h>
- #include <algorithm>
- using namespace std;
- int main() {
- // [C_ST38-中] 字數統計
- string Str;
- string sen;
- while(getline(cin, Str))
- {
- sen = sen + Str;
- }
- vector<char> no_symbol;
- vector<char> to_small;
- for(int i = 0;i < sen.size();i++)
- {
- if((sen[i] >= 'A' && sen[i] <= 'Z') ||
- (sen[i] >= 'a' && sen[i] <= 'z') ||
- (sen[i] >= '0' && sen[i] <= '9') ||
- sen[i] == ' ')
- {
- no_symbol.push_back(sen[i]);
- if(sen[i] >= 'A' && sen[i] <= 'Z')
- {
- to_small.push_back(sen[i]+32);
- }
- else
- {
- to_small.push_back(sen[i]);
- }
- }
- }
- string S = "";//原字
- string s = "";//小寫字
- for(int i = 0;i < no_symbol.size();i++)
- {
- S = S + no_symbol[i];
- s = s + to_small[i];
- }
- stringstream ss(S);
- string token;
- //字串切割
- int c = 0;
- vector<string> ori_word;
- while (getline(ss, token, ' '))
- {
- ori_word.push_back(token);
- c++;
- }
- stringstream sss(s);
- //字串切割
- c = 0;
- vector<string> small_word;
- while (getline(sss, token, ' '))
- {
- small_word.push_back(token);
- c++;
- }
- string key[c];
- int value[c];
- int C = 0;
- int loc = 0;
- for(int i = 0;i < ori_word.size();i++)
- {
- bool in_the_map = false;//都不存在在map中
- for(int j = 0;j < i;j++)
- {
- if(small_word[i] == small_word[j] && i != j)
- {
- in_the_map = true;
- for(int k = 0;k < C;k++)
- {
- if(ori_word[j] == key[k])
- {
- loc = k;
- }
- }
- }
- }
- if(in_the_map == true)
- {
- value[loc]++;
- }
- else if(find(key, key+c, ori_word[i]) == key+c)
- //沒找到
- {
- key[C] = ori_word[i];
- value[C] = 1;
- C++;
- }
- else//找到
- {
- for(int j = 0;j < C;j++)
- {
- if(ori_word[i] == key[j])
- {
- value[j]++;
- }
- }
- }
- }
- for(int i = 0; i < C;i++)
- {
- cout << key[i] << " : " << value[i] << endl;
- }
- return 0;
- }