Android 台灣中文網

標題: c++ 問題 [打印本頁]

作者: Leung-Jacky    時間: 2014-12-11 20:22
標題: c++ 問題
如果我想把我輸入的字串(英文)轉成相反變成
A= Z
B= Y
...
...
...
...
...
...
...
...
...
...
...
...
Z= A
有何辦法,能給一點例子嗎??
PS: 左是我輸入的...
作者: flyingcap232    時間: 2014-12-11 20:22
提供三種
1- if else if
  if (in == A)
    out == Z
  else if (in == B)
    out == Y
...

2- switch
  switch (in)
  case A:
    out = Z
    break
  case B:
    out = Y
    break
...

3- calculation
out = Z - in + A

作者: arickchiu    時間: 2014-12-15 12:33
#include <iostream>
#include <string>

int main() {
    for (std::string line; std::getline(std::cin, line);) {
        std::cout << line.substr(2,1)
                  << "="
                  << line.substr(0,1)        
                  << std::endl;
    }
    return 0;
}
作者: 0948217712    時間: 2015-3-1 22:15
樓上雖然可用,但連寫 30 幾個 if else 未免太..........

較好的解法是

ans = 90-X+65; //X 是輸入字元的內碼,用於計算就是十進制, ans 就是答案的 ASCII code, 一行攪定




歡迎光臨 Android 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1