2017年12月4日 星期一

[C_ST56-易] 字串轉換(C++)

[C_ST56-易] 字串轉換

問題描述
假設每個英文字母都有一個與其互補的英文字母,如 [A/a] 的互補字母為 [Z/z] 、 [B/b] 的互補字母為 [Y/y] 以此類推。再將每個英文字母給定一個數值 , 無論其大小寫,如 [A/a]=1 、 [B/b]=2 、 [C/c]=3 、…、 [Z/z]=26 。當輸入一連串大小寫英文字母雜湊的字串,請將這字串全部轉成其互補字母反序印出並將輸入字串內的英文字母轉為數字算出總和。
輸入格式 
輸入 一英文字串。
輸出格式 
輸出 互補字串並算出原字串的數字和 。
Example
Sample Input:Sample Output:
abc
xYz
zyx 6
cBa 75


  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int main() {  
  5.     // [C_ST56-易] 字串轉換  
  6.     string text;  
  7.     int len;//字串長度  
  8.     int sum = 0;//加總  
  9.     while(cin >> text)  
  10.     {  
  11.         len = text.size();  
  12.         char c_text[len];  
  13.         for(int i = 0;i < len;i++)  
  14.         {  
  15.             if(65 <= (char)text[i] && (char)text[i] <= 90)//大寫  
  16.             {  
  17.                 sum = sum + (text[i]-64);  
  18.                 //計算原字串的數字和  
  19.                 c_text[i] = 'Z' - (text[i]-'A');  
  20.                 //互補字串  
  21.             }  
  22.             else if(97 <= (char)text[i] && (char)text[i] <= 122)//小寫  
  23.             {  
  24.                 sum = sum + (text[i]-96);  
  25.                 //計算原字串的數字和  
  26.                 c_text[i] = 'z' - (text[i]-'a');  
  27.                 //互補字串  
  28.             }  
  29.         }  
  30.         //輸出  
  31.         for(int i = 0;i < len;i++)  
  32.         {  
  33.             cout << c_text[i];  
  34.         }  
  35.         cout << " " << sum << endl;  
  36.         sum = 0;  
  37.     }  
  38.     return 0;  
  39. }  

題目32. 字母往後移(C++)

題目32. 字母往後移

問題描述
志明跟春嬌是班上的一對情侶,他們有寫交換日記來打發時間的習慣,為了防止他們寫的內容被幫忙傳的同學,或者是不小心被老師沒收,而曝光了裡面寫的東西,他們想到了一個辦法,就是把內容的所有字母都往後數幾次的字母替代,而往後數幾次的數目就寫在內容的下一行。但是,問題來了,春嬌覺得每次寫完都要在數來數去的轉化成”加密”格式,實在是太麻煩了。但又礙於不想被輕易的看到內容,於是她拜託你寫個程式幫忙她可以直接把寫好的內容轉化成”加密”的型態。加密結果不會影響原字母的大小寫,且數字部分亦作相同處理,但不處理符號及特殊字元及中文。
輸入說明
第一行為想輸入的內容,不超過 100 個字
第二行為打完你想輸入的內容之後,換行輸入你想要往後替代的數目。
輸出說明
印出轉換後的句子。
範例
Sample InputSample Output
How are you?
2
Jqy ctg aqw?


  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int main() {  
  5.     // 題目32. 字母往後移  
  6.     string text;  
  7.     int n;  
  8.     getline(cin, text);  
  9.     cin >> n;  
  10.     char c_text[text.size()];  
  11.     for(int i = 0;i < text.size();i++)  
  12.     {  
  13.         if(65 <= (char)text[i] && (char)text[i] <= 90)//大寫  
  14.         {  
  15.             if((int)text[i]+n <= 90)  
  16.             {  
  17.                 c_text[i] = (char)(text[i]+n);  
  18.             }  
  19.             else  
  20.             {  
  21.                 c_text[i] = (char)(((int)text[i]+n-1)-'Z'+'A');  
  22.             }  
  23.         }  
  24.         else if(97 <= (char)text[i] && (char)text[i] <= 122)//小寫  
  25.         {  
  26.             if((int)text[i]+n <= 122)  
  27.             {  
  28.                 c_text[i] = (char)(text[i]+n);  
  29.             }  
  30.             else  
  31.             {  
  32.                 c_text[i] = (char)(((int)text[i]+n-1)-122+97);  
  33.             }  
  34.         }  
  35.         else if(48 <= (char)text[i] && (char)text[i] <= 57)//數字  
  36.         {  
  37.             if((int)text[i]+n <= 57)  
  38.             {  
  39.                 c_text[i] = (char)(text[i]+n);  
  40.             }  
  41.             else  
  42.             {  
  43.                 c_text[i] = (char)(((int)text[i]+n-1)-57+48);  
  44.             }  
  45.         }  
  46.         else  
  47.         {  
  48.             c_text[i] = text[i];      
  49.         }  
  50.     }  
  51.     for(int i = 0;i < text.size();i++)  
  52.     {  
  53.         cout << c_text[i];  
  54.     }  
  55.     cout << endl;  
  56.     return 0;  
  57. }  

題目17. 英文斷詞(C++)

題目17. 英文斷詞

問題描述 :
斷詞在自然語言的研究上是個很重要的步驟,主要就是將關鍵字從句子中斷出,英文的斷詞較為簡單,就根據句子中的空格將英文字隔開。
輸入說明 :
輸入一句英文敘述句。 字元數≤1000。
輸出說明 :
將輸入的句子進行斷詞,將斷出的關鍵字依照句子中的出現排序列印出。全部轉成小寫,列印出的關鍵字不得重複,關鍵字間以一個空格隔開,最後一個關鍵字後面進行換行。例如輸入 How do you do ,則輸出 how do you 。
範例 :
輸入範例輸出範例
How do you dohow do you



  1. #include <iostream>  
  2. #include <string>  
  3. #include <sstream>  
  4. #include <stdio.h>  
  5. #include <ctype.h>  
  6.   
  7. using namespace std;  
  8.   
  9. int main() {  
  10.     // 題目17. 英文斷詞  
  11.     string text;  
  12.     getline(cin, text);//使用者輸入  
  13.     //全轉小寫  
  14.     for(int i = 0;i < text.size();i++)  
  15.     {  
  16.         text[i] = (char)tolower(text[i]);  
  17.     }  
  18.     string tok[1000];//切割後的字串  
  19.     string fin[1000];//沒有重複的才放入fin[]  
  20.     int count = 0;  
  21.     bool exist = true;  
  22.     fill(tok, tok+1000, "");  
  23.     fill(fin, fin+1000, "");  
  24.     stringstream ss(text);  
  25.     string token;  
  26.     //字串切割  
  27.     while (getline(ss, token, ' '))  
  28.     {  
  29.         tok[count] = token;  
  30.         count++;  
  31.     }  
  32.     int fpos = 0;  
  33.     //比對  
  34.     for (int i = 0; i < count; i++)  
  35.     {  
  36.         //tok[], fin[]  
  37.         //尋找tok[]如果沒有存在在fin中[]就存入fin[]  
  38.         for(int j = 0;j < count;j++)  
  39.         {  
  40.             if(tok[i] == fin[j])//如果存在在fin就false  
  41.             {  
  42.                 exist = false;  
  43.             }  
  44.         }  
  45.         if(exist == true)//不存在在fin就放入fin  
  46.         {  
  47.             fin[fpos] = tok[i];  
  48.             fpos++;  
  49.         }  
  50.         exist = true;  
  51.     }  
  52.     //輸出  
  53.     for (int i = 0; i < fpos; i++)  
  54.     {   
  55.         if(i == fpos-1)  
  56.         {  
  57.             cout << fin[i] << endl;  
  58.         }  
  59.         else  
  60.         {  
  61.             cout << fin[i] << " ";  
  62.         }  
  63.     }  
  64.     return 0;  
  65. }