2017年12月16日 星期六

[C_AR170-中] 手機打字母(C++)

[C_AR170-中] 手機打字母

Time Limit: 1 seconds
問題描述 :
智慧型手機尚未普及時,很多人用手機傳簡訊幾乎使用手機上的按鍵打出想要的英文字母。手機上的按鍵如下:
1
, .
2
ABC/abc
3
DEF/def
4
GHI/ghi
5
JKL/jkl
6
MNO/mno
7
PQRS/pqrs
8
TUV/tuv
9
WXYZ/wxyz
*
Shift
0
Space
#
初始預設為大寫模式,比如我們需要打一個英文字母 E ,則需要按二下 ”3” ;若需要打英文字母 q 則按一下 ”*” 切換為小寫模式,再按 ”77” ;若要換回大寫模式再按 ”*” ;不管大寫或小寫模式皆能打 ”,” 和 ”.” 以及空格。現在輸入一串英文字母,輸出按鈕的順序。
輸入說明 :
僅一行輸入包含 n(1 <= n <= 29) 個字元,字元僅包含大小寫的英文字母、 ”,” 和 ”.” 以及空格 。
輸出說明 :
依範例的格式輸出答案。
範例 :
Sample Input:Sample Output:
HELLO4433555555666
apple, AppLE.*2775553310*2*77*5553311



  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int main() {  
  5.     // [C_AR170-中] 手機打字母  
  6.     string text;  
  7.     getline(cin, text);  
  8.     string s[26] = {"2","22","222","3","33","333","4","44","444","5","55","555","6","66","666","7","77","777","7777","8","88","888","9","99","999","9999"};  
  9.     string result = "";  
  10.     bool capital = true;//true = 大寫,false = 小寫  
  11.     for(int i = 0;i < text.size();i++)  
  12.     {  
  13.         if('A' <= text[i] && text[i] <= 'Z')//大寫  
  14.         {  
  15.             if(capital == false)//現在是小寫  
  16.             {  
  17.                 result = result + "*";  
  18.                 result = result + (s[text[i]-'A']);  
  19.                 capital = true;//切換成大寫  
  20.             }  
  21.             else  
  22.             {  
  23.                 result = result + (s[text[i]-'A']);  
  24.             }  
  25.         }  
  26.         else if('a' <= text[i] && text[i] <= 'z')//小寫  
  27.         {  
  28.             if(capital == true)//現在是大寫  
  29.             {  
  30.                 result = result + "*";  
  31.                 result = result + (s[text[i]-'a']);  
  32.                 capital = false;//切換成小寫  
  33.             }  
  34.             else  
  35.             {  
  36.                 result = result + (s[text[i]-'a']);  
  37.             }  
  38.         }  
  39.         else if('0' <= text[i] && text[i] <= '9')//數字  
  40.         {  
  41.             result = result + (s[text[i]-'0']);  
  42.         }  
  43.         else if(text[i] == ' ')//space  
  44.         {  
  45.             result = result + "0";  
  46.         }  
  47.         else if(text[i] == ',')//,  
  48.         {  
  49.             result = result + "1";  
  50.         }  
  51.         else if(text[i] == '.')//.  
  52.         {  
  53.             result = result + "11";  
  54.         }  
  55.     }  
  56.     cout << result << endl;  
  57.     return 0;  
  58. }  

沒有留言:

張貼留言