2018年3月27日 星期二

[C_ST38-中] 字數統計(C++)

[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] : [ 出現次數 ]
範例:
Sample InputSample 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











  1. #include <iostream>  
  2. #include <vector>  
  3. #include <map>  
  4. #include <string>  
  5. #include <sstream>  
  6. #include <stdio.h>  
  7. #include <ctype.h>  
  8. #include <algorithm>  
  9. using namespace std;  
  10.   
  11. int main() {  
  12.     // [C_ST38-中] 字數統計  
  13.     string Str;  
  14.     string sen;  
  15.     while(getline(cin, Str))  
  16.     {  
  17.         sen = sen + Str;  
  18.     }  
  19.       
  20.     vector<char> no_symbol;  
  21.     vector<char> to_small;  
  22.     for(int i = 0;i < sen.size();i++)  
  23.     {  
  24.         if((sen[i] >= 'A' && sen[i] <= 'Z') ||   
  25.         (sen[i] >= 'a' && sen[i] <= 'z') ||   
  26.         (sen[i] >= '0' && sen[i] <= '9') ||   
  27.         sen[i] == ' ')  
  28.         {  
  29.             no_symbol.push_back(sen[i]);  
  30.             if(sen[i] >= 'A' && sen[i] <= 'Z')  
  31.             {  
  32.                 to_small.push_back(sen[i]+32);  
  33.             }  
  34.             else  
  35.             {  
  36.                 to_small.push_back(sen[i]);  
  37.             }  
  38.         }  
  39.     }  
  40.     string S = "";//原字  
  41.     string s = "";//小寫字  
  42.     for(int i = 0;i < no_symbol.size();i++)  
  43.     {  
  44.         S = S + no_symbol[i];  
  45.         s = s + to_small[i];  
  46.     }  
  47.     stringstream ss(S);  
  48.     string token;  
  49.     //字串切割  
  50.     int c = 0;  
  51.     vector<string> ori_word;  
  52.     while (getline(ss, token, ' '))  
  53.     {  
  54.         ori_word.push_back(token);  
  55.         c++;  
  56.     }  
  57.       
  58.     stringstream sss(s);  
  59.     //字串切割  
  60.     c = 0;  
  61.     vector<string> small_word;  
  62.     while (getline(sss, token, ' '))  
  63.     {  
  64.         small_word.push_back(token);  
  65.         c++;  
  66.     }  
  67.       
  68.     string key[c];  
  69.     int value[c];  
  70.     int C = 0;  
  71.     int loc = 0;  
  72.     for(int i = 0;i < ori_word.size();i++)  
  73.     {  
  74.         bool in_the_map = false;//都不存在在map中  
  75.         for(int j = 0;j < i;j++)  
  76.         {  
  77.             if(small_word[i] == small_word[j] && i != j)  
  78.             {  
  79.                 in_the_map = true;  
  80.                 for(int k = 0;k < C;k++)  
  81.                 {  
  82.                     if(ori_word[j] == key[k])  
  83.                     {  
  84.                         loc = k;  
  85.                     }  
  86.                 }  
  87.             }  
  88.         }  
  89.         if(in_the_map == true)  
  90.         {  
  91.             value[loc]++;  
  92.         }  
  93.         else if(find(key, key+c, ori_word[i]) == key+c)  
  94.         //沒找到  
  95.         {  
  96.             key[C] = ori_word[i];  
  97.             value[C] = 1;  
  98.             C++;  
  99.         }  
  100.         else//找到  
  101.         {  
  102.             for(int j = 0;j < C;j++)  
  103.             {  
  104.                 if(ori_word[i] == key[j])  
  105.                 {  
  106.                     value[j]++;  
  107.                 }  
  108.             }  
  109.         }  
  110.               
  111.     }  
  112.       
  113.     for(int i = 0; i < C;i++)  
  114.     {  
  115.         cout << key[i] << " : " << value[i] << endl;  
  116.     }  
  117.   
  118.     return 0;  
  119. }  

2018年3月17日 星期六

程式設計I -- 字典(map)

使用map前要先引入
#include<map>


1. 建構map

map<string , int> amap;

amap 是一個 map, 鍵(key) 的型別是 string,值(value)的型別是 int 。
可以根據不同的需求,建構不同的型別。



2. 新增資料
在amap中新增一筆 key 為 "String" , value 為 1 的資料。

---方法一---
效率高但寫法複雜
amap.insert(make_pair("String", 1));

 ---方法二---
寫法簡單但效率慢
amap["String"] = 1;



3. 判斷資料在不在 map 中

---方法一---
使用find()看key是否存在於amap中

if (amap.find("String") == amap.end())
  cout << "沒找到" << endl;
else
  cout << "找到" << endl;

}

解釋:
find 會在 amap 中由開始 (begin() ) 找到尾巴 ( end() )。
如果沒找到,就會停在 end() 的地方。

---方法二---
使用count()看key是否存在於amap中

if (c.count(18) == 0)
  cout << "沒找到" << endl;
else
  cout << "找到" << endl;

}

解釋:
Count 會計算出現的次數
有出現      count() 回傳 1
沒有出現  count() 回傳 0



4. 輸出map內的資料

---方法一---
Cout<<amap["String"]<<endl;
輸出在amap中 key 為"String"的值(value)


---方法二---
使用迭代器

for (map<string, int>::iterator iter = amap.begin(); iter != amap.end(); iter++)
{
  if (iter->second > maxFreq)
  {
  maxFreq = iter->second;
  maxChar = iter->first;
  }

}

解釋:
iter->first      代表鍵 (key)
iter->second 代表值 (value)

---方法三---

for (pair<string, int> pp : amap)
{
  if (pp.second > maxFreq)
  {
  maxFreq = pp.second;
  maxChar = pp.first;
  }

}


5. 刪除元素
呼叫 earse 刪除key值。
amap.earse("String");